-
Notifications
You must be signed in to change notification settings - Fork 5
/
exampleTokens.cup
208 lines (185 loc) · 4.46 KB
/
exampleTokens.cup
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
import java_cup.runtime.*;
terminal READ;
terminal PRINT;
terminal TIMES;
terminal PLUS;
terminal MINUS;
terminal DIVIDE;
terminal SEMI;
terminal EQ;
terminal VAR;
terminal VARF;
terminal LPAREN;
terminal RPAREN;
terminal COMMA;
terminal LBRACE;
terminal RBRACE;
terminal LBRKT;
terminal RBRKT;
terminal VOID;
terminal RETURN;
terminal String ID;
terminal String STR;
terminal int INTLIT;
terminal double FLOATLIT;
non terminal Program program;
non terminal StmtList stmtList;
non terminal Stmt stmt;
non terminal Asn asn;
non terminal ReadVar readVar;
non terminal PrintVar printVar;
non terminal OptionalAsn optionalAsn;
non terminal Expr expr;
non terminal BinaryOp binaryOp;
non terminal Type type;
non terminal ArgList argList;
non terminal Arg arg;
non terminal FunStmt funStmt;
non terminal FunCall funCall;
non terminal Params params;
non terminal StmtList fieldsNMethods;
non terminal StmtList methodList;
precedence left PLUS, MINUS;
precedence left TIMES, DIVIDE;
program ::= fieldsNMethods:f stmtList:s
{: RESULT = new Program(f, s);:}
;
stmtList ::=
stmt:s stmtList:sl
{: RESULT = new StmtList(s, sl); :}
|
stmt:s
{: RESULT = new StmtList(s); :}
;
fieldsNMethods ::=
type:t ID:i optionalAsn:o SEMI fieldsNMethods:f
{: RESULT = new StmtList(new Decl(t,i,o), f); :}
|
type:t ID:i LBRKT INTLIT:n RBRKT SEMI fieldsNMethods:f
{: RESULT = new StmtList(new Decl(t,i,n), f); :}
|
type:t ID:i LPAREN argList:a RPAREN LBRACE fieldsNMethods:f stmtList:s RETURN expr:e SEMI RBRACE methodList:m
{: RESULT = new StmtList(new Decl(t,i,a,f,s,e), m); :}
|
VOID ID:i LPAREN argList:a RPAREN LBRACE fieldsNMethods:f stmtList:s RBRACE methodList:m
{: RESULT = new StmtList(new Decl(i,a,f,s), m); :}
|
{: RESULT = null; :}
;
methodList ::=
type:t ID:i LPAREN argList:a RPAREN LBRACE fieldsNMethods:f stmtList:s RETURN expr:e SEMI RBRACE methodList:m
{: RESULT = new StmtList(new Decl(t,i,a,f,s,e), m); :}
|
VOID ID:i LPAREN argList:a RPAREN LBRACE fieldsNMethods:f stmtList:s RBRACE methodList:m
{: RESULT = new StmtList(new Decl(i,a,f,s), m); :}
|
{: RESULT = null; :}
;
stmt ::=
asn:a
{: RESULT = a; :}
|
readVar:r
{: RESULT = r; :}
|
printVar:p
{: RESULT = p; :}
|
funStmt:f
{: RESULT = f; :}
;
argList ::=
arg:a argList:l
{: RESULT = new ArgList(a,l); :}
|
{: RESULT = null; :}
;
arg ::=
type:t ID:i
{: RESULT = new Arg(t,i,false); :}
|
type:t ID:i LBRKT RBRKT
{: RESULT = new Arg(t,i,true); :}
;
type ::=
VAR
{: RESULT = new Type(false); :}
|
VARF
{: RESULT = new Type(true); :}
;
asn ::=
ID:i EQ expr:e SEMI
{: RESULT = new Asn(i,e); :}
;
readVar ::=
READ ID:i SEMI
{: RESULT = new ReadVar(i); :}
|
READ ID:i LBRKT expr:e RBRKT SEMI
{: RESULT = new ReadVar(i,e); :}
;
printVar ::=
PRINT expr:e SEMI
{: RESULT = new PrintVar(e); :}
;
funStmt ::=
funCall:f SEMI
{: RESULT = new FunStmt(f); :}
;
funCall ::=
ID:i LPAREN params:p RPAREN
{: RESULT = new FunCall(i,p); :}
|
ID:i LPAREN RPAREN
{: RESULT = new FunCall(i); :}
;
params ::=
expr:e COMMA params:p
{: RESULT = new Params(e,p); :}
|
expr:e
{: RESULT = new Params(e); :}
;
optionalAsn ::=
EQ expr:e
{: RESULT = new OptionalAsn(e); :}
|
{: RESULT = null; :}
;
expr ::=
binaryOp:b
{: RESULT = new Expr(b); :}
|
INTLIT:i
{: RESULT = new Expr(i); :}
|
FLOATLIT:f
{: RESULT = new Expr(f); :}
|
ID:i
{: RESULT = new Expr(i); :}
|
ID:i LBRKT expr:e RBRKT
{: RESULT = new Expr(i,e); :}
|
funCall:f
{: RESULT = new Expr(f); :}
;
binaryOp ::=
expr:lhs PLUS expr:rhs
{:
BinaryOp b = new BinaryOp(lhs, rhs);
b.setSym("+");
RESULT = b;
:}
|
expr:lhs MINUS expr:rhs
{: RESULT = new BinaryOp(lhs, "-", rhs); :}
|
expr:lhs TIMES expr:rhs
{: RESULT = new BinaryOp(lhs, "*", rhs); :}
|
expr:lhs DIVIDE expr:rhs
{: RESULT = new BinaryOp(lhs, "/", rhs); :}
;