-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtoy.cup
More file actions
329 lines (289 loc) · 10.9 KB
/
toy.cup
File metadata and controls
329 lines (289 loc) · 10.9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
/* Import the class java_cup.runtime.* */
import java_cup.runtime.*;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/* Parser code to change the way the parser reports errors (include
line and column number of the error). */
parser code {:
private boolean pop;
java_cup.runtime.Scanner currentLexer;
private String token;
private ArrayList<String> tokenList;
private List<List<String>> reduceShift;
private int currPos;
/*constructor*/
public parser(java_cup.runtime.Scanner s,String fileName) {
super(s);
currentLexer=s;
tokenList= new ArrayList<>();
reduceShift=new ArrayList<>();
currPos=0;
pop=true;
try {
Lexer lexer=new Lexer(new FileReader(fileName),fileName) ;
do {
tokenList.add((String) lexer.next_token().value);
reduceShift.add(new ArrayList<>());
} while (!lexer.isZzAtEOF());
token=tokenList.get(0);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/* Change the method report_error so it will display the line and
column of where the error occurred in the input as well as the
reason for the error which is passed into the method in the
String 'message'. */
public void report_error(String message, Object info) {
/* Create a StringBuilder called 'm' with the string 'Error' in it. */
StringBuilder m = new StringBuilder("Error");
/* Check if the information passed to the method is the same
type as the type java_cup.runtime.Symbol. */
if (info instanceof java_cup.runtime.Symbol) {
/* Declare a java_cup.runtime.Symbol object 's' with the
information in the object info that is being typecasted
as a java_cup.runtime.Symbol object. */
java_cup.runtime.Symbol s = ((java_cup.runtime.Symbol) info);
/* Check if the line number in the input is greater or
equal to zero. */
if (s.left >= 0) {
/* Add to the end of the StringBuilder error message
the line number of the error in the input. */
m.append(" in line "+(s.left+1));
/* Check if the column number in the input is greater
or equal to zero. */
if (s.right >= 0)
/* Add to the end of the StringBuilder error message
the column number of the error in the input. */
m.append(", column "+(s.right+1));
}
}
/* Add to the end of the StringBuilder error message created in
this method the message that was passed into this method. */
m.append(" : "+message);
/* Print the contents of the StringBuilder 'm', which contains
an error message, out on a line. */
//System.err.println(m);
}
/* Change the method report_fatal_error so when it reports a fatal
error it will display the line and column number of where the
fatal error occurred in the input as well as the reason for the
fatal error which is passed into the method in the object
'message' and then exit.*/
public void report_fatal_error(String message, Object info) {
try {
String next = (String) currentLexer.next_token().value;
while (tokenList.size()!=1 && !tokenList.get(0).equals(next)) {
reduceShift.get(currPos).add(tokenList.remove(0));
if (tokenList.isEmpty() || tokenList.get(0).equals(next))
break;
reduceShift.get(currPos).add(" [shift]");
currPos++;
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
print();
System.out.println("[reject]");
//report_error(message, info);
System.exit(1);
}
private void print() {
int size=reduceShift.size();
for (int i=0;i<size;i++) {
List<String> list=reduceShift.remove(0);
for (int j=0; j<list.size();j++)
if (j==0)
System.out.printf("%-30s",list.get(j));
else {
if (list.get(j).equals(" [shift]"))
System.out.println(list.get(j));
else
System.out.print(list.get(j));
}
}
}
/*
============paste inside java_cup.runtime.Symbol CUP$parser$do_action_part00000000================
while (tokenList.size()!=1 &&!token.equals(parser.cur_token.value)) {
if (pop) {
reduceShift.get(currPos).add(tokenList.remove(0));
}
reduceShift.get(currPos).add(" [shift]");
pop=true;
token=tokenList.get(0);
currPos++;
}
if (pop) {
if (tokenList.size()!=1 ) {
token=tokenList.remove(0);
reduceShift.get(currPos).add(token);
}
else
reduceShift.get(currPos).add("empty string");
pop=false;
}
/* select the action based on the action number */
if (CUP$parser$act_num>0)
reduceShift.get(currPos).add(" [reduce "+CUP$parser$act_num+"]");
//System.out.print(" [reduce "+CUP$parser$act_num+"]");
/*===================accept=========================
print();
System.out.println("\n[Accept]");
======================================================================================*/
:};
/* ------------Declaration of Terminals and Non Terminals Section----------- */
/* Terminals (tokens returned by the scanner).
Terminals that have no value are listed first and then terminals
that do have an value, in this case an string value, are listed on
the next line down. */
terminal _break, _class,_else, _extends, _for, _if,_implements,_interface;
terminal _new,_newarray, _null, _println, _readln,_return,_void, _while;
terminal _plus, _minus,_uminus, _multiplication, _division,_mod, _less, _lessequal;
terminal _greater,_greaterequal, _equal, _notequal, _and,_or, _not, _assignop;
terminal _semicolon,_comma, _period, _leftparen, _rightparen;
terminal _leftbracket, _rightbracket, _leftbrace, _rightbrace;
terminal Integer _intconstant,_int;
terminal Double _doubleconstant, _double;
terminal String _stringconstant, _id, _string;
terminal Boolean _booleanconstant,_boolean;
// Non terminals used in the grammar section.
nonterminal Program,P,Decl,VariableDecl,Variable,Type,Type1, FunctionDecl,
Formals,Var,ClassDecl,Extends,Implement,Id,Field,InterfaceDecl,
Prototype,StmtBlock,VarDecl,Statement,Stmt,IfStmt,WhileStmt,ForStmt,
BreakStmt, ReturnStmt, PrintStmt, Expression, Expr, Lvalue,Call,
Actuals,Constant;
/* -------------Precedence and Associatively of Terminals Section----------- */
precedence nonassoc _assignop;
precedence left _or;
precedence left _and;
precedence nonassoc _equal,_notequal;
precedence nonassoc _less,_lessequal,_greater,_greaterequal;
precedence left _plus,_minus;
precedence left _multiplication, _division,_mod;
precedence left _not,_uminus;
precedence left _leftbracket,_period;
precedence left _leftparen;
/* ----------------------------Grammar Section-------------------- */
// The grammar for our parser.
Program ::= P{:print();
System.out.println("\n[Accept]");:};
P ::= Decl P
| Decl ;
Decl ::= VariableDecl
| FunctionDecl
| ClassDecl
| InterfaceDecl;
VariableDecl ::= Variable _semicolon;
Variable ::= Type _id;
Type ::= _int Type1
| _double Type1
| _boolean Type1
| _string Type1
| _id Type1;
Type1 ::=_leftbracket _rightbracket Type1
|;//empty string
FunctionDecl ::= Type _id _leftparen Formals _rightparen StmtBlock
|Type _id _leftparen _rightparen StmtBlock
|_void _id _leftparen _rightparen StmtBlock
| _void _id _leftparen Formals _rightparen StmtBlock;
Formals ::= Var;
Var ::= Variable _comma Var
| Variable;
ClassDecl ::= _class _id Extends Implement _leftbrace _rightbrace
|_class _id Implement _leftbrace _rightbrace
|_class _id Implement _leftbrace Field _rightbrace
| _class _id Extends _leftbrace Field _rightbrace
| _class _id Extends _leftbrace _rightbrace
| _class _id _leftbrace Field _rightbrace
| _class _id _leftbrace _rightbrace
| _class _id Extends Implement _leftbrace Field _rightbrace;
Extends ::= _extends _id;
Implement ::= _implements Id;
Id ::= Id _comma _id
|_id;
Field ::= VariableDecl Field
| FunctionDecl Field
| VariableDecl
|FunctionDecl ;
InterfaceDecl ::= _interface _id _leftbrace Prototype _rightbrace
| _interface _id _leftbrace _rightbrace;
Prototype ::= Type _id _leftparen Formals _rightparen _semicolon
| _void _id _leftparen Formals _rightparen _semicolon
| Type _id _leftparen Formals _rightparen _semicolon Prototype
| _void _id _leftparen Formals _rightparen _semicolon Prototype;
StmtBlock ::= _leftbrace Statement _rightbrace
| _leftbrace VarDecl _rightbrace
| _leftbrace _rightbrace
| _leftbrace VarDecl Statement _rightbrace;
VarDecl ::= VarDecl VariableDecl
|VariableDecl;
Statement ::= Stmt Statement
| Stmt;
Stmt ::= Expr _semicolon
| _semicolon
| IfStmt
| WhileStmt
| ForStmt
| BreakStmt
| ReturnStmt
| PrintStmt
| StmtBlock;
IfStmt ::= _if _leftparen Expr _rightparen Stmt _else Stmt
| _if _leftparen Expr _rightparen Stmt;
WhileStmt ::= _while _leftparen Expr _rightparen Stmt;
ForStmt ::= _for _leftparen Expr _semicolon Expr _semicolon Expr _rightparen Stmt
| _for _leftparen _semicolon Expr _semicolon Expr _rightparen Stmt
| _for _leftparen _semicolon Expr _semicolon _rightparen Stmt
| _for _leftparen Expr _semicolon Expr _semicolon _rightparen Stmt;
BreakStmt ::= _break _semicolon;
ReturnStmt ::= _return Expr _semicolon
| _return _semicolon;
PrintStmt ::= _println _leftparen Expression _rightparen _semicolon;
Expression ::= Expression _comma Expr
| Expr;
Expr ::= Lvalue _assignop Expr
| Constant
| Lvalue
| Call
| _leftparen Expr _rightparen
| Expr _plus Expr
| Expr _minus Expr
| Expr _multiplication Expr
| Expr _division Expr
| Expr _mod Expr
| _minus Expr
%prec _uminus
| Expr _less Expr
| Expr _lessequal Expr
| Expr _greater Expr
| Expr _greaterequal Expr
| Expr _equal Expr
| Expr _notequal Expr
| Expr _and Expr
| Expr _or Expr
| _not Expr
| _readln _leftparen _rightparen
| _new _leftparen _id _rightparen
| _newarray _leftparen _intconstant _comma Type _rightparen;
Lvalue ::= _id
| Lvalue _leftbracket Expr _rightbracket
| Lvalue _period _id;
Call ::= _id _leftparen Actuals _rightparen
| _id _leftparen _rightparen
|_id _period _id _leftparen _rightparen
| _id _period _id _leftparen Actuals _rightparen;
Actuals ::= Expression;
Constant ::= _intconstant
| _doubleconstant
| _stringconstant
| _booleanconstant
| _null;