forked from rescript-lang/rescript
-
Notifications
You must be signed in to change notification settings - Fork 0
/
arith_parser.mly
48 lines (36 loc) · 867 Bytes
/
arith_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
%{
open Arith_syntax
%}
/* Lexemes */
%token <float> NUMERAL
%token <string> IDENT
%token PLUS
%token MINUS
%token TIMES
%token DIVIDE
%token UMINUS
%token LPAREN
%token RPAREN
%token EOF
/* Precedence and associativity */
%left PLUS MINUS
%left TIMES DIVIDE
%nonassoc UMINUS
/* Top level rule */
%start toplevel
%type <Arith_syntax.expression> toplevel
%%
/* Grammar */
toplevel: expression EOF { $1 }
;
expression:
| NUMERAL { Numeral $1 }
| IDENT { Variable $1 }
| expression PLUS expression { Plus ($1, $3) }
| expression MINUS expression { Minus ($1, $3) }
| expression TIMES expression { Times ($1, $3) }
| expression DIVIDE expression { Divide ($1, $3) }
| MINUS expression %prec UMINUS { Negate $2 }
| LPAREN expression RPAREN { $2 }
;
%%