1 (* Sample interactive calculator for ML-Yacc *)
3 fun lookup "bogus" = 10000
10 (* %pos declares the type of positions for terminals.
11 Each symbol has an associated left and right position. *)
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
26 %prefer PLUS TIMES DIV SUB
34 (* the parser returns the value associated with the expression *)
36 START : PRINT EXP (print (Int.toString EXP);
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)