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 addsuboperator;
217 nonterminal ParseNode multdivoperator;
223 precedence left EQ, NE;
224 precedence left LT, LE, GE, GT;
226 precedence left ADD, SUB;
227 precedence left MULT, DIV;
231 precedence left DOT, DOTINV;
233 // PRODUCTION RULES /////////////////////////////////////////////////////
237 constraints:constraints constraint:constraint
239 debugMessage(PRODSTRING);
240 constraints.addChild(constraint);
241 RESULT = constraints;
244 | constraint:constraint
246 debugMessage(PRODSTRING);
247 ParseNode constraints = new ParseNode("constraints", parser.curLine(1));
248 constraints.addChild(constraint);
249 RESULT = constraints;
255 optcrash:crash OPENBRACKET quantifiers:quantifiers CLOSEBRACKET COMMA body:body SEMICOLON
257 debugMessage(PRODSTRING);
258 ParseNode constraint = new ParseNode("constraint", parser.curLine(7));
260 constraint.addChild(crash);
262 if (quantifiers != null) {
263 constraint.addChild(quantifiers);
265 constraint.addChild(body);
274 debugMessage(PRODSTRING);
275 RESULT = new ParseNode("crash", parser.curLine(1));
280 debugMessage(PRODSTRING);
287 quantifiers:quantifiers COMMA quantifier:quantifier
289 debugMessage(PRODSTRING);
290 quantifiers.addChild(quantifier);
291 RESULT = quantifiers;
294 | quantifier:quantifier
296 debugMessage(PRODSTRING);
297 ParseNode quantifiers = new ParseNode("quantifiers", parser.curLine(1));
298 quantifiers.addChild(quantifier);
299 RESULT = quantifiers;
304 debugMessage(PRODSTRING);
311 FORALL ID:var IN set:set
313 debugMessage(PRODSTRING);
314 ParseNode q = new ParseNode("quantifier", parser.curLine(4));
315 q.addChild("forall", parser.curLine(4));
316 q.addChild("var", parser.curLine(3)).addChild(var);
320 | FORALL LT ID:r1 COMMA ID:r2 GT IN ID:relation
322 debugMessage(PRODSTRING);
323 ParseNode q = new ParseNode("quantifier", parser.curLine(7));
324 q.addChild("relation", parser.curLine(1)).addChild(relation);
325 q.addChild("left", parser.curLine(5)).addChild(r1);
326 q.addChild("right", parser.curLine(3)).addChild(r2);
335 debugMessage(PRODSTRING);
336 ParseNode set = new ParseNode("set", parser.curLine(1));
337 set.addChild("name").addChild(setname);
341 | OPENBRACE listofliterals:list CLOSEBRACE
343 debugMessage(PRODSTRING);
344 ParseNode set = new ParseNode("set", parser.curLine(3));
352 listofliterals:list COMMA literal:literal
354 debugMessage(PRODSTRING);
355 list.addChild(literal);
361 debugMessage(PRODSTRING);
362 ParseNode list = new ParseNode("listofliterals", parser.curLine(1));
363 list.addChild(literal);
370 body:body1 AND body:body2
372 debugMessage(PRODSTRING);
373 ParseNode body = new ParseNode("body", parser.curLine(3));
374 body.addChild("and").addChild("left", parser.curLine(3)).addChild(body1);
375 body.getChild("and").addChild("right", parser.curLine(1)).addChild(body2);
379 | body:body1 OR body:body2
381 debugMessage(PRODSTRING);
382 ParseNode body = new ParseNode("body", parser.curLine(3));
383 body.addChild("or").addChild("left", parser.curLine(3)).addChild(body1);
384 body.getChild("or").addChild("right", parser.curLine(1)).addChild(body2);
390 debugMessage(PRODSTRING);
391 ParseNode body = new ParseNode("body", parser.curLine(2));
392 body.addChild("not").addChild(body1);
396 | OPENPAREN body:body CLOSEPAREN
398 debugMessage(PRODSTRING);
402 | predicate:predicate
404 debugMessage(PRODSTRING);
405 ParseNode body = new ParseNode("body", parser.curLine(1));
406 body.addChild(predicate);
413 expr:expr IN setexpr:setexpr
415 debugMessage(PRODSTRING);
416 ParseNode inclusion = (new ParseNode("predicate", parser.curLine(3))).addChild("inclusion");
417 inclusion.addChild(expr);
418 inclusion.addChild(setexpr);
419 RESULT = inclusion.getRoot();
422 | expr:lexpr compare:compare expr:rexpr
424 debugMessage(PRODSTRING);
425 ParseNode comparison = (new ParseNode("predicate", parser.curLine(3))).addChild("expr").addChild("operator");
426 comparison.addChild("op").addChild(compare);
427 comparison.addChild("left", parser.curLine(2)).addChild(lexpr);
428 comparison.addChild("right", parser.curLine(2)).addChild(rexpr);
429 RESULT = comparison.getRoot();
438 debugMessage(PRODSTRING);
439 ParseNode set = new ParseNode("setexpr", parser.curLine(1));
440 set.addChild("set").addChild(setname);
444 | ID:var DOT ID:relation
446 debugMessage(PRODSTRING);
447 ParseNode set = new ParseNode("setexpr", parser.curLine(3));
448 set.addChild("dot").addChild("quantifiervar", parser.curLine(3)).addChild(var);
449 set.getChild("dot").addChild("relation", parser.curLine(1)).addChild(relation);
453 | ID:var DOTINV ID:relation
455 debugMessage(PRODSTRING);
456 ParseNode set = new ParseNode("setexpr", parser.curLine(3));
457 set.addChild("dotinv").addChild("quantifiervar", parser.curLine(3)).addChild(var);
458 set.getChild("dotinv").addChild("relation", parser.curLine(1)).addChild(relation);
462 | setexpr:setexpr DOT ID:relation
464 debugMessage(PRODSTRING);
465 ParseNode set = new ParseNode("setexpr", parser.curLine(3));
466 set.addChild("dotset").addChild(setexpr);
467 set.getChild("dotset").addChild("relation", parser.curLine(1)).addChild(relation);
471 | setexpr:setexpr DOTINV ID:relation
473 debugMessage(PRODSTRING);
474 ParseNode set = new ParseNode("setexpr", parser.curLine(3));
475 set.addChild("dotinvset").addChild(setexpr);
476 set.getChild("dotinvset").addChild("relation", parser.curLine(1)).addChild(relation);
483 SUM OPENPAREN ID:set DOT ID:relation CLOSEPAREN
485 debugMessage(PRODSTRING);
486 ParseNode expr = new ParseNode("expr", parser.curLine(1));
487 expr.addChild("sumexpr", parser.curLine(3));
488 expr.getChild("sumexpr").addChild("dot").addChild("set", parser.curLine(3)).addChild(set);
489 expr.getChild("sumexpr").getChild("dot").addChild("relation", parser.curLine(1)).addChild(relation);
495 debugMessage(PRODSTRING);
496 ParseNode expr = new ParseNode("expr", parser.curLine(1));
497 expr.addChild("var").addChild(var);
501 | OPENPAREN expr:expr CLOSEPAREN
503 debugMessage(PRODSTRING);
509 debugMessage(PRODSTRING);
510 ParseNode expr = new ParseNode("expr", parser.curLine(4));
511 expr.addChild(literal);
515 | expr:expr DOT ID:relname
517 debugMessage(PRODSTRING);
518 ParseNode relation = (new ParseNode("expr", parser.curLine(3))).addChild("relation");
519 relation.addChild(expr);
520 relation.addChild("name").addChild(relname);
521 RESULT = relation.getRoot();
524 | expr:expr DOTINV ID:relname
526 debugMessage(PRODSTRING);
527 ParseNode relation = (new ParseNode("expr", parser.curLine(3))).addChild("relation");
528 relation.addChild(expr);
529 relation.addChild("name").addChild(relname);
530 relation.addChild("inv");
531 RESULT = relation.getRoot();
534 | expr:expr1 addsuboperator:operator expr:expr2
536 debugMessage(PRODSTRING);
537 ParseNode op = (new ParseNode("expr", parser.curLine(3))).addChild("operator");
538 op.addChild("op").addChild(operator);
539 op.addChild("left", parser.curLine(3)).addChild(expr1);
540 op.addChild("right", parser.curLine(1)).addChild(expr2);
541 RESULT = op.getRoot();
544 | expr:expr1 multdivoperator:operator expr:expr2
546 debugMessage(PRODSTRING);
547 ParseNode op = (new ParseNode("expr", parser.curLine(3))).addChild("operator");
548 op.addChild("op").addChild(operator);
549 op.addChild("left", parser.curLine(3)).addChild(expr1);
550 op.addChild("right", parser.curLine(1)).addChild(expr2);
551 RESULT = op.getRoot();
554 | SIZEOF OPENPAREN setexpr:setexpr CLOSEPAREN
556 ParseNode sizeof = (new ParseNode("expr", parser.curLine(4))).addChild("sizeof");
557 sizeof.addChild(setexpr);
558 RESULT = sizeof.getRoot();
566 debugMessage(PRODSTRING);
567 RESULT = new ParseNode("add", parser.curLine(1));
572 debugMessage(PRODSTRING);
573 RESULT = new ParseNode("sub", parser.curLine(1));
579 debugMessage(PRODSTRING);
580 RESULT = new ParseNode("mult", parser.curLine(1));
584 debugMessage(PRODSTRING);
585 RESULT = new ParseNode("div", parser.curLine(1));
593 debugMessage(PRODSTRING);
594 RESULT = new ParseNode("lt", parser.curLine(1));
599 debugMessage(PRODSTRING);
600 RESULT = new ParseNode("gt", parser.curLine(1));
605 debugMessage(PRODSTRING);
606 RESULT = new ParseNode("le", parser.curLine(1));
611 debugMessage(PRODSTRING);
612 RESULT = new ParseNode("ge", parser.curLine(1));
617 debugMessage(PRODSTRING);
618 RESULT = new ParseNode("eq", parser.curLine(1));
623 debugMessage(PRODSTRING);
624 RESULT = new ParseNode("ne", parser.curLine(1));
632 debugMessage(PRODSTRING);
633 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("true").getRoot();
638 debugMessage(PRODSTRING);
639 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("false").getRoot();
644 debugMessage(PRODSTRING);
645 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild("0").getRoot();
650 debugMessage(PRODSTRING);
651 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild(dec).getRoot();
656 debugMessage(PRODSTRING);
657 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild("-"+dec).getRoot();
662 debugMessage(PRODSTRING);
663 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("string").addChild(str).getRoot();
668 debugMessage(PRODSTRING);
669 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("char").addChild(chr).getRoot();
672 | LITERAL OPENPAREN ID:literal CLOSEPAREN
674 debugMessage(PRODSTRING);
675 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("token").addChild(literal).getRoot();