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;
183 // NON-TERMINALS /////////////////////////////////////////////////////////
187 ------------------------------------------------------------------------*/
188 nonterminal ParseNode structures;
189 nonterminal ParseNode structure;
190 nonterminal ParseNode optsubtype;
191 nonterminal ParseNode labelsandfields;
192 nonterminal ParseNode label;
193 nonterminal ParseNode field;
194 nonterminal ParseNode optptr;
195 nonterminal ParseNode type;
196 nonterminal ParseNode optindex;
197 nonterminal ParseNode expr;
198 nonterminal ParseNode simple_expr;
199 nonterminal ParseNode location;
200 nonterminal ParseNode operator;
201 nonterminal ParseNode literal;
205 precedence right EQ, NE;
206 precedence right LT, LE, GE, GT;
207 precedence left ADD, SUB;
208 precedence left MULT, DIV;
212 // PRODUCTION RULES /////////////////////////////////////////////////////
214 start with structures;
218 structures:structures structure:structure
220 debugMessage(PRODSTRING);
221 structures.addChild(structure);
225 | structure:structure
227 debugMessage(PRODSTRING);
228 ParseNode structures = new ParseNode("structures", parser.curLine(1));
229 structures.addChild(structure);
236 STRUCTURE ID:typename optsubtype:subtype OPENBRACE labelsandfields:lf CLOSEBRACE
238 debugMessage(PRODSTRING);
239 ParseNode structure = new ParseNode("structure", parser.curLine(6));
240 structure.addChild("name", parser.curLine(5)).addChild(typename);
241 if (subtype != null) {
242 structure.addChild(subtype);
244 structure.addChild(lf);
248 | ID:type MULT ID:name SEMICOLON
250 debugMessage(PRODSTRING);
251 ParseNode global = new ParseNode("global", parser.curLine(4));
252 global.addChild("type").addChild(type);
253 global.addChild("name").addChild(name);
262 debugMessage(PRODSTRING);
263 ParseNode subtype = new ParseNode("subtype", parser.curLine(3));
264 subtype.addChild(type);
270 debugMessage(PRODSTRING);
277 labelsandfields:lf label:label
279 debugMessage(PRODSTRING);
280 lf.getChild("labels").addChild(label);
284 | labelsandfields:lf field:field
286 debugMessage(PRODSTRING);
287 lf.getChild("fields").addChild(field);
293 debugMessage(PRODSTRING);
294 ParseNode lf = new ParseNode("lf");
295 lf.addChild("labels", parser.curLine(1)).addChild(label);
296 lf.addChild("fields", parser.curLine(1));
302 debugMessage(PRODSTRING);
303 ParseNode lf = new ParseNode("lf");
304 lf.addChild("fields", parser.curLine(1)).addChild(field);
305 lf.addChild("labels", parser.curLine(1));
312 LABEL ID:field optindex:index COLON type:type ID:name SEMICOLON
314 debugMessage(PRODSTRING);
315 ParseNode label = new ParseNode("label", parser.curLine(6));
316 label.addChild("name", parser.curLine(2)).addChild(name);
318 label.addChild(index);
320 label.addChild(type);
321 label.addChild("field", parser.curLine(5)).addChild(field);
328 OPENBRACKET expr:expr CLOSEBRACKET
330 debugMessage(PRODSTRING);
331 ParseNode index = new ParseNode("index", parser.curLine(2));
332 index.addChild(expr);
338 debugMessage(PRODSTRING);
345 RESERVED type:type optindex:index SEMICOLON
347 debugMessage(PRODSTRING);
348 ParseNode field = new ParseNode("field", parser.curLine(4));
349 field.addChild(type);
350 field.addChild("reserved");
352 field.addChild(index);
357 | type:type optptr:optptr ID:name optindex:index SEMICOLON
359 debugMessage(PRODSTRING);
360 ParseNode field = new ParseNode("field", parser.curLine(5));
361 field.addChild(type);
362 if (optptr != null) {
363 field.addChild(optptr);
365 field.addChild("name", parser.curLine(3)).addChild(name);
367 field.addChild(index);
377 debugMessage(PRODSTRING);
378 RESULT = new ParseNode("*", parser.curLine(1));
383 debugMessage(PRODSTRING);
389 /*** expression interface *********************************/
395 debugMessage(PRODSTRING);
396 ParseNode se = new ParseNode("simple_expr", parser.curLine(1));
397 se.addChild(location);
406 debugMessage(PRODSTRING);
407 ParseNode loc = new ParseNode("location", parser.curLine(1));
408 loc.addChild("var").addChild(var);
412 | simple_expr:dotexpr DOT ID:field
414 debugMessage(PRODSTRING);
415 ParseNode dot = (new ParseNode("location", parser.curLine(3))).addChild("dot");
416 dot.addChild(dotexpr);
417 dot.addChild("field", parser.curLine(1)).addChild(field);
418 RESULT = dot.getRoot();
421 | simple_expr:dotexpr DOT ID:field OPENBRACKET expr:index CLOSEBRACKET
423 debugMessage(PRODSTRING);
424 ParseNode dot = (new ParseNode("location", parser.curLine(6))).addChild("dot");
425 dot.addChild(dotexpr);
426 dot.addChild("field", parser.curLine(4)).addChild(field);
427 dot.addChild("index", parser.curLine(2)).addChild(index);
428 RESULT = dot.getRoot();
431 | CAST OPENPAREN ID:type COMMA simple_expr:expr CLOSEPAREN
433 debugMessage(PRODSTRING);
434 ParseNode cast = (new ParseNode("location", parser.curLine(6))).addChild("cast");
435 cast.addChild("type").addChild(type);
437 RESULT = cast.getRoot();
445 debugMessage(PRODSTRING);
446 ParseNode expr = new ParseNode("expr", parser.curLine(1));
451 | OPENPAREN expr:expr CLOSEPAREN
453 debugMessage(PRODSTRING);
457 | LITERAL OPENPAREN literal:literal CLOSEPAREN
459 debugMessage(PRODSTRING);
460 ParseNode expr = new ParseNode("expr", parser.curLine(4));
461 expr.addChild(literal);
465 | expr:expr1 operator:operator expr:expr2
467 debugMessage(PRODSTRING);
468 ParseNode op = (new ParseNode("expr", parser.curLine(3))).addChild("operator");
469 op.addChild("op").addChild(operator);
470 op.addChild("left", parser.curLine(3)).addChild(expr1);
471 op.addChild("right", parser.curLine(1)).addChild(expr2);
472 RESULT = op.getRoot();
476 /**** standard ***************************************************/
482 debugMessage(PRODSTRING);
483 RESULT = new ParseNode("add", parser.curLine(1));
488 debugMessage(PRODSTRING);
489 RESULT = new ParseNode("sub", parser.curLine(1));
494 debugMessage(PRODSTRING);
495 RESULT = new ParseNode("mult", parser.curLine(1));
500 debugMessage(PRODSTRING);
501 RESULT = new ParseNode("div", parser.curLine(1));
509 debugMessage(PRODSTRING);
510 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("true").getRoot();
515 debugMessage(PRODSTRING);
516 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("false").getRoot();
521 debugMessage(PRODSTRING);
522 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild(dec).getRoot();
527 debugMessage(PRODSTRING);
528 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("string").addChild(str).getRoot();
533 debugMessage(PRODSTRING);
534 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("char").addChild(chr).getRoot();
539 debugMessage(PRODSTRING);
540 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("token").addChild(literal).getRoot();
548 debugMessage(PRODSTRING);
549 ParseNode type = new ParseNode("type", parser.curLine(1));
550 type.addChild("bit");
556 debugMessage(PRODSTRING);
557 ParseNode type = new ParseNode("type", parser.curLine(1));
558 type.addChild("byte");
564 debugMessage(PRODSTRING);
565 ParseNode type = new ParseNode("type", parser.curLine(1));
566 type.addChild("short");
572 debugMessage(PRODSTRING);
573 ParseNode type = new ParseNode("type", parser.curLine(1));
574 type.addChild("int");
580 debugMessage(PRODSTRING);
581 ParseNode type = new ParseNode("type", parser.curLine(1));
582 type.addChild(typename);