-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser.mly
235 lines (161 loc) · 6.17 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
%{ (* HEADER *)
open Syntaxtree;;
%}
%token <string> IDE
%token <int> NUM
%token <float> REAL
%token TRUE FALSE
%token PROGRAM VAR ARRAY OF INT FLOAT PROCEDURE FUNCTION BEGIN END
%token IF THEN ELSE WHILE DO REPEAT UNTIL FOR TO WRITE CALL
%token PLUS MINUS TIMES DIVISION EQUAL LESSEQUAL LESS AND OR NOT
%token ASSIGN
%token SEMICOLON COLON COMMA
%token LS RS LP RP
%token GREATEREQUAL GREATER
%token RECORD DOT
%token EOF
%start program
%type<Syntaxtree.program> program
%%
program
: PROGRAM opt_rec_list opt_dec_list opt_proc_list cmd EOF
{ Program($2,$3,$4,$5) }
;
opt_rec_list
: { [] }
| rec_list { $1 }
;
rec_list
: rec_ { [$1] }
| rec_ rec_list { $1::$2 }
;
opt_dec_list
: { [] }
| dec_list { $1 }
;
dec_list
: dec { [$1] }
| dec dec_list { $1::$2 }
;
rec_
: RECORD ide BEGIN dec_list END SEMICOLON { Record($2,$4) }
;
dec
: VAR ide COLON gType SEMICOLON { Dec($2,$4) }
;
gType
: bType { Basic($1) }
| ide { UserType($1) }
| ARRAY LS NUM RS OF bType { Vector($3,$6) }
| ARRAY LS NUM COMMA NUM RS OF bType { Matrix($3,$5,$8) }
;
bType
: INT { Int }
| FLOAT { Float }
;
opt_proc_list
: { [] }
| proc_list { $1 }
;
proc_list
: proc { [$1] }
| proc proc_list { $1::$2 }
;
proc
: PROCEDURE ide LP opt_param_list RP opt_dec_list cmd
{ Proc($2,$4,$6,$7) }
| FUNCTION ide LP opt_param_list RP COLON ret opt_dec_list cmd
{ Func($2,$4,$7,$8,$9) }
;
opt_param_list
: { [] }
| param_list { $1 }
;
param_list
: param { [$1] }
| param COMMA param_list { $1::$3 }
;
param
: ide COLON bType { Par($1,$3) }
;
ret
: bType { Ret($1) }
;
cmd
: lexp ASSIGN aexp SEMICOLON { Ass($1,$3) }
| BEGIN opt_cmd_list END SEMICOLON { Blk($2) }
| IF bexp THEN cmd ELSE cmd { Ite($2,$4,$6) }
| WHILE bexp DO cmd { While($2,$4) }
| REPEAT cmd UNTIL bexp SEMICOLON { Repeat($2,$4) }
| FOR ide ASSIGN aexp TO aexp DO cmd { For($2,$4,$6,$8) }
| WRITE aexp SEMICOLON { Write($2) }
| CALL ide LP opt_aexp_list RP SEMICOLON { PCall($2,$4) }
;
opt_cmd_list
: { [] }
| cmd_list { $1 }
;
cmd_list
: cmd { [$1] }
| cmd cmd_list { $1::$2 }
;
lexp
: ide { LVar($1) }
| ide LS aexp RS { LVec($1,$3) }
| ide LS aexp COMMA aexp RS { LMat($1,$3,$5) }
| ide DOT lexp { LRec($1,$3) }
;
bexp_factor
: TRUE { B(true) }
| FALSE { B(false) }
| aexp EQUAL aexp { Equ($1,$3) }
| aexp LESSEQUAL aexp { LE($1,$3) }
| aexp LESS aexp { LT($1,$3) }
| aexp GREATEREQUAL aexp { GE($1,$3) }
| aexp GREATER aexp { GT($1,$3) }
| NOT bexp_factor { Not($2) }
| LP bexp RP { $2 }
;
bexp_term
: bexp_term AND bexp_factor { And($1,$3) }
| bexp_factor { $1 }
;
bexp
: bexp OR bexp_term { Or($1,$3) }
| bexp_term { $1 }
;
aexp_factor
: NUM { N($1) }
| REAL { R($1) }
| ide { Var($1) }
| ide LS aexp RS { Vec($1,$3) }
| ide LS aexp COMMA aexp RS { Mat($1,$3,$5) }
| ide DOT ide { Rec($1,$3) }
| ide DOT ide LS aexp RS { RecV($1,$3,$5) }
| ide DOT ide LS aexp COMMA aexp RS { RecM($1,$3,$5,$7) }
| CALL ide LP opt_aexp_list RP { FCall($2,$4) }
| LP aexp RP { $2 }
;
aexp_term
: aexp_term TIMES aexp_factor { Mul($1,$3) }
| aexp_term DIVISION aexp_factor { Div($1,$3) }
| aexp_factor { $1 }
;
aexp
: aexp PLUS aexp_term { Sum($1,$3) }
| aexp MINUS aexp_term { Sub($1,$3) }
| aexp_term { $1 }
;
opt_aexp_list
: { [] }
| aexp_list { $1 }
;
aexp_list
: aexp { [$1] }
| aexp COMMA aexp_list { $1::$3 }
;
ide
: IDE { Ide($1) }
;
%%
(* FOOTER *)