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;
184 // NON-TERMINALS /////////////////////////////////////////////////////////
188 ------------------------------------------------------------------------*/
189 nonterminal ParseNode structures;
190 nonterminal ParseNode structure;
191 nonterminal ParseNode optsubtype;
192 nonterminal ParseNode labelsandfields;
193 nonterminal ParseNode label;
194 nonterminal ParseNode field;
195 nonterminal ParseNode optptr;
196 nonterminal ParseNode type;
197 nonterminal ParseNode primtype;
198 nonterminal ParseNode optindex;
199 nonterminal ParseNode expr;
200 nonterminal ParseNode simple_expr;
201 nonterminal ParseNode location;
202 nonterminal ParseNode operator;
203 nonterminal ParseNode literal;
207 precedence right EQ, NE;
208 precedence right LT, LE, GE, GT;
209 precedence left ADD, SUB;
210 precedence left MULT, DIV;
214 // PRODUCTION RULES /////////////////////////////////////////////////////
216 start with structures;
220 structures:structures structure:structure
222 debugMessage(PRODSTRING);
223 structures.addChild(structure);
227 | structure:structure
229 debugMessage(PRODSTRING);
230 ParseNode structures = new ParseNode("structures", parser.curLine(1));
231 structures.addChild(structure);
238 STRUCTURE ID:typename optsubtype:subtype OPENBRACE labelsandfields:lf CLOSEBRACE
240 debugMessage(PRODSTRING);
241 ParseNode structure = new ParseNode("structure", parser.curLine(6));
242 structure.addChild("name", parser.curLine(5)).addChild(typename);
243 if (subtype != null) {
244 structure.addChild(subtype);
246 structure.addChild(lf);
250 | ID:type MULT ID:name SEMICOLON
252 debugMessage(PRODSTRING);
253 ParseNode global = new ParseNode("global", parser.curLine(4));
254 global.addChild("ptr");
255 global.addChild("type").addChild(type);
256 global.addChild("name").addChild(name);
260 | primtype:type ID:name SEMICOLON
262 debugMessage(PRODSTRING);
263 ParseNode global = new ParseNode("global", parser.curLine(4));
264 global.addChild(type);
265 global.addChild("name").addChild(name);
274 debugMessage(PRODSTRING);
275 ParseNode subtype = new ParseNode("subtype", parser.curLine(3));
276 subtype.addChild(type);
284 debugMessage(PRODSTRING);
285 ParseNode subtype = new ParseNode("subclass", parser.curLine(3));
286 subtype.addChild(type);
292 debugMessage(PRODSTRING);
299 labelsandfields:lf label:label
301 debugMessage(PRODSTRING);
302 lf.getChild("labels").addChild(label);
306 | labelsandfields:lf field:field
308 debugMessage(PRODSTRING);
309 lf.getChild("fields").addChild(field);
315 debugMessage(PRODSTRING);
316 ParseNode lf = new ParseNode("lf");
317 lf.addChild("labels", parser.curLine(1)).addChild(label);
318 lf.addChild("fields", parser.curLine(1));
324 debugMessage(PRODSTRING);
325 ParseNode lf = new ParseNode("lf");
326 lf.addChild("fields", parser.curLine(1)).addChild(field);
327 lf.addChild("labels", parser.curLine(1));
334 LABEL ID:field optindex:index COLON type:type ID:name SEMICOLON
336 debugMessage(PRODSTRING);
337 ParseNode label = new ParseNode("label", parser.curLine(6));
338 label.addChild("name", parser.curLine(2)).addChild(name);
340 label.addChild(index);
342 label.addChild(type);
343 label.addChild("field", parser.curLine(5)).addChild(field);
350 OPENBRACKET expr:expr CLOSEBRACKET
352 debugMessage(PRODSTRING);
353 ParseNode index = new ParseNode("index", parser.curLine(2));
354 index.addChild(expr);
360 debugMessage(PRODSTRING);
367 RESERVED type:type optindex:index SEMICOLON
369 debugMessage(PRODSTRING);
370 ParseNode field = new ParseNode("field", parser.curLine(4));
371 field.addChild(type);
372 field.addChild("reserved");
374 field.addChild(index);
379 | type:type optptr:optptr ID:name optindex:index SEMICOLON
381 debugMessage(PRODSTRING);
382 ParseNode field = new ParseNode("field", parser.curLine(5));
383 field.addChild(type);
384 if (optptr != null) {
385 field.addChild(optptr);
387 field.addChild("name", parser.curLine(3)).addChild(name);
389 field.addChild(index);
399 debugMessage(PRODSTRING);
400 RESULT = new ParseNode("*", parser.curLine(1));
405 debugMessage(PRODSTRING);
411 /*** expression interface *********************************/
417 debugMessage(PRODSTRING);
418 ParseNode se = new ParseNode("simple_expr", parser.curLine(1));
419 se.addChild(location);
428 debugMessage(PRODSTRING);
429 ParseNode loc = new ParseNode("location", parser.curLine(1));
430 loc.addChild("var").addChild(var);
434 | simple_expr:dotexpr DOT ID:field
436 debugMessage(PRODSTRING);
437 ParseNode dot = (new ParseNode("location", parser.curLine(3))).addChild("dot");
438 dot.addChild(dotexpr);
439 dot.addChild("field", parser.curLine(1)).addChild(field);
440 RESULT = dot.getRoot();
443 | simple_expr:dotexpr DOT ID:field OPENBRACKET expr:index CLOSEBRACKET
445 debugMessage(PRODSTRING);
446 ParseNode dot = (new ParseNode("location", parser.curLine(6))).addChild("dot");
447 dot.addChild(dotexpr);
448 dot.addChild("field", parser.curLine(4)).addChild(field);
449 dot.addChild("index", parser.curLine(2)).addChild(index);
450 RESULT = dot.getRoot();
453 | CAST OPENPAREN ID:type COMMA simple_expr:expr CLOSEPAREN
455 debugMessage(PRODSTRING);
456 ParseNode cast = (new ParseNode("location", parser.curLine(6))).addChild("cast");
457 cast.addChild("type").addChild(type);
459 RESULT = cast.getRoot();
467 debugMessage(PRODSTRING);
468 ParseNode expr = new ParseNode("expr", parser.curLine(1));
473 | OPENPAREN expr:expr CLOSEPAREN
475 debugMessage(PRODSTRING);
481 debugMessage(PRODSTRING);
482 ParseNode expr = new ParseNode("expr", parser.curLine(4));
483 expr.addChild(literal);
487 | expr:expr1 operator:operator expr:expr2
489 debugMessage(PRODSTRING);
490 ParseNode op = (new ParseNode("expr", parser.curLine(3))).addChild("operator");
491 op.addChild("op").addChild(operator);
492 op.addChild("left", parser.curLine(3)).addChild(expr1);
493 op.addChild("right", parser.curLine(1)).addChild(expr2);
494 RESULT = op.getRoot();
498 /**** standard ***************************************************/
504 debugMessage(PRODSTRING);
505 RESULT = new ParseNode("add", parser.curLine(1));
510 debugMessage(PRODSTRING);
511 RESULT = new ParseNode("sub", parser.curLine(1));
516 debugMessage(PRODSTRING);
517 RESULT = new ParseNode("mult", parser.curLine(1));
522 debugMessage(PRODSTRING);
523 RESULT = new ParseNode("div", parser.curLine(1));
531 debugMessage(PRODSTRING);
532 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("true").getRoot();
537 debugMessage(PRODSTRING);
538 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("false").getRoot();
543 debugMessage(PRODSTRING);
544 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild(dec).getRoot();
549 debugMessage(PRODSTRING);
550 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild("-"+dec).getRoot();
555 debugMessage(PRODSTRING);
556 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("string").addChild(str).getRoot();
561 debugMessage(PRODSTRING);
562 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("char").addChild(chr).getRoot();
565 | LITERAL OPENPAREN ID:literal CLOSEPAREN
567 debugMessage(PRODSTRING);
568 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("token").addChild(literal).getRoot();
576 debugMessage(PRODSTRING);
577 ParseNode type = new ParseNode("type", parser.curLine(1));
578 type.addChild("bit");
584 debugMessage(PRODSTRING);
585 ParseNode type = new ParseNode("type", parser.curLine(1));
586 type.addChild("byte");
592 debugMessage(PRODSTRING);
593 ParseNode type = new ParseNode("type", parser.curLine(1));
594 type.addChild("short");
600 debugMessage(PRODSTRING);
601 ParseNode type = new ParseNode("type", parser.curLine(1));
602 type.addChild("int");
608 debugMessage(PRODSTRING);
609 ParseNode type = new ParseNode("type", parser.curLine(1));
610 type.addChild(typename);
619 debugMessage(PRODSTRING);
620 ParseNode type = new ParseNode("type", parser.curLine(1));
621 type.addChild("bit");
627 debugMessage(PRODSTRING);
628 ParseNode type = new ParseNode("type", parser.curLine(1));
629 type.addChild("byte");
635 debugMessage(PRODSTRING);
636 ParseNode type = new ParseNode("type", parser.curLine(1));
637 type.addChild("short");
643 debugMessage(PRODSTRING);
644 ParseNode type = new ParseNode("type", parser.curLine(1));
645 type.addChild("int");