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 void syntax_error (java_cup.runtime.Symbol current) {
62 CUP$CDLParser$actions.errors = true;
63 Symbol symbol = (Symbol) current;
64 report_error("CDL: Syntax error at line " + (symbol.line + 1)
65 + ", column " + LineCount.getColumn(symbol.left) + ": " + current.value, current);
68 public void report_fatal_error (String message, Object info) {
71 report_error(message, info);
72 CUP$CDLParser$actions.errors = true;
75 public int curPos () {
76 return cur_token.left;
79 public int curLine (int back) {
80 Stack st = new Stack();
83 for (i = 0; i < back; i++) {
87 java_cup.runtime.Symbol s;
88 s = (java_cup.runtime.Symbol) st.peek();
90 for (i = 0; i < back; i++) {
94 return LineCount.getLine(s.left);
99 // TERMINALS /////////////////////////////////////////////////////////////
104 terminal String DECIMAL;
105 terminal String CHAR;
106 terminal String STRING;
112 terminal OPENBRACKET;
113 terminal CLOSEBRACKET;
180 // NON-TERMINALS /////////////////////////////////////////////////////////
184 ------------------------------------------------------------------------*/
185 nonterminal ParseNode constraints;
186 nonterminal ParseNode constraint;
187 nonterminal ParseNode optcrash;
188 nonterminal ParseNode quantifiers;
189 nonterminal ParseNode quantifier;
190 nonterminal ParseNode set;
191 nonterminal ParseNode listofliterals;
192 nonterminal ParseNode literal;
193 nonterminal ParseNode body;
194 nonterminal ParseNode predicate;
195 nonterminal ParseNode setexpr;
197 nonterminal ParseNode compare;
198 nonterminal ParseNode expr;
199 nonterminal ParseNode operator;
205 precedence left EQ, NE;
206 precedence left LT, LE, GE, GT;
208 precedence left ADD, SUB;
209 precedence left MULT, DIV;
213 precedence left DOT, DOTINV;
215 // PRODUCTION RULES /////////////////////////////////////////////////////
219 constraints:constraints constraint:constraint
221 debugMessage(PRODSTRING);
222 constraints.addChild(constraint);
223 RESULT = constraints;
226 | constraint:constraint
228 debugMessage(PRODSTRING);
229 ParseNode constraints = new ParseNode("constraints", parser.curLine(1));
230 constraints.addChild(constraint);
231 RESULT = constraints;
237 optcrash:crash OPENBRACKET quantifiers:quantifiers CLOSEBRACKET COMMA body:body SEMICOLON
239 debugMessage(PRODSTRING);
240 ParseNode constraint = new ParseNode("constraint", parser.curLine(7));
242 constraint.addChild(crash);
244 if (quantifiers != null) {
245 constraint.addChild(quantifiers);
247 constraint.addChild(body);
256 debugMessage(PRODSTRING);
257 RESULT = new ParseNode("crash", parser.curLine(1));
262 debugMessage(PRODSTRING);
269 quantifiers:quantifiers COMMA quantifier:quantifier
271 debugMessage(PRODSTRING);
272 quantifiers.addChild(quantifier);
273 RESULT = quantifiers;
276 | quantifier:quantifier
278 debugMessage(PRODSTRING);
279 ParseNode quantifiers = new ParseNode("quantifiers", parser.curLine(1));
280 quantifiers.addChild(quantifier);
281 RESULT = quantifiers;
286 debugMessage(PRODSTRING);
293 FORALL ID:var IN set:set
295 debugMessage(PRODSTRING);
296 ParseNode q = new ParseNode("quantifier", parser.curLine(4));
297 q.addChild("forall", parser.curLine(4));
298 q.addChild("var", parser.curLine(3)).addChild(var);
302 | FORALL LT ID:r1 COMMA ID:r2 GT IN ID:relation
304 debugMessage(PRODSTRING);
305 ParseNode q = new ParseNode("quantifier", parser.curLine(7));
306 q.addChild("relation", parser.curLine(1)).addChild(relation);
307 q.addChild("left", parser.curLine(5)).addChild(r1);
308 q.addChild("right", parser.curLine(3)).addChild(r2);
317 debugMessage(PRODSTRING);
318 ParseNode set = new ParseNode("set", parser.curLine(1));
319 set.addChild("name").addChild(setname);
323 | OPENBRACE listofliterals:list CLOSEBRACE
325 debugMessage(PRODSTRING);
326 ParseNode set = new ParseNode("set", parser.curLine(3));
334 listofliterals:list COMMA literal:literal
336 debugMessage(PRODSTRING);
337 list.addChild(literal);
343 debugMessage(PRODSTRING);
344 ParseNode list = new ParseNode("listofliterals", parser.curLine(1));
345 list.addChild(literal);
352 body:body1 AND body:body2
354 debugMessage(PRODSTRING);
355 ParseNode body = new ParseNode("body", parser.curLine(3));
356 body.addChild("and").addChild("left", parser.curLine(3)).addChild(body1);
357 body.getChild("and").addChild("right", parser.curLine(1)).addChild(body2);
361 | body:body1 OR body:body2
363 debugMessage(PRODSTRING);
364 ParseNode body = new ParseNode("body", parser.curLine(3));
365 body.addChild("or").addChild("left", parser.curLine(3)).addChild(body1);
366 body.getChild("or").addChild("right", parser.curLine(1)).addChild(body2);
372 debugMessage(PRODSTRING);
373 ParseNode body = new ParseNode("body", parser.curLine(2));
374 body.addChild("not").addChild(body1);
378 | OPENPAREN body:body CLOSEPAREN
380 debugMessage(PRODSTRING);
384 | predicate:predicate
386 debugMessage(PRODSTRING);
387 ParseNode body = new ParseNode("body", parser.curLine(1));
388 body.addChild(predicate);
395 expr:expr IN setexpr:setexpr
397 debugMessage(PRODSTRING);
398 ParseNode inclusion = (new ParseNode("predicate", parser.curLine(3))).addChild("inclusion");
399 inclusion.addChild(expr);
400 inclusion.addChild(setexpr);
401 RESULT = inclusion.getRoot();
404 | expr:lexpr compare:compare expr:rexpr
406 debugMessage(PRODSTRING);
407 ParseNode comparison = (new ParseNode("predicate", parser.curLine(3))).addChild("expr").addChild("operator");
408 comparison.addChild("op").addChild(compare);
409 comparison.addChild("left", parser.curLine(2)).addChild(lexpr);
410 comparison.addChild("right", parser.curLine(2)).addChild(rexpr);
411 RESULT = comparison.getRoot();
420 debugMessage(PRODSTRING);
421 ParseNode set = new ParseNode("setexpr", parser.curLine(1));
422 set.addChild("set").addChild(setname);
426 | ID:var DOT ID:relation
428 debugMessage(PRODSTRING);
429 ParseNode set = new ParseNode("setexpr", parser.curLine(3));
430 set.addChild("dot").addChild("quantifiervar", parser.curLine(3)).addChild(var);
431 set.getChild("dot").addChild("relation", parser.curLine(1)).addChild(relation);
435 | ID:var DOTINV ID:relation
437 debugMessage(PRODSTRING);
438 ParseNode set = new ParseNode("setexpr", parser.curLine(3));
439 set.addChild("dotinv").addChild("quantifiervar", parser.curLine(3)).addChild(var);
440 set.getChild("dotinv").addChild("relation", parser.curLine(1)).addChild(relation);
449 debugMessage(PRODSTRING);
450 ParseNode expr = new ParseNode("expr", parser.curLine(1));
451 expr.addChild("var").addChild(var);
455 | OPENPAREN expr:expr CLOSEPAREN
457 debugMessage(PRODSTRING);
461 | LITERAL OPENPAREN literal:literal CLOSEPAREN
463 debugMessage(PRODSTRING);
464 ParseNode expr = new ParseNode("expr", parser.curLine(4));
465 expr.addChild(literal);
469 | expr:expr DOT ID:relname
471 debugMessage(PRODSTRING);
472 ParseNode relation = (new ParseNode("expr", parser.curLine(3))).addChild("relation");
473 relation.addChild(expr);
474 relation.addChild("name").addChild(relname);
475 RESULT = relation.getRoot();
478 | expr:expr DOTINV ID:relname
480 debugMessage(PRODSTRING);
481 ParseNode relation = (new ParseNode("expr", parser.curLine(3))).addChild("relation");
482 relation.addChild(expr);
483 relation.addChild("name").addChild(relname);
484 relation.addChild("inv");
485 RESULT = relation.getRoot();
488 | expr:expr1 operator:operator expr:expr2
490 debugMessage(PRODSTRING);
491 ParseNode op = (new ParseNode("expr", parser.curLine(3))).addChild("operator");
492 op.addChild("op").addChild(operator);
493 op.addChild("left", parser.curLine(3)).addChild(expr1);
494 op.addChild("right", parser.curLine(1)).addChild(expr2);
495 RESULT = op.getRoot();
498 | SIZEOF OPENPAREN setexpr:setexpr CLOSEPAREN
500 ParseNode sizeof = (new ParseNode("expr", parser.curLine(4))).addChild("sizeof");
501 sizeof.addChild(setexpr);
502 RESULT = sizeof.getRoot();
510 debugMessage(PRODSTRING);
511 RESULT = new ParseNode("add", parser.curLine(1));
516 debugMessage(PRODSTRING);
517 RESULT = new ParseNode("sub", parser.curLine(1));
522 debugMessage(PRODSTRING);
523 RESULT = new ParseNode("mult", parser.curLine(1));
528 debugMessage(PRODSTRING);
529 RESULT = new ParseNode("div", parser.curLine(1));
537 debugMessage(PRODSTRING);
538 RESULT = new ParseNode("lt", parser.curLine(1));
543 debugMessage(PRODSTRING);
544 RESULT = new ParseNode("gt", parser.curLine(1));
549 debugMessage(PRODSTRING);
550 RESULT = new ParseNode("le", parser.curLine(1));
555 debugMessage(PRODSTRING);
556 RESULT = new ParseNode("ge", parser.curLine(1));
561 debugMessage(PRODSTRING);
562 RESULT = new ParseNode("eq", parser.curLine(1));
567 debugMessage(PRODSTRING);
568 RESULT = new ParseNode("ne", parser.curLine(1));
576 debugMessage(PRODSTRING);
577 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("true").getRoot();
582 debugMessage(PRODSTRING);
583 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("false").getRoot();
588 debugMessage(PRODSTRING);
589 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild(dec).getRoot();
594 debugMessage(PRODSTRING);
595 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("string").addChild(str).getRoot();
600 debugMessage(PRODSTRING);
601 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("char").addChild(chr).getRoot();
606 debugMessage(PRODSTRING);
607 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("token").addChild(literal).getRoot();