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);
466 debugMessage(PRODSTRING);
467 ParseNode expr = new ParseNode("expr", parser.curLine(1));
468 expr.addChild("var").addChild(var);
472 | OPENPAREN expr:expr CLOSEPAREN
474 debugMessage(PRODSTRING);
480 debugMessage(PRODSTRING);
481 ParseNode expr = new ParseNode("expr", parser.curLine(4));
482 expr.addChild(literal);
486 | expr:expr DOT ID:relname
488 debugMessage(PRODSTRING);
489 ParseNode relation = (new ParseNode("expr", parser.curLine(3))).addChild("relation");
490 relation.addChild(expr);
491 relation.addChild("name").addChild(relname);
492 RESULT = relation.getRoot();
495 | expr:expr DOTINV ID:relname
497 debugMessage(PRODSTRING);
498 ParseNode relation = (new ParseNode("expr", parser.curLine(3))).addChild("relation");
499 relation.addChild(expr);
500 relation.addChild("name").addChild(relname);
501 relation.addChild("inv");
502 RESULT = relation.getRoot();
505 | expr:expr1 operator:operator expr:expr2
507 debugMessage(PRODSTRING);
508 ParseNode op = (new ParseNode("expr", parser.curLine(3))).addChild("operator");
509 op.addChild("op").addChild(operator);
510 op.addChild("left", parser.curLine(3)).addChild(expr1);
511 op.addChild("right", parser.curLine(1)).addChild(expr2);
512 RESULT = op.getRoot();
515 | SIZEOF OPENPAREN setexpr:setexpr CLOSEPAREN
517 ParseNode sizeof = (new ParseNode("expr", parser.curLine(4))).addChild("sizeof");
518 sizeof.addChild(setexpr);
519 RESULT = sizeof.getRoot();
527 debugMessage(PRODSTRING);
528 RESULT = new ParseNode("add", parser.curLine(1));
533 debugMessage(PRODSTRING);
534 RESULT = new ParseNode("sub", parser.curLine(1));
539 debugMessage(PRODSTRING);
540 RESULT = new ParseNode("mult", parser.curLine(1));
545 debugMessage(PRODSTRING);
546 RESULT = new ParseNode("div", parser.curLine(1));
554 debugMessage(PRODSTRING);
555 RESULT = new ParseNode("lt", parser.curLine(1));
560 debugMessage(PRODSTRING);
561 RESULT = new ParseNode("gt", parser.curLine(1));
566 debugMessage(PRODSTRING);
567 RESULT = new ParseNode("le", parser.curLine(1));
572 debugMessage(PRODSTRING);
573 RESULT = new ParseNode("ge", parser.curLine(1));
578 debugMessage(PRODSTRING);
579 RESULT = new ParseNode("eq", parser.curLine(1));
584 debugMessage(PRODSTRING);
585 RESULT = new ParseNode("ne", parser.curLine(1));
593 debugMessage(PRODSTRING);
594 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("true").getRoot();
599 debugMessage(PRODSTRING);
600 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("false").getRoot();
605 debugMessage(PRODSTRING);
606 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild("0").getRoot();
611 debugMessage(PRODSTRING);
612 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild(dec).getRoot();
617 debugMessage(PRODSTRING);
618 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("string").addChild(str).getRoot();
623 debugMessage(PRODSTRING);
624 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("char").addChild(chr).getRoot();
627 | LITERAL OPENPAREN ID:literal CLOSEPAREN
629 debugMessage(PRODSTRING);
630 RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("token").addChild(literal).getRoot();