Import Upstream version 20180207
[hcoop/debian/mlton.git] / mlyacc / examples / calc / calc.grm
CommitLineData
7f918cf1
CE
1(* Sample interactive calculator for ML-Yacc *)
2
3fun lookup "bogus" = 10000
4 | lookup s = 0
5
6%%
7
8%eop EOF SEMI
9
10(* %pos declares the type of positions for terminals.
11 Each symbol has an associated left and right position. *)
12
13%pos int
14
15%left SUB PLUS
16%left TIMES DIV
17%right CARAT
18
19%term ID of string | NUM of int | PLUS | TIMES | PRINT |
20 SEMI | EOF | CARAT | DIV | SUB
21%nonterm EXP of int | START of int option
22
23%name Calc
24
25%subst PRINT for ID
26%prefer PLUS TIMES DIV SUB
27%keyword PRINT SEMI
28
29%noshift EOF
30%value ID ("bogus")
31%verbose
32%%
33
34(* the parser returns the value associated with the expression *)
35
36 START : PRINT EXP (print (Int.toString EXP);
37 print "\n";
38 SOME EXP)
39 | EXP (SOME EXP)
40 | (NONE)
41 EXP : NUM (NUM)
42 | ID (lookup ID)
43 | EXP PLUS EXP (EXP1+EXP2)
44 | EXP TIMES EXP (EXP1*EXP2)
45 | EXP DIV EXP (EXP1 div EXP2)
46 | EXP SUB EXP (EXP1-EXP2)
47 | EXP CARAT EXP (let fun e (m,0) = 1
48 | e (m,l) = m*e(m,l-1)
49 in e (EXP1,EXP2)
50 end)