Debian Codegraph @

lib/mllpt-lib/ml-lpt/ml-antlr/ll1/compute-nullable.sml

raw
1 (* compute-nullable.sml
2 *
3 * COPYRIGHT (c) 2006
4 * John Reppy (http://www.cs.uchicago.edu/~jhr)
5 * Aaron Turon (http://www.cs.uchicago.edu/~adrassi)
6 * All rights reserved.
7 *
8 * Computation of nullable test on nonterminals.
9 *)
10
11 structure ComputeNullable : sig
12
13 datatype result = Result of {
14 nullable : LLKSpec.nonterm -> bool,
15 nullableItem : LLKSpec.item -> bool,
16 nullableItems : LLKSpec.item list -> bool
17 }
18
19 val compute : LLKSpec.prod list -> result
20
21 end = struct
22
23 datatype result = Result of {
24 nullable : LLKSpec.nonterm -> bool,
25 nullableItem : LLKSpec.item -> bool,
26 nullableItems : LLKSpec.item list -> bool
27 }
28
29 fun nullableItem ns item = (case item
30 of (LLKSpec.TOK _) => false
31 | (LLKSpec.NONTERM nt) => Nonterm.Set.member(ns, nt)
32 | (LLKSpec.CLOS _) => true
33 | (LLKSpec.POSCLOS nt) => Nonterm.Set.member(ns, nt)
34 | (LLKSpec.OPT _) => true
35 (* end case *))
36
37 fun nullableItems ns = List.all (nullableItem ns)
38
39 fun compute (rules : LLKSpec.prod list) = let
40 fun doRule (LLKSpec.PROD{lhs, rhs, ...}, ns) =
41 if nullableItems ns rhs
42 then Nonterm.Set.add(ns, lhs)
43 else ns
44 fun loopToFixedPt ns = let
45 val ns' = List.foldl doRule ns rules
46 in
47 if Nonterm.Set.numItems ns' > Nonterm.Set.numItems ns
48 then loopToFixedPt ns'
49 else ns
50 end
51 (* compute the set of nullable nonterminals *)
52 val ns = loopToFixedPt Nonterm.Set.empty
53 in Result {
54 nullable = fn nt => Nonterm.Set.member(ns, nt),
55 nullableItem = nullableItem ns,
56 nullableItems = nullableItems ns
57 }
58 end
59
60 end