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;
196 // NON-TERMINALS /////////////////////////////////////////////////////////
200 ------------------------------------------------------------------------*/
201 nonterminal ParseNode constraints;
202 nonterminal ParseNode constraint;
203 nonterminal ParseNode optcrash;
204 nonterminal ParseNode quantifiers;
205 nonterminal ParseNode quantifier;
206 nonterminal ParseNode set;
207 nonterminal ParseNode listofliterals;
208 nonterminal ParseNode literal;
209 nonterminal ParseNode body;
210 nonterminal ParseNode predicate;
211 nonterminal ParseNode setexpr;
213 nonterminal ParseNode compare;
214 nonterminal ParseNode expr;
215 nonterminal ParseNode operator;
221 precedence left EQ, NE;
222 precedence left LT, LE, GE, GT;
224 precedence left ADD, SUB;
225 precedence left MULT, DIV;
229 precedence left DOT, DOTINV;
231 // PRODUCTION RULES /////////////////////////////////////////////////////
235 constraints:constraints constraint:constraint
237 debugMessage(PRODSTRING);
238 constraints.addChild(constraint);
239 RESULT = constraints;
242 | constraint:constraint
244 debugMessage(PRODSTRING);
245 ParseNode constraints = new ParseNode("constraints", parser.curLine(1));
246 constraints.addChild(constraint);
247 RESULT = constraints;
253 optcrash:crash OPENBRACKET quantifiers:quantifiers CLOSEBRACKET COMMA body:body SEMICOLON
255 debugMessage(PRODSTRING);
256 ParseNode constraint = new ParseNode("constraint", parser.curLine(7));
258 constraint.addChild(crash);
260 if (quantifiers != null) {
261 constraint.addChild(quantifiers);
263 constraint.addChild(body);
272 debugMessage(PRODSTRING);
273 RESULT = new ParseNode("crash", parser.curLine(1));
278 debugMessage(PRODSTRING);
285 quantifiers:quantifiers COMMA quantifier:quantifier
287 debugMessage(PRODSTRING);
288 quantifiers.addChild(quantifier);
289 RESULT = quantifiers;
292 | quantifier:quantifier
294 debugMessage(PRODSTRING);
295 ParseNode quantifiers = new ParseNode("quantifiers", parser.curLine(1));
296 quantifiers.addChild(quantifier);
297 RESULT = quantifiers;
302 debugMessage(PRODSTRING);
309 FORALL ID:var IN set:set
311 debugMessage(PRODSTRING);
312 ParseNode q = new ParseNode("quantifier", parser.curLine(4));
313 q.addChild("forall", parser.curLine(4));
314 q.addChild("var", parser.curLine(3)).addChild(var);
318 | FORALL LT ID:r1 COMMA ID:r2 GT IN ID:relation
320 debugMessage(PRODSTRING);
321 ParseNode q = new ParseNode("quantifier", parser.curLine(7));
322 q.addChild("relation", parser.curLine(1)).addChild(relation);
323 q.addChild("left", parser.curLine(5)).addChild(r1);
324 q.addChild("right", parser.curLine(3)).addChild(r2);
333 debugMessage(PRODSTRING);
334 ParseNode set = new ParseNode("set", parser.curLine(1));
335 set.addChild("name").addChild(setname);
339 | OPENBRACE listofliterals:list CLOSEBRACE
341 debugMessage(PRODSTRING);
342 ParseNode set = new ParseNode("set", parser.curLine(3));
350 listofliterals:list COMMA literal:literal
352 debugMessage(PRODSTRING);
353 list.addChild(literal);
359 debugMessage(PRODSTRING);
360 ParseNode list = new ParseNode("listofliterals", parser.curLine(1));
361 list.addChild(literal);
368 body:body1 AND body:body2
370 debugMessage(PRODSTRING);
371 ParseNode body = new ParseNode("body", parser.curLine(3));
372 body.addChild("and").addChild("left", parser.curLine(3)).addChild(body1);
373 body.getChild("and").addChild("right", parser.curLine(1)).addChild(body2);
377 | body:body1 OR body:body2
379 debugMessage(PRODSTRING);
380 ParseNode body = new ParseNode("body", parser.curLine(3));
381 body.addChild("or").addChild("left", parser.curLine(3)).addChild(body1);
382 body.getChild("or").addChild("right", parser.curLine(1)).addChild(body2);
388 debugMessage(PRODSTRING);
389 ParseNode body = new ParseNode("body", parser.curLine(2));
390 body.addChild("not").addChild(body1);
394 | OPENPAREN body:body CLOSEPAREN
396 debugMessage(PRODSTRING);
400 | predicate:predicate
402 debugMessage(PRODSTRING);
403 ParseNode body = new ParseNode("body", parser.curLine(1));
404 body.addChild(predicate);
411 expr:expr IN setexpr:setexpr
413 debugMessage(PRODSTRING);
414 ParseNode inclusion = (new ParseNode("predicate", parser.curLine(3))).addChild("inclusion");
415 inclusion.addChild(expr);
416 inclusion.addChild(setexpr);
417 RESULT = inclusion.getRoot();
420 | expr:lexpr compare:compare expr:rexpr
422 debugMessage(PRODSTRING);
423 ParseNode comparison = (new ParseNode("predicate", parser.curLine(3))).addChild("expr").addChild("operator");
424 comparison.addChild("op").addChild(compare);
425 comparison.addChild("left", parser.curLine(2)).addChild(lexpr);
426 comparison.addChild("right", parser.curLine(2)).addChild(rexpr);
427 RESULT = comparison.getRoot();
436 debugMessage(PRODSTRING);
437 ParseNode set = new ParseNode("setexpr", parser.curLine(1));
438 set.addChild("set").addChild(setname);
442 | ID:var DOT ID:relation
444 debugMessage(PRODSTRING);
445 ParseNode set = new ParseNode("setexpr", parser.curLine(3));
446 set.addChild("dot").addChild("quantifiervar", parser.curLine(3)).addChild(var);
447 set.getChild("dot").addChild("relation", parser.curLine(1)).addChild(relation);
451 | ID:var DOTINV ID:relation
453 debugMessage(PRODSTRING);
454 ParseNode set = new ParseNode("setexpr", parser.curLine(3));
455 set.addChild("dotinv").addChild("quantifiervar", parser.curLine(3)).addChild(var);
456 set.getChild("dotinv").addChild("relation", parser.curLine(1)).addChild(relation);
465 debugMessage(PRODSTRING);
466 ParseNode expr = new ParseNode("expr", parser.curLine(1));
467 expr.addChild("var").addChild(var);
471 | OPENPAREN expr:expr CLOSEPAREN
473 debugMessage(PRODSTRING);
479 debugMessage(PRODSTRING);
480 ParseNode expr = new ParseNode("expr", parser.curLine(4));
481 expr.addChild(literal);
485 | expr:expr DOT ID:relname
487 debugMessage(PRODSTRING);
488 ParseNode relation = (new ParseNode("expr", parser.curLine(3))).addChild("relation");
489 relation.addChild(expr);
490 relation.addChild("name").addChild(relname);
491 RESULT = relation.getRoot();
494 | expr:expr DOTINV ID:relname
496 debugMessage(PRODSTRING);
497 ParseNode relation = (new ParseNode("expr", parser.curLine(3))).addChild("relation");
498 relation.addChild(expr);
499 relation.addChild("name").addChild(relname);
500 relation.addChild("inv");
501 RESULT = relation.getRoot();
504 | expr:expr1 operator:operator expr:expr2
506 debugMessage(PRODSTRING);
507 ParseNode op = (new ParseNode("expr", parser.curLine(3))).addChild("operator");
508 op.addChild("op").addChild(operator);
509 op.addChild("left", parser.curLine(3)).addChild(expr1);
510 op.addChild("right", parser.curLine(1)).addChild(expr2);
511 RESULT = op.getRoot();
514 | SIZEOF OPENPAREN setexpr:setexpr CLOSEPAREN
516 ParseNode sizeof = (new ParseNode("expr", parser.curLine(4))).addChild("sizeof");
517 sizeof.addChild(setexpr);
518 RESULT = sizeof.getRoot();
526 debugMessage(PRODSTRING);
527 RESULT = new ParseNode("add", parser.curLine(1));
532 debugMessage(PRODSTRING);
533 RESULT = new ParseNode("sub", parser.curLine(1));
538 debugMessage(PRODSTRING);
539 RESULT = new ParseNode("mult", parser.curLine(1));
544 debugMessage(PRODSTRING);
545 RESULT = new ParseNode("div", parser.curLine(1));
553 debugMessage(PRODSTRING);
554 RESULT = new ParseNode("lt", parser.curLine(1));
559 debugMessage(PRODSTRING);
560 RESULT = new ParseNode("gt", parser.curLine(1));
565 debugMessage(PRODSTRING);
566 RESULT = new ParseNode("le", parser.curLine(1));
571 debugMessage(PRODSTRING);
572 RESULT = new ParseNode("ge", parser.curLine(1));
577 debugMessage(PRODSTRING);
578 RESULT = new ParseNode("eq", parser.curLine(1));
583 debugMessage(PRODSTRING);
584 RESULT = new ParseNode("ne", parser.curLine(1));
592 debugMessage(PRODSTRING);
593 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("true").getRoot();
598 debugMessage(PRODSTRING);
599 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("false").getRoot();
604 debugMessage(PRODSTRING);
605 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild("0").getRoot();
610 debugMessage(PRODSTRING);
611 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild(dec).getRoot();
616 debugMessage(PRODSTRING);
617 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("string").addChild(str).getRoot();
622 debugMessage(PRODSTRING);
623 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("char").addChild(chr).getRoot();
626 | LITERAL OPENPAREN ID:literal CLOSEPAREN
628 debugMessage(PRODSTRING);
629 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("token").addChild(literal).getRoot();