-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparse.ml
More file actions
66 lines (44 loc) · 1.43 KB
/
parse.ml
File metadata and controls
66 lines (44 loc) · 1.43 KB
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
datatype 'a Maybe = OK of 'a | NotSo;
datatype Token = N of string | I of int
| R of real | S of string | V of string
| Begin | End | OBR |CBR |SC | Com ;
(* This is a Scheme-style definition of term *)
datatype Term = B of Token | Ap of Term * Term list;
(*
datatype Expr = B of Token | Ap of Token * Term list | Sum of Term * Term
| Product of Term * Term;
datatype Statement = Assignment of Token * Expr | If of Expr * Expr * Expr;
datatype 'a Parse = P of 'a * Token list;
*)
datatype Parse = P of Term * Token list;
fun member x [] = false
| member x (y::l) = if x=y then true else member x l ;
fun mk_parser_singleton l [] = NotSo
| mk_parser_singleton l (tok::toks) =
if member tok l then OK(P(B(tok),toks))
else NotSo;
val parse_x = mk_parser_singleton [V("x")];
parse_x [V("x"), I(2)];
datatype Parse = P of Term * Token list
fun number [] = NotSo
| number (I(i)::toks) = OK(P(I(i), toks))
| number (tok :: toks) = NotSo;
fun mk_parse_seq p1 p2 bld [] = Fail
| mk_parse_seq p1 p2 bld toks =
let val r1 = p1 toks
in
case r1 of
Fail => fail
| OK(term1,toks1) =>
let val r2 = p2 toks1
in
case r2 of
Fail => Fail
OK(term2,toks2) =>
;
val a = ref(23);
int * a = malloc(sizeof int);
* a = 23;
a := 3;
!a;
case [] of [] => 34 | x::y => x;