-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharithexp.ml
More file actions
29 lines (22 loc) · 827 Bytes
/
Copy patharithexp.ml
File metadata and controls
29 lines (22 loc) · 827 Bytes
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
open Genlex
let lexer = make_lexer [ "(";")";"+";"-";"*" ]
let rec parse_exp env = parser
| [< e1 = parse_atom env; e2 = parse_rem1 env e1 >] -> e2
and parse_atom env = parser
| [< 'Ident x >] -> env x
| [< 'Int n >] -> n
| [< 'Kwd "("; e = parse_exp env; 'Kwd ")" >] -> e
| [< >] -> invalid_arg "Parsing failure (atom)"
and parse_rem1 env acc = parser
| [< 'Kwd "*"; e1 = parse_atom env;
e2 = parse_rem1 env (acc*e1) >] -> e2
| [< 'Kwd "+"; e1 = parse_atom env;
e2 = parse_rem2 env acc e1 >] -> e2
| [< >] -> acc
and parse_rem2 env acc1 acc2 = parser
| [< 'Kwd "*"; e1 = parse_atom env;
e2 = parse_rem1 env (acc2*e1) >] -> acc1 + e2
| [< 'Kwd "+"; e1 = parse_atom env;
e2 = parse_rem2 env (acc1+acc2) e1 >] -> e2
| [< >] -> acc1+acc2
let parse env s = parse_exp env (lexer (Stream.of_string s))