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