-
Notifications
You must be signed in to change notification settings - Fork 0
/
Parser.mly
377 lines (314 loc) · 10 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
%{
open Type
open Int32
open Lexing
exception Parse_Error of string
let parse_error info = let spos = Parsing.symbol_start_pos ()
(* and epos = Parsing.symbol_end_pos () *) in
raise (Parse_Error (Printf.sprintf "%s - line %d (%d): %s" spos.pos_fname spos.pos_lnum (spos.pos_cnum - spos.pos_bol) info))
%}
/* package */
%token PACKAGE END
%token PRIVATE PUBLIC
%token OPEN
%token COLONCOLON
/* top-level */
%token TYPE CONST NODE FUNCTION
%token RETURNS VAR LET TEL
%token CLOCK
%token EOF
/* operators */
%token PLUS MINUS MULT DIVIDE MOD DIV /* arithmetic */
%token AND OR XOR NOT SHARP /* logic */
%token EQ NE LT LTEQ GT GTEQ /* relation */
%token WHEN PRE CURRENT FBY ARROW TIMES MATCH MERGE /* tempo */
%token IF THEN ELSE CASE OF /* switch */
%token DOT /* struct */
%token CARET AT REVERSE DOTDOT /* array */
/* brackets */
%token LPAREN RPAREN
%token LBRACKET RBRACKET
%token LBRACE RBRACE
%token LANGLE RANGLE
/* delimiters */
%token COLON SEMICOLON COMMA PIPE
/* constants */
%token <int32> CONST_INT
%token TRUE FALSE
%token <float> CONST_REAL
%token <char> CONST_CHAR
/* types */
%token BOOL INT REAL CHAR
%token ENUM
/* others */
%token <string> IDENT
%token UNDERSCORE
/* precedences */
%nonassoc TIMES
%nonassoc ELSE
%nonassoc ARROW
%nonassoc OR XOR
%nonassoc AND
%nonassoc EQ NE LT LTEQ GT GTEQ
%left PLUS MINUS
%left MULT DIVIDE MOD DIV
%nonassoc UPLUS UMINUS
%nonassoc PRE CURRENT
%nonassoc REVERSE INT REAL
%nonassoc WHEN
%nonassoc NOT
%nonassoc CARRET
%nonassoc AT
%nonassoc DOT INDEX
%start file
%type <Type.program> file
%%
file:
| decls EOF { $1 }
;
decls:
| TYPE type_decls decls { $3 }
| CONST const_decls decls { $3 }
| user_op_decl decls { { $2 with nodes = (node_name
$1.header, $1) :: $2.nodes; } }
| /* empty */ { { types=[]; nodes=[] } }
;
type_decls:
| type_decl { 1 }
| type_decl type_decls { 1 }
type_decl:
| type_decl0 EQ type_expr SEMICOLON { 1 }
;
type_decl0:
| ident_nonlocal { 1 }
;
type_expr:
| BOOL { TBool }
| INT { TInt }
| REAL { TReal }
| CHAR { TChar }
| ident_nonlocal { TIdent $1 }
// | LBRACE field_decls RBRACE { 1 }
// | type_expr CARET expr { 1 }
;
//
// field_decls:
// field_decl { 1 }
// | field_decl COMMA field_decls { 1 }
// ;
//
// field_decl:
// IDENT COLON type_expr { 1 }
// ;
const_decls:
| const_decl { 1 }
| const_decl const_decls { 1 }
;
const_decl:
| const_decl0 COLON type_expr EQ expr SEMICOLON { 1 }
;
const_decl0:
| ident_nonlocal { 1 }
;
user_op_decl:
| node_header SEMICOLON
{ { header=$1; locals = []; equations=[] } }
| node_header equations_single SEMICOLON
{ { header=$1; locals = []; equations=[] } }
| node_header optional_semicolon LET equations TEL optional_semicolon
{ { header=$1; locals = []; equations=$4 } }
| node_header2 optional_semicolon LET equations TEL optional_semicolon
{ { header=fst $1; locals = snd $1; equations=$4 } }
;
node_header:
| op_kind ident_nonlocal params RETURNS params { ($1, $2, $3, $5) }
;
node_header2:
| node_header local_block { ($1, $2) }
;
/* Ignoring the difference between a node and a function */
op_kind:
| NODE { Node }
| FUNCTION { Function }
;
local_block:
| VAR var_decls2 { $2 }
;
var_decls2:
| /* empty */ { [] }
| var_decl SEMICOLON var_decls2 { $1 @ $3 }
;
params:
| LPAREN RPAREN { [] }
| LPAREN var_decls RPAREN { $2 }
;
var_decls:
| var_decl { $1 }
| var_decl SEMICOLON var_decls { $1 @ $3 }
;
var_decl:
| var_ids COLON type_expr when_decl { expand_vars $1 $3 $4 }
;
when_decl:
| WHEN csexpr { Some $2 }
| /* empty */ { None }
;
csexpr:
| IDENT { CWhen $1 }
| NOT IDENT { CNot $2 }
| IDENT MATCH ident_nonlocal { CMatch ($1, $3) }
;
var_ids:
| IDENT { [$1] }
| IDENT COMMA var_ids { $1 :: $3 }
;
optional_semicolon:
| SEMICOLON { 1 }
| /* empty */ { 1 }
;
equations_single:
| equation { [$1] }
;
equations:
| equation SEMICOLON equations { $1 :: $3 }
| /* empty */ { [] }
;
equation:
| lhs EQ expr { ($1, $3) }
;
lhs:
| lhs_id { [$1] }
| lhs_id COMMA lhs { $1 :: $3 }
;
lhs_id:
| ident_local { LIdent $1 }
| UNDERSCORE { Underscore }
;
/* Local variables will shadow any non-local variable.
There are no warning reported. */
ident_expr:
| IDENT { VIdent $1 }
;
ident_nonlocal:
| IDENT { $1 }
;
ident_label:
| IDENT { $1 }
;
ident_local:
| IDENT { $1 }
;
clock_expr:
| ident_local { CWhen $1 }
| NOT ident_local { CNot $2 }
| ident_local MATCH ident_nonlocal { CMatch ($1, $3) }
;
elist:
| /* empty */ { [] }
| expr { [$1] }
| expr COMMA elist { $1 :: $3 }
expr:
| ident_expr { RValue $1 }
| const { RValue $1 }
| list_expr { $1 }
| tempo_expr { $1 }
| arith_expr { $1 }
| relation_expr { $1 }
| bool_expr { $1 }
| switch_expr { $1 }
| apply_expr { $1 }
// | array_expr
// | struct_expr
;
const:
| TRUE { VBool true }
| FALSE { VBool false }
| CONST_CHAR { VChar $1 }
| CONST_INT { VInt $1 }
| CONST_REAL { VReal $1 }
;
const_patt:
| TRUE { VBool true }
| FALSE { VBool false }
| CONST_CHAR { VChar $1 }
| CONST_INT { VInt $1 }
| MINUS CONST_INT { VInt (neg $2) }
| CONST_REAL { VReal $1 }
;
list_expr:
| LPAREN elist RPAREN { Elist $2 }
;
tempo_expr:
| PRE expr { Pre $2 }
| CURRENT expr { Current $2 }
| expr ARROW expr { Arrow ($1, $3) }
| expr WHEN clock_expr { When ($1, $3) }
// | FBY LPAREN elist SEMICOLON CONST_INT SEMICOLON elist RPAREN
//
;
arith_expr:
| MINUS expr %prec UMINUS { Neg $2 }
| PLUS expr %prec UPLUS { $2 }
| INT expr { IntConv $2 } //TODO is this?
| REAL expr { RealConv $2 } //TODO
| expr PLUS expr { Add ($1,$3) }
| expr MINUS expr { Minus ($1,$3) }
| expr MULT expr { Mult ($1,$3) }
| expr DIVIDE expr { Divide ($1,$3) }
| expr DIV expr { Div ($1,$3) }
| expr MOD expr { Mod ($1,$3) }
;
relation_expr:
| expr EQ expr { Eq ($1, $3) }
| expr NE expr { Ne ($1, $3) }
| expr LT expr { Lt ($1, $3) }
| expr GT expr { Gt ($1, $3) }
| expr LTEQ expr { Lteq ($1, $3) }
| expr GTEQ expr { Gteq ($1, $3) }
;
bool_expr:
| NOT expr { Not $2 }
| expr AND expr { And ($1, $3) }
| expr OR expr { Or ($1, $3) }
| expr XOR expr { Xor ($1, $3) }
;
switch_expr:
| IF expr THEN expr ELSE expr { If ($2, $4, $6) }
| LPAREN CASE expr OF case_exprs RPAREN { Case ($3, $5) }
;
case_exprs:
| PIPE UNDERSCORE COLON expr { [(PUnderscore, $4)] }
| PIPE case_expr { [$2] }
| PIPE case_expr case_exprs { $2 :: $3 }
;
case_expr:
| const_patt COLON expr { (PValue $1, $3) }
;
apply_expr:
| ident_nonlocal LPAREN elist RPAREN { Apply (0, $1, $3) }
;
//
// struct_expr:
// expr DOT IDENT
// | LBRACE label_exprs RBRACE
//
// ;
//
// label_exprs:
// label_expr
// | label_expr COMMA label_exprs
// ;
//
// label_expr:
// IDENT COLON expr
// ;
//
// array_expr:
// expr index
// | expr CARET expr
// ;
//
// index:
// LBRACK expr RBRACK
// ;
//