-
Notifications
You must be signed in to change notification settings - Fork 0
/
test1.grammar
34 lines (31 loc) · 884 Bytes
/
test1.grammar
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
# Unambiguous LR grammar for simple calculator.
# F# first test
valuetype int
nonterminals E T F
#nonterminal E int
#nonterminal T int
#nonterminal F int
terminals + *
# these will be used later...
# The first two args are same as for rust, third is string representing
# type of token returned by lexer, last arg is a function of type
# string -> value to be applied to the string text of the parsed token
#valueterminal realnum ~ float ~ Float ~ float
valueterminal number ~ int ~ Num ~ (fun x->int x)
lexterminal LPAREN (
lexterminal RPAREN )
topsym E
E --> E:e + T:t { e + t }
E --> T:t { t }
#T --> T:t * F:f { t*f }
T ==> T:t * F:f {
let tf = t*f
if f<>0 && ((tf%f)+tf/f<>t) then
let (ln,cl) = parser.position(1)
printfn "Warning: arithmetic overflow line %d, column %d" ln cl
t*f
} <==
T --> F:f { f }
F --> LPAREN E:e RPAREN { e }
F --> number:n { n }
EOF