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 optindex;
198 nonterminal ParseNode expr;
199 nonterminal ParseNode simple_expr;
200 nonterminal ParseNode location;
201 nonterminal ParseNode operator;
202 nonterminal ParseNode literal;
206 precedence right EQ, NE;
207 precedence right LT, LE, GE, GT;
208 precedence left ADD, SUB;
209 precedence left MULT, DIV;
213 // PRODUCTION RULES /////////////////////////////////////////////////////
215 start with structures;
219 structures:structures structure:structure
221 debugMessage(PRODSTRING);
222 structures.addChild(structure);
226 | structure:structure
228 debugMessage(PRODSTRING);
229 ParseNode structures = new ParseNode("structures", parser.curLine(1));
230 structures.addChild(structure);
237 STRUCTURE ID:typename optsubtype:subtype OPENBRACE labelsandfields:lf CLOSEBRACE
239 debugMessage(PRODSTRING);
240 ParseNode structure = new ParseNode("structure", parser.curLine(6));
241 structure.addChild("name", parser.curLine(5)).addChild(typename);
242 if (subtype != null) {
243 structure.addChild(subtype);
245 structure.addChild(lf);
249 | ID:type MULT ID:name SEMICOLON
251 debugMessage(PRODSTRING);
252 ParseNode global = new ParseNode("global", parser.curLine(4));
253 global.addChild("type").addChild(type);
254 global.addChild("name").addChild(name);
263 debugMessage(PRODSTRING);
264 ParseNode subtype = new ParseNode("subtype", parser.curLine(3));
265 subtype.addChild(type);
273 debugMessage(PRODSTRING);
274 ParseNode subtype = new ParseNode("subclass", parser.curLine(3));
275 subtype.addChild(type);
281 debugMessage(PRODSTRING);
288 labelsandfields:lf label:label
290 debugMessage(PRODSTRING);
291 lf.getChild("labels").addChild(label);
295 | labelsandfields:lf field:field
297 debugMessage(PRODSTRING);
298 lf.getChild("fields").addChild(field);
304 debugMessage(PRODSTRING);
305 ParseNode lf = new ParseNode("lf");
306 lf.addChild("labels", parser.curLine(1)).addChild(label);
307 lf.addChild("fields", parser.curLine(1));
313 debugMessage(PRODSTRING);
314 ParseNode lf = new ParseNode("lf");
315 lf.addChild("fields", parser.curLine(1)).addChild(field);
316 lf.addChild("labels", parser.curLine(1));
323 LABEL ID:field optindex:index COLON type:type ID:name SEMICOLON
325 debugMessage(PRODSTRING);
326 ParseNode label = new ParseNode("label", parser.curLine(6));
327 label.addChild("name", parser.curLine(2)).addChild(name);
329 label.addChild(index);
331 label.addChild(type);
332 label.addChild("field", parser.curLine(5)).addChild(field);
339 OPENBRACKET expr:expr CLOSEBRACKET
341 debugMessage(PRODSTRING);
342 ParseNode index = new ParseNode("index", parser.curLine(2));
343 index.addChild(expr);
349 debugMessage(PRODSTRING);
356 RESERVED type:type optindex:index SEMICOLON
358 debugMessage(PRODSTRING);
359 ParseNode field = new ParseNode("field", parser.curLine(4));
360 field.addChild(type);
361 field.addChild("reserved");
363 field.addChild(index);
368 | type:type optptr:optptr ID:name optindex:index SEMICOLON
370 debugMessage(PRODSTRING);
371 ParseNode field = new ParseNode("field", parser.curLine(5));
372 field.addChild(type);
373 if (optptr != null) {
374 field.addChild(optptr);
376 field.addChild("name", parser.curLine(3)).addChild(name);
378 field.addChild(index);
388 debugMessage(PRODSTRING);
389 RESULT = new ParseNode("*", parser.curLine(1));
394 debugMessage(PRODSTRING);
400 /*** expression interface *********************************/
406 debugMessage(PRODSTRING);
407 ParseNode se = new ParseNode("simple_expr", parser.curLine(1));
408 se.addChild(location);
417 debugMessage(PRODSTRING);
418 ParseNode loc = new ParseNode("location", parser.curLine(1));
419 loc.addChild("var").addChild(var);
423 | simple_expr:dotexpr DOT ID:field
425 debugMessage(PRODSTRING);
426 ParseNode dot = (new ParseNode("location", parser.curLine(3))).addChild("dot");
427 dot.addChild(dotexpr);
428 dot.addChild("field", parser.curLine(1)).addChild(field);
429 RESULT = dot.getRoot();
432 | simple_expr:dotexpr DOT ID:field OPENBRACKET expr:index CLOSEBRACKET
434 debugMessage(PRODSTRING);
435 ParseNode dot = (new ParseNode("location", parser.curLine(6))).addChild("dot");
436 dot.addChild(dotexpr);
437 dot.addChild("field", parser.curLine(4)).addChild(field);
438 dot.addChild("index", parser.curLine(2)).addChild(index);
439 RESULT = dot.getRoot();
442 | CAST OPENPAREN ID:type COMMA simple_expr:expr CLOSEPAREN
444 debugMessage(PRODSTRING);
445 ParseNode cast = (new ParseNode("location", parser.curLine(6))).addChild("cast");
446 cast.addChild("type").addChild(type);
448 RESULT = cast.getRoot();
456 debugMessage(PRODSTRING);
457 ParseNode expr = new ParseNode("expr", parser.curLine(1));
462 | OPENPAREN expr:expr CLOSEPAREN
464 debugMessage(PRODSTRING);
470 debugMessage(PRODSTRING);
471 ParseNode expr = new ParseNode("expr", parser.curLine(4));
472 expr.addChild(literal);
476 | expr:expr1 operator:operator expr:expr2
478 debugMessage(PRODSTRING);
479 ParseNode op = (new ParseNode("expr", parser.curLine(3))).addChild("operator");
480 op.addChild("op").addChild(operator);
481 op.addChild("left", parser.curLine(3)).addChild(expr1);
482 op.addChild("right", parser.curLine(1)).addChild(expr2);
483 RESULT = op.getRoot();
487 /**** standard ***************************************************/
493 debugMessage(PRODSTRING);
494 RESULT = new ParseNode("add", parser.curLine(1));
499 debugMessage(PRODSTRING);
500 RESULT = new ParseNode("sub", parser.curLine(1));
505 debugMessage(PRODSTRING);
506 RESULT = new ParseNode("mult", parser.curLine(1));
511 debugMessage(PRODSTRING);
512 RESULT = new ParseNode("div", parser.curLine(1));
520 debugMessage(PRODSTRING);
521 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("true").getRoot();
526 debugMessage(PRODSTRING);
527 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("false").getRoot();
532 debugMessage(PRODSTRING);
533 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild(dec).getRoot();
538 debugMessage(PRODSTRING);
539 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("string").addChild(str).getRoot();
544 debugMessage(PRODSTRING);
545 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("char").addChild(chr).getRoot();
548 | LITERAL OPENPAREN ID:literal CLOSEPAREN
550 debugMessage(PRODSTRING);
551 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("token").addChild(literal).getRoot();
559 debugMessage(PRODSTRING);
560 ParseNode type = new ParseNode("type", parser.curLine(1));
561 type.addChild("bit");
567 debugMessage(PRODSTRING);
568 ParseNode type = new ParseNode("type", parser.curLine(1));
569 type.addChild("byte");
575 debugMessage(PRODSTRING);
576 ParseNode type = new ParseNode("type", parser.curLine(1));
577 type.addChild("short");
583 debugMessage(PRODSTRING);
584 ParseNode type = new ParseNode("type", parser.curLine(1));
585 type.addChild("int");
591 debugMessage(PRODSTRING);
592 ParseNode type = new ParseNode("type", parser.curLine(1));
593 type.addChild(typename);