Fixed some analysis problems...
[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 SUBCLASS;
168     terminal OF;
169
170     terminal SEMICOLON;
171     terminal COLON;
172
173     terminal SET;
174     terminal ARROW;
175     terminal MANY;
176     terminal BAR;
177
178     terminal PARTITION;
179     terminal ELEMENT;
180     terminal DELAY;
181     terminal STATIC;
182
183     terminal NULL;
184     terminal CRASH;
185
186 // NON-TERMINALS /////////////////////////////////////////////////////////
187
188 /*
189                 TYPE                    NAME
190 ------------------------------------------------------------------------*/
191 nonterminal     ParseNode               spacedefs;
192 nonterminal     ParseNode               space;
193 nonterminal     ParseNode               mult;
194 nonterminal     ParseNode               optstatic;
195 nonterminal     ParseNode               optpartition;
196 nonterminal     ParseNode               setlist;
197 nonterminal     ParseNode               type;
198 nonterminal     ParseNode               optrange;
199
200 precedence left OR;
201 precedence left AND;
202 precedence right EQ, NE; 
203 precedence right LT, LE, GE, GT;
204 precedence left ADD, SUB;
205 precedence left MULT, DIV;
206 precedence left NOT;
207 precedence left DOT;
208
209 // PRODUCTION RULES  /////////////////////////////////////////////////////
210
211 start with spacedefs;
212
213 spacedefs ::= 
214           spacedefs:spacedefs space:space
215         {:
216         debugMessage(PRODSTRING);
217         spacedefs.addChild(space);
218         RESULT = spacedefs;
219         :}
220           | space:space
221         {:
222         debugMessage(PRODSTRING);
223         ParseNode spacedefs = new ParseNode("space", parser.curLine(1));
224         spacedefs.addChild(space);
225         RESULT = spacedefs;
226         :}
227           ;
228
229 space ::= 
230       SET ID:setname OPENPAREN type:settype CLOSEPAREN SEMICOLON
231         {:
232         debugMessage(PRODSTRING);
233         ParseNode set = new ParseNode("setdefinition", parser.curLine(6));
234         set.addChild("name", parser.curLine(5)).addChild(setname);
235         set.addChild(settype);
236         RESULT = set;
237         :}
238       | SET ID:setname OPENPAREN type:settype CLOSEPAREN COLON optpartition:partition setlist:setlist SEMICOLON
239         {:
240         debugMessage(PRODSTRING);
241         ParseNode set = new ParseNode("setdefinition", parser.curLine(8));
242         set.addChild("name", parser.curLine(7)).addChild(setname);
243         set.addChild(settype);
244         if (partition != null) set.addChild(partition);
245         if (setlist != null) set.addChild(setlist);
246         RESULT = set;
247         :}
248       | ID:name optstatic:optstatic COLON type:domain ARROW type:range optrange:optrange SEMICOLON 
249         {:
250         debugMessage(PRODSTRING);
251         ParseNode relation = new ParseNode("relationdefinition", parser.curLine(8));
252         if (optstatic != null) relation.addChild(optstatic);
253         relation.addChild("name", parser.curLine(7)).addChild(name);
254         relation.addChild("domain").addChild(domain);
255         relation.addChild("range").addChild(range);
256         if (optrange != null) { 
257             relation.getChild("domain").addChild(optrange.getChild("domainmult"));
258             relation.getChild("range").addChild(optrange.getChild("rangemult"));
259         }
260         RESULT = relation;
261         :}
262       ;
263
264 optrange ::= 
265         OPENPAREN mult:domainmult ARROW mult:rangemult CLOSEPAREN 
266            {:
267              debugMessage(PRODSTRING);
268              ParseNode optrange = new ParseNode("optrange", parser.curLine(5));
269              optrange.addChild("domainmult").addChild(domainmult);
270              optrange.addChild("rangemult").addChild(rangemult);
271              RESULT = optrange;
272            :}
273         | /* nothing */
274            {:
275              RESULT = null;
276            :}
277         ;
278
279 mult ::=
280      DECIMAL:one
281         {:
282         debugMessage(PRODSTRING);
283         ParseNode mult = new ParseNode("mult", parser.curLine(1));
284         mult.addChild(one);
285         RESULT = mult;
286         :}
287      | MANY
288         {:
289         debugMessage(PRODSTRING);
290         ParseNode mult = new ParseNode("mult", parser.curLine(1));
291         mult.addChild("many");
292         RESULT = mult;
293         :}
294      ;
295
296 optstatic ::= 
297           STATIC
298         {:
299         debugMessage(PRODSTRING);
300         RESULT = new ParseNode("static", parser.curLine(1));
301         :}
302           | /* nothing */
303         {:
304         debugMessage(PRODSTRING);
305         RESULT = null;
306         :}
307           ;
308
309 optpartition ::= 
310              PARTITION
311         {:
312         debugMessage(PRODSTRING);
313         RESULT = new ParseNode("partition", parser.curLine(1));
314         :}
315              | /* nothing */
316         {:
317         debugMessage(PRODSTRING);
318         RESULT = null;
319         :}
320              ;
321
322 setlist ::=
323         setlist:setlist BAR ID:set
324         {:
325         debugMessage(PRODSTRING);
326         setlist.addChild(set, parser.curLine(1));
327         RESULT = setlist;
328         :}
329         | ID:set
330         {:
331         debugMessage(PRODSTRING);
332         ParseNode setlist = new ParseNode("setlist");
333         setlist.addChild(set, parser.curLine(1));
334         RESULT = setlist;
335         :}
336         |
337         {:
338         debugMessage(PRODSTRING);
339         RESULT = null;
340         :}
341         ;
342
343 type ::= 
344      BIT
345         {:
346         debugMessage(PRODSTRING);
347         ParseNode type = new ParseNode("type", parser.curLine(1));
348         type.addChild("bit");
349         RESULT = type;
350         :}
351      | BYTE
352         {:
353         debugMessage(PRODSTRING);
354         ParseNode type = new ParseNode("type", parser.curLine(1));
355         type.addChild("byte");
356         RESULT = type;
357         :}
358      | SHORT
359         {:
360         debugMessage(PRODSTRING);
361         ParseNode type = new ParseNode("type", parser.curLine(1));
362         type.addChild("short");
363         RESULT = type;
364         :}
365      | INT 
366         {:
367         debugMessage(PRODSTRING);
368         ParseNode type = new ParseNode("type", parser.curLine(1));
369         type.addChild("int");
370         RESULT = type;
371         :}
372      | ID:typename
373         {:
374         debugMessage(PRODSTRING);
375         ParseNode type = new ParseNode("type", parser.curLine(1));
376         type.addChild(typename);
377         RESULT = type;
378         :}
379      ;
380
381
382
383
384
385