comparison lab0/parser.mly @ 0:bfdcc3820b32

Basis
author Mike Spivey <mike@cs.ox.ac.uk>
date Thu, 05 Oct 2017 08:04:15 +0100
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:bfdcc3820b32
1 /* lab1/parser.mly */
2 /* Copyright (c) 2017 J. M. Spivey */
3
4 %{
5 open Tree
6 %}
7
8 %token <string> IDENT
9 %token <float> NUMBER
10 %token PLUS MINUS TIMES DIVIDE OPEN CLOSE EQUAL EOF BADTOK
11
12 %type <string * Tree.expr> equation
13
14 %start equation
15
16 %%
17
18 equation :
19 expr EOF { ("it", $1) }
20 | IDENT EQUAL expr EOF { ($1, $3) } ;
21
22 expr :
23 term { $1 }
24 | expr PLUS term { Binop (Plus, $1, $3) }
25 | expr MINUS term { Binop (Minus, $1, $3) } ;
26
27 term :
28 factor { $1 }
29 | term TIMES factor { Binop (Times, $1, $3) }
30 | term DIVIDE factor { Binop (Divide, $1, $3) } ;
31
32 factor :
33 NUMBER { Number $1 }
34 | IDENT { Variable $1 }
35 | OPEN expr CLOSE { $2 } ;
36