phorward / unicc

LALR parser generator targetting C, C++, Python, JavaScript, JSON and XML
MIT License
58 stars 9 forks source link

More details on precedence to resolve parser conflicts #25

Closed mingodad closed 2 years ago

mingodad commented 2 years ago

Comparing yacc/bison and lemon on how they use rule precedence to resolve grammar conflicts they differ on how to do it as show bellow:

Can this have a bit of explanation ?

mingodad commented 2 years ago

Also lemon uses rule precedence to resolve reduce/reduce conflicts.

phorward commented 2 years ago

Hello @mingodad, thank you for asking.

In unicc, the precedence of a production is resolved similarly to yacc: The precedence of the rightmost symbol with a precedence setting will be used. Instead of only terminal symbols, unicc also uses the precedence settings of nonterminal symbols.

The corresponding position in the code is here. unicc2 has the corresponding algorithm here.

mingodad commented 2 years ago

Thank you for reply ! Looking through the code you've pointed out it seems that this comment is misleading https://github.com/phorward/unicc/blob/85ec57b3edeb6fe79cf0dbe7fc93def3928413c5/src/rewrite.c#L425 because it's using the same procedure as the next loop to look for the rightmost .

phorward commented 2 years ago

You're right, the comment is wrong here.