How to avoid conflicts in grammar?

Parsers: What are good ways to avoid conflicts in grammar?

  • I have a grammar file — https://github.com/itrelease/fubar-script/blob/jsast/src/grammar.js but I get conflicts — https://gist.github.com/2597616 and I don't really know how to solve this. If someone could explain me it would be helpful.

  • Answer:

    These conflicts imply that your grammar is ambiguous. That is, for some strings the grammar can be parsed in multiple ways by the parser because at some point more than one rule is applicable during the derivation of the parse tree. This is obviously disastrous because it makes your compiler's behavior dependent on the parser itself, so the parser raises an error instead. A good way to design a grammar is to incrementally add features and resolve any conflicts that turn up. In your case, all the conflicts appear between the FormalParameterList and PrimaryExpressionNoBrace non-terminals whenever there is a THIS or IDENTIFIER token in the string. You should probably be able to resolve this by creating a new non-terminal that parses the conflicting cases and using it in both FormalParameterList and PrimaryExpressionNoBrace.

Shrey Banga at Quora Visit the source

Was this solution helpful to you?

Other answers

Avoiding ambiguity is not always possible and never easy, but luckily there are ambiguity detection methods and tools around. Have a look at my (ex-)colleague’s publication list, his main focus of research was disambiguation of grammars: http://researchr.org/profile/basbasten/publications

Vadim Zaytsev

Related Q & A:

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.