-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathParser.mly
416 lines (375 loc) · 14.7 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
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
%{
open Format
open Lexing
open Error
open Identifier
open Types
open Symbol
open Printing
open QuadTypes
open Quads
open Parsing
open Semantic
(* Uncomment for more debugging... *)
(* Parsing.set_trace true;; *)
(* Simple Function to get Expression Position *)
let get_binop_pos () =
(rhs_start_pos 1,
rhs_start_pos 3)
(* Function to Register the parameter list of a function *)
let rec registerParams p = function
|[]-> ()
|((p_name, p_type, p_ref)::t) -> (
ignore (newParameter(id_make p_name) p_type p_ref p true);
registerParams p t;
)
(* Function to Register a Function *)
let rec registerFunction id param_list ret_type isLib=
let p = newFunction (id_make id) true isLib in
openScope ret_type;
registerParams p param_list;
endFunctionHeader p ret_type;
if(isLib) then closeScope p else ();
p
(* Called to Register the external Library *)
let registerLibrary() =
ignore(registerFunction "writeInteger"
[("n", TYPE_int, PASS_BY_VALUE)]
TYPE_proc true);
ignore(registerFunction "writeByte"
[("b", TYPE_byte, PASS_BY_VALUE)]
TYPE_proc true);
ignore(registerFunction "writeChar"
[("b", TYPE_byte, PASS_BY_VALUE)]
TYPE_proc true);
ignore(registerFunction "writeString"
[("s", TYPE_array(TYPE_byte,0), PASS_BY_REFERENCE)]
TYPE_proc true);
ignore(registerFunction "readInteger"
[]
TYPE_int true);
ignore(registerFunction "readByte"
[]
TYPE_byte true);
ignore(registerFunction "readChar"
[]
TYPE_byte true);
ignore(registerFunction "readString"
[("n", TYPE_int, PASS_BY_VALUE);
("s", TYPE_array(TYPE_byte,0), PASS_BY_REFERENCE)]
TYPE_proc true);
ignore(registerFunction "extend"
[("b",TYPE_byte, PASS_BY_VALUE)]
TYPE_int true);
ignore(registerFunction "shrink"
[("i", TYPE_int, PASS_BY_VALUE)]
TYPE_byte true);
ignore(registerFunction "strlen"
[("s", TYPE_array(TYPE_byte,0), PASS_BY_REFERENCE)]
TYPE_int true);
ignore(registerFunction "strcmp"
[("s1", TYPE_array(TYPE_byte,0), PASS_BY_REFERENCE);
("s2", TYPE_array(TYPE_byte,0), PASS_BY_REFERENCE)]
TYPE_int true);
ignore(registerFunction "strcpy"
[("trg", TYPE_array(TYPE_byte,0), PASS_BY_REFERENCE);
("src", TYPE_array(TYPE_byte,0), PASS_BY_REFERENCE)]
TYPE_proc true);
ignore(registerFunction "strcat"
[("trg", TYPE_array(TYPE_byte,0), PASS_BY_REFERENCE);
("src", TYPE_array(TYPE_byte,0), PASS_BY_REFERENCE)]
TYPE_proc true);
;;
%}
%token<(string*string)> T_Char
%token<(string*string)> T_String
%token<string> T_Const
%token<string> T_Id
%token T_Byte
%token T_Else
%token T_False
%token T_If
%token T_Int
%token T_Proc
%token T_Ref
%token T_Return
%token T_While
%token T_True
%token T_Set
%token T_Add
%token T_Sub
%token T_Mult
%token T_Div
%token T_Mod
%token T_Not
%token T_And
%token T_Or
%token T_Eq
%token T_Neq
%token T_Leq
%token T_Le
%token T_Geq
%token T_Ge
%token T_LParen
%token T_RParen
%token T_LSq_Bracket
%token T_RSq_Bracket
%token T_LCur_Bracket
%token T_RCur_Bracket
%token T_Comma
%token T_Colon
%token T_Semicolon
%token T_No_type
%token T_Eof
%left T_Or
%left T_And
%nonassoc T_Eq T_Neq T_Ge T_Geq T_Le T_Leq
%left T_Add T_Sub
%left T_Mult T_Div T_Mod
%nonassoc T_Not
%start program
%type <QuadTypes.quad_t list> program
%type <expr_ret_type> expr
%type <expr_ret_type> func_call
%type <expr_ret_type> l_value
%type <cond_ret_type> cond
%type <QuadTypes.quad_t list> local_def
%%
program: initialization first_func_def T_Eof {$2}
| error T_Eof {
fatal "Invalid program: \
Invalid main function definition.";
raise Terminate
};
initialization: { initSymbolTable 256 };
first_func_def: func_header reg_lib local_def compound_stmt {
check_first_proc $1;
handle_func_def (id_name $1.entry_id) $3 $4;
};
reg_lib: { registerLibrary() }
func_def: func_header local_def compound_stmt {
closeScope $1;
handle_func_def (id_name $1.entry_id) $2 $3;
}
func_header: T_Id T_LParen fpar_list T_RParen T_Colon r_type {
registerFunction $1 $3 $6 false
};
fpar_list: { [] }
| fpar_def fpar_list2 { ($1::$2) }
fpar_list2: { [] }
| T_Comma fpar_def fpar_list2 { ($2::$3) };
fpar_def: T_Id T_Colon ttype {
if (check_array_reference $1 $3 (symbol_start_pos ()))
then ($1, $3, PASS_BY_VALUE)
else ($1, $3, PASS_BY_REFERENCE)
}
| T_Id T_Colon T_Ref ttype {
($1, $4, PASS_BY_REFERENCE)
};
data_type: T_Int { TYPE_int }
| T_Byte { TYPE_byte };
ttype: data_type {$1}
| data_type T_LSq_Bracket T_RSq_Bracket {
TYPE_array($1,0)
}
| data_type error T_RSq_Bracket {
let pos = Parsing.symbol_start_pos () in
error "Missing '[' in parameter declaration \
in line: %d, position: %d."
pos.pos_lnum (pos.pos_cnum - pos.pos_bol);
TYPE_none
}
| data_type error T_Eof {
let pos = Parsing.symbol_start_pos () in
error "Missing '[' in parameter declaration \
in line: %d, position: %d."
pos.pos_lnum (pos.pos_cnum - pos.pos_bol);
TYPE_none
};
r_type: data_type {$1}
| T_Proc {TYPE_proc};
local_def: func_def local_def { $2@$1 }
| var_def local_def { $2 }
| { [] };
var_def: T_Id T_Colon data_type T_Semicolon {
ignore (newVariable(id_make $1) $3 true);
}
| T_Id T_Colon data_type T_LSq_Bracket T_Const
T_RSq_Bracket T_Semicolon {
ignore(newVariable(id_make $1)
(TYPE_array($3,int_of_string $5)) true);
}
| T_Id T_Colon data_type error T_Semicolon {
let pos = Parsing.symbol_start_pos () in
error "Invalid Variable Definition [%s]: Expected \
';' or '[const]' after datatype declaration: \
[%s], at Line: %d, Position: %d"
$1 (string_of_typ $3)
pos.pos_lnum (pos.pos_cnum - pos.pos_bol)
}
| T_Id T_Colon error T_Semicolon {
let pos = Parsing.symbol_start_pos () in
error "Invalid Variable Definition [%s]: Wrong \
datatype after ':', at Line: %d, Position: %d"
$1 pos.pos_lnum (pos.pos_cnum - pos.pos_bol)
}
| T_Id error T_Semicolon {
let pos = Parsing.symbol_start_pos () in
error "Invalid Variable Definition [%s]: Expected \
':' after variable name, at Line: %d, Position: %d"
$1 pos.pos_lnum (pos.pos_cnum - pos.pos_bol)}
| T_Id error T_Eof {
let pos = Parsing.symbol_start_pos () in
fatal "Invalid local Definition at Line: %d, \
Position: %d. Missing Ending Semicolon."
pos.pos_lnum (pos.pos_cnum - pos.pos_bol)
}
stmt: T_Semicolon {[]}
| l_value T_Set expr T_Semicolon {
handle_assignment (dereference $1) $3 (get_binop_pos())
}
| compound_stmt {$1}
| func_call T_Semicolon {
check_func_proc $1 (symbol_start_pos ())
}
| if_stmt{$1}
| T_While T_LParen cond T_RParen stmt {
handle_while_stmt $3 $5
}
| T_Return expr T_Semicolon {
handle_return_expr $2 (symbol_start_pos ())
}
| T_Return T_Semicolon {
handle_return_proc (symbol_start_pos ())
}
| error T_Semicolon {
let pos = Parsing.symbol_start_pos () in
error "Invalid statement starting at line %d, \
position %d"
pos.pos_lnum (pos.pos_cnum - pos.pos_bol);
[]
}
| error T_RCur_Bracket {
let pos = Parsing.symbol_start_pos () in
fatal "Statement starting at Line: %d, \
Position: %d is missing ending semicolon"
pos.pos_lnum (pos.pos_cnum - pos.pos_bol);
[]
}
| error T_Eof {
let pos = Parsing.symbol_start_pos () in
fatal "Statement starting at Line: %d, \
Position: %d is missing ending semicolon"
pos.pos_lnum (pos.pos_cnum - pos.pos_bol);
[]
};
if_stmt: T_If T_LParen cond T_RParen stmt {
handle_if_stmt $3 $5
}
| T_If T_LParen cond T_RParen stmt T_Else stmt {
handle_if_else_stmt $3 $5 $7
}
| T_If T_LParen cond error T_Semicolon {
let pos = Parsing.symbol_start_pos () in
error "Invalid If-Statement: Expected ')' \
after condition at Line: %d, Position: %d"
pos.pos_lnum (pos.pos_cnum - pos.pos_bol);
[]
}
| T_If T_LParen error T_Semicolon {
let pos = Parsing.symbol_start_pos () in
error "Invalid If-Statement: Expected ')' \
after condition at Line: %d, Position: %d"
pos.pos_lnum (pos.pos_cnum - pos.pos_bol);
[]
}
| T_If error T_Semicolon {
let pos = Parsing.symbol_start_pos () in
error "Invalid If-Statement: Expected '(' \
after 'if' at Line: %d, Position: %d"
pos.pos_lnum (pos.pos_cnum - pos.pos_bol);
[]
};
compound_stmt: T_LCur_Bracket stmt_list T_RCur_Bracket {$2}
| T_LCur_Bracket T_RCur_Bracket {[]};
stmt_list: stmt_list stmt { $2 @ $1 }
| stmt { $1 };
func_call: T_Id T_LParen T_RParen {
let pos = Parsing.symbol_start_pos () in
handle_func_call $1 pos []
}
| T_Id T_LParen expr_list T_RParen {
let pos = Parsing.symbol_start_pos () in
handle_func_call $1 pos $3
};
expr_list: expr{[$1]}
| expr_list T_Comma expr{($3::$1)};
expr: expr T_Add expr {
handle_expression "+" $1 $3 (get_binop_pos ())
}
| expr T_Sub expr {
handle_expression "-" $1 $3 (get_binop_pos ())
}
| expr T_Mult expr {
handle_expression "*" $1 $3 (get_binop_pos ())
}
| expr T_Div expr {
handle_expression "/" $1 $3 (get_binop_pos ())
}
| expr T_Mod expr {
handle_expression "%" $1 $3 (get_binop_pos ())
}
| T_LParen expr T_RParen {$2}
| T_Const {
{code = []; place = Quad_int($1)}
}
| T_Char {
{code = []; place = Quad_char((snd $1))}
}
| T_String {
{code = []; place = Quad_string((fst $1))}
}
| l_value { dereference $1 }
| func_call {
check_func_expr $1 (symbol_start_pos())
}
| T_Add expr %prec T_Not{
handle_unary_expression "+" $2 (rhs_start_pos 2)
}
| T_Sub expr %prec T_Not{
handle_unary_expression "-" $2 (rhs_start_pos 2)
};
l_value: T_Id {
handle_simple_lvalue $1 (symbol_start_pos ())
}
| T_Id T_LSq_Bracket expr T_RSq_Bracket {
let context = (rhs_start_pos 3, rhs_end_pos 3) in
let pos = symbol_start_pos () in
handle_array_lvalue $1 pos context $3
};
cond: T_True {handle_cond_const true}
| T_False{handle_cond_const false}
| T_LParen cond T_RParen {$2}
| T_Not cond {
{$2 with q_true = $2.q_false; q_false = $2.q_true}
}
| expr T_Eq expr {
handle_comparison "==" $1 $3 (get_binop_pos())
}
| expr T_Neq expr {
handle_comparison "!=" $1 $3 (get_binop_pos())
}
| expr T_Leq expr {
handle_comparison "<=" $1 $3 (get_binop_pos())
}
| expr T_Geq expr {
handle_comparison ">=" $1 $3 (get_binop_pos())
}
| expr T_Le expr {
handle_comparison "<" $1 $3 (get_binop_pos())
}
| expr T_Ge expr {
handle_comparison ">" $1 $3 (get_binop_pos())
}
| cond T_And cond {handle_and $1 $3}
| cond T_Or cond {handle_or $1 $3}