-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathparser.mly
182 lines (150 loc) · 3.19 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
%{
open LMJ
let swap = List.map (fun (x, y) -> (y, x))
%}
%token <int32> INT_CONST
%token <bool> BOOL_CONST
%token INTEGER BOOLEAN
%token <string Location.t> IDENT
%token CLASS PUBLIC STATIC VOID MAIN STRING EXTENDS RETURN
%token PLUS MINUS TIMES NOT LT AND
%token COMMA SEMICOLON
%token ASSIGN
%token LPAREN RPAREN LBRACKET RBRACKET LBRACE RBRACE
%token THIS NEW DOT LENGTH
%token SYSO
%token IF ELSE WHILE
%token EOF
%left AND
%nonassoc LT
%left PLUS MINUS
%left TIMES
%nonassoc NOT
%nonassoc DOT LBRACKET
%start program
%type <LMJ.program> program
%%
program:
| m = main_class d = defs EOF
{
let c, a, i = m in
{
name = c;
defs = d;
main_args = a;
main = i
}
}
main_class:
| CLASS c = IDENT
LBRACE
PUBLIC STATIC VOID MAIN LPAREN STRING LBRACKET RBRACKET a = IDENT RPAREN
LBRACE
i = instruction
RBRACE
RBRACE
{ (c, a, i) }
defs:
| c = list(clas)
{ c }
clas:
| CLASS name = IDENT e = option(preceded(EXTENDS, IDENT))
LBRACE
a = list(pair(typ, terminated(IDENT, SEMICOLON)))
m = list(metho)
RBRACE
{
name,
{
extends = e;
attributes = swap a;
methods = m;
}
}
metho:
| PUBLIC t = typ name = IDENT
LPAREN
f = separated_list(COMMA, pair(typ, IDENT))
RPAREN
LBRACE
ds = declarations_and_statements
RETURN e = expression SEMICOLON
RBRACE
{
let d, s = fst ds, snd ds in
name,
{
formals = swap f;
result = t;
locals = d;
body = s;
return = e;
}
}
declarations_and_statements:
| t = typ id = IDENT SEMICOLON r = declarations_and_statements
{
let d, s = r in
((id, t) :: d, s)
}
| s = list(instruction)
{ ([], s) }
expression:
| e = raw_expression
{ Location.make $startpos $endpos e }
| LPAREN e = expression RPAREN
{ e }
raw_expression:
| i = INT_CONST
{ EConst (ConstInt i) }
| b = BOOL_CONST
{ EConst (ConstBool b) }
| id = IDENT
{ EGetVar id }
| e1 = expression op = binop e2 = expression
{ EBinOp (op, e1, e2) }
| o = expression DOT c = IDENT LPAREN actuals = separated_list(COMMA, expression) RPAREN
{ EMethodCall (o, c, actuals) }
| a = expression LBRACKET i = expression RBRACKET
{ EArrayGet (a, i) }
| NEW INTEGER LBRACKET e = expression RBRACKET
{ EArrayAlloc e }
| a = expression DOT LENGTH
{ EArrayLength a }
| THIS
{ EThis }
| NEW id = IDENT LPAREN RPAREN
{ EObjectAlloc id }
| NOT e = expression
{ EUnOp (UOpNot, e) }
%inline binop:
| PLUS { OpAdd }
| MINUS { OpSub }
| TIMES { OpMul }
| LT { OpLt }
| AND { OpAnd }
instruction:
| b = block
{ b }
| id = IDENT ASSIGN e = expression SEMICOLON
{ ISetVar (id, e) }
| a = IDENT LBRACKET i = expression RBRACKET ASSIGN e = expression SEMICOLON
{ IArraySet (a, i, e) }
| SYSO LPAREN e = expression RPAREN SEMICOLON
{ ISyso e }
| IF LPAREN c = expression RPAREN i1 = instruction ELSE i2 = instruction
{ IIf (c, i1, i2) }
| WHILE LPAREN c = expression RPAREN i = instruction
{ IWhile (c, i) }
block:
| LBRACE is = list(instruction) RBRACE
{ IBlock is }
typ:
| INTEGER
{ TypInt }
| BOOLEAN
{ TypBool }
| INTEGER LBRACKET RBRACKET
{ TypIntArray }
| id = IDENT
{ Typ id }