2 import MCC.IR.ParseNode;
3 import MCC.IR.ParseNodeVector;
8 public static boolean errors;
9 public static boolean debug;
11 // debugMessage: writes debug production message only if debug = true
13 void debugMessage (String production) {
15 System.out.println("Applying production: " + production);
19 String unescape (String str) {
20 StringBuffer sb = new StringBuffer();
22 // Note that we skip the first and last characters (they're "'s)
23 for (i = 1; i < str.length() - 1; i++) {
24 if (str.charAt(i) == '\\') {
26 switch (str.charAt(i)) {
43 System.err.print("Error in string literal: ");
44 System.err.println(str.charAt(i));
45 System.err.println("Aborting...");
49 sb.append(str.charAt(i));
60 public String filename;
62 public void syntax_error (java_cup.runtime.Symbol current) {
64 CUP$TDLParser$actions.errors = true;
65 Symbol symbol = (Symbol) current;
66 report_error(filename+":"+(symbol.line+1)+": Syntax error at column "
67 + (LineCount.getColumn(symbol.left)+1) +": " + current.value, current);
72 public void report_fatal_error (String message, Object info) {
75 report_error(message, info);
76 CUP$TDLParser$actions.errors = true;
79 public int curPos () {
80 return cur_token.left;
83 public int curLine (int back) {
84 Stack st = new Stack();
87 for (i = 0; i < back; i++) {
91 java_cup.runtime.Symbol s;
92 s = (java_cup.runtime.Symbol) st.peek();
94 for (i = 0; i < back; i++) {
98 return LineCount.getLine(s.left);
103 // TERMINALS /////////////////////////////////////////////////////////////
108 terminal String DECIMAL;
109 terminal String CHAR;
110 terminal String STRING;
116 terminal OPENBRACKET;
117 terminal CLOSEBRACKET;
186 // NON-TERMINALS /////////////////////////////////////////////////////////
190 ------------------------------------------------------------------------*/
191 nonterminal ParseNode structures;
192 nonterminal ParseNode structure;
193 nonterminal ParseNode optsubtype;
194 nonterminal ParseNode labelsandfields;
195 nonterminal ParseNode label;
196 nonterminal ParseNode field;
197 nonterminal ParseNode optptr;
198 nonterminal ParseNode type;
199 nonterminal ParseNode primtype;
200 nonterminal ParseNode optindex;
201 nonterminal ParseNode expr;
202 nonterminal ParseNode simple_expr;
203 nonterminal ParseNode location;
204 nonterminal ParseNode operator;
205 nonterminal ParseNode literal;
209 precedence right EQ, NE;
210 precedence right LT, LE, GE, GT;
211 precedence left ADD, SUB;
212 precedence left MULT, DIV;
216 // PRODUCTION RULES /////////////////////////////////////////////////////
218 start with structures;
222 structures:structures structure:structure
224 debugMessage(PRODSTRING);
225 structures.addChild(structure);
229 | structure:structure
231 debugMessage(PRODSTRING);
232 ParseNode structures = new ParseNode("structures", parser.curLine(1));
233 structures.addChild(structure);
240 STRUCTURE ID:typename optsubtype:subtype OPENBRACE labelsandfields:lf CLOSEBRACE
242 debugMessage(PRODSTRING);
243 ParseNode structure = new ParseNode("structure", parser.curLine(6));
244 structure.addChild("name", parser.curLine(5)).addChild(typename);
245 if (subtype != null) {
246 structure.addChild(subtype);
248 structure.addChild(lf);
252 STRUCTURE ID:typename optsubtype:subtype OPENBRACE CLOSEBRACE
254 debugMessage(PRODSTRING);
255 ParseNode structure = new ParseNode("structure", parser.curLine(6));
256 structure.addChild("name", parser.curLine(5)).addChild(typename);
257 if (subtype != null) {
258 structure.addChild(subtype);
263 | ID:type MULT ID:name SEMICOLON
265 debugMessage(PRODSTRING);
266 ParseNode global = new ParseNode("global", parser.curLine(4));
267 global.addChild("ptr");
268 global.addChild("type").addChild(type);
269 global.addChild("name").addChild(name);
273 | primtype:type ID:name SEMICOLON
275 debugMessage(PRODSTRING);
276 ParseNode global = new ParseNode("global", parser.curLine(4));
277 global.addChild(type);
278 global.addChild("name").addChild(name);
287 debugMessage(PRODSTRING);
288 ParseNode subtype = new ParseNode("subtype", parser.curLine(3));
289 subtype.addChild(type);
297 debugMessage(PRODSTRING);
298 ParseNode subtype = new ParseNode("subclass", parser.curLine(3));
299 subtype.addChild(type);
305 debugMessage(PRODSTRING);
312 labelsandfields:lf label:label
314 debugMessage(PRODSTRING);
315 lf.getChild("labels").addChild(label);
319 | labelsandfields:lf field:field
321 debugMessage(PRODSTRING);
322 lf.getChild("fields").addChild(field);
328 debugMessage(PRODSTRING);
329 ParseNode lf = new ParseNode("lf");
330 lf.addChild("labels", parser.curLine(1)).addChild(label);
331 lf.addChild("fields", parser.curLine(1));
337 debugMessage(PRODSTRING);
338 ParseNode lf = new ParseNode("lf");
339 lf.addChild("fields", parser.curLine(1)).addChild(field);
340 lf.addChild("labels", parser.curLine(1));
347 LABEL ID:field optindex:index COLON type:type ID:name SEMICOLON
349 debugMessage(PRODSTRING);
350 ParseNode label = new ParseNode("label", parser.curLine(6));
351 label.addChild("name", parser.curLine(2)).addChild(name);
353 label.addChild(index);
355 label.addChild(type);
356 label.addChild("field", parser.curLine(5)).addChild(field);
363 OPENBRACKET expr:expr CLOSEBRACKET
365 debugMessage(PRODSTRING);
366 ParseNode index = new ParseNode("index", parser.curLine(2));
367 index.addChild(expr);
373 debugMessage(PRODSTRING);
380 RESERVED type:type optindex:index SEMICOLON
382 debugMessage(PRODSTRING);
383 ParseNode field = new ParseNode("field", parser.curLine(4));
384 field.addChild(type);
385 field.addChild("reserved");
387 field.addChild(index);
392 | type:type optptr:optptr ID:name optindex:index SEMICOLON
394 debugMessage(PRODSTRING);
395 ParseNode field = new ParseNode("field", parser.curLine(5));
396 field.addChild(type);
397 if (optptr != null) {
398 field.addChild(optptr);
400 field.addChild("name", parser.curLine(3)).addChild(name);
402 field.addChild(index);
412 debugMessage(PRODSTRING);
413 RESULT = new ParseNode("*", parser.curLine(1));
418 debugMessage(PRODSTRING);
424 /*** expression interface *********************************/
430 debugMessage(PRODSTRING);
431 ParseNode se = new ParseNode("simple_expr", parser.curLine(1));
432 se.addChild(location);
441 debugMessage(PRODSTRING);
442 ParseNode loc = new ParseNode("location", parser.curLine(1));
443 loc.addChild("var").addChild(var);
447 | simple_expr:dotexpr DOT ID:field
449 debugMessage(PRODSTRING);
450 ParseNode dot = (new ParseNode("location", parser.curLine(3))).addChild("dot");
451 dot.addChild(dotexpr);
452 dot.addChild("field", parser.curLine(1)).addChild(field);
453 RESULT = dot.getRoot();
456 | simple_expr:dotexpr DOT ID:field OPENBRACKET expr:index CLOSEBRACKET
458 debugMessage(PRODSTRING);
459 ParseNode dot = (new ParseNode("location", parser.curLine(6))).addChild("dot");
460 dot.addChild(dotexpr);
461 dot.addChild("field", parser.curLine(4)).addChild(field);
462 dot.addChild("index", parser.curLine(2)).addChild(index);
463 RESULT = dot.getRoot();
466 | CAST OPENPAREN ID:type COMMA simple_expr:expr CLOSEPAREN
468 debugMessage(PRODSTRING);
469 ParseNode cast = (new ParseNode("location", parser.curLine(6))).addChild("cast");
470 cast.addChild("type").addChild(type);
472 RESULT = cast.getRoot();
480 debugMessage(PRODSTRING);
481 ParseNode expr = new ParseNode("expr", parser.curLine(1));
482 ParseNode sexpr = new ParseNode("simple_expr", parser.curLine(1));
483 ParseNode loc = new ParseNode("location", parser.curLine(1));
484 loc.addChild("thisfield",parser.curLine(1)).addChild(field);
486 expr.addChild(sexpr);
491 debugMessage(PRODSTRING);
492 ParseNode expr = new ParseNode("expr", parser.curLine(1));
497 | OPENPAREN expr:expr CLOSEPAREN
499 debugMessage(PRODSTRING);
505 debugMessage(PRODSTRING);
506 ParseNode expr = new ParseNode("expr", parser.curLine(4));
507 expr.addChild(literal);
511 | expr:expr1 operator:operator expr:expr2
513 debugMessage(PRODSTRING);
514 ParseNode op = (new ParseNode("expr", parser.curLine(3))).addChild("operator");
515 op.addChild("op").addChild(operator);
516 op.addChild("left", parser.curLine(3)).addChild(expr1);
517 op.addChild("right", parser.curLine(1)).addChild(expr2);
518 RESULT = op.getRoot();
522 /**** standard ***************************************************/
528 debugMessage(PRODSTRING);
529 RESULT = new ParseNode("add", parser.curLine(1));
534 debugMessage(PRODSTRING);
535 RESULT = new ParseNode("sub", parser.curLine(1));
540 debugMessage(PRODSTRING);
541 RESULT = new ParseNode("mult", parser.curLine(1));
546 debugMessage(PRODSTRING);
547 RESULT = new ParseNode("div", parser.curLine(1));
555 debugMessage(PRODSTRING);
556 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("true").getRoot();
561 debugMessage(PRODSTRING);
562 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("false").getRoot();
567 debugMessage(PRODSTRING);
568 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild(dec).getRoot();
573 debugMessage(PRODSTRING);
574 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild("-"+dec).getRoot();
579 debugMessage(PRODSTRING);
580 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("string").addChild(str).getRoot();
585 debugMessage(PRODSTRING);
586 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("char").addChild(chr).getRoot();
589 | LITERAL OPENPAREN ID:literal CLOSEPAREN
591 debugMessage(PRODSTRING);
592 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("token").addChild(literal).getRoot();
600 debugMessage(PRODSTRING);
601 ParseNode type = new ParseNode("type", parser.curLine(1));
602 type.addChild("bit");
608 debugMessage(PRODSTRING);
609 ParseNode type = new ParseNode("type", parser.curLine(1));
610 type.addChild("byte");
616 debugMessage(PRODSTRING);
617 ParseNode type = new ParseNode("type", parser.curLine(1));
618 type.addChild("short");
624 debugMessage(PRODSTRING);
625 ParseNode type = new ParseNode("type", parser.curLine(1));
626 type.addChild("int");
632 debugMessage(PRODSTRING);
633 ParseNode type = new ParseNode("type", parser.curLine(1));
634 type.addChild(typename);
643 debugMessage(PRODSTRING);
644 ParseNode type = new ParseNode("type", parser.curLine(1));
645 type.addChild("bit");
651 debugMessage(PRODSTRING);
652 ParseNode type = new ParseNode("type", parser.curLine(1));
653 type.addChild("byte");
659 debugMessage(PRODSTRING);
660 ParseNode type = new ParseNode("type", parser.curLine(1));
661 type.addChild("short");
667 debugMessage(PRODSTRING);
668 ParseNode type = new ParseNode("type", parser.curLine(1));
669 type.addChild("int");