help-bison
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

how to resolve if/elseif/else ambiguity?


From: SirZooro
Subject: how to resolve if/elseif/else ambiguity?
Date: Tue, 12 Jun 2007 13:12:02 -0700 (PDT)

I am trying to create grammar which has if/elseif/else instruction. However I
am unable to get rid of shift/reduce conflicts. I found
http://www.parsifalsoft.com/ifelse.html page which helped me a bit, but
still I have problems. My grammar is as follow:
[code]
%token IF ELSE ELSEIF FOR NUMBER STMT '(' ')';
%%
prog : stmt;

expr: NUMBER;

stmt: open_stmt
        | closed_stmt;

open_stmt: IF '(' expr ')' stmt
        | IF '(' expr ')' closed_stmt open_else
        | FOR '(' expr ')' open_stmt;

closed_stmt: STMT
        | IF '(' expr ')' closed_stmt closed_else
        | FOR '(' expr ')' closed_stmt;

open_else: ELSE open_stmt
        | ELSEIF '(' expr ')' open_stmt
        | ELSEIF '(' expr ')' closed_stmt open_else;

closed_else: ELSE closed_stmt
        | ELSEIF '(' expr ')' closed_stmt
        | ELSEIF '(' expr ')' closed_stmt closed_else;
[/code]

Bison reports following problem:
[code]
state 30

   11 open_else: . ELSE open_stmt
   12          | . ELSEIF '(' expr ')' open_stmt
   13          | . ELSEIF '(' expr ')' closed_stmt open_else
   13          | ELSEIF '(' expr ')' closed_stmt . open_else
   14 closed_else: . ELSE closed_stmt
   15            | . ELSEIF '(' expr ')' closed_stmt  [$end, ELSE, ELSEIF]
   15            | ELSEIF '(' expr ')' closed_stmt .  [$end, ELSE, ELSEIF]
   16            | . ELSEIF '(' expr ')' closed_stmt closed_else
   16            | ELSEIF '(' expr ')' closed_stmt . closed_else

    ELSE    shift, and go to state 20
    ELSEIF  shift, and go to state 21

    ELSE      [reduce using rule 15 (closed_else)]
    ELSEIF    [reduce using rule 15 (closed_else)]
    $default  reduce using rule 15 (closed_else)

    open_else    go to state 31
    closed_else  go to state 32
[/code]

'Rule 15' is the following rule: 
closed_else: ELSEIF '(' expr ')' closed_stmt

All suggestions are welcome.

Daniel
-- 
View this message in context: 
http://www.nabble.com/how-to-resolve-if-elseif-else-ambiguity--tf3909599.html#a11085363
Sent from the Gnu - Bison - Help mailing list archive at Nabble.com.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]