8
votes

I'm playing around with parsec and realized that I had an ambiguous grammar. Obviously that's an error on my part, but I'm sort of used to yacc-style parser generators letting me know I'm being dumb. Parsec just eats characters in the order you give it parsers (yeah, I know about try).

Is there any way to make parsec tell me when my grammar isn't left-factored? Programs that do work for me are great.

Thanks!

(I know that shift-reduce has to do with a different kind of parser technology. I simply mean to describe ambiguous grammars.)

1

1 Answers

8
votes

I am not a Parsec expert, so I'm likely to be corrected, but I don't think this is possible, for the simple reason that Parsec knows nothing about your grammar.

Or put another way, while your grammar may be ambiguous, your Parsec parser is not, and a program has no way of determining that some other arrangement of parsec combinators, which produces a different output for equivalent input, is also a valid representation of an unspecified grammar.

Since you do have a grammar, you might prefer to use happy and alex, which will give you a much more lexx/yacc-like experience.

An interesting project might be to adapt the BNFC to produce an AST of parsec combinators to represent a grammar, but I suspect this would be a non-trivial task.