-
Notifications
You must be signed in to change notification settings - Fork 1
/
parser.mly
462 lines (396 loc) · 15.5 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
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
%{
open Ast
open Runtime
open Symboltable
open Typechecker
let get_super name =
(* TODO move this to typechecker? *)
class_table#get name
let make_class name super members =
let field_table : astMember symbol_table = new symbol_table in
let method_table : astMember symbol_table = new symbol_table in
let ctor = ref (make_default_ctor name) in
let ctor_found = ref false in
let add_symbol s =
match s with
| Field (_, _, var_decl) -> field_table#put var_decl.name s
| Method (n, _, _, _, _, _) -> method_table#put n s
| Constructor (_, _, _, _, _) ->
if !ctor_found then
type_err_list :=
("Duplicate constructor: " ^ strMember s) ::
!type_err_list
else begin
ctor_found := true;
ctor := s
end;
in
List.iter add_symbol members;
let c = {t = ClassType(name); super = Some super; constructor = !ctor;
fieldTable = field_table; methodTable = method_table} in
class_table#put name c;
c
let make_method name t mods formals statements =
let var_table : astType symbol_table = new symbol_table in
(* let add_symbol s =
match s with
| DeclStatement (dt, decls) ->
List.iter (fun d -> var_table#put d.name dt) decls
| _ -> ()
in
List.iter add_symbol statements;*)
List.iter (fun (f : astFormal) -> var_table#put f.name f.t) formals;
Method(name, t, var_table, mods, formals, statements)
let make_constructor t mods formals statements =
let var_table : astType symbol_table = new symbol_table in
(* let add_symbol s =
match s with
| DeclStatement (dt, decls) ->
List.iter (fun d -> var_table#put d.name dt) decls
| _ -> ()
in
List.iter add_symbol statements;*)
List.iter (fun (f : astFormal) -> var_table#put f.name f.t) formals;
Constructor(t, var_table, mods, formals, statements)
let make_while e s =
(* let var_table = ref (new symbol_table) in
(match s with
| DeclStatement (dt, decls) ->
List.iter (fun d -> !var_table#put d.name dt) decls
| BlockStatement (btable, _) -> var_table := btable#clone
| _ -> ());
WhileStatement(!var_table, e, s) *)
WhileStatement (new symbol_table, e, s)
let make_if e st sfo =
(*let true_table = ref (new symbol_table) in
let false_table = ref (new symbol_table) in
(match st with
| DeclStatement (dt, decls) ->
List.iter (fun d -> !true_table#put d.name dt) decls
| BlockStatement (btable, _) -> true_table := btable#clone
| _ -> ());
match sfo with
| Some sf ->
((match sf with
| DeclStatement (dt, decls) ->
List.iter (fun d -> !false_table#put d.name dt) decls
| BlockStatement (btable, _) -> false_table := btable#clone
| _ -> ());
IfStatement(!true_table, Some !false_table, e, st, sfo))
| None -> IfStatement(!true_table, None, e, st, sfo)*)
match sfo with
| Some sf -> IfStatement(new symbol_table, Some new symbol_table, e, st,
sfo)
| None -> IfStatement(new symbol_table, None, e, st, sfo)
let make_block statements =
(*let var_table : astType symbol_table = new symbol_table in
let add_symbol s =
match s with
| DeclStatement (dt, decls) ->
List.iter (fun d -> var_table#put d.name dt) decls
| _ -> ();
in
List.iter add_symbol statements;
BlockStatement(var_table, statements)*)
BlockStatement(new symbol_table, statements)
let make_default_typebox () : astTypeBox = {t = ClassType("Object")}
%}
%token BREAK
%token CLASS
%token CONTINUE
%token ELSE
%token EXTENDS
%token IF
%token NEW
%token PRIVATE
%token PROTECTED
%token PUBLIC
%token RETURN
%token STATIC
%token SUPER
%token THIS
%token WHILE
%token <string> ID
%token <int> INT
%token <char> CHAR
%token <string> STRING
%token <bool> BOOL
%token NULL
%token BOOLTYPE
%token CHARTYPE
%token INTTYPE
%token VOIDTYPE
%token LPAREN
%token RPAREN
%token LBRACE
%token RBRACE
%token LBRACKET
%token RBRACKET
%token SEMICOLON
%token COMMA
%token DOT
%token ASGN
%token GREATER
%token LESS
%token NOT
%token EQUALS
%token GEQ
%token LEQ
%token NEQ
%token PLUS
%token MINUS
%token TIMES
%token DIV
%token AND
%token OR
%token MOD
%token EOF
%right ASGN
%left OR
%left AND
%left EQUALS NEQ
%nonassoc GEQ LEQ GREATER LESS
%left PLUS MINUS
%left MOD DIV TIMES
%nonassoc NOT UMINUS UPLUS
/*
RE: LOWER_THAN_ELSE dangling else solution: see
https://stackoverflow.com/questions/1737460/how-to-find-shift-reduce-conflict-in-this-yacc-file
*/
%nonassoc LOWER_THAN_ELSE
%nonassoc ELSE
/* TODO this is not elegant when combined with the class table - fix? */
%start classList
%type <Ast.astClass list> classList
%%
classList
: singleClass classList { [$1]@$2 }
| singleClass { [$1] }
| EOF { [] }
;
singleClass
: CLASS ID LBRACE RBRACE { make_class $2 object_class
[] }
| CLASS ID super LBRACE RBRACE { make_class $2 $3 [] }
| CLASS ID LBRACE memberlist RBRACE { make_class $2 object_class
$4 }
| CLASS ID super LBRACE memberlist RBRACE { make_class $2 $3 $5 }
;
super
: EXTENDS ID { get_super $2 }
;
memberlist
: member memberlist { $1@$2 }
| member { $1 }
;
member
: field { $1 }
| methodDecl { $1 }
| ctor { $1 }
;
field
: modifierlist typeD varDeclList SEMICOLON { List.map
(fun d -> Field($1, $2, d))
$3 }
| typeD varDeclList SEMICOLON { List.map
(fun d ->
Field([Public], $1, d)) $2 }
;
modifierlist
: modifier modifierlist { [$1]@$2 }
| modifier { [$1] }
;
methodDecl
: modifierlist typeD ID formalArgs block { [make_method $3 $2 $1 $4 $5] }
| typeD ID formalArgs block { [make_method $2 $1 [Public] $3 $4] }
;
ctor
: modifierlist ID formalArgs block { [make_constructor
(ClassType($2)) $1 $3 $4] }
| ID formalArgs block { [make_constructor
(ClassType($1)) [Public] $2
$3] }
;
modifier
: STATIC { Static }
| PUBLIC { Public }
| PRIVATE { Private }
| PROTECTED { Protected }
;
formalArgs
: LPAREN formalArgList RPAREN { $2 }
| LPAREN RPAREN { [] }
;
formalArgList
: formalArg COMMA formalArgList { [$1]@$3 }
| formalArg { [$1] }
;
formalArg
: typeD varDeclId
{
let declName, declCnt = $2 in
if declCnt != 0 then
match $1 with
| ClassType s ->
{name = declName; t = ArrayType(ClassType(s), declCnt)}
| ArrayType (arrayT, c) ->
{name = declName; t = ArrayType(arrayT, c + declCnt)}
| BoolType ->
{name = declName; t = ArrayType(BoolType, declCnt)}
| CharType ->
{name = declName; t = ArrayType(CharType, declCnt)}
| IntType ->
{name = declName; t = ArrayType(IntType, declCnt)}
(* TODO should a void type in a formal arg be a parse error? Or
maybe offload to type checker *)
| VoidType ->
{name = declName; t = ArrayType(VoidType, declCnt)}
(* TODO these aren't really parse errors but semantic errors *)
| MethodType (_, _, _) -> raise Parsing.Parse_error
else
{name = declName; t = $1}
}
;
typeD
: primitiveType { $1 }
| ID { ClassType($1) }
| primitiveType bracketList { ArrayType($1, $2) }
| ID bracketList { ArrayType(ClassType($1), $2) }
;
bracketList
: LBRACKET RBRACKET bracketList { 1 + $3}
| LBRACKET RBRACKET { 1 }
;
primitiveType
: BOOLTYPE { BoolType }
| CHARTYPE { CharType }
| INTTYPE { IntType }
| VOIDTYPE { VoidType }
;
varDeclList
: varDecl COMMA varDeclList { [$1]@$3 }
| varDecl { [$1] }
;
varDecl
: varDeclId ASGN expr { { name = fst $1
; dim = snd $1
; expr = Some $3
} }
| varDeclId { { name = fst $1
; dim = snd $1
; expr = None
} }
;
varDeclId
: varDeclId LBRACKET RBRACKET { fst $1, snd $1 + 1 }
| ID { $1, 0 }
;
block
: LBRACE statementList RBRACE { $2 }
| LBRACE RBRACE { [] }
;
statementList
: statement statementList { [$1]@$2 }
| statement { [$1] }
;
statement
: SEMICOLON { EmptyStatement }
| typeD varDeclList SEMICOLON { DeclStatement($1, $2) }
| IF LPAREN expr RPAREN statement
%prec LOWER_THAN_ELSE { make_if $3 $5 None }
| IF LPAREN expr RPAREN statement ELSE
statement { make_if $3 $5 (Some $7) }
| expr SEMICOLON { ExprStatement($1) }
| WHILE LPAREN expr RPAREN statement { WhileStatement(
new symbol_table, $3, $5) }
| RETURN SEMICOLON { ReturnStatement(None) }
| RETURN expr SEMICOLON { ReturnStatement(Some $2) }
| CONTINUE SEMICOLON { ContinueStatement }
| BREAK SEMICOLON { BreakStatement }
| block { make_block $1 }
| SUPER actualArgs SEMICOLON { SuperStatement($2) }
;
expr
: expr ASGN expr { BinOpExpr(Asgn, $1, $3) }
| expr GREATER expr { BinOpExpr(Greater, $1, $3) }
| expr LESS expr { BinOpExpr(Less, $1, $3) }
| expr EQUALS expr { BinOpExpr(Equals, $1, $3) }
| expr GEQ expr { BinOpExpr(Geq, $1, $3) }
| expr LEQ expr { BinOpExpr(Leq, $1, $3) }
| expr NEQ expr { BinOpExpr(Neq, $1, $3) }
| expr PLUS expr { BinOpExpr(BinPlus, $1, $3) }
| expr MINUS expr { BinOpExpr(BinMinus, $1, $3) }
| expr TIMES expr { BinOpExpr(Times, $1, $3) }
| expr DIV expr { BinOpExpr(Div, $1, $3) }
| expr AND expr { BinOpExpr(And, $1, $3) }
| expr OR expr { BinOpExpr(Or, $1, $3) }
| expr MOD expr { BinOpExpr(Mod, $1, $3) }
| PLUS expr %prec UPLUS { UnOpExpr(UnPlus, $2) }
| MINUS expr %prec UMINUS { UnOpExpr(UnMinus, $2) }
| NOT expr { UnOpExpr(Not, $2) }
| primary { PrimaryExpr($1) }
;
primary
: newArrayExpr { NewArrayPrimary(
make_default_typebox (),
$1) }
| nonNewArrayExpr { NonNewArrayPrimary(
make_default_typebox (),
$1) }
| ID { IdPrimary(make_default_typebox
(), $1) }
;
newArrayExpr
: NEW ID dimensionList { { t = ClassType($2)
; dimList = $3
} }
| NEW primitiveType dimensionList { { t = $2
; dimList = $3
}}
;
dimensionList
: dimension dimensionList { [$1]@$2 }
| dimension { [$1] }
;
dimension
: LBRACKET expr RBRACKET { $2 }
;
nonNewArrayExpr
: literal { LiteralExpr($1) }
| THIS { ThisExpr }
| LPAREN expr RPAREN { ParenExpr($2) }
| NEW ID actualArgs { NewObjExpr($2, $3) }
| ID actualArgs { ThisCallExpr($1, $2) }
| primary DOT ID actualArgs { MethodCallExpr($1, $3, $4) }
| SUPER DOT ID actualArgs { SuperCallExpr($3, $4) }
| arrayExpr { $1 }
| fieldExpr { $1 }
;
fieldExpr
: primary DOT ID { FieldExpr($1, $3)}
| SUPER DOT ID { SuperFieldExpr($3) }
;
arrayExpr
: ID dimension { ArrayExpr(IdPrimary(
make_default_typebox (),
$1), $2) }
| nonNewArrayExpr dimension { ArrayExpr(NonNewArrayPrimary(
make_default_typebox (),
$1), $2) }
;
literal
: NULL { NullLiteral }
| BOOL { BoolLiteral($1) }
| INT { IntLiteral($1) }
| CHAR { CharLiteral($1) }
| STRING { StringLiteral($1) }
;
actualArgs
: LPAREN exprList RPAREN { $2 }
| LPAREN RPAREN { [] }
;
exprList
: expr COMMA exprList { [$1]@$3 }
| expr { [$1] }
;
%%