Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any way to make parsec report "shift-reduce" conflicts?

Tags:

haskell

parsec

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.)

like image 700
Hans Avatar asked Aug 29 '12 04:08

Hans


People also ask

How do you handle shift and reduce conflict?

Rule 1. If there is a shift-reduce conflict in situations where no precedence rules have been created to resolve the conflict, the default action is to shift. The conflict is also reported in the yacc output so you can check that shifting is actually what you want.

How do you identify a shift-reduce conflict?

In a shift-reduce conflict, the default is to shift. In a reduce-reduce conflict, the default is to reduce by the earlier grammar rule (in the yacc specification). Rule 1 implies that reductions are deferred in favor of shifts when there is a choice.


1 Answers

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.

like image 66
John L Avatar answered Oct 07 '22 01:10

John L