|
6 | 6 | > select_first_reduction
|
7 | 7 | > ) where
|
8 | 8 |
|
9 |
| -> import Happy.Grammar |
| 9 | +> import Happy.Tabular.NameSet ( NameSet ) |
10 | 10 | > import Happy.Tabular.First
|
| 11 | +> import qualified Happy.Tabular.LALR as LALR |
11 | 12 | > import Happy.Tabular.LALR
|
12 |
| -> import Happy.Tabular.NameSet (NameSet) |
| 13 | +> ( Lr0Item, Lr1Item, ItemSetWithGotos |
| 14 | +> , precalcClosure0, propLookaheads, calcLookaheads, mergeLookaheadInfo |
| 15 | +> ) |
| 16 | +> import Happy.Tabular.Tables |
| 17 | +> import Happy.Grammar |
| 18 | +> import Happy.Tabular.FindRedundancies |
13 | 19 |
|
14 |
| -> import Data.Array( Array, assocs, elems, (!) ) |
15 |
| -> import Data.List ( nub ) |
| 20 | +> import Data.Array ( Array ) |
16 | 21 |
|
17 | 22 | > data Tables =
|
18 | 23 | > Tables {
|
|
34 | 39 | > genTables select_reductions g =
|
35 | 40 | > let first = {-# SCC "First" #-} (mkFirst g)
|
36 | 41 | > closures = {-# SCC "Closures" #-} (precalcClosure0 g)
|
37 |
| -> lr0items = {-# SCC "LR0_Sets" #-} (genLR0items g closures) |
| 42 | +> lr0items = {-# SCC "LR0_Sets" #-} (LALR.genLR0items g closures) |
38 | 43 | > (la_spont, la_prop)
|
39 | 44 | > = {-# SCC "Prop" #-} (propLookaheads g lr0items first)
|
40 | 45 | > lookaheads = {-# SCC "Calc" #-} (calcLookaheads (length lr0items) la_spont la_prop)
|
41 | 46 | > lr1items = {-# SCC "Merge" #-} (mergeLookaheadInfo lookaheads lr0items)
|
42 |
| -> gotoTable = {-# SCC "Goto" #-} (genGotoTable g lr0items) |
43 |
| -> actionTable = {-# SCC "Action" #-} (genActionTable g first lr1items) |
44 |
| -> conflicts = {-# SCC "Conflict" #-} (countConflicts actionTable) |
| 47 | +> gotoTable = {-# SCC "Goto" #-} (LALR.genGotoTable g lr0items) |
| 48 | +> actionTable = {-# SCC "Action" #-} (LALR.genActionTable g first lr1items) |
| 49 | +> conflicts = {-# SCC "Conflict" #-} (LALR.countConflicts actionTable) |
45 | 50 | > redundancies = find_redundancies select_reductions g actionTable
|
46 | 51 | > in Tables { lr0items, la_spont, la_prop, lookaheads, lr1items,
|
47 | 52 | > gotoTable, actionTable, conflicts, redundancies }
|
48 |
| - |
49 |
| ------------------------------------------------------------------------------ |
50 |
| -Find unused rules and tokens |
51 |
| - |
52 |
| -> find_redundancies |
53 |
| -> :: SelectReductions -> Grammar -> ActionTable -> ([Int], [String]) |
54 |
| -> find_redundancies extract_reductions g action_table = |
55 |
| -> (unused_rules, map (env !) unused_terminals) |
56 |
| -> where |
57 |
| -> Grammar { terminals = terms, |
58 |
| -> token_names = env, |
59 |
| -> eof_term = eof, |
60 |
| -> starts = starts', |
61 |
| -> productions = productions' |
62 |
| -> } = g |
63 |
| -> actions = concat (map assocs (elems action_table)) |
64 |
| -> start_rules = [ 0 .. (length starts' - 1) ] |
65 |
| -> used_rules = start_rules ++ |
66 |
| -> nub [ r | (_,a) <- actions, r <- extract_reductions a ] |
67 |
| -> used_tokens = errorTok : eof : |
68 |
| -> nub [ t | (t,a) <- actions, is_shift a ] |
69 |
| -> n_prods = length productions' |
70 |
| -> unused_terminals = filter (`notElem` used_tokens) terms |
71 |
| -> unused_rules = filter (`notElem` used_rules ) [0..n_prods-1] |
72 |
| - |
73 |
| -> is_shift :: LRAction -> Bool |
74 |
| -> is_shift (LR'Shift _ _) = True |
75 |
| -> is_shift (LR'Multiple _ LR'Shift{}) = True |
76 |
| -> is_shift _ = False |
77 |
| - |
78 |
| ---- |
79 |
| -selects what counts as a reduction when calculating used/unused |
80 |
| - |
81 |
| -> type SelectReductions = LRAction -> [Int] |
82 |
| - |
83 |
| -> select_all_reductions :: SelectReductions |
84 |
| -> select_all_reductions = go |
85 |
| -> where go (LR'Reduce r _) = [r] |
86 |
| -> go (LR'Multiple as a) = concatMap go (a : as) |
87 |
| -> go _ = [] |
88 |
| - |
89 |
| -> select_first_reduction :: SelectReductions |
90 |
| -> select_first_reduction = go |
91 |
| -> where go (LR'Reduce r _) = [r] |
92 |
| -> go (LR'Multiple _ a) = go a -- eg R/R conflict |
93 |
| -> go _ = [] |
0 commit comments