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