revert runtime file.
[repair.git] / Repair / RepairCompiler / MCC / TDL.cup
1 package MCC;
2 import MCC.IR.ParseNode;
3 import MCC.IR.ParseNodeVector;
4 import java.util.*;
5
6 action code {:
7
8         public static boolean errors;
9         public static boolean debug;
10
11         // debugMessage: writes debug production message only if debug = true
12
13         void debugMessage (String production) {
14                 if (debug) {
15                         System.out.println("Applying production: " + production);
16                 }
17         }
18
19         String unescape (String str) {
20             StringBuffer sb = new StringBuffer();
21             int i;
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) == '\\') {
25                     i++;
26                     switch (str.charAt(i)) {
27                     case '\"':
28                         sb.append('\"');
29                         break;
30                     case '\'':
31                         sb.append('\'');
32                         break;
33                     case '\\':
34                         sb.append('\\');
35                         break;
36                     case 't':
37                         sb.append('\t');
38                         break;
39                     case 'n':
40                         sb.append('\n');
41                         break;
42                     default:
43                         System.err.print("Error in string literal: ");
44                         System.err.println(str.charAt(i));
45                         System.err.println("Aborting...");
46                         break;
47                     }
48                 } else {
49                     sb.append(str.charAt(i));
50                 }
51             }
52             return sb.toString();
53         }
54 :}
55
56 init with {: :}
57
58 parser code {:
59
60         public String filename;
61        
62         public void syntax_error (java_cup.runtime.Symbol current) {
63
64                 CUP$TDLParser$actions.errors = true;
65                 Symbol symbol = (Symbol) current;
66                 report_error(filename+":"+(symbol.line+1)+": Syntax error at column " 
67                 + (LineCount.getColumn(symbol.left)+1) +": " + current.value, current);
68                 System.out.println();
69                 System.exit(0);
70         }
71
72         public void report_fatal_error (String message, Object info) {
73                 
74                  done_parsing();
75                  report_error(message, info);
76                  CUP$TDLParser$actions.errors = true;
77         }
78
79         public int curPos () {
80                 return cur_token.left;
81         }
82
83         public int curLine (int back) {
84                 Stack st = new Stack();
85                 int i;
86
87                 for (i = 0; i < back; i++) {
88                         st.push(stack.pop());
89                 }
90
91                 java_cup.runtime.Symbol s;
92                 s = (java_cup.runtime.Symbol) st.peek();
93
94                 for (i = 0; i < back; i++) {
95                         stack.push(st.pop());
96                 }
97
98                 return LineCount.getLine(s.left);
99         }
100         
101 :}
102
103 // TERMINALS /////////////////////////////////////////////////////////////
104
105     terminal BAD;
106
107     terminal String ID;
108     terminal String DECIMAL;
109     terminal String CHAR;
110     terminal String STRING;
111
112     terminal OPENBRACE;
113     terminal CLOSEBRACE;
114     terminal OPENPAREN;
115     terminal CLOSEPAREN; 
116     terminal OPENBRACKET;
117     terminal CLOSEBRACKET;
118
119     terminal ADD; 
120     terminal SUB; 
121     terminal MULT; 
122     terminal DIV;
123     terminal SUM;
124
125     terminal NOT;
126     terminal LT;
127     terminal GT;
128     terminal LE;
129     terminal GE;
130     terminal EQ;
131     terminal NE;
132
133     terminal FORALL;
134     terminal IN;
135     terminal INTEST;
136
137     terminal COMMA;
138     terminal SIZEOF;
139
140     terminal DOT;
141     terminal DOTINV;
142
143     terminal AND;
144     terminal OR;
145
146     terminal LITERAL;
147
148     terminal IMPLIES;
149     terminal TRUE;
150     terminal FALSE;
151     terminal ISVALID;
152     terminal FOR;
153     terminal TO;
154     terminal CAST;
155
156     terminal PARAM;
157     terminal STRUCTURE;
158     terminal RESERVED;
159     terminal BIT;
160     terminal BYTE;
161     terminal SHORT;
162       
163     terminal LABEL;
164     terminal INT;
165     terminal SUBTYPE;
166     terminal SUBCLASS;
167     terminal OF;
168
169     terminal SEMICOLON;
170     terminal COLON;
171
172     terminal SET;
173     terminal ARROW;
174     terminal MANY;
175     terminal BAR;
176
177     terminal PARTITION;
178     terminal ELEMENT;
179     terminal DELAY;
180     terminal STATIC;
181
182     terminal NULL;
183     terminal CRASH;
184
185 // NON-TERMINALS /////////////////////////////////////////////////////////
186
187 /*
188                 TYPE                    NAME
189 ------------------------------------------------------------------------*/
190 nonterminal     ParseNode               structures;
191 nonterminal     ParseNode               structure;
192 nonterminal     ParseNode               optsubtype;
193 nonterminal     ParseNode               labelsandfields;
194 nonterminal     ParseNode               label;
195 nonterminal     ParseNode               field;
196 nonterminal     ParseNode               optptr;
197 nonterminal     ParseNode               type;
198 nonterminal     ParseNode               primtype;
199 nonterminal     ParseNode               optindex;
200 nonterminal     ParseNode               expr;
201 nonterminal     ParseNode               simple_expr;
202 nonterminal     ParseNode               location;
203 nonterminal     ParseNode               operator;
204 nonterminal     ParseNode               literal;
205
206 precedence left OR;
207 precedence left AND;
208 precedence right EQ, NE; 
209 precedence right LT, LE, GE, GT;
210 precedence left ADD, SUB;
211 precedence left MULT, DIV;
212 precedence left NOT;
213 precedence left DOT;
214
215 // PRODUCTION RULES  /////////////////////////////////////////////////////
216
217 start with structures;
218
219 structures ::= 
220                    
221         structures:structures structure:structure
222         {:
223         debugMessage(PRODSTRING);
224         structures.addChild(structure);
225         RESULT = structures;
226         :}
227            
228         | structure:structure 
229         {:
230         debugMessage(PRODSTRING);
231         ParseNode structures = new ParseNode("structures", parser.curLine(1));
232         structures.addChild(structure);
233         RESULT = structures;
234         :}
235         ;
236
237 structure ::= 
238           
239         STRUCTURE ID:typename optsubtype:subtype OPENBRACE labelsandfields:lf CLOSEBRACE
240         {:
241         debugMessage(PRODSTRING);
242         ParseNode structure = new ParseNode("structure", parser.curLine(6));
243         structure.addChild("name", parser.curLine(5)).addChild(typename);
244         if (subtype != null) {
245          structure.addChild(subtype);
246         }
247         structure.addChild(lf);
248         RESULT = structure;
249         :}
250
251         | ID:type MULT ID:name SEMICOLON
252         {:
253         debugMessage(PRODSTRING);
254         ParseNode global = new ParseNode("global", parser.curLine(4));
255         global.addChild("ptr");
256         global.addChild("type").addChild(type);
257         global.addChild("name").addChild(name);
258         RESULT = global;
259         :}
260
261         | primtype:type ID:name SEMICOLON
262         {:
263         debugMessage(PRODSTRING);
264         ParseNode global = new ParseNode("global", parser.curLine(4));
265         global.addChild(type);
266         global.addChild("name").addChild(name);
267         RESULT = global;
268         :}
269         ;
270
271 optsubtype ::= 
272                 /* subtype */
273         SUBTYPE OF ID:type
274         {:
275         debugMessage(PRODSTRING);
276         ParseNode subtype = new ParseNode("subtype", parser.curLine(3));
277         subtype.addChild(type);
278         RESULT = subtype;
279         :}
280
281         | /* subclass */
282
283         SUBCLASS OF ID:type
284         {:
285         debugMessage(PRODSTRING);
286         ParseNode subtype = new ParseNode("subclass", parser.curLine(3));
287         subtype.addChild(type);
288         RESULT = subtype;
289         :}
290         
291         | /* nothing */
292         {:
293         debugMessage(PRODSTRING);
294         RESULT = null;
295         :}
296            ;
297
298 labelsandfields ::= 
299
300         labelsandfields:lf label:label
301         {:
302         debugMessage(PRODSTRING);
303         lf.getChild("labels").addChild(label);
304         RESULT = lf;
305         :}
306                 
307         | labelsandfields:lf field:field
308         {:
309         debugMessage(PRODSTRING);
310         lf.getChild("fields").addChild(field);
311         RESULT = lf;
312         :}
313                 
314         | label:label
315         {:
316         debugMessage(PRODSTRING);
317         ParseNode lf = new ParseNode("lf");
318         lf.addChild("labels", parser.curLine(1)).addChild(label);
319         lf.addChild("fields", parser.curLine(1));
320         RESULT = lf;
321         :}
322                 
323         | field:field
324         {:
325         debugMessage(PRODSTRING);
326         ParseNode lf = new ParseNode("lf");
327         lf.addChild("fields", parser.curLine(1)).addChild(field);
328         lf.addChild("labels", parser.curLine(1));
329         RESULT = lf;
330         :}
331         ;
332
333 label ::= 
334
335         LABEL ID:field optindex:index COLON type:type ID:name SEMICOLON
336         {:
337         debugMessage(PRODSTRING);
338         ParseNode label = new ParseNode("label", parser.curLine(6));
339         label.addChild("name", parser.curLine(2)).addChild(name);
340         if (index != null) {
341          label.addChild(index);
342         }
343         label.addChild(type);
344         label.addChild("field", parser.curLine(5)).addChild(field);
345         RESULT = label;
346         :}
347         ;
348
349 optindex ::= 
350
351         OPENBRACKET expr:expr CLOSEBRACKET
352         {:
353         debugMessage(PRODSTRING);
354         ParseNode index = new ParseNode("index", parser.curLine(2));
355         index.addChild(expr);
356         RESULT = index;
357         :}
358
359         | /* nothing */
360         {:
361         debugMessage(PRODSTRING);
362         RESULT = null;
363         :}
364         ;
365
366 field ::= 
367      
368         RESERVED type:type optindex:index SEMICOLON
369         {:
370         debugMessage(PRODSTRING);
371         ParseNode field = new ParseNode("field", parser.curLine(4));
372         field.addChild(type);
373         field.addChild("reserved");
374         if (index != null) {
375          field.addChild(index);
376         }       
377         RESULT = field;
378         :}
379       
380         | type:type optptr:optptr ID:name optindex:index SEMICOLON
381         {:
382         debugMessage(PRODSTRING);
383         ParseNode field = new ParseNode("field", parser.curLine(5));
384         field.addChild(type);
385         if (optptr != null) {
386          field.addChild(optptr);
387         }
388         field.addChild("name", parser.curLine(3)).addChild(name);
389         if (index != null) {
390          field.addChild(index);
391         }
392         RESULT = field;
393         :}
394         ;
395
396 optptr ::=
397        
398         MULT
399         {:
400         debugMessage(PRODSTRING);
401         RESULT = new ParseNode("*", parser.curLine(1));
402         :}
403        
404         | /* nothing */
405         {:
406         debugMessage(PRODSTRING);
407         RESULT = null;
408         :}
409         ;
410
411
412 /*** expression interface *********************************/
413
414 simple_expr ::= 
415         
416         location:location
417         {:
418         debugMessage(PRODSTRING);
419         ParseNode se = new ParseNode("simple_expr", parser.curLine(1));
420         se.addChild(location);
421         RESULT = se;
422         :}
423         ;
424
425 location ::=
426
427         ID:var
428         {:
429         debugMessage(PRODSTRING);
430         ParseNode loc = new ParseNode("location", parser.curLine(1));   
431         loc.addChild("var").addChild(var);
432         RESULT = loc;
433         :}
434
435         | simple_expr:dotexpr DOT ID:field
436         {:
437         debugMessage(PRODSTRING);
438         ParseNode dot = (new ParseNode("location", parser.curLine(3))).addChild("dot");
439         dot.addChild(dotexpr);
440         dot.addChild("field", parser.curLine(1)).addChild(field);
441         RESULT = dot.getRoot();
442         :}
443
444         | simple_expr:dotexpr DOT ID:field OPENBRACKET expr:index CLOSEBRACKET
445         {:
446         debugMessage(PRODSTRING);
447         ParseNode dot = (new ParseNode("location", parser.curLine(6))).addChild("dot");
448         dot.addChild(dotexpr);
449         dot.addChild("field", parser.curLine(4)).addChild(field);
450         dot.addChild("index", parser.curLine(2)).addChild(index);
451         RESULT = dot.getRoot();
452         :}
453
454         | CAST OPENPAREN ID:type COMMA simple_expr:expr CLOSEPAREN
455         {:
456         debugMessage(PRODSTRING);
457         ParseNode cast = (new ParseNode("location", parser.curLine(6))).addChild("cast");
458         cast.addChild("type").addChild(type);
459         cast.addChild(expr);
460         RESULT = cast.getRoot();
461         :}
462         ;
463      
464 expr ::= 
465
466         simple_expr:se 
467         {:
468         debugMessage(PRODSTRING);
469         ParseNode expr = new ParseNode("expr", parser.curLine(1));
470         expr.addChild(se);
471         RESULT = expr;
472         :}
473
474         | OPENPAREN expr:expr CLOSEPAREN 
475         {:
476         debugMessage(PRODSTRING);
477         RESULT = expr;
478         :}     
479     
480         | literal:literal
481         {:
482         debugMessage(PRODSTRING);
483         ParseNode expr = new ParseNode("expr", parser.curLine(4));
484         expr.addChild(literal);
485         RESULT = expr;
486         :}
487         
488         | expr:expr1 operator:operator expr:expr2
489         {:
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();
496         :}         
497         ;             
498         
499 /**** standard ***************************************************/
500
501 operator ::=
502           
503         ADD 
504         {:
505         debugMessage(PRODSTRING);
506         RESULT = new ParseNode("add", parser.curLine(1));
507         :}
508           
509         | SUB
510         {:
511         debugMessage(PRODSTRING);
512         RESULT = new ParseNode("sub", parser.curLine(1));
513         :}
514           
515         | MULT
516         {:
517         debugMessage(PRODSTRING);
518         RESULT = new ParseNode("mult", parser.curLine(1));
519         :}
520           
521         | DIV
522         {:
523         debugMessage(PRODSTRING);
524         RESULT = new ParseNode("div", parser.curLine(1));
525         :}
526           ;
527
528 literal ::=
529          
530         TRUE
531         {:
532         debugMessage(PRODSTRING);
533         RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("true").getRoot();
534         :}
535          
536         | FALSE
537         {:
538         debugMessage(PRODSTRING);
539         RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("false").getRoot();
540         :}
541          
542         | DECIMAL:dec
543         {:
544         debugMessage(PRODSTRING);
545         RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild(dec).getRoot();
546         :}
547
548         | SUB DECIMAL:dec
549         {:
550         debugMessage(PRODSTRING);
551         RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild("-"+dec).getRoot();
552         :}
553          
554         | STRING:str
555         {:
556         debugMessage(PRODSTRING);
557         RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("string").addChild(str).getRoot();
558         :}
559          
560         | CHAR:chr
561         {:
562         debugMessage(PRODSTRING);
563         RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("char").addChild(chr).getRoot();
564         :}
565          
566         | LITERAL OPENPAREN ID:literal CLOSEPAREN
567         {:
568         debugMessage(PRODSTRING);
569         RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("token").addChild(literal).getRoot();
570         :}
571         ;
572
573 type ::= 
574      
575         BIT
576         {:
577         debugMessage(PRODSTRING);
578         ParseNode type = new ParseNode("type", parser.curLine(1));
579         type.addChild("bit");
580         RESULT = type;
581         :}
582      
583         | BYTE
584         {:
585         debugMessage(PRODSTRING);
586         ParseNode type = new ParseNode("type", parser.curLine(1));
587         type.addChild("byte");
588         RESULT = type;
589         :}
590      
591         | SHORT
592         {:
593         debugMessage(PRODSTRING);
594         ParseNode type = new ParseNode("type", parser.curLine(1));
595         type.addChild("short");
596         RESULT = type;
597         :}
598      
599         | INT 
600         {:
601         debugMessage(PRODSTRING);
602         ParseNode type = new ParseNode("type", parser.curLine(1));
603         type.addChild("int");
604         RESULT = type;
605         :}
606      
607         | ID:typename
608         {:
609         debugMessage(PRODSTRING);
610         ParseNode type = new ParseNode("type", parser.curLine(1));
611         type.addChild(typename);
612         RESULT = type;
613         :}
614         ;
615
616 primtype ::= 
617      
618         BIT
619         {:
620         debugMessage(PRODSTRING);
621         ParseNode type = new ParseNode("type", parser.curLine(1));
622         type.addChild("bit");
623         RESULT = type;
624         :}
625      
626         | BYTE
627         {:
628         debugMessage(PRODSTRING);
629         ParseNode type = new ParseNode("type", parser.curLine(1));
630         type.addChild("byte");
631         RESULT = type;
632         :}
633      
634         | SHORT
635         {:
636         debugMessage(PRODSTRING);
637         ParseNode type = new ParseNode("type", parser.curLine(1));
638         type.addChild("short");
639         RESULT = type;
640         :}
641      
642         | INT 
643         {:
644         debugMessage(PRODSTRING);
645         ParseNode type = new ParseNode("type", parser.curLine(1));
646         type.addChild("int");
647         RESULT = type;
648         :}
649 ;