Committing changes to leftsize->rightSize, more comments, and handling
[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 void syntax_error (java_cup.runtime.Symbol current) {
61
62                 CUP$TDLParser$actions.errors = true;
63                 Symbol symbol = (Symbol) current;
64                 report_error("TDL: Syntax error at line " + (symbol.line + 1)
65                 + ", column " + LineCount.getColumn(symbol.left) + ": " + current.value, current);
66         }
67
68         public void report_fatal_error (String message, Object info) {
69                 
70                  done_parsing();
71                  report_error(message, info);
72                  CUP$TDLParser$actions.errors = true;
73         }
74
75         public int curPos () {
76                 return cur_token.left;
77         }
78
79         public int curLine (int back) {
80                 Stack st = new Stack();
81                 int i;
82
83                 for (i = 0; i < back; i++) {
84                         st.push(stack.pop());
85                 }
86
87                 java_cup.runtime.Symbol s;
88                 s = (java_cup.runtime.Symbol) st.peek();
89
90                 for (i = 0; i < back; i++) {
91                         stack.push(st.pop());
92                 }
93
94                 return LineCount.getLine(s.left);
95         }
96         
97 :}
98
99 // TERMINALS /////////////////////////////////////////////////////////////
100
101     terminal BAD;
102
103     terminal String ID;
104     terminal String DECIMAL;
105     terminal String CHAR;
106     terminal String STRING;
107
108     terminal OPENBRACE;
109     terminal CLOSEBRACE;
110     terminal OPENPAREN;
111     terminal CLOSEPAREN; 
112     terminal OPENBRACKET;
113     terminal CLOSEBRACKET;
114
115     terminal ADD; 
116     terminal SUB; 
117     terminal MULT; 
118     terminal DIV;
119
120     terminal NOT;
121     terminal LT;
122     terminal GT;
123     terminal LE;
124     terminal GE;
125     terminal EQ;
126     terminal NE;
127
128     terminal FORALL;
129     terminal IN;
130     terminal INTEST;
131
132     terminal COMMA;
133     terminal SIZEOF;
134
135     terminal DOT;
136     terminal DOTINV;
137
138     terminal AND;
139     terminal OR;
140
141     terminal LITERAL;
142
143     terminal IMPLIES;
144     terminal TRUE;
145     terminal FALSE;
146     terminal ISVALID;
147     terminal FOR;
148     terminal TO;
149     terminal CAST;
150
151     terminal PARAM;
152     terminal STRUCTURE;
153     terminal RESERVED;
154     terminal BIT;
155     terminal BYTE;
156     terminal SHORT;
157       
158     terminal LABEL;
159     terminal INT;
160     terminal SUBTYPE;
161     terminal OF;
162
163     terminal SEMICOLON;
164     terminal COLON;
165
166     terminal SET;
167     terminal ARROW;
168     terminal MANY;
169     terminal BAR;
170
171     terminal PARTITION;
172     terminal ELEMENT;
173     terminal DELAY;
174     terminal STATIC;
175
176     terminal NULL;
177     terminal CRASH;
178
179 // NON-TERMINALS /////////////////////////////////////////////////////////
180
181 /*
182                 TYPE                    NAME
183 ------------------------------------------------------------------------*/
184 nonterminal     ParseNode               structures;
185 nonterminal     ParseNode               structure;
186 nonterminal     ParseNode               optsubtype;
187 nonterminal     ParseNode               labelsandfields;
188 nonterminal     ParseNode               label;
189 nonterminal     ParseNode               field;
190 nonterminal     ParseNode               optptr;
191 nonterminal     ParseNode               type;
192 nonterminal     ParseNode               optindex;
193 nonterminal     ParseNode               expr;
194 nonterminal     ParseNode               simple_expr;
195 nonterminal     ParseNode               location;
196 nonterminal     ParseNode               operator;
197 nonterminal     ParseNode               literal;
198
199 precedence left OR;
200 precedence left AND;
201 precedence right EQ, NE; 
202 precedence right LT, LE, GE, GT;
203 precedence left ADD, SUB;
204 precedence left MULT, DIV;
205 precedence left NOT;
206 precedence left DOT;
207
208 // PRODUCTION RULES  /////////////////////////////////////////////////////
209
210 start with structures;
211
212 structures ::= 
213                    
214         structures:structures structure:structure
215         {:
216         debugMessage(PRODSTRING);
217         structures.addChild(structure);
218         RESULT = structures;
219         :}
220            
221         | structure:structure 
222         {:
223         debugMessage(PRODSTRING);
224         ParseNode structures = new ParseNode("structures", parser.curLine(1));
225         structures.addChild(structure);
226         RESULT = structures;
227         :}
228         ;
229
230 structure ::= 
231           
232         STRUCTURE ID:typename optsubtype:subtype OPENBRACE labelsandfields:lf CLOSEBRACE
233         {:
234         debugMessage(PRODSTRING);
235         ParseNode structure = new ParseNode("structure", parser.curLine(6));
236         structure.addChild("name", parser.curLine(5)).addChild(typename);
237         if (subtype != null) {
238          structure.addChild(subtype);
239         }
240         structure.addChild(lf);
241         RESULT = structure;
242         :}
243
244         | ID:type MULT ID:name SEMICOLON
245         {:
246         debugMessage(PRODSTRING);
247         ParseNode global = new ParseNode("global", parser.curLine(4));
248         global.addChild("type").addChild(type);
249         global.addChild("name").addChild(name);
250         RESULT = global;
251         :}
252         ;
253
254 optsubtype ::= 
255            
256         SUBTYPE OF ID:type
257         {:
258         debugMessage(PRODSTRING);
259         ParseNode subtype = new ParseNode("subtype", parser.curLine(3));
260         subtype.addChild(type);
261         RESULT = subtype;
262         :}
263         
264         | /* nothing */
265         {:
266         debugMessage(PRODSTRING);
267         RESULT = null;
268         :}
269            ;
270
271 labelsandfields ::= 
272
273         labelsandfields:lf label:label
274         {:
275         debugMessage(PRODSTRING);
276         lf.getChild("labels").addChild(label);
277         RESULT = lf;
278         :}
279                 
280         | labelsandfields:lf field:field
281         {:
282         debugMessage(PRODSTRING);
283         lf.getChild("fields").addChild(field);
284         RESULT = lf;
285         :}
286                 
287         | label:label
288         {:
289         debugMessage(PRODSTRING);
290         ParseNode lf = new ParseNode("lf");
291         lf.addChild("labels", parser.curLine(1)).addChild(label);
292         lf.addChild("fields", parser.curLine(1));
293         RESULT = lf;
294         :}
295                 
296         | field:field
297         {:
298         debugMessage(PRODSTRING);
299         ParseNode lf = new ParseNode("lf");
300         lf.addChild("fields", parser.curLine(1)).addChild(field);
301         lf.addChild("labels", parser.curLine(1));
302         RESULT = lf;
303         :}
304         ;
305
306 label ::= 
307
308         LABEL ID:field optindex:index COLON type:type ID:name SEMICOLON
309         {:
310         debugMessage(PRODSTRING);
311         ParseNode label = new ParseNode("label", parser.curLine(6));
312         label.addChild("name", parser.curLine(2)).addChild(name);
313         if (index != null) {
314          label.addChild(index);
315         }
316         label.addChild(type);
317         label.addChild("field", parser.curLine(5)).addChild(field);
318         RESULT = label;
319         :}
320         ;
321
322 optindex ::= 
323
324         OPENBRACKET expr:expr CLOSEBRACKET
325         {:
326         debugMessage(PRODSTRING);
327         ParseNode index = new ParseNode("index", parser.curLine(2));
328         index.addChild(expr);
329         RESULT = index;
330         :}
331
332         | /* nothing */
333         {:
334         debugMessage(PRODSTRING);
335         RESULT = null;
336         :}
337         ;
338
339 field ::= 
340      
341         RESERVED type:type optindex:index SEMICOLON
342         {:
343         debugMessage(PRODSTRING);
344         ParseNode field = new ParseNode("field", parser.curLine(4));
345         field.addChild(type);
346         field.addChild("reserved");
347         if (index != null) {
348          field.addChild(index);
349         }       
350         RESULT = field;
351         :}
352       
353         | type:type optptr:optptr ID:name optindex:index SEMICOLON
354         {:
355         debugMessage(PRODSTRING);
356         ParseNode field = new ParseNode("field", parser.curLine(5));
357         field.addChild(type);
358         if (optptr != null) {
359          field.addChild(optptr);
360         }
361         field.addChild("name", parser.curLine(3)).addChild(name);
362         if (index != null) {
363          field.addChild(index);
364         }
365         RESULT = field;
366         :}
367         ;
368
369 optptr ::=
370        
371         MULT
372         {:
373         debugMessage(PRODSTRING);
374         RESULT = new ParseNode("*", parser.curLine(1));
375         :}
376        
377         | /* nothing */
378         {:
379         debugMessage(PRODSTRING);
380         RESULT = null;
381         :}
382         ;
383
384
385 /*** expression interface *********************************/
386
387 simple_expr ::= 
388         
389         location:location
390         {:
391         debugMessage(PRODSTRING);
392         ParseNode se = new ParseNode("simple_expr", parser.curLine(1));
393         se.addChild(location);
394         RESULT = se;
395         :}
396         ;
397
398 location ::=
399
400         ID:var
401         {:
402         debugMessage(PRODSTRING);
403         ParseNode loc = new ParseNode("location", parser.curLine(1));   
404         loc.addChild("var").addChild(var);
405         RESULT = loc;
406         :}
407
408         | simple_expr:dotexpr DOT ID:field
409         {:
410         debugMessage(PRODSTRING);
411         ParseNode dot = (new ParseNode("location", parser.curLine(3))).addChild("dot");
412         dot.addChild(dotexpr);
413         dot.addChild("field", parser.curLine(1)).addChild(field);
414         RESULT = dot.getRoot();
415         :}
416
417         | simple_expr:dotexpr DOT ID:field OPENBRACKET expr:index CLOSEBRACKET
418         {:
419         debugMessage(PRODSTRING);
420         ParseNode dot = (new ParseNode("location", parser.curLine(6))).addChild("dot");
421         dot.addChild(dotexpr);
422         dot.addChild("field", parser.curLine(4)).addChild(field);
423         dot.addChild("index", parser.curLine(2)).addChild(index);
424         RESULT = dot.getRoot();
425         :}
426
427         | CAST OPENPAREN ID:type COMMA simple_expr:expr CLOSEPAREN
428         {:
429         debugMessage(PRODSTRING);
430         ParseNode cast = (new ParseNode("location", parser.curLine(6))).addChild("cast");
431         cast.addChild("type").addChild(type);
432         cast.addChild(expr);
433         RESULT = cast.getRoot();
434         :}
435         ;
436      
437 expr ::= 
438
439         simple_expr:se 
440         {:
441         debugMessage(PRODSTRING);
442         ParseNode expr = new ParseNode("expr", parser.curLine(1));
443         expr.addChild(se);
444         RESULT = expr;
445         :}
446
447         | OPENPAREN expr:expr CLOSEPAREN 
448         {:
449         debugMessage(PRODSTRING);
450         RESULT = expr;
451         :}     
452     
453         | LITERAL OPENPAREN literal:literal CLOSEPAREN       
454         {:
455         debugMessage(PRODSTRING);
456         ParseNode expr = new ParseNode("expr", parser.curLine(4));
457         expr.addChild(literal);
458         RESULT = expr;
459         :}
460         
461         | expr:expr1 operator:operator expr:expr2
462         {:
463         debugMessage(PRODSTRING);
464         ParseNode op = (new ParseNode("expr", parser.curLine(3))).addChild("operator");
465         op.addChild("op").addChild(operator);
466         op.addChild("left", parser.curLine(3)).addChild(expr1);
467         op.addChild("right", parser.curLine(1)).addChild(expr2);
468         RESULT = op.getRoot();
469         :}         
470         ;             
471         
472 /**** standard ***************************************************/
473
474 operator ::=
475           
476         ADD 
477         {:
478         debugMessage(PRODSTRING);
479         RESULT = new ParseNode("add", parser.curLine(1));
480         :}
481           
482         | SUB
483         {:
484         debugMessage(PRODSTRING);
485         RESULT = new ParseNode("sub", parser.curLine(1));
486         :}
487           
488         | MULT
489         {:
490         debugMessage(PRODSTRING);
491         RESULT = new ParseNode("mult", parser.curLine(1));
492         :}
493           
494         | DIV
495         {:
496         debugMessage(PRODSTRING);
497         RESULT = new ParseNode("div", parser.curLine(1));
498         :}
499           ;
500
501 literal ::=
502          
503         TRUE
504         {:
505         debugMessage(PRODSTRING);
506         RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("true").getRoot();
507         :}
508          
509         | FALSE
510         {:
511         debugMessage(PRODSTRING);
512         RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("boolean").addChild("false").getRoot();
513         :}
514          
515         | DECIMAL:dec
516         {:
517         debugMessage(PRODSTRING);
518         RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("decimal").addChild(dec).getRoot();
519         :}
520          
521         | STRING:str
522         {:
523         debugMessage(PRODSTRING);
524         RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("string").addChild(str).getRoot();
525         :}
526          
527         | CHAR:chr
528         {:
529         debugMessage(PRODSTRING);
530         RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("char").addChild(chr).getRoot();
531         :}
532          
533         | ID:literal
534         {:
535         debugMessage(PRODSTRING);
536         RESULT = (new ParseNode("literal", parser.curLine(1))).addChild("token").addChild(literal).getRoot();
537         :}
538         ;
539
540 type ::= 
541      
542         BIT
543         {:
544         debugMessage(PRODSTRING);
545         ParseNode type = new ParseNode("type", parser.curLine(1));
546         type.addChild("bit");
547         RESULT = type;
548         :}
549      
550         | BYTE
551         {:
552         debugMessage(PRODSTRING);
553         ParseNode type = new ParseNode("type", parser.curLine(1));
554         type.addChild("byte");
555         RESULT = type;
556         :}
557      
558         | SHORT
559         {:
560         debugMessage(PRODSTRING);
561         ParseNode type = new ParseNode("type", parser.curLine(1));
562         type.addChild("short");
563         RESULT = type;
564         :}
565      
566         | INT 
567         {:
568         debugMessage(PRODSTRING);
569         ParseNode type = new ParseNode("type", parser.curLine(1));
570         type.addChild("int");
571         RESULT = type;
572         :}
573      
574         | ID:typename
575         {:
576         debugMessage(PRODSTRING);
577         ParseNode type = new ParseNode("type", parser.curLine(1));
578         type.addChild(typename);
579         RESULT = type;
580         :}
581         ;