
On Thu, Apr 10, 2008 at 2:05 AM, Jackm139
I'm trying to write a function to recognize a context free grammar, but I keep getting pattern match failure errors. This is what I have:
data Grammar c = Brule c c c | Rule c c
gez = [(Brule 'S' 'p' 'D'),(Brule 'D' 't' 'E'),(Rule 'E' 'j')]
recog :: String -> String -> [Grammar Char] -> Bool recog a b list = case list of [Brule x y z] -> if a == [x] then recog [z] b list else recog a b list [Rule x y] -> True
I am stumped as to what this function is trying to do. But your pattern matches are not total; in particular you only handle lists of a single element. I suggest either: recog a b list = case list of (Brule x y z : rules) -> ... (involving 'rules') (Rule x y : rules) -> ... (involving 'rules') [] -> ... (case for the empty list) Or: recog a b (rule:rules) = case rule of Brule x y z -> ... Rule x y -> ... recog a b [] = ... But the point is that you have to say how to handle lists of more thn one element. Luke