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);
75 if (current.value.equals("true") || isInteger)
76 System.out.println("Did you mean literal("+current.value+")?");
82 public void report_fatal_error (String message, Object info) {
85 report_error(message, info);
86 CUP$CDLParser$actions.errors = true;
89 public int curPos () {
90 return cur_token.left;
93 public int curLine (int back) {
94 Stack st = new Stack();
97 for (i = 0; i < back; i++) {
101 java_cup.runtime.Symbol s;
102 s = (java_cup.runtime.Symbol) st.peek();
104 for (i = 0; i < back; i++) {
105 stack.push(st.pop());
108 return LineCount.getLine(s.left);
113 // TERMINALS /////////////////////////////////////////////////////////////
118 terminal String DECIMAL;
119 terminal String CHAR;
120 terminal String STRING;
126 terminal OPENBRACKET;
127 terminal CLOSEBRACKET;
194 // NON-TERMINALS /////////////////////////////////////////////////////////
198 ------------------------------------------------------------------------*/
199 nonterminal ParseNode constraints;
200 nonterminal ParseNode constraint;
201 nonterminal ParseNode optcrash;
202 nonterminal ParseNode quantifiers;
203 nonterminal ParseNode quantifier;
204 nonterminal ParseNode set;
205 nonterminal ParseNode listofliterals;
206 nonterminal ParseNode literal;
207 nonterminal ParseNode body;
208 nonterminal ParseNode predicate;
209 nonterminal ParseNode setexpr;
211 nonterminal ParseNode compare;
212 nonterminal ParseNode expr;
213 nonterminal ParseNode operator;
219 precedence left EQ, NE;
220 precedence left LT, LE, GE, GT;
222 precedence left ADD, SUB;
223 precedence left MULT, DIV;
227 precedence left DOT, DOTINV;
229 // PRODUCTION RULES /////////////////////////////////////////////////////
233 constraints:constraints constraint:constraint
235 debugMessage(PRODSTRING);
236 constraints.addChild(constraint);
237 RESULT = constraints;
240 | constraint:constraint
242 debugMessage(PRODSTRING);
243 ParseNode constraints = new ParseNode("constraints", parser.curLine(1));
244 constraints.addChild(constraint);
245 RESULT = constraints;
251 optcrash:crash OPENBRACKET quantifiers:quantifiers CLOSEBRACKET COMMA body:body SEMICOLON
253 debugMessage(PRODSTRING);
254 ParseNode constraint = new ParseNode("constraint", parser.curLine(7));
256 constraint.addChild(crash);
258 if (quantifiers != null) {
259 constraint.addChild(quantifiers);
261 constraint.addChild(body);
270 debugMessage(PRODSTRING);
271 RESULT = new ParseNode("crash", parser.curLine(1));
276 debugMessage(PRODSTRING);
283 quantifiers:quantifiers COMMA quantifier:quantifier
285 debugMessage(PRODSTRING);
286 quantifiers.addChild(quantifier);
287 RESULT = quantifiers;
290 | quantifier:quantifier
292 debugMessage(PRODSTRING);
293 ParseNode quantifiers = new ParseNode("quantifiers", parser.curLine(1));
294 quantifiers.addChild(quantifier);
295 RESULT = quantifiers;
300 debugMessage(PRODSTRING);
307 FORALL ID:var IN set:set
309 debugMessage(PRODSTRING);
310 ParseNode q = new ParseNode("quantifier", parser.curLine(4));
311 q.addChild("forall", parser.curLine(4));
312 q.addChild("var", parser.curLine(3)).addChild(var);
316 | FORALL LT ID:r1 COMMA ID:r2 GT IN ID:relation
318 debugMessage(PRODSTRING);
319 ParseNode q = new ParseNode("quantifier", parser.curLine(7));
320 q.addChild("relation", parser.curLine(1)).addChild(relation);
321 q.addChild("left", parser.curLine(5)).addChild(r1);
322 q.addChild("right", parser.curLine(3)).addChild(r2);
331 debugMessage(PRODSTRING);
332 ParseNode set = new ParseNode("set", parser.curLine(1));
333 set.addChild("name").addChild(setname);
337 | OPENBRACE listofliterals:list CLOSEBRACE
339 debugMessage(PRODSTRING);
340 ParseNode set = new ParseNode("set", parser.curLine(3));
348 listofliterals:list COMMA literal:literal
350 debugMessage(PRODSTRING);
351 list.addChild(literal);
357 debugMessage(PRODSTRING);
358 ParseNode list = new ParseNode("listofliterals", parser.curLine(1));
359 list.addChild(literal);
366 body:body1 AND body:body2
368 debugMessage(PRODSTRING);
369 ParseNode body = new ParseNode("body", parser.curLine(3));
370 body.addChild("and").addChild("left", parser.curLine(3)).addChild(body1);
371 body.getChild("and").addChild("right", parser.curLine(1)).addChild(body2);
375 | body:body1 OR body:body2
377 debugMessage(PRODSTRING);
378 ParseNode body = new ParseNode("body", parser.curLine(3));
379 body.addChild("or").addChild("left", parser.curLine(3)).addChild(body1);
380 body.getChild("or").addChild("right", parser.curLine(1)).addChild(body2);
386 debugMessage(PRODSTRING);
387 ParseNode body = new ParseNode("body", parser.curLine(2));
388 body.addChild("not").addChild(body1);
392 | OPENPAREN body:body CLOSEPAREN
394 debugMessage(PRODSTRING);
398 | predicate:predicate
400 debugMessage(PRODSTRING);
401 ParseNode body = new ParseNode("body", parser.curLine(1));
402 body.addChild(predicate);
409 expr:expr IN setexpr:setexpr
411 debugMessage(PRODSTRING);
412 ParseNode inclusion = (new ParseNode("predicate", parser.curLine(3))).addChild("inclusion");
413 inclusion.addChild(expr);
414 inclusion.addChild(setexpr);
415 RESULT = inclusion.getRoot();
418 | expr:lexpr compare:compare expr:rexpr
420 debugMessage(PRODSTRING);
421 ParseNode comparison = (new ParseNode("predicate", parser.curLine(3))).addChild("expr").addChild("operator");
422 comparison.addChild("op").addChild(compare);
423 comparison.addChild("left", parser.curLine(2)).addChild(lexpr);
424 comparison.addChild("right", parser.curLine(2)).addChild(rexpr);
425 RESULT = comparison.getRoot();
434 debugMessage(PRODSTRING);
435 ParseNode set = new ParseNode("setexpr", parser.curLine(1));
436 set.addChild("set").addChild(setname);
440 | ID:var DOT ID:relation
442 debugMessage(PRODSTRING);
443 ParseNode set = new ParseNode("setexpr", parser.curLine(3));
444 set.addChild("dot").addChild("quantifiervar", parser.curLine(3)).addChild(var);
445 set.getChild("dot").addChild("relation", parser.curLine(1)).addChild(relation);
449 | ID:var DOTINV ID:relation
451 debugMessage(PRODSTRING);
452 ParseNode set = new ParseNode("setexpr", parser.curLine(3));
453 set.addChild("dotinv").addChild("quantifiervar", parser.curLine(3)).addChild(var);
454 set.getChild("dotinv").addChild("relation", parser.curLine(1)).addChild(relation);
463 debugMessage(PRODSTRING);
464 ParseNode expr = new ParseNode("expr", parser.curLine(1));
465 expr.addChild("var").addChild(var);
469 | OPENPAREN expr:expr CLOSEPAREN
471 debugMessage(PRODSTRING);
477 debugMessage(PRODSTRING);
478 ParseNode expr = new ParseNode("expr", parser.curLine(4));
479 expr.addChild(literal);
483 | expr:expr DOT ID:relname
485 debugMessage(PRODSTRING);
486 ParseNode relation = (new ParseNode("expr", parser.curLine(3))).addChild("relation");
487 relation.addChild(expr);
488 relation.addChild("name").addChild(relname);
489 RESULT = relation.getRoot();
492 | expr:expr DOTINV ID:relname
494 debugMessage(PRODSTRING);
495 ParseNode relation = (new ParseNode("expr", parser.curLine(3))).addChild("relation");
496 relation.addChild(expr);
497 relation.addChild("name").addChild(relname);
498 relation.addChild("inv");
499 RESULT = relation.getRoot();
502 | expr:expr1 operator:operator expr:expr2
504 debugMessage(PRODSTRING);
505 ParseNode op = (new ParseNode("expr", parser.curLine(3))).addChild("operator");
506 op.addChild("op").addChild(operator);
507 op.addChild("left", parser.curLine(3)).addChild(expr1);
508 op.addChild("right", parser.curLine(1)).addChild(expr2);
509 RESULT = op.getRoot();
512 | SIZEOF OPENPAREN setexpr:setexpr CLOSEPAREN
514 ParseNode sizeof = (new ParseNode("expr", parser.curLine(4))).addChild("sizeof");
515 sizeof.addChild(setexpr);
516 RESULT = sizeof.getRoot();
524 debugMessage(PRODSTRING);
525 RESULT = new ParseNode("add", parser.curLine(1));
530 debugMessage(PRODSTRING);
531 RESULT = new ParseNode("sub", parser.curLine(1));
536 debugMessage(PRODSTRING);
537 RESULT = new ParseNode("mult", parser.curLine(1));
542 debugMessage(PRODSTRING);
543 RESULT = new ParseNode("div", parser.curLine(1));
551 debugMessage(PRODSTRING);
552 RESULT = new ParseNode("lt", parser.curLine(1));
557 debugMessage(PRODSTRING);
558 RESULT = new ParseNode("gt", parser.curLine(1));
563 debugMessage(PRODSTRING);
564 RESULT = new ParseNode("le", parser.curLine(1));
569 debugMessage(PRODSTRING);
570 RESULT = new ParseNode("ge", parser.curLine(1));
575 debugMessage(PRODSTRING);
576 RESULT = new ParseNode("eq", parser.curLine(1));
581 debugMessage(PRODSTRING);
582 RESULT = new ParseNode("ne", parser.curLine(1));
590 debugMessage(PRODSTRING);
591 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("true").getRoot();
596 debugMessage(PRODSTRING);
597 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("false").getRoot();
602 debugMessage(PRODSTRING);
603 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild("0").getRoot();
608 debugMessage(PRODSTRING);
609 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild(dec).getRoot();
614 debugMessage(PRODSTRING);
615 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("string").addChild(str).getRoot();
620 debugMessage(PRODSTRING);
621 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("char").addChild(chr).getRoot();
624 | LITERAL OPENPAREN ID:literal CLOSEPAREN
626 debugMessage(PRODSTRING);
627 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("token").addChild(literal).getRoot();