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 void syntax_error (java_cup.runtime.Symbol current) {
62 CUP$TDLParser$actions.errors = true;
63 Symbol symbol = (Symbol) current;
64 report_error("TDL: Syntax error at line " + (symbol.line + 1)
65 + ", column " + LineCount.getColumn(symbol.left) + ": " + current.value, current);
68 public void report_fatal_error (String message, Object info) {
71 report_error(message, info);
72 CUP$TDLParser$actions.errors = true;
75 public int curPos () {
76 return cur_token.left;
79 public int curLine (int back) {
80 Stack st = new Stack();
83 for (i = 0; i < back; i++) {
87 java_cup.runtime.Symbol s;
88 s = (java_cup.runtime.Symbol) st.peek();
90 for (i = 0; i < back; i++) {
94 return LineCount.getLine(s.left);
99 // TERMINALS /////////////////////////////////////////////////////////////
104 terminal String DECIMAL;
105 terminal String CHAR;
106 terminal String STRING;
112 terminal OPENBRACKET;
113 terminal CLOSEBRACKET;
179 // NON-TERMINALS /////////////////////////////////////////////////////////
183 ------------------------------------------------------------------------*/
184 nonterminal ParseNode structures;
185 nonterminal ParseNode structure;
186 nonterminal ParseNode optsubtype;
187 nonterminal ParseNode labelsandfields;
188 nonterminal ParseNode label;
189 nonterminal ParseNode field;
190 nonterminal ParseNode optptr;
191 nonterminal ParseNode type;
192 nonterminal ParseNode optindex;
193 nonterminal ParseNode expr;
194 nonterminal ParseNode simple_expr;
195 nonterminal ParseNode location;
196 nonterminal ParseNode operator;
197 nonterminal ParseNode literal;
201 precedence right EQ, NE;
202 precedence right LT, LE, GE, GT;
203 precedence left ADD, SUB;
204 precedence left MULT, DIV;
208 // PRODUCTION RULES /////////////////////////////////////////////////////
210 start with structures;
214 structures:structures structure:structure
216 debugMessage(PRODSTRING);
217 structures.addChild(structure);
221 | structure:structure
223 debugMessage(PRODSTRING);
224 ParseNode structures = new ParseNode("structures", parser.curLine(1));
225 structures.addChild(structure);
232 STRUCTURE ID:typename optsubtype:subtype OPENBRACE labelsandfields:lf CLOSEBRACE
234 debugMessage(PRODSTRING);
235 ParseNode structure = new ParseNode("structure", parser.curLine(6));
236 structure.addChild("name", parser.curLine(5)).addChild(typename);
237 if (subtype != null) {
238 structure.addChild(subtype);
240 structure.addChild(lf);
244 | ID:type MULT ID:name SEMICOLON
246 debugMessage(PRODSTRING);
247 ParseNode global = new ParseNode("global", parser.curLine(4));
248 global.addChild("type").addChild(type);
249 global.addChild("name").addChild(name);
258 debugMessage(PRODSTRING);
259 ParseNode subtype = new ParseNode("subtype", parser.curLine(3));
260 subtype.addChild(type);
266 debugMessage(PRODSTRING);
273 labelsandfields:lf label:label
275 debugMessage(PRODSTRING);
276 lf.getChild("labels").addChild(label);
280 | labelsandfields:lf field:field
282 debugMessage(PRODSTRING);
283 lf.getChild("fields").addChild(field);
289 debugMessage(PRODSTRING);
290 ParseNode lf = new ParseNode("lf");
291 lf.addChild("labels", parser.curLine(1)).addChild(label);
292 lf.addChild("fields", parser.curLine(1));
298 debugMessage(PRODSTRING);
299 ParseNode lf = new ParseNode("lf");
300 lf.addChild("fields", parser.curLine(1)).addChild(field);
301 lf.addChild("labels", parser.curLine(1));
308 LABEL ID:field optindex:index COLON type:type ID:name SEMICOLON
310 debugMessage(PRODSTRING);
311 ParseNode label = new ParseNode("label", parser.curLine(6));
312 label.addChild("name", parser.curLine(2)).addChild(name);
314 label.addChild(index);
316 label.addChild(type);
317 label.addChild("field", parser.curLine(5)).addChild(field);
324 OPENBRACKET expr:expr CLOSEBRACKET
326 debugMessage(PRODSTRING);
327 ParseNode index = new ParseNode("index", parser.curLine(2));
328 index.addChild(expr);
334 debugMessage(PRODSTRING);
341 RESERVED type:type optindex:index SEMICOLON
343 debugMessage(PRODSTRING);
344 ParseNode field = new ParseNode("field", parser.curLine(4));
345 field.addChild(type);
346 field.addChild("reserved");
348 field.addChild(index);
353 | type:type optptr:optptr ID:name optindex:index SEMICOLON
355 debugMessage(PRODSTRING);
356 ParseNode field = new ParseNode("field", parser.curLine(5));
357 field.addChild(type);
358 if (optptr != null) {
359 field.addChild(optptr);
361 field.addChild("name", parser.curLine(3)).addChild(name);
363 field.addChild(index);
373 debugMessage(PRODSTRING);
374 RESULT = new ParseNode("*", parser.curLine(1));
379 debugMessage(PRODSTRING);
385 /*** expression interface *********************************/
391 debugMessage(PRODSTRING);
392 ParseNode se = new ParseNode("simple_expr", parser.curLine(1));
393 se.addChild(location);
402 debugMessage(PRODSTRING);
403 ParseNode loc = new ParseNode("location", parser.curLine(1));
404 loc.addChild("var").addChild(var);
408 | simple_expr:dotexpr DOT ID:field
410 debugMessage(PRODSTRING);
411 ParseNode dot = (new ParseNode("location", parser.curLine(3))).addChild("dot");
412 dot.addChild(dotexpr);
413 dot.addChild("field", parser.curLine(1)).addChild(field);
414 RESULT = dot.getRoot();
417 | simple_expr:dotexpr DOT ID:field OPENBRACKET expr:index CLOSEBRACKET
419 debugMessage(PRODSTRING);
420 ParseNode dot = (new ParseNode("location", parser.curLine(6))).addChild("dot");
421 dot.addChild(dotexpr);
422 dot.addChild("field", parser.curLine(4)).addChild(field);
423 dot.addChild("index", parser.curLine(2)).addChild(index);
424 RESULT = dot.getRoot();
427 | CAST OPENPAREN ID:type COMMA simple_expr:expr CLOSEPAREN
429 debugMessage(PRODSTRING);
430 ParseNode cast = (new ParseNode("location", parser.curLine(6))).addChild("cast");
431 cast.addChild("type").addChild(type);
433 RESULT = cast.getRoot();
441 debugMessage(PRODSTRING);
442 ParseNode expr = new ParseNode("expr", parser.curLine(1));
447 | OPENPAREN expr:expr CLOSEPAREN
449 debugMessage(PRODSTRING);
453 | LITERAL OPENPAREN literal:literal CLOSEPAREN
455 debugMessage(PRODSTRING);
456 ParseNode expr = new ParseNode("expr", parser.curLine(4));
457 expr.addChild(literal);
461 | expr:expr1 operator:operator expr:expr2
463 debugMessage(PRODSTRING);
464 ParseNode op = (new ParseNode("expr", parser.curLine(3))).addChild("operator");
465 op.addChild("op").addChild(operator);
466 op.addChild("left", parser.curLine(3)).addChild(expr1);
467 op.addChild("right", parser.curLine(1)).addChild(expr2);
468 RESULT = op.getRoot();
472 /**** standard ***************************************************/
478 debugMessage(PRODSTRING);
479 RESULT = new ParseNode("add", parser.curLine(1));
484 debugMessage(PRODSTRING);
485 RESULT = new ParseNode("sub", parser.curLine(1));
490 debugMessage(PRODSTRING);
491 RESULT = new ParseNode("mult", parser.curLine(1));
496 debugMessage(PRODSTRING);
497 RESULT = new ParseNode("div", parser.curLine(1));
505 debugMessage(PRODSTRING);
506 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("true").getRoot();
511 debugMessage(PRODSTRING);
512 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("false").getRoot();
517 debugMessage(PRODSTRING);
518 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild(dec).getRoot();
523 debugMessage(PRODSTRING);
524 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("string").addChild(str).getRoot();
529 debugMessage(PRODSTRING);
530 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("char").addChild(chr).getRoot();
535 debugMessage(PRODSTRING);
536 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("token").addChild(literal).getRoot();
544 debugMessage(PRODSTRING);
545 ParseNode type = new ParseNode("type", parser.curLine(1));
546 type.addChild("bit");
552 debugMessage(PRODSTRING);
553 ParseNode type = new ParseNode("type", parser.curLine(1));
554 type.addChild("byte");
560 debugMessage(PRODSTRING);
561 ParseNode type = new ParseNode("type", parser.curLine(1));
562 type.addChild("short");
568 debugMessage(PRODSTRING);
569 ParseNode type = new ParseNode("type", parser.curLine(1));
570 type.addChild("int");
576 debugMessage(PRODSTRING);
577 ParseNode type = new ParseNode("type", parser.curLine(1));
578 type.addChild(typename);