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 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289
|
module Lookahead ( ActionTable, groupActions, current, ppActionTable
, klookahead )
where
import Grammar
import LR0
import qualified OrdUniqListSet as Set
import qualified SearchTree as FM
import SearchTree ( FM )
import MergeSort
import Prettier hiding ( concat, empty, group )
import qualified Prettier as PP
import Base
import Options
import IO
import Maybe
import Monad ( when )
type ActionTable = FM State [Action]
groupActions :: Table -> ActionTable
groupActions table = FM.fromList_C (++) [(current a, [a]) | a <- table ]
current :: Action -> State
current (Shift (s, _, _)) = s
current (Reduce Nil (s, _, _) _ _ _)
= s
current (Reduce (_ :> (_, _, s)) _ _ _ _)
= s
ppActionTable :: ActionTable -> Doc
ppActionTable table = PP.concat [ header ("State " ++ show (snumber s))
<> pretty acts <> nl <> nl
| (s, acts) <- FM.toList table ]
fixedpoint :: (Ord a, Show a, Eq v) => [a] -> ((a -> v) -> (a -> v)) -> ((a -> v) -> (a -> v))
fixedpoint dom step start = FM.unsafeLookup (lfp step' start')
where start' = FM.fromOrdList [ (a, start a) | a <- dom ]
step' fm = FM.fromOrdList [ (a, step (FM.unsafeLookup fm) a) | a <- dom ]
lfp :: (Eq a) => (a -> a) -> a -> a
lfp f a
| a == a' = a
| otherwise = lfp f a'
where a' = f a
nullableOf g = nullable (fixedpoint (nonterminals g) step start)
where start n = False
step f n = or [ nullable f (rrhs r) | r <- productionsOf g n ]
nullable f [] = True
nullable f (v : vs)
| terminal v = False
| otherwise = f v && nullable f vs
klookahead :: [Flag] -> Grammar -> GotoTable -> Table -> IO ActionTable
klookahead opts g gotoTable table'
= do verb "* Computing k-lookahead information ..."
verb (" " ++ show (sum [ 1 | n <- nonterminals g, nullable [n] ]) ++ " nullable nonterminals")
debug "e-reachable" (pretty [ (e, ereachable e) | e <- gotoTable ])
debug "reachable" (pretty [ (e, reachable e) | e <- gotoTable ])
return (fmap (map add) table)
where
pageWidth = head ([ w | Pagewidth w <- opts ] ++ [80]) `max` 40
debug s a = when (Debug `elem` opts) (hPutStr stderr t) >> return t
where t = cjustifyWith '*' pageWidth (" " ++ s++ " ") ++ "\n\n"
++ render (Page pageWidth) a ++ "\n\n"
verb = verbose opts
k = lookahead opts
table = groupActions table'
lookup s = applyWithDefault (FM.lookup table) [] s
add a@(Shift _) = a
add (Reduce st e _ p i) = Reduce st e (prune k (lainfo e)) p i
lainfo :: Edge -> Future
lainfo e@(_, _, s) = fromList [ (t, lainfo e')
| s /= errorState
, (_, _, s') <- Set.toList (reachable e)
, Shift e'@(_, t, _) <- lookup s' ]
nullable = nullableOf g
ereachable = fixedpoint gotoTable step start
where
start e = Set.empty
step f e@(_, _, s) = Set.singleton e `Set.union`
Set.unionMany [ f (s, v, goto s v)
| Item _ _ _ (v : _) _ <- toList (items s)
, nullable [v] ]
reachable = fixedpoint gotoTable step start
where
start e = Set.empty
step f e@(s0, _, s) = ereachable e `Set.union`
Set.unionMany [ f (s', v, s'')
| Item _ v (l :> _) r _ <- toList (items s)
, nullable r
, s' <- backtrack l s0
, let s'' = goto s' v
, s'' /= errorState ]
reachableWrong = fixedpoint gotoTable step start --- this is an oversimplification
where
start e = Set.empty
step f e@(s0, _, s) = Set.singleton e `Set.union`
Set.unionMany [ f (s, v, goto s v)
| Item _ _ (_ :> _) (v : _) _ <- toList (items s)
, nullable [v] ] `Set.union`
Set.unionMany [ f (s', v, s'')
| Item _ v (l :> _) [] _ <- toList (items s)
, s' <- backtrack l s0
, let s'' = goto s' v
, s'' /= errorState ]
backtrack Nil s = [ s ]
backtrack (vs :> v) s = [ x
| s' <- Set.list (invGoto v s)
, x <- backtrack vs s' ]
goto s v = applyWithDefault (FM.lookup fm) errorState (s, v)
where fm = FM.fromList [ ((si, vi), si') | (si, vi, si') <- gotoTable ]
invGoto v s' = applyWithDefault (FM.lookup fm) Set.empty (v, s')
where fm = FM.fromList_C Set.union
[ ((vi, si'), Set.singleton si) | (si, vi, si') <- gotoTable ]
errorState :: State
errorState = State 0 (Set.empty :\/ Set.empty)
oldklookahead :: [Flag] -> Int -> Table -> IO ActionTable
oldklookahead opts maxRHS table'
= do verb "* Computing k-lookahead information ..."
return (fmap (map add) table)
where
verb = verbose opts
k = lookahead opts
table = groupActions table'
lookup s = applyWithDefault (FM.lookup table) [] s
add a@(Shift _) = a
add (Reduce st e _ p i) = Reduce st e (prune k (lainfo 1 (Nil :> e))) p i
lainfo :: Int -> RevList Edge -> Future
lainfo j st0 = --if reachable /= reachable'
--then error ("LOOKAHEAD ("++ show st0 ++ "):" ++ show reachable ++ "\n" ++ show reachable')
--else
fromList [ (t, lainfo (j + 1) (st' :> e))
| st' <- Set.toList reachable
, Shift e@(_, t, _) <- lookup (cur st') ]
-- , modifier t == Copy ]
where
reachable = Set.fixedpoint forward (Set.singleton st0)
forward sts = Set.fromList
[ revTake (10*maxRHS) (nst :> e) -- prune the stack
| st <- Set.toList sts
, Reduce st' e _ _ _ <- lookup (cur st)
, nst <- matches st st' ]
{-
`Set.union` Set.fromList
[ st :> e | st <- Set.toList sts
, Shift e@(_, t, _) <- lookup (cur st)
, modifier t == Insert ]
-}
reachable' = fmap unConf $ Set.fixedpoint forward' (Set.singleton (Conf st0))
forward' sts = Set.fromList
[ Conf (nst :> e)
| Conf st <- Set.toList sts
, Reduce st' e _ _ _ <- lookup (cur st)
, nst <- matches st st' ]
cur :: RevList Edge -> State
cur Nil = impossible "current"
cur (_ :> (_, _, s)) = s
--matches st Nil e = [revTake 2 (st :> e)] -- | current st == s ]
--matches st st' e = matches' st st' e
matches :: RevList Edge -> RevList Edge -> [RevList Edge]
matches Nil _st' = [Nil]
matches st Nil = [st]
matches (st :> t) (st' :> t') = [ nst | t == t', nst <- matches st st' ]
-- | t == t' = matches st st'
-- | otherwise = []
newtype Conf = Conf { unConf :: RevList Edge }
instance Eq Conf where
Conf s1 == Conf s2 = cur s1 == cur s2
instance Ord Conf where
compare (Conf s1) (Conf s2)
= compare (cur s1) (cur s2)
|