
Am Donnerstag, 26. Juni 2008 12:40 schrieb Eric:
Hi all,
I'm using the Parsec library to parse the following grammar
expr = atom+ atom = integer | var | (expr)
The input to the parser is a list of (Token, SourcePos). I have the following code for atom:
atom = try variable <|> try integerr <|> do{sat(== Reserved "("); (e,pos) <- expression; sat(==Reserved ")"); return (e,pos)}
sat p = do (t,pos) <- item if p t then return(t,pos) else pzero
When I run the code on the input "(_ineg_ 0)" the parser fails, but removing
sat(==Reserved ")")
and it succeeds. Can any one see why?
E.
I can't be absolutely sure without seeing your code for item and expression, possibly also variable and integerr, but I'm rather sure it's a case of a missing 'try'. I'd expect 'expression' using something like many1 atom , then when parsing the tokens of "(_ineg_ 0)", which, ignoring the SourcePos are [Reserved "(", Varid "_ineg_", INum 0, Reserved ")"], as it should be, variable and integerr fail, so the third branch is entered, sat (== Reserved "(") succeeds, the Varid and INum are parsed and finally only [Reserved ")"] is left over, on which atom is tried again. variable and integerr again fail gracefully, so sat (== Reserved "(") is tried on it, which fails, but unfortunately not gracefully, because item returns (Consumed _) and thus the overall failure consumes. Because of that, many1 atom fails and the overall parse fails. Can't be exactly that, though because then removing sat (== Reserved ")") shouldn't help. Anyway, I need at least the failure message, better the complete code, to diagnose. Cheers, Daniel