More bug fixes...and debug flags
[repair.git] / Repair / RepairCompiler / MCC / SDL.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$SDLParser$actions.errors = true;
65                 Symbol symbol = (Symbol) current;               
66
67                 report_error(filename+":"+(symbol.line+1)+": Syntax error at column " 
68                 + (LineCount.getColumn(symbol.left)+1) +": " + current.value, current);
69
70                 System.out.println();
71                 System.exit(0);
72         }
73
74         public void report_fatal_error (String message, Object info) {
75                 
76                  done_parsing();
77                  report_error(message, info);
78                  CUP$SDLParser$actions.errors = true;
79         }
80
81         public int curPos () {
82                 return cur_token.left;
83         }
84
85         public int curLine (int back) {
86                 Stack st = new Stack();
87                 int i;
88
89                 for (i = 0; i < back; i++) {
90                         st.push(stack.pop());
91                 }
92
93                 java_cup.runtime.Symbol s;
94                 s = (java_cup.runtime.Symbol) st.peek();
95
96                 for (i = 0; i < back; i++) {
97                         stack.push(st.pop());
98                 }
99
100                 return LineCount.getLine(s.left);
101         }
102         
103 :}
104
105 // TERMINALS /////////////////////////////////////////////////////////////
106
107     terminal BAD;
108
109     terminal String ID;
110     terminal String DECIMAL;
111     terminal String CHAR;
112     terminal String STRING;
113
114     terminal OPENBRACE;
115     terminal CLOSEBRACE;
116     terminal OPENPAREN;
117     terminal CLOSEPAREN; 
118     terminal OPENBRACKET;
119     terminal CLOSEBRACKET;
120
121     terminal ADD; 
122     terminal SUB; 
123     terminal MULT; 
124     terminal DIV;
125
126     terminal NOT;
127     terminal LT;
128     terminal GT;
129     terminal LE;
130     terminal GE;
131     terminal EQ;
132     terminal NE;
133
134     terminal FORALL;
135     terminal IN;
136     terminal INTEST;
137
138     terminal COMMA;
139     terminal SIZEOF;
140
141     terminal DOT;
142     terminal DOTINV;
143
144     terminal AND;
145     terminal OR;
146
147     terminal LITERAL;
148
149     terminal IMPLIES;
150     terminal TRUE;
151     terminal FALSE;
152     terminal ISVALID;
153     terminal FOR;
154     terminal TO;
155     terminal CAST;
156
157     terminal PARAM;
158     terminal STRUCTURE;
159     terminal RESERVED;
160     terminal BIT;
161     terminal BYTE;
162     terminal SHORT;
163       
164     terminal LABEL;
165     terminal INT;
166     terminal SUBTYPE;
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               spacedefs;
191 nonterminal     ParseNode               space;
192 nonterminal     ParseNode               mult;
193 nonterminal     ParseNode               optstatic;
194 nonterminal     ParseNode               optpartition;
195 nonterminal     ParseNode               setlist;
196 nonterminal     ParseNode               type;
197 nonterminal     ParseNode               optrange;
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 spacedefs;
211
212 spacedefs ::= 
213           spacedefs:spacedefs space:space
214         {:
215         debugMessage(PRODSTRING);
216         spacedefs.addChild(space);
217         RESULT = spacedefs;
218         :}
219           | space:space
220         {:
221         debugMessage(PRODSTRING);
222         ParseNode spacedefs = new ParseNode("space", parser.curLine(1));
223         spacedefs.addChild(space);
224         RESULT = spacedefs;
225         :}
226           ;
227
228 space ::= 
229       SET ID:setname OPENPAREN type:settype CLOSEPAREN SEMICOLON
230         {:
231         debugMessage(PRODSTRING);
232         ParseNode set = new ParseNode("setdefinition", parser.curLine(6));
233         set.addChild("name", parser.curLine(5)).addChild(setname);
234         set.addChild(settype);
235         RESULT = set;
236         :}
237       | SET ID:setname OPENPAREN type:settype CLOSEPAREN COLON optpartition:partition setlist:setlist SEMICOLON
238         {:
239         debugMessage(PRODSTRING);
240         ParseNode set = new ParseNode("setdefinition", parser.curLine(8));
241         set.addChild("name", parser.curLine(7)).addChild(setname);
242         set.addChild(settype);
243         if (partition != null) set.addChild(partition);
244         if (setlist != null) set.addChild(setlist);
245         RESULT = set;
246         :}
247       | ID:name optstatic:optstatic COLON type:domain ARROW type:range optrange:optrange SEMICOLON 
248         {:
249         debugMessage(PRODSTRING);
250         ParseNode relation = new ParseNode("relationdefinition", parser.curLine(8));
251         if (optstatic != null) relation.addChild(optstatic);
252         relation.addChild("name", parser.curLine(7)).addChild(name);
253         relation.addChild("domain").addChild(domain);
254         relation.addChild("range").addChild(range);
255         if (optrange != null) { 
256             relation.getChild("domain").addChild(optrange.getChild("domainmult"));
257             relation.getChild("range").addChild(optrange.getChild("rangemult"));
258         }
259         RESULT = relation;
260         :}
261       ;
262
263 optrange ::= 
264         OPENPAREN mult:domainmult ARROW mult:rangemult CLOSEPAREN 
265            {:
266              debugMessage(PRODSTRING);
267              ParseNode optrange = new ParseNode("optrange", parser.curLine(5));
268              optrange.addChild("domainmult").addChild(domainmult);
269              optrange.addChild("rangemult").addChild(rangemult);
270              RESULT = optrange;
271            :}
272         | /* nothing */
273            {:
274              RESULT = null;
275            :}
276         ;
277
278 mult ::=
279      DECIMAL:one
280         {:
281         debugMessage(PRODSTRING);
282         ParseNode mult = new ParseNode("mult", parser.curLine(1));
283         mult.addChild(one);
284         RESULT = mult;
285         :}
286      | MANY
287         {:
288         debugMessage(PRODSTRING);
289         ParseNode mult = new ParseNode("mult", parser.curLine(1));
290         mult.addChild("many");
291         RESULT = mult;
292         :}
293      ;
294
295 optstatic ::= 
296           STATIC
297         {:
298         debugMessage(PRODSTRING);
299         RESULT = new ParseNode("static", parser.curLine(1));
300         :}
301           | /* nothing */
302         {:
303         debugMessage(PRODSTRING);
304         RESULT = null;
305         :}
306           ;
307
308 optpartition ::= 
309              PARTITION
310         {:
311         debugMessage(PRODSTRING);
312         RESULT = new ParseNode("partition", parser.curLine(1));
313         :}
314              | /* nothing */
315         {:
316         debugMessage(PRODSTRING);
317         RESULT = null;
318         :}
319              ;
320
321 setlist ::=
322         setlist:setlist BAR ID:set
323         {:
324         debugMessage(PRODSTRING);
325         setlist.addChild(set, parser.curLine(1));
326         RESULT = setlist;
327         :}
328         | ID:set
329         {:
330         debugMessage(PRODSTRING);
331         ParseNode setlist = new ParseNode("setlist");
332         setlist.addChild(set, parser.curLine(1));
333         RESULT = setlist;
334         :}
335         |
336         {:
337         debugMessage(PRODSTRING);
338         RESULT = null;
339         :}
340         ;
341
342 type ::= 
343      BIT
344         {:
345         debugMessage(PRODSTRING);
346         ParseNode type = new ParseNode("type", parser.curLine(1));
347         type.addChild("bit");
348         RESULT = type;
349         :}
350      | BYTE
351         {:
352         debugMessage(PRODSTRING);
353         ParseNode type = new ParseNode("type", parser.curLine(1));
354         type.addChild("byte");
355         RESULT = type;
356         :}
357      | SHORT
358         {:
359         debugMessage(PRODSTRING);
360         ParseNode type = new ParseNode("type", parser.curLine(1));
361         type.addChild("short");
362         RESULT = type;
363         :}
364      | INT 
365         {:
366         debugMessage(PRODSTRING);
367         ParseNode type = new ParseNode("type", parser.curLine(1));
368         type.addChild("int");
369         RESULT = type;
370         :}
371      | ID:typename
372         {:
373         debugMessage(PRODSTRING);
374         ParseNode type = new ParseNode("type", parser.curLine(1));
375         type.addChild(typename);
376         RESULT = type;
377         :}
378      ;
379
380
381
382
383
384