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