module CF where import Util import Control.Monad -- the type of context-free grammars is parametrized -- over the type categories and the type of tokens data CF a b = CF { start :: a, nrules :: Table a [NRule a], trules :: Table b [TRule a b] } deriving (Eq,Ord,Show) type NRule a = (a,[a]) type TRule a b = (b,a) (|>) :: a -> [a] -> NRule a (|>) = (,) (|:) :: b -> a -> TRule a b (|:) = (,) mkCF :: (Ord a, Ord b) => a -> [NRule a] -> [TRule a b] -> CF a b mkCF s nrs trs = CF { start = s, nrules = mkTableBy fst nrs, trules = mkTableBy fst trs } rhss :: Eq a => CF a b -> a -> [[a]] rhss cf c = map snd $ lookupList c (nrules cf) tokenCats :: Eq b => CF a b -> b -> [a] tokenCats cf x = map snd $ lookupList x (trules cf) allNRules :: CF a b -> [NRule a] allNRules = concatMap snd . nrules