-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.y
272 lines (223 loc) · 3.35 KB
/
parser.y
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
%{
#include <stdio.h>
int yylex();
int yyerror(char *s);
%}
%token UMINUS
%token NOELSE
%token T_ID T_INTLITERAL T_DOUBLELITERAL T_STRINGLITERAL T_BOOLEANLITERAL
T_Void T_Int T_Double T_Bool T_String T_Class T_Interface T_Null T_This T_Extends
T_Implements T_For T_While T_If T_Else T_Return T_Break T_Continue T_New T_NewArray T_Print
T_ReadInteger T_ReadLine T_Dtoi T_Itod T_Btoi T_Itob T_Private T_Protected T_Public
T_LessEqual T_GreaterEqual T_Equal T_NotEqual T_And T_Or T_Dims
UNDEFINED_TOKEN
TOKENCOUNT
%type <id> T_ID
%type <integerLiteral> T_INTLITERAL
%type <doubleLiteral> T_DOUBLELITERAL
%type <stringLiteral> T_STRINGLITERAL
%type <booleanLiteral> T_BOOLEANLITERAL
%union{
int integerLiteral;
bool booleanLiteral;
char *stringLiteral;
double doubleLiteral;
char *id;
}
%nonassoc NOELSE
%nonassoc T_Else
%nonassoc '='
%left T_Or
%left T_And
%nonassoc T_Equal T_NotEqual
%nonassoc '<' T_LessEqual '>' T_GreaterEqual
%left '+' '-'
%left '*' '/' '%'
%nonassoc '!' UMINUS
%nonassoc '[' '.'
%left T_Dims
%%
Program:
Decl
| Decl Program
;
Decl:
VariableDecl
| FunctionDecl
| ClassDecl
| InterfaceDecl
;
VariableDecls:
VariableDecls VariableDecl
|
;
VariableDecl:
Variable ';'
;
Variable:
Type T_ID
;
Type:
T_Int
| T_Double
| T_Bool
| T_String
| T_ID
| Type T_Dims
;
FunctionDecl:
Type T_ID '(' Formals ')' StmtBlock
| T_Void T_ID '(' Formals ')' StmtBlock
;
Formals:
Variable FormalsContinue
|
;
FormalsContinue:
',' Variable FormalsContinue
|
;
ClassDecl:
T_Class T_ID Extends Implements '{' Fields '}'
;
Extends:
T_Extends T_ID
|
;
Implements:
T_Implements T_ID ImplementsContinue
|
;
ImplementsContinue:
',' T_Implements T_ID ImplementsContinue
|
;
Fields:
Field Fields
|
;
Field:
AccessMode VariableDecl
| AccessMode FunctionDecl
;
AccessMode :
T_Private
| T_Protected
| T_Public
|
;
InterfaceDecl:
T_Interface T_ID '{' Prototypes '}'
;
Prototypes:
Prototype Prototypes
|
;
Prototype:
Type T_ID '(' Formals ')' ';'
| T_Void T_ID '(' Formals ')' ';'
;
StmtBlock:
'{' VariableDecls Stmts '}'
;
Stmts:
Stmt Stmts
|
;
Stmt :
Expr ';'
| ';'
| IfStmt
| WhileStmt
| ForStmt
| BreakStmt
| ContinueStmt
| ReturnStmt
| PrintStmt
| StmtBlock
;
IfStmt:
T_If '(' Expr ')' Stmt ElseStmt
;
ElseStmt:
T_Else Stmt
| %prec NOELSE
;
WhileStmt:
T_While '(' Expr ')' Stmt
;
ForStmt:
T_For '(' ExprOrNot ';' Expr ';' ExprOrNot ')' Stmt
;
ReturnStmt:
T_Return ExprOrNot ';'
;
BreakStmt:
T_Break ';'
;
ContinueStmt:
T_Continue ';'
;
PrintStmt:
T_Print '(' Expr ExprContinue ')' ';'
;
ExprOrNot:
Expr
|
;
Expr:
LValue '=' Expr
| Constant
| LValue
| T_This
| Call
| '(' Expr ')'
| Expr '+' Expr
| Expr '-' Expr
| Expr '*' Expr
| Expr '/' Expr
| Expr '%' Expr
| '-' Expr %prec UMINUS
| Expr '<' Expr
| Expr T_LessEqual Expr
| Expr '>' Expr
| Expr T_GreaterEqual Expr
| Expr T_Equal Expr
| Expr T_NotEqual Expr
| Expr T_And Expr
| Expr T_Or Expr
| '!' Expr
| T_ReadInteger '(' ')'
| T_ReadLine '(' ')'
| T_New T_ID
| T_NewArray '(' Expr ',' Type ')'
| T_Itod '(' Expr ')'
| T_Dtoi '(' Expr ')'
| T_Itob '(' Expr ')'
| T_Btoi '(' Expr ')'
;
ExprContinue:
',' Expr ExprContinue
|
;
LValue:
T_ID
| Expr '.' T_ID
| Expr '[' Expr ']'
;
Call:
T_ID '(' Actuals ')'
| Expr '.' T_ID '(' Actuals ')'
;
Actuals:
Expr ExprContinue
|
;
Constant:
T_INTLITERAL
| T_DOUBLELITERAL
| T_BOOLEANLITERAL
| T_STRINGLITERAL
| T_Null
;
%%