zaach / jison

Bison in JavaScript.
http://jison.org
4.36k stars 450 forks source link

Please help to resolve IF THEN ELSE issue in AlaSQL parser #279

Open agershun opened 9 years ago

agershun commented 9 years ago

Thank you again for the great software! May I ask you to help to resolve one issue in the grammar file, which we can not solve.

This is a grammar file https://github.com/agershun/alasql/blob/master/src/alasqlparser.jison

When I parse my file, I receive the following warnings: Even with this warning all tests passed ok. How to modify grammar file to remove this warning?

    Conflict in grammar: multiple actions possible when lookahead token is ELSE in state 231
    - reduce by rule: If -> IF Expression AStatement
    - shift token (then go to state 381)

    States with conflicts:
    State 231
      If -> IF Expression AStatement .ElseStatement #lookaheads= GO SEMICOLON EOF END ELSE
      If -> IF Expression AStatement . #lookaheads= GO SEMICOLON EOF END ELSE
      ElseStatement -> .ELSE AStatement #lookaheads= EOF SEMICOLON GO END ELSE

These messages related to lines 1986-2003, which are:

        IF Expression AStatement ElseStatement 
    | IF Expression AStatement
    ;

ElseStatement
    : ELSE AStatement 
    ;

Could you help me with this problem?

nolanlawson commented 9 years ago

The Bison docs have a section on fixing conflicts. In general, though, I believe the standard advice is, "It's really hard to write unambiguous grammars. If you can, great. If you can't, then it's usually not a problem." I am a newb to Jison, though, so take that with a grain of salt! :)

Also just a small piece of advice on simplifying your grammar: if you use the ebnf extended syntax, you can do stuff like:

IF Expression AStatement ElseStatement?

There's an example of this in my Jison Debugger; check the "musical chords" sample in the dropdown list.

agershun commented 9 years ago

@nolanlawson - Thank you, great and useful tool! Is there way to to turn on ebnf in Jison?

I also tryed to use this Bison documentation parts, and tried to play with %left, %right and other flags - but everything had no desired effect.

nolanlawson commented 9 years ago

@agershun Yup, check out the Jison Debugger's "musical chords" grammar, where I enable %ebnf. This feature is described in https://gist.github.com/zaach/1659274

agershun commented 9 years ago

Thank you, Nolan! I will create the special issue to rewrite parser with this ebnf notation. May be I can solve this 'IF_THEN-ELSE' occasionally as well :)

agershun commented 9 years ago

@nolanlawson I love this ebnf!!! It seems, that it does not reduce the size of the output file, but the source file is much much smaller and cleaner :+1:

nolanlawson commented 9 years ago

Yeah ebnf is awesome. :)

agershun commented 9 years ago

Unfortunately, %ebnf has a serious bug. See https://github.com/zaach/jison/issues/254