model and checks
[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 void syntax_error (java_cup.runtime.Symbol current) {
61
62                 CUP$SDLParser$actions.errors = true;
63                 Symbol symbol = (Symbol) current;
64                 report_error("SDL: 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$SDLParser$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               spacedefs;
185 nonterminal     ParseNode               space;
186 nonterminal     ParseNode               mult;
187 nonterminal     ParseNode               optstatic;
188 nonterminal     ParseNode               optpartition;
189 nonterminal     ParseNode               setlist;
190 nonterminal     ParseNode               type;
191
192 precedence left OR;
193 precedence left AND;
194 precedence right EQ, NE; 
195 precedence right LT, LE, GE, GT;
196 precedence left ADD, SUB;
197 precedence left MULT, DIV;
198 precedence left NOT;
199 precedence left DOT;
200
201 // PRODUCTION RULES  /////////////////////////////////////////////////////
202
203 start with spacedefs;
204
205 spacedefs ::= 
206           spacedefs:spacedefs space:space
207         {:
208         debugMessage(PRODSTRING);
209         spacedefs.addChild(space);
210         RESULT = spacedefs;
211         :}
212           | space:space
213         {:
214         debugMessage(PRODSTRING);
215         ParseNode spacedefs = new ParseNode("space", parser.curLine(1));
216         spacedefs.addChild(space);
217         RESULT = spacedefs;
218         :}
219           ;
220
221 space ::= 
222       SET ID:setname OPENPAREN type:settype CLOSEPAREN SEMICOLON
223         {:
224         debugMessage(PRODSTRING);
225         ParseNode set = new ParseNode("setdefinition", parser.curLine(6));
226         set.addChild("name", parser.curLine(5)).addChild(setname);
227         set.addChild(settype);
228         RESULT = set;
229         :}
230       | SET ID:setname OPENPAREN type:settype CLOSEPAREN COLON optpartition:partition setlist:setlist SEMICOLON
231         {:
232         debugMessage(PRODSTRING);
233         ParseNode set = new ParseNode("setdefinition", parser.curLine(8));
234         set.addChild("name", parser.curLine(7)).addChild(setname);
235         set.addChild(settype);
236         if (partition != null) set.addChild(partition);
237         if (setlist != null) set.addChild(setlist);
238         RESULT = set;
239         :}
240       | ID:name optstatic:optstatic COLON type:domain ARROW type:range OPENPAREN mult:domainmult ARROW mult:rangemult CLOSEPAREN SEMICOLON 
241         {:
242         debugMessage(PRODSTRING);
243         ParseNode relation = new ParseNode("relationdefinition", parser.curLine(12));
244         if (optstatic != null) relation.addChild(optstatic);
245         relation.addChild("name", parser.curLine(11)).addChild(name);
246         relation.addChild("domain").addChild(domain);
247         relation.addChild("range").addChild(range);
248         relation.getChild("domain").addChild(domainmult);
249         relation.getChild("range").addChild(rangemult);
250         RESULT = relation;
251         :}
252       ;
253
254 mult ::=
255      DECIMAL:one
256         {:
257         debugMessage(PRODSTRING);
258         ParseNode mult = new ParseNode("mult", parser.curLine(1));
259         mult.addChild(one);
260         RESULT = mult;
261         :}
262      | MANY
263         {:
264         debugMessage(PRODSTRING);
265         ParseNode mult = new ParseNode("mult", parser.curLine(1));
266         mult.addChild("many");
267         RESULT = mult;
268         :}
269      ;
270
271 optstatic ::= 
272           STATIC
273         {:
274         debugMessage(PRODSTRING);
275         RESULT = new ParseNode("static", parser.curLine(1));
276         :}
277           | /* nothing */
278         {:
279         debugMessage(PRODSTRING);
280         RESULT = null;
281         :}
282           ;
283
284 optpartition ::= 
285              PARTITION
286         {:
287         debugMessage(PRODSTRING);
288         RESULT = new ParseNode("partition", parser.curLine(1));
289         :}
290              | /* nothing */
291         {:
292         debugMessage(PRODSTRING);
293         RESULT = null;
294         :}
295              ;
296
297 setlist ::=
298         setlist:setlist BAR ID:set
299         {:
300         debugMessage(PRODSTRING);
301         setlist.addChild(set, parser.curLine(1));
302         RESULT = setlist;
303         :}
304         | ID:set
305         {:
306         debugMessage(PRODSTRING);
307         ParseNode setlist = new ParseNode("setlist");
308         setlist.addChild(set, parser.curLine(1));
309         RESULT = setlist;
310         :}
311         |
312         {:
313         debugMessage(PRODSTRING);
314         RESULT = null;
315         :}
316         ;
317
318 type ::= 
319      BIT
320         {:
321         debugMessage(PRODSTRING);
322         ParseNode type = new ParseNode("type", parser.curLine(1));
323         type.addChild("bit");
324         RESULT = type;
325         :}
326      | BYTE
327         {:
328         debugMessage(PRODSTRING);
329         ParseNode type = new ParseNode("type", parser.curLine(1));
330         type.addChild("byte");
331         RESULT = type;
332         :}
333      | SHORT
334         {:
335         debugMessage(PRODSTRING);
336         ParseNode type = new ParseNode("type", parser.curLine(1));
337         type.addChild("short");
338         RESULT = type;
339         :}
340      | INT 
341         {:
342         debugMessage(PRODSTRING);
343         ParseNode type = new ParseNode("type", parser.curLine(1));
344         type.addChild("int");
345         RESULT = type;
346         :}
347      | ID:typename
348         {:
349         debugMessage(PRODSTRING);
350         ParseNode type = new ParseNode("type", parser.curLine(1));
351         type.addChild(typename);
352         RESULT = type;
353         :}
354      ;
355
356
357
358
359
360