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;
178 // NON-TERMINALS /////////////////////////////////////////////////////////
182 ------------------------------------------------------------------------*/
183 nonterminal ParseNode structures;
184 nonterminal ParseNode structure;
185 nonterminal ParseNode optsubtype;
186 nonterminal ParseNode labelsandfields;
187 nonterminal ParseNode label;
188 nonterminal ParseNode field;
189 nonterminal ParseNode optptr;
190 nonterminal ParseNode type;
191 nonterminal ParseNode optindex;
192 nonterminal ParseNode expr;
193 nonterminal ParseNode simple_expr;
194 nonterminal ParseNode location;
195 nonterminal ParseNode operator;
196 nonterminal ParseNode literal;
200 precedence right EQ, NE;
201 precedence right LT, LE, GE, GT;
202 precedence left ADD, SUB;
203 precedence left MULT, DIV;
207 // PRODUCTION RULES /////////////////////////////////////////////////////
209 start with structures;
213 structures:structures structure:structure
215 debugMessage(PRODSTRING);
216 structures.addChild(structure);
220 | structure:structure
222 debugMessage(PRODSTRING);
223 ParseNode structures = new ParseNode("structures", parser.curLine(1));
224 structures.addChild(structure);
231 STRUCTURE ID:typename optsubtype:subtype OPENBRACE labelsandfields:lf CLOSEBRACE
233 debugMessage(PRODSTRING);
234 ParseNode structure = new ParseNode("structure", parser.curLine(6));
235 structure.addChild("name", parser.curLine(5)).addChild(typename);
236 if (subtype != null) {
237 structure.addChild(subtype);
239 structure.addChild(lf);
243 | ID:type MULT ID:name SEMICOLON
245 debugMessage(PRODSTRING);
246 ParseNode global = new ParseNode("global", parser.curLine(4));
247 global.addChild("type").addChild(type);
248 global.addChild("name").addChild(name);
257 debugMessage(PRODSTRING);
258 ParseNode subtype = new ParseNode("subtype", parser.curLine(3));
259 subtype.addChild(type);
265 debugMessage(PRODSTRING);
272 labelsandfields:lf label:label
274 debugMessage(PRODSTRING);
275 lf.getChild("labels").addChild(label);
279 | labelsandfields:lf field:field
281 debugMessage(PRODSTRING);
282 lf.getChild("fields").addChild(field);
288 debugMessage(PRODSTRING);
289 ParseNode lf = new ParseNode("lf");
290 lf.addChild("labels", parser.curLine(1)).addChild(label);
291 lf.addChild("fields", parser.curLine(1));
297 debugMessage(PRODSTRING);
298 ParseNode lf = new ParseNode("lf");
299 lf.addChild("fields", parser.curLine(1)).addChild(field);
300 lf.addChild("labels", parser.curLine(1));
307 LABEL ID:field optindex:index COLON type:type ID:name SEMICOLON
309 debugMessage(PRODSTRING);
310 ParseNode label = new ParseNode("label", parser.curLine(6));
311 label.addChild("name", parser.curLine(2)).addChild(name);
313 label.addChild(index);
315 label.addChild(type);
316 label.addChild("field", parser.curLine(5)).addChild(field);
323 OPENBRACKET expr:expr CLOSEBRACKET
325 debugMessage(PRODSTRING);
326 ParseNode index = new ParseNode("index", parser.curLine(2));
327 index.addChild(expr);
333 debugMessage(PRODSTRING);
340 RESERVED type:type optindex:index SEMICOLON
342 debugMessage(PRODSTRING);
343 ParseNode field = new ParseNode("field", parser.curLine(4));
344 field.addChild(type);
345 field.addChild("reserved");
347 field.addChild(index);
352 | type:type optptr:optptr ID:name optindex:index SEMICOLON
354 debugMessage(PRODSTRING);
355 ParseNode field = new ParseNode("field", parser.curLine(5));
356 field.addChild(type);
357 if (optptr != null) {
358 field.addChild(optptr);
360 field.addChild("name", parser.curLine(3)).addChild(name);
362 field.addChild(index);
372 debugMessage(PRODSTRING);
373 RESULT = new ParseNode("*", parser.curLine(1));
378 debugMessage(PRODSTRING);
384 /*** expression interface *********************************/
390 debugMessage(PRODSTRING);
391 ParseNode se = new ParseNode("simple_expr", parser.curLine(1));
392 se.addChild(location);
401 debugMessage(PRODSTRING);
402 ParseNode loc = new ParseNode("location", parser.curLine(1));
403 loc.addChild("var").addChild(var);
407 | simple_expr:dotexpr DOT ID:field
409 debugMessage(PRODSTRING);
410 ParseNode dot = (new ParseNode("location", parser.curLine(3))).addChild("dot");
411 dot.addChild(dotexpr);
412 dot.addChild("field", parser.curLine(1)).addChild(field);
413 RESULT = dot.getRoot();
416 | simple_expr:dotexpr DOT ID:field OPENBRACKET expr:index CLOSEBRACKET
418 debugMessage(PRODSTRING);
419 ParseNode dot = (new ParseNode("location", parser.curLine(6))).addChild("dot");
420 dot.addChild(dotexpr);
421 dot.addChild("field", parser.curLine(4)).addChild(field);
422 dot.addChild("index", parser.curLine(2)).addChild(index);
423 RESULT = dot.getRoot();
426 | CAST OPENPAREN ID:type COMMA simple_expr:expr CLOSEPAREN
428 debugMessage(PRODSTRING);
429 ParseNode cast = (new ParseNode("location", parser.curLine(6))).addChild("cast");
430 cast.addChild("type").addChild(type);
432 RESULT = cast.getRoot();
440 debugMessage(PRODSTRING);
441 ParseNode expr = new ParseNode("expr", parser.curLine(1));
446 | OPENPAREN expr:expr CLOSEPAREN
448 debugMessage(PRODSTRING);
452 | LITERAL OPENPAREN literal:literal CLOSEPAREN
454 debugMessage(PRODSTRING);
455 ParseNode expr = new ParseNode("expr", parser.curLine(4));
456 expr.addChild(literal);
460 | expr:expr1 operator:operator expr:expr2
462 debugMessage(PRODSTRING);
463 ParseNode op = (new ParseNode("expr", parser.curLine(3))).addChild("operator");
464 op.addChild("op").addChild(operator);
465 op.addChild("left", parser.curLine(3)).addChild(expr1);
466 op.addChild("right", parser.curLine(1)).addChild(expr2);
467 RESULT = op.getRoot();
471 /**** standard ***************************************************/
477 debugMessage(PRODSTRING);
478 RESULT = new ParseNode("add", parser.curLine(1));
483 debugMessage(PRODSTRING);
484 RESULT = new ParseNode("sub", parser.curLine(1));
489 debugMessage(PRODSTRING);
490 RESULT = new ParseNode("mult", parser.curLine(1));
495 debugMessage(PRODSTRING);
496 RESULT = new ParseNode("div", parser.curLine(1));
504 debugMessage(PRODSTRING);
505 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("true").getRoot();
510 debugMessage(PRODSTRING);
511 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild(dec).getRoot();
516 debugMessage(PRODSTRING);
517 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("string").addChild(str).getRoot();
522 debugMessage(PRODSTRING);
523 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("char").addChild(chr).getRoot();
528 debugMessage(PRODSTRING);
529 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("token").addChild(literal).getRoot();
537 debugMessage(PRODSTRING);
538 ParseNode type = new ParseNode("type", parser.curLine(1));
539 type.addChild("bit");
545 debugMessage(PRODSTRING);
546 ParseNode type = new ParseNode("type", parser.curLine(1));
547 type.addChild("byte");
553 debugMessage(PRODSTRING);
554 ParseNode type = new ParseNode("type", parser.curLine(1));
555 type.addChild("short");
561 debugMessage(PRODSTRING);
562 ParseNode type = new ParseNode("type", parser.curLine(1));
563 type.addChild("int");
569 debugMessage(PRODSTRING);
570 ParseNode type = new ParseNode("type", parser.curLine(1));
571 type.addChild(typename);