-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparser.mly~
38 lines (30 loc) · 859 Bytes
/
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
%{
open Ast
%}
%token <int> NUM
%token <string> IDENT
%token TRUE FALSE
%token IF
%token PLUS MINUS TIMES DIV
%token LPAR RPAR
%token EOL
%start line
%type <Ast.expr> line
%%
line:
expr EOL { $1 }
;
expr:
NUM { ASTNum($1) }
| IDENT { ASTId($1) }
| TRUE { ASTBool(true) }
| FALSE { ASTBool(false) }
| LPAR IF expr expr expr RPAR { ASTIf($3, $4, $5) }
| LPAR PLUS expr expr RPAR { ASTPrim(Ast.Add, $3, $4) }
| LPAR MINUS expr expr RPAR { ASTPrim(Ast.Sub, $3, $4) }
| LPAR TIMES expr expr RPAR { ASTPrim(Ast.Mul, $3, $4) }
| LPAR DIV expr expr RPAR { ASTPrim(Ast.Div, $3, $4) }
exprs:
expr { $1 }
| expr exprs { ASTExpressions($1, $2) }
;