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$CDLParser$actions.errors = true;
65 Symbol symbol = (Symbol) current;
67 boolean isInteger = true;
69 Integer.parseInt(current.value.toString());
70 } catch(NumberFormatException e) { isInteger = false;}
72 report_error(filename+":"+(symbol.line+1)+": Syntax error at column "
73 + (LineCount.getColumn(symbol.left)+1) +": " + current.value, current);
76 if (current.value.equals("true") || isInteger)
77 System.out.println("Did you mean literal("+current.value+")?");
84 public void report_fatal_error (String message, Object info) {
87 report_error(message, info);
88 CUP$CDLParser$actions.errors = true;
91 public int curPos () {
92 return cur_token.left;
95 public int curLine (int back) {
96 Stack st = new Stack();
99 for (i = 0; i < back; i++) {
100 st.push(stack.pop());
103 java_cup.runtime.Symbol s;
104 s = (java_cup.runtime.Symbol) st.peek();
106 for (i = 0; i < back; i++) {
107 stack.push(st.pop());
110 return LineCount.getLine(s.left);
115 // TERMINALS /////////////////////////////////////////////////////////////
120 terminal String DECIMAL;
121 terminal String CHAR;
122 terminal String STRING;
128 terminal OPENBRACKET;
129 terminal CLOSEBRACKET;
197 // NON-TERMINALS /////////////////////////////////////////////////////////
201 ------------------------------------------------------------------------*/
202 nonterminal ParseNode constraints;
203 nonterminal ParseNode constraint;
204 nonterminal ParseNode optcrash;
205 nonterminal ParseNode quantifiers;
206 nonterminal ParseNode quantifier;
207 nonterminal ParseNode set;
208 nonterminal ParseNode listofliterals;
209 nonterminal ParseNode literal;
210 nonterminal ParseNode body;
211 nonterminal ParseNode predicate;
212 nonterminal ParseNode setexpr;
214 nonterminal ParseNode compare;
215 nonterminal ParseNode expr;
216 nonterminal ParseNode operator;
222 precedence left EQ, NE;
223 precedence left LT, LE, GE, GT;
225 precedence left ADD, SUB;
226 precedence left MULT, DIV;
230 precedence left DOT, DOTINV;
232 // PRODUCTION RULES /////////////////////////////////////////////////////
236 constraints:constraints constraint:constraint
238 debugMessage(PRODSTRING);
239 constraints.addChild(constraint);
240 RESULT = constraints;
243 | constraint:constraint
245 debugMessage(PRODSTRING);
246 ParseNode constraints = new ParseNode("constraints", parser.curLine(1));
247 constraints.addChild(constraint);
248 RESULT = constraints;
254 optcrash:crash OPENBRACKET quantifiers:quantifiers CLOSEBRACKET COMMA body:body SEMICOLON
256 debugMessage(PRODSTRING);
257 ParseNode constraint = new ParseNode("constraint", parser.curLine(7));
259 constraint.addChild(crash);
261 if (quantifiers != null) {
262 constraint.addChild(quantifiers);
264 constraint.addChild(body);
273 debugMessage(PRODSTRING);
274 RESULT = new ParseNode("crash", parser.curLine(1));
279 debugMessage(PRODSTRING);
286 quantifiers:quantifiers COMMA quantifier:quantifier
288 debugMessage(PRODSTRING);
289 quantifiers.addChild(quantifier);
290 RESULT = quantifiers;
293 | quantifier:quantifier
295 debugMessage(PRODSTRING);
296 ParseNode quantifiers = new ParseNode("quantifiers", parser.curLine(1));
297 quantifiers.addChild(quantifier);
298 RESULT = quantifiers;
303 debugMessage(PRODSTRING);
310 FORALL ID:var IN set:set
312 debugMessage(PRODSTRING);
313 ParseNode q = new ParseNode("quantifier", parser.curLine(4));
314 q.addChild("forall", parser.curLine(4));
315 q.addChild("var", parser.curLine(3)).addChild(var);
319 | FORALL LT ID:r1 COMMA ID:r2 GT IN ID:relation
321 debugMessage(PRODSTRING);
322 ParseNode q = new ParseNode("quantifier", parser.curLine(7));
323 q.addChild("relation", parser.curLine(1)).addChild(relation);
324 q.addChild("left", parser.curLine(5)).addChild(r1);
325 q.addChild("right", parser.curLine(3)).addChild(r2);
334 debugMessage(PRODSTRING);
335 ParseNode set = new ParseNode("set", parser.curLine(1));
336 set.addChild("name").addChild(setname);
340 | OPENBRACE listofliterals:list CLOSEBRACE
342 debugMessage(PRODSTRING);
343 ParseNode set = new ParseNode("set", parser.curLine(3));
351 listofliterals:list COMMA literal:literal
353 debugMessage(PRODSTRING);
354 list.addChild(literal);
360 debugMessage(PRODSTRING);
361 ParseNode list = new ParseNode("listofliterals", parser.curLine(1));
362 list.addChild(literal);
369 body:body1 AND body:body2
371 debugMessage(PRODSTRING);
372 ParseNode body = new ParseNode("body", parser.curLine(3));
373 body.addChild("and").addChild("left", parser.curLine(3)).addChild(body1);
374 body.getChild("and").addChild("right", parser.curLine(1)).addChild(body2);
378 | body:body1 OR body:body2
380 debugMessage(PRODSTRING);
381 ParseNode body = new ParseNode("body", parser.curLine(3));
382 body.addChild("or").addChild("left", parser.curLine(3)).addChild(body1);
383 body.getChild("or").addChild("right", parser.curLine(1)).addChild(body2);
389 debugMessage(PRODSTRING);
390 ParseNode body = new ParseNode("body", parser.curLine(2));
391 body.addChild("not").addChild(body1);
395 | OPENPAREN body:body CLOSEPAREN
397 debugMessage(PRODSTRING);
401 | predicate:predicate
403 debugMessage(PRODSTRING);
404 ParseNode body = new ParseNode("body", parser.curLine(1));
405 body.addChild(predicate);
412 expr:expr IN setexpr:setexpr
414 debugMessage(PRODSTRING);
415 ParseNode inclusion = (new ParseNode("predicate", parser.curLine(3))).addChild("inclusion");
416 inclusion.addChild(expr);
417 inclusion.addChild(setexpr);
418 RESULT = inclusion.getRoot();
421 | expr:lexpr compare:compare expr:rexpr
423 debugMessage(PRODSTRING);
424 ParseNode comparison = (new ParseNode("predicate", parser.curLine(3))).addChild("expr").addChild("operator");
425 comparison.addChild("op").addChild(compare);
426 comparison.addChild("left", parser.curLine(2)).addChild(lexpr);
427 comparison.addChild("right", parser.curLine(2)).addChild(rexpr);
428 RESULT = comparison.getRoot();
437 debugMessage(PRODSTRING);
438 ParseNode set = new ParseNode("setexpr", parser.curLine(1));
439 set.addChild("set").addChild(setname);
443 | ID:var DOT ID:relation
445 debugMessage(PRODSTRING);
446 ParseNode set = new ParseNode("setexpr", parser.curLine(3));
447 set.addChild("dot").addChild("quantifiervar", parser.curLine(3)).addChild(var);
448 set.getChild("dot").addChild("relation", parser.curLine(1)).addChild(relation);
452 | ID:var DOTINV ID:relation
454 debugMessage(PRODSTRING);
455 ParseNode set = new ParseNode("setexpr", parser.curLine(3));
456 set.addChild("dotinv").addChild("quantifiervar", parser.curLine(3)).addChild(var);
457 set.getChild("dotinv").addChild("relation", parser.curLine(1)).addChild(relation);
461 | setexpr:setexpr DOT ID:relation
463 debugMessage(PRODSTRING);
464 ParseNode set = new ParseNode("setexpr", parser.curLine(3));
465 set.addChild("dotset").addChild(setexpr);
466 set.getChild("dotset").addChild("relation", parser.curLine(1)).addChild(relation);
470 | setexpr:setexpr DOTINV ID:relation
472 debugMessage(PRODSTRING);
473 ParseNode set = new ParseNode("setexpr", parser.curLine(3));
474 set.addChild("dotinvset").addChild(setexpr);
475 set.getChild("dotinvset").addChild("relation", parser.curLine(1)).addChild(relation);
482 SUM OPENPAREN ID:set DOT ID:relation CLOSEPAREN
484 debugMessage(PRODSTRING);
485 ParseNode expr = new ParseNode("expr", parser.curLine(1));
486 expr.addChild("sumexpr", parser.curLine(3));
487 expr.getChild("sumexpr").addChild("dot").addChild("set", parser.curLine(3)).addChild(set);
488 expr.getChild("sumexpr").getChild("dot").addChild("relation", parser.curLine(1)).addChild(relation);
494 debugMessage(PRODSTRING);
495 ParseNode expr = new ParseNode("expr", parser.curLine(1));
496 expr.addChild("var").addChild(var);
500 | OPENPAREN expr:expr CLOSEPAREN
502 debugMessage(PRODSTRING);
508 debugMessage(PRODSTRING);
509 ParseNode expr = new ParseNode("expr", parser.curLine(4));
510 expr.addChild(literal);
514 | expr:expr DOT ID:relname
516 debugMessage(PRODSTRING);
517 ParseNode relation = (new ParseNode("expr", parser.curLine(3))).addChild("relation");
518 relation.addChild(expr);
519 relation.addChild("name").addChild(relname);
520 RESULT = relation.getRoot();
523 | expr:expr DOTINV ID:relname
525 debugMessage(PRODSTRING);
526 ParseNode relation = (new ParseNode("expr", parser.curLine(3))).addChild("relation");
527 relation.addChild(expr);
528 relation.addChild("name").addChild(relname);
529 relation.addChild("inv");
530 RESULT = relation.getRoot();
533 | expr:expr1 operator:operator expr:expr2
535 debugMessage(PRODSTRING);
536 ParseNode op = (new ParseNode("expr", parser.curLine(3))).addChild("operator");
537 op.addChild("op").addChild(operator);
538 op.addChild("left", parser.curLine(3)).addChild(expr1);
539 op.addChild("right", parser.curLine(1)).addChild(expr2);
540 RESULT = op.getRoot();
543 | SIZEOF OPENPAREN setexpr:setexpr CLOSEPAREN
545 ParseNode sizeof = (new ParseNode("expr", parser.curLine(4))).addChild("sizeof");
546 sizeof.addChild(setexpr);
547 RESULT = sizeof.getRoot();
555 debugMessage(PRODSTRING);
556 RESULT = new ParseNode("add", parser.curLine(1));
561 debugMessage(PRODSTRING);
562 RESULT = new ParseNode("sub", parser.curLine(1));
567 debugMessage(PRODSTRING);
568 RESULT = new ParseNode("mult", parser.curLine(1));
573 debugMessage(PRODSTRING);
574 RESULT = new ParseNode("div", parser.curLine(1));
582 debugMessage(PRODSTRING);
583 RESULT = new ParseNode("lt", parser.curLine(1));
588 debugMessage(PRODSTRING);
589 RESULT = new ParseNode("gt", parser.curLine(1));
594 debugMessage(PRODSTRING);
595 RESULT = new ParseNode("le", parser.curLine(1));
600 debugMessage(PRODSTRING);
601 RESULT = new ParseNode("ge", parser.curLine(1));
606 debugMessage(PRODSTRING);
607 RESULT = new ParseNode("eq", parser.curLine(1));
612 debugMessage(PRODSTRING);
613 RESULT = new ParseNode("ne", parser.curLine(1));
621 debugMessage(PRODSTRING);
622 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("true").getRoot();
627 debugMessage(PRODSTRING);
628 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("false").getRoot();
633 debugMessage(PRODSTRING);
634 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild("0").getRoot();
639 debugMessage(PRODSTRING);
640 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild(dec).getRoot();
645 debugMessage(PRODSTRING);
646 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild("-"+dec).getRoot();
651 debugMessage(PRODSTRING);
652 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("string").addChild(str).getRoot();
657 debugMessage(PRODSTRING);
658 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("char").addChild(chr).getRoot();
661 | LITERAL OPENPAREN ID:literal CLOSEPAREN
663 debugMessage(PRODSTRING);
664 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("token").addChild(literal).getRoot();