-
Notifications
You must be signed in to change notification settings - Fork 1
/
mlpolyr.grm
399 lines (350 loc) · 9.4 KB
/
mlpolyr.grm
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
(* -*- sml-yacc -*-
* mlpolyr.grm
*
* The MLPolyR Grammar (for ML-Yacc).
*
* Copyright (c) 2005 by Matthias Blume ([email protected])
*)
%%
%term NAME of Ast.symbol
| NUMBER of Ast.integer
| SMALLNUM of int
| STRING of string
| KW_andalso | KW_orelse
| KW_if | KW_then | KW_else
| KW_false | KW_true
| KW_let | KW_in | KW_end
| KW_fun | KW_and | KW_val
| KW_fn
| KW_match | KW_with
| KW_cases | KW_default | KW_nocases
| KW_as
| KW_where
| KW_case | KW_of
| KW_try | KW_handling | KW_rehandling
| KW_raise
| KW_isnull
| KW_not
| LP | RP | LCB | RCB | LCBB | RCBB | LSB | RSB
| DEQ | LTEQ | LT | GTEQ | GT | NEQ
| DCOLON
| PLUS | MINUS | TIMES | DIV | MOD
| EQ | DOT | COMMA | SEMI | EXCLAM | ASSIGN
| BAR | DARROW
| COLON
| WILD | DOTDOTDOT
| BQ | PLUSPLUS
| EOF
%nonterm program of Ast.program
| function of Ast.function
| functions of Ast.function list
| morefunctions of Ast.function list
| rc of Ast.reccases
| rcl of Ast.reccases list
| morercl of Ast.reccases list
| optrcl of Ast.reccases list
| def of Ast.def
| defs of Ast.def list
| exp of Ast.exp
| exp0 of Ast.exp
| dtmatch of Ast.dtmatch
| recordexp of Purity.purity * Ast.exp Ast.field list
| rowexp of Ast.exp Ast.field list
| fieldexpseq of Ast.exp Ast.field list
| fieldexp of Ast.exp Ast.field
| expseq' of Ast.exp list
| seqexp of Ast.exp
| explistopt of Ast.exp list
| explist' of Ast.exp list
| atomicexp of Ast.exp
| selexp of Ast.exp
| selexp0 of Ast.exp
| appexp of Ast.exp
| appexp0 of Ast.exp
| label of Ast.mlabel
| con of Ast.mlabel
| number of LiteralData.integer
| formals of Ast.pat list
| pat0 of Ast.pat
| pat of Ast.pat
| patlist of Ast.pat list
| rowpat of Ast.pat Ast.field list
| fieldpatlist of Ast.pat Ast.field list
| fieldpat of Ast.pat Ast.field
| mr of Ast.mrule
| mrlist of Ast.mrule list
| handlers of Ast.mrule list * Ast.lambda option * Ast.mrule list
| rehandling of Ast.mrule list
| handling of Ast.mrule list * Ast.lambda option
| defaultmatch of Ast.exp option
%left KW_else DARROW COLON KW_of KW_with KW_raise
%left ASSIGN
%left KW_where
%left KW_as
%left KW_orelse
%left KW_andalso
%left DEQ LTEQ LT GTEQ GT NEQ
%right DCOLON
%left PLUS MINUS PLUSPLUS
%left TIMES DIV MOD
%pos int
%verbose
%start program
%eop EOF
%noshift EOF
%name MLPolyR
%%
program : exp
((exp, (expleft, expright)))
function : NAME formals EQ exp
(Ast.FUN (NAME, formals, exp, (NAMEleft, expright)))
functions : function morefunctions
(function :: morefunctions)
morefunctions : (* empty *)
([])
| KW_and functions
(functions)
optrcl : (* empty *)
([])
| KW_with KW_cases rcl
(rcl)
rcl : rc morercl
(rc :: morercl)
morercl : (* empty *)
([])
| KW_and rcl
(rcl)
rc : NAME EQ exp
(Ast.RC (NAME, exp, (NAMEleft, expright)))
formals : pat
([pat])
| pat formals
(pat :: formals)
pat0 : WILD
(Ast.WILDpat)
| NAME
(Ast.VARpat NAME)
| LP RP
(Ast.TUPLEpat [])
| LP pat RP
(pat)
| LP patlist RP
(Ast.TUPLEpat patlist)
| LCB rowpat RCB
(Ast.RECORDpat (Purity.Pure, rowpat))
| LCBB rowpat RCBB
(Ast.RECORDpat (Purity.Impure, rowpat))
| BQ LCB rowpat RCB
(Ast.MATCHpat rowpat)
| pat KW_as pat
(Ast.ANDpat (pat1, pat2))
pat : pat0
(Ast.MARKpat (pat0, (pat0left, pat0right)))
patlist : pat COMMA pat
([pat1, pat2])
| pat COMMA patlist
(pat :: patlist)
rowpat : (* empty *)
([])
| fieldpatlist
(fieldpatlist)
fieldpatlist : fieldpat
([fieldpat])
| fieldpat COMMA fieldpatlist
(fieldpat :: fieldpatlist)
fieldpat : NAME
((SOME (RecordLabel.SYMlab NAME, (NAMEleft, NAMEright)),
Ast.VARpat NAME))
| label EQ pat
((SOME label, pat))
| DOTDOTDOT
((NONE, Ast.WILDpat))
| DOTDOTDOT EQ pat
((NONE, pat))
def : KW_val pat EQ exp
(Ast.VALdef (pat, exp))
| KW_fun functions optrcl
(Ast.FUNdef (functions, optrcl, (KW_funleft, functionsright)))
defs : (* empty *)
([])
| def defs
(def :: defs)
exp0 : KW_let defs KW_in seqexp KW_end
(Ast.LETexp (defs, seqexp))
| KW_if exp KW_then exp KW_else exp
(Ast.IFexp (exp1, exp2, exp3))
| KW_case exp KW_of dtmatch
(Ast.CASEexp (exp, dtmatch))
| KW_raise exp
(Ast.RAISEexp exp)
| KW_try pat EQ exp KW_in exp handlers KW_end
(Ast.TRYexp { scrutinee = exp1,
success = (pat, exp2),
handling = #1 handlers,
catchall = #2 handlers,
rehandling = #3 handlers })
| KW_match exp KW_with exp
(Ast.PSCASEexp (exp1, exp2))
| KW_cases mrlist defaultmatch
(Ast.MATCHexp (mrlist, defaultmatch))
| exp KW_where recordexp
(Ast.WHEREexp (#1 recordexp, exp, #2 recordexp))
| exp KW_orelse exp
(Ast.BINOPexp (Ast.BOOLCONN Ast.ORELSE, exp1, exp2))
| exp KW_andalso exp
(Ast.BINOPexp (Ast.BOOLCONN Ast.ANDALSO, exp1, exp2))
| exp DEQ exp
(Ast.BINOPexp (Ast.CMP Oper.EQ, exp1, exp2))
| exp LTEQ exp
(Ast.BINOPexp (Ast.CMP Oper.LTEQ, exp1, exp2))
| exp LT exp
(Ast.BINOPexp (Ast.CMP Oper.LT, exp1, exp2))
| exp GTEQ exp
(Ast.BINOPexp (Ast.CMP Oper.GTEQ, exp1, exp2))
| exp GT exp
(Ast.BINOPexp (Ast.CMP Oper.GT, exp1, exp2))
| exp NEQ exp
(Ast.BINOPexp (Ast.CMP Oper.NEQ, exp1, exp2))
| exp DCOLON exp
(Ast.BINOPexp (Ast.CONS, exp1, exp2))
| exp PLUS exp
(Ast.BINOPexp (Ast.ARITH Oper.PLUS, exp1, exp2))
| exp MINUS exp
(Ast.BINOPexp (Ast.ARITH Oper.MINUS, exp1, exp2))
| exp TIMES exp
(Ast.BINOPexp (Ast.ARITH Oper.TIMES, exp1, exp2))
| exp DIV exp
(Ast.BINOPexp (Ast.ARITH Oper.DIV, exp1, exp2))
| exp MOD exp
(Ast.BINOPexp (Ast.ARITH Oper.MOD, exp1, exp2))
| KW_fn pat DARROW exp
(Ast.FNexp (pat, exp))
| appexp
(appexp)
| selexp EXCLAM label ASSIGN exp
(Ast.ASSIGNexp (selexp, label, exp))
dtmatch : LSB RSB DARROW exp BAR pat DCOLON pat DARROW exp
({ nilcase = exp1, conscase = (pat1, pat2, exp2) })
| pat DCOLON pat DARROW exp BAR LSB RSB DARROW exp
({ nilcase = exp2, conscase = (pat1, pat2, exp1) })
exp : exp0
(Ast.MARKexp (exp0, (exp0left, exp0right)))
appexp0 : selexp
(selexp)
| appexp selexp
(Ast.APPexp (appexp, selexp))
| MINUS selexp
(Ast.UOPexp (Ast.UMINUS, selexp))
| KW_isnull selexp
(Ast.UOPexp (Ast.ISNULL, selexp))
| KW_not selexp
(Ast.UOPexp (Ast.NOT, selexp))
| con selexp
(Ast.CONexp (con, selexp))
appexp : appexp0
(Ast.MARKexp (appexp0, (appexp0left, appexp0right)))
selexp0 : atomicexp
(atomicexp)
| selexp DOT label
(Ast.SELexp (Purity.Pure, selexp, label))
| selexp EXCLAM label
(Ast.SELexp (Purity.Impure, selexp, label))
| selexp PLUSPLUS label
(Ast.SWIDENexp (selexp, label))
selexp : selexp0
(Ast.MARKexp (selexp0, (selexp0left, selexp0right)))
atomicexp : KW_true
(Ast.BOOLexp true)
| KW_false
(Ast.BOOLexp false)
| number
(Ast.NUMBERexp number)
| STRING
(Ast.STRINGexp STRING)
| NAME
(Ast.VARexp NAME)
| LP RP
(Ast.SEQexp [])
| LP exp RP
(exp)
| LP expseq' RP
(Ast.SEQexp expseq')
| LP explist' RP
(Ast.TUPLEexp explist')
| LSB explistopt RSB
(Ast.LISTexp explistopt)
| recordexp
(Ast.RECORDexp recordexp)
| KW_nocases
(Ast.MATCHexp ([], NONE))
recordexp : LCB rowexp RCB
(Purity.Pure, rowexp)
| LCBB rowexp RCBB
(Purity.Impure, rowexp)
rowexp : (* empty *)
([])
| fieldexpseq
(fieldexpseq)
fieldexpseq : fieldexp
([fieldexp])
| fieldexp COMMA fieldexpseq
(fieldexp :: fieldexpseq)
fieldexp : label EQ exp
((SOME label, exp))
| NAME
((SOME (RecordLabel.SYMlab NAME, (NAMEleft, NAMEright)),
Ast.VARexp NAME))
| DOTDOTDOT EQ exp
((NONE, exp))
label : NAME
(RecordLabel.SYMlab NAME, (NAMEleft, NAMEright))
| SMALLNUM
(RecordLabel.NUMlab SMALLNUM, (SMALLNUMleft, SMALLNUMright))
con : BQ label
(label)
explistopt : (* empty *)
([])
| exp
([exp])
| explist'
(explist')
explist' : exp COMMA exp
([exp1, exp2])
| exp COMMA explist'
(exp :: explist')
expseq' : exp SEMI exp
([exp1, exp2])
| exp SEMI expseq'
(exp :: expseq')
seqexp : exp
(exp)
| expseq'
(Ast.SEQexp expseq')
number : NUMBER
(NUMBER * 2)
| SMALLNUM
(LiteralData.fromInt SMALLNUM * 2)
mr : BQ label pat DARROW exp
(label, (pat, exp), (BQleft, expright))
mrlist : mr
([mr])
| mr BAR mrlist
(mr :: mrlist)
rehandling : (* empty *)
([])
| KW_rehandling mrlist
(mrlist)
handling : pat DARROW exp
([], SOME (pat, exp))
| mr
([mr], NONE)
| mr BAR handling
(mr :: #1 handling, #2 handling)
handlers : rehandling
([], NONE, rehandling)
| KW_handling handling rehandling
(#1 handling, #2 handling, rehandling)
defaultmatch : (* KW_nodefault *)
(NONE)
| KW_default COLON exp
(SOME exp)