-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.mly
executable file
·367 lines (319 loc) · 7.99 KB
/
Parser.mly
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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
%{
open Printf
open Types
open Ast
open Identifier
(*open Semantic*)
open Error
(*open Parsing*)
(*open Output*)
(*open QuadTypes
open Quads*)
open Lexing
%}
/* Ocamlyacc Declarations */
%token BOOL
%token BREAK
%token BYREF
%token CONTINUE
%token DELETE
%token DOUBLE
%token ELSE
%token FOR
%token FALSE
%token IF
%token INT
%token NEW
%token <int> INT_NUM
%token <float> DOUBLE_NUM
%token NULL
%token RETURN
%token TRUE
%token VOID
%token <string> IDENT
%token <char> CHAR_V
%token CHAR
%token <string> STRING
%token ASSIGN
%token PLUS
%token MINUS
%token TIMES
%token DIV
%token GREATER
%token LESS
%token MOD
%token AMPERSAND
%token NOT
%token Q_MARK
%token COLON
%token COMMA
%token EQUAL
%token N_EQUAL
%token GREAT_EQ
%token LESS_EQ
%token AND
%token OR
%token PPLUS
%token MMINUS
%token ASSIGN_ADD
%token ASSIGN_MINUS
%token ASSIGN_TIMES
%token ASSIGN_DIV
%token ASSIGN_MOD
%token SEMICOLON
%token LPAREN
%token RPAREN
%token LBRACK
%token RBRACK
%token LCBRACK
%token RCBRACK
%token INCLUDE
%token EOF
//%token PLUS MINUS MULTIPLY DIVIDE CARET
%token <string> VAR
%token <float->float> FNCT
/*peirama
%right ASSIGN ASSIGN_TIMES ASSIGN_DIV ASSIGN_MOD ASSIGN_ADD ASSIGN_MINUS PPLUS MMINUS SEMICOLON
%left LESS GREATER EQUAL N_EQUAL LESS_EQ GREAT_EQ AND OR COMMA
%left PLUS MINUS
%left TIMES DIV MOD
%nonassoc UMINUS
%nonassoc LOWER_THAN_ELSE
%nonassoc ELSE
%nonassoc TtypeaS //sto ptr
%nonassoc OTHER //sto expression
%nonassoc OPOP //sto expression_list
*/
/*--------------------------ORDER---------------------------*/
/*DANGLING IF */
%nonassoc LOWER_THAN_ELSE
%nonassoc ELSE
/* ------------*/
%left COMMA
%nonassoc OPOP
%nonassoc TtypeaS /*for typea_t shift/reduce conflict */
%right ASSIGNMENT
%nonassoc Q_MARK
%right COMPIF
%left OR
%left AND
%left LESS GREATER EQUAL N_EQUAL LESS_EQ GREAT_EQ
%left LOPERATOR /* all binary_operators for shift/reduce*/
%right ASSIGN ASSIGN_TIMES ASSIGN_DIV ASSIGN_MOD ASSIGN_ADD
%left PLUS MINUS
%left TIMES DIV MOD
%nonassoc CASTING /*bonus*/
%nonassoc PPLUS MMINUS
%nonassoc NEW DELETE
%nonassoc UNOP
%nonassoc LPAREN RPAREN
%nonassoc POSTFIX
%nonassoc PREFIX
%nonassoc APP
%start start
%type <unit> start
%type <unit> includes
%type <unit> includ
%type <ast_declaration list> program
%type <ast_declaration list> declarations
%type <ast_declaration> declaration
%type <ast_declaration> var_declaration
%type <ast_declaration> func_declaration
%type <ast_declaration> func_definition
%type <ast_declarator list> declarators
%type <ast_declarator> declarator
%type <ast_type> typea
%type <ast_type> b_typea
%type <ast_param list> op_parameter_list
%type <ast_param list> parameter_list
%type <ast_param> parameter
%type <ast_param list> parameters
%type <ast_stmt> statement
%type <ast_stmt list> statements
%type <string> op_ident
%type <string> op_ident_colon
%type <ast_expr option> op_exp
%type <ast_expr> expression
%type <ast_expr> op_brack_exp
%type <ast_expr list> expression_list
%type <ast_expr list> op_exp_list
%type <ast_expr> constant_exp
%type <ast_unop> unary_op
%type <ast_unas> unary_assignment
%type <ast_bop> binary_op_a_p
%type <ast_bop> binary_op_a_t
%type <ast_bop> binary_op_l
%type <ast_bas> binary_assignment
%%
start: includes program EOF { ast_tree := Some $2 }
;
/*initialization : { ignore(initSymbolTable 256);
}*/
includes: /* empty */ { () }
| includes includ { () }
;
includ: INCLUDE STRING { () }
;
program : declaration { [$1]}
| program declaration { $1 @ [$2] }
;
declaration : var_declaration { $1 }
| func_declaration { $1 }
| func_definition { $1 }
;
var_declaration : typea declarators SEMICOLON { Vardecl ($1, $2) }
;
declarators: declarator { [$1] }
| declarators COMMA declarator { $1 @ [$3] }
;
typea: b_typea { $1 }
| typea TIMES %prec TtypeaS { Tptr $1 }
;
/*ptr: empty {}
| ptr TIMES %prec TtypeaS {}
;*/
b_typea: INT { Tint }
| DOUBLE { Tdouble }
| BOOL { Tbool }
| CHAR { Tchar }
| VOID {Tvoid}
;
declarator: IDENT { Decl ($1, None) }
| IDENT LBRACK constant_exp RBRACK { Decl ($1, Some $3) }
;
func_declaration : typea IDENT LPAREN op_parameter_list RPAREN SEMICOLON { Fundecl (($1), $2, $4) }
/*| VOID IDENT LPAREN op_parameter_list RPAREN SEMICOLON { Fundecl (Tvoid, $2, $4) }*/
;
op_parameter_list: /* empty */ { [] }
| parameter_list { $1 }
;
parameter_list:
parameter parameters { $1 :: $2 }
;
parameters :
/*empty*/ { [] }
| COMMA parameter parameters { $2 :: $3 }
;
parameter :
BYREF typea IDENT { ParamByRef ($2, $3) }
| typea IDENT { Param ($1, $2) }
/*
parameter_list : parameter {}
|parameter_list COMMA parameter {}
;
*/
func_definition: typea IDENT LPAREN op_parameter_list RPAREN LCBRACK declarations statements RCBRACK { Fundefi (($1), $2, $4, $7, $8) }
/*| VOID IDENT LPAREN op_parameter_list RPAREN LCBRACK declarations statements RCBRACK { Fundefi (Tvoid, $2, $4, $7, $8) } */
;
declarations: /* empty */ { [] }
|declarations declaration { $1 @ [$2] }
;
statements: /* empty */ { [] }
|statements statement { $1 @ [$2] }
;
op_exp :
/* empty */ { None }
| expression { Some $1 }
;
op_ident_colon :
/* empty */ { "" }
| IDENT COLON { $1 }
;
op_ident :
/* empty */ { "" }
| IDENT { $1 }
;
statement : SEMICOLON { Snull }
| expression SEMICOLON { Sexpr $1}
| LCBRACK statements RCBRACK { Slist $2 }
| IF LPAREN expression RPAREN statement %prec LOWER_THAN_ELSE { Sif ($3, $5, None) }
| IF LPAREN expression RPAREN statement ELSE statement { Sif ($3, $5, Some $7) }
| op_ident_colon FOR LPAREN op_exp SEMICOLON op_exp SEMICOLON op_exp RPAREN statement { Sfor (Some $1, $4, $6, $8,$10) }
| FOR LPAREN op_exp SEMICOLON op_exp SEMICOLON op_exp RPAREN statement { Sfor (None, $3, $5, $7,$9) }
| CONTINUE SEMICOLON { Scont None }
| CONTINUE op_ident SEMICOLON { Scont (Some $2) }
| BREAK SEMICOLON { Sbrk None }
| BREAK op_ident SEMICOLON { Sbrk (Some $2) }
| RETURN op_exp SEMICOLON { Sreturn $2}
;
expression : IDENT { Eid $1 }
| LPAREN expression RPAREN { $2 }
| TRUE { Ebool true }
| FALSE { Ebool false }
| NULL { Enull }
| INT_NUM { Eint $1 }
| CHAR_V { Echar $1 }
| DOUBLE_NUM { Edoub $1 }
| STRING { Estr $1 }
| IDENT LPAREN op_exp_list RPAREN %prec APP{ Eapp ($1, $3) }
| unary_op expression %prec UNOP { Eunop ($2, $1) }
| unary_assignment expression %prec PREFIX { Eunas ($2, $1) }
| LPAREN typea RPAREN expression %prec CASTING { Ecast ($2, $4) }
| NEW typea op_brack_exp { Enew ($2,Some $3) }
| NEW typea SEMICOLON { Enew ($2, None) }
| DELETE expression { Edel $2 }
/*| DELETE expression %prec UMINUS {}*/
| expression LBRACK expression RBRACK %prec POSTFIX { Emat($1, $3) }
| expression binary_opand expression %prec AND { Ebop ($1, $3, $2) }
| expression binary_opor expression %prec OR { Ebop ($1, $3, $2) }
| expression binary_op_a_p expression %prec PLUS { Ebop ($1, $3, $2) }
| expression binary_op_a_t expression %prec TIMES { Ebop ($1, $3, $2) }
| expression binary_op_l expression %prec LOPERATOR { Ebop ($1, $3, $2) }
| expression unary_assignment %prec POSTFIX { Eunas1 ($1, $2) }
| expression binary_assignment expression %prec ASSIGNMENT { Ebas ($1, $3, $2) }
| expression Q_MARK expression COLON expression %prec COMPIF { Eif ($1, $3, $5) }
;
op_exp_list : /* empty */ { [] }
| expression_list { $1 }
;
op_brack_exp : /*empty */ { Eint 1 }
| LBRACK expression RBRACK { $2 }
;
expression_list : expression %prec OPOP { [$1] }
| expression_list COMMA expression %prec COMMA{ $1 @ [$3] }
;
constant_exp : expression { $1 }
;
unary_op :
AMPERSAND { Tuamp }
| TIMES { Tutim }
| PLUS { Tupl }
| MINUS { Tumin }
| NOT { Tunot }
;
binary_op_a_t :
TIMES { Tbtim }
| DIV { Tbdiv }
| MOD { Tbmod }
;
binary_op_a_p :
PLUS { Tbpl }
| MINUS { Tbmin }
;
binary_op_l :
LESS { Tblss }
| GREATER { Tbgrt }
| LESS_EQ { Tbleq }
| GREAT_EQ { Tbgeq }
| EQUAL { Tbeq }
| N_EQUAL { Tbneq }
| COMMA { Tbcom }
;
binary_opand :
AND { Tband }
;
binary_opor:
OR { Tbor }
;
unary_assignment :
PPLUS { Tppl }
| MMINUS { Tmmin }
;
binary_assignment :
ASSIGN { Tba }
| ASSIGN_TIMES { Tbatim }
| ASSIGN_DIV { Tbadiv }
| ASSIGN_MOD { Tbamod }
| ASSIGN_ADD { Tbapl }
| ASSIGN_MINUS { Tbamin }
;