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