-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexpr.grm
56 lines (41 loc) · 1.4 KB
/
expr.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
(* This is the preamble where you can have arbitrary sml code. For us
it is empty *)
%%
(* The terminals or tokens of the language *)
%term CONST of int
| PLUS
| MINUS
| MUL
| DIV
| LPAREN
| RPAREN
| EOF
| NEWLINE
(* The nonterminals of the language *)
%nonterm EXP of Ast.Expr
| EXPS of Ast.Expr list
| PROGRAM of Ast.Expr list
%eop EOF (* What token to use as end of parsing token *)
%verbose (* Generates a printed form of the table in expr.grm.desc *)
%pure
%name Expr (* The prefix to use on the name of LrValFun structure *)
%noshift EOF
(*
Operator precedence and associativity. The %left says the operator is
left associative and the precedence increase as you go down this list.
*)
%left PLUS MINUS (* + and - are of same precedence *)
%left MUL DIV (* higher than + and - *)
(* The type that captures position in the input *)
%pos int
%%
PROGRAM : EXPS ( EXPS )
| EXPS NEWLINE ( EXPS )
EXPS : (* empty *) ( [] )
| EXP NEWLINE EXPS ( EXP :: EXPS )
EXP : CONST ( Ast.Const CONST )
| EXP PLUS EXP ( Ast.plus EXP1 EXP2 )
| EXP MINUS EXP ( Ast.minus EXP1 EXP2 )
| EXP MUL EXP ( Ast.mul EXP1 EXP2 )
| EXP DIV EXP ( Ast.div EXP1 EXP2 )
| LPAREN EXP RPAREN ( EXP1 )