1
-----------------------------------------------------------------------------
6
(c) 1993-2001 Andy Gill, Simon Marlow
7
-----------------------------------------------------------------------------
9
> module Info (genInfoFile) where
11
> import Version ( version )
12
> import LALR ( Lr0Item )
13
> import GenUtils ( str, interleave, interleave', ljustify )
20
Produce a file of parser information, useful for debugging the parser.
28
> -> Array Int (Int,Int)
30
> -> [Int] -- unused rules
31
> -> [String] -- unused terminals
35
> (Grammar { productions = prods
36
> , lookupProdNo = lookupProd
37
> , lookupProdsOfName = lookupProdNos
38
> , non_terminals = nonterms
42
> , first_term = fst_term
44
> action goto tokens conflictArray filename unused_rules unused_terminals
57
> = banner ("Info file generated by Happy Version " ++
58
> version ++ " from " ++ filename)
62
> . foldr (.) id (map showConflictsState (assocs conflictArray))
65
> showConflictsState (state, (0,0)) = id
66
> showConflictsState (state, (sr,rr))
70
> . interleave' " and " (
72
> then [ shows sr . str " shift/reduce conflicts" ]
75
> then [ shows rr . str " reduce/reduce conflicts" ]
80
> (case unused_rules of
82
> _ -> interleave "\n" (
83
> map (\r -> str "rule "
88
> . (case unused_terminals of
90
> _ -> interleave "\n" (
91
> map (\t -> str "terminal "
99
> . interleave "\n" (zipWith showProduction prods [ 0 :: Int .. ])
102
> showProduction (nt, toks, sem, prec) i
107
> . interleave " " (map showName toks))
108
> . str " (" . shows i . str ")"
112
> . interleave "\n" (zipWith showState
113
> (map setToList items) [ 0 :: Int .. ])
116
> = str "State ". shows n
118
> . interleave "\n" (map showItem [ (r,d) | (r,d) <- state, d /= 0 ])
120
> . foldr (.) id (map showAction (assocs (action ! n)))
122
> . foldr (.) id (map showGoto (assocs (goto ! n)))
124
> showItem (rule,dot)
129
> . interleave " " (map showName beforeDot)
131
> . interleave " " (map showName afterDot))
132
> . str " (rule " . shows rule . str ")"
134
> (nt, toks, sem, prec) = lookupProd rule
135
> (beforeDot, afterDot) = splitAt dot toks
137
> showAction (t, LR'Fail)
139
> showAction (t, act)
145
> showAction' LR'MustFail
147
> showAction' (LR'Shift n _)
148
> = str "shift, and enter state "
150
> showAction' LR'Accept
152
> showAction' (LR'Reduce n _)
153
> = str "reduce using rule "
155
> showAction' (LR'Multiple as a)
159
> (map (\a -> str "\t\t\t(" . showAction' a . str ")")
160
> (nub (filter (/= a) as)))
162
> showGoto (nt, NoGoto)
164
> showGoto (nt, Goto n)
167
> . str "goto state "
172
> = banner "Terminals"
173
> . interleave "\n" (map showTerminal tokens)
179
> . str "{ " . str s . str " }"
182
> = banner "Non-terminals"
183
> . interleave "\n" (map showNonTerminal nonterms)
189
> . (if (length nt_rules == 1)
191
> else str " rules ")
192
> . foldr1 (\a b -> a . str ", " . b) nt_rules
193
> where nt_rules = map shows (lookupProdNos nt)
196
> = banner "Grammar Totals"
197
> . str "Number of rules: " . shows (length prods)
198
> . str "\nNumber of terminals: " . shows (length tokens)
199
> . str "\nNumber of non-terminals: " . shows (length nonterms)
200
> . str "\nNumber of states: " . shows (length items)
204
> showName = str . nameOf
205
> showJName j = str . ljustify j . nameOf
207
> ljuststr n s = str (ljustify n (s ""))
210
> = str "-----------------------------------------------------------------------------\n"
212
> . str "\n-----------------------------------------------------------------------------\n"