0

I am trying to build a if()else if() else if() else example.Is there any way to fix this without %nonassoc and specify the terminal symbol? As you see at code i have some flex and bison.At flex there is not any keyword elseif but only else and if.

Flex

keyword_if [Ii][Ff]   
keyword_else [Ee][Ll][Ss][ee] 
keyword_start [Ss][Tt][Aa][Rr][Tt]
keyword_then [Tt][Hh][Ee][Nn]

{keyword_if} return key_if;
{keyword_else} return key_else;
{keyword_start} return key_start;
{keyword_then} return key_then;
Bison

%token key_if
%token key_else
%token key_start
%token key_then

%start:program;


program:start commands ;

commands:command
        |commands command;

command:ifcommand
        |whilecommand
        |forcommand;


ifcommand:key_if exp key_then commands checkelse;

checkelse:/*empty*/
|key_else commands;

That works for the simple version that there are nested if inside else and not like this

if a == 10 then

       /* some commands */


   else if a == 20  then

          /*some commands*/

   else if a == 30 then

       /*some commands*/

   else
       /*some commands*/
rici
  • 234,347
  • 28
  • 237
  • 341
G-V
  • 98
  • 1
  • 12
  • 1
    http://en.m.wikipedia.org/wiki/Dangling_else – avim May 20 '15 at 06:00
  • Perhaps answered in [how to resolve a shift reduce conflict](http://stackoverflow.com/questions/12065125/how-to-resolve-a-shift-reduce-conflict-forcing-a-shift-or-a-reduce). – Brian Tompsett - 汤莱恩 May 20 '15 at 09:02
  • Possible duplicate of [Reforming the grammar to remove shift reduce conflict in if-then-else](http://stackoverflow.com/questions/12731922/reforming-the-grammar-to-remove-shift-reduce-conflict-in-if-then-else) – Brian Tompsett - 汤莱恩 May 22 '16 at 10:11

0 Answers0