help-bison
[Top][All Lists]
Advanced

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

Re: Conditional execution rules


From: Axel Kittenberger
Subject: Re: Conditional execution rules
Date: Sun, 5 Aug 2001 19:09:36 +0200

Well I can try in example how I handled that, but I guess there are many 
possible solutions, and some may be even better than my stuff, only will have 
to find it :o) I'll post some the relevant snippets, that may give you and 
idea how things could be done (one possible way).

statement:
        .......
    |  
        IF '(' expr ')'
                  { P.start_if($3);  }
        command possible_else
                  { $$ = P.finish_if(); }         
    |   
        ....
;

possible_else:
        /* nothing */
        |
          ELSE command
              { /* do nothing */} 
;


command:
          statement
                      { P.add_statement(<Node> $1);}
        |
          block
                      { P.add_statement(<Node> $1);}
;   

block:
          '{'
              { P.start_block();  }
          statements '}'
              { $$ = P.finish_block(); }
;                                                                            

------

IF and ELSE, are the 'if' and 'else' tokens. 
See? Since command recursivly allows a statement again you can stack if 
statements, just like you are used to it..

The above grammar will produce 1 shift/reduce conflict, known well as the 
"tangling else" problem. If you have in example:
if (a)
  if (b)
     foo();
  else bar();

The second else could theoretically belong to both if's, most languages (umm 
actually all I know of :o) attach the else to the inner if, but in a users 
point of view I never noticed it either way, since in this case braces are 
really indicated.

If you want to see the complete source of this snippet you can take a look at
http://www.dtone.org . Thats the project I'm maintaining. BTW: we're always 
looking for people who are interrested in this matter and maybe want help to 
improve the language / compiler. I hope the guys here at bison will forgive 
me for this shameless little advertisment :o),

- Axel



reply via email to

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