Tawab Masood
Tawab Masood

Reputation: 1

How to end the if statement in Bison?

if_stmt: tok_if '(' condition ')' '{' root '}'  {debugBison(26);}
    ;  
 
condition : expression {debugBison(19); if ($1==0.0){exit(0);}}
 ;

i am trying to implement if condition in bison, everything is working fine just when the condition is false it exits the whole execution and does not executes any line after this , what i want to do is that i just want to skip the this line (in which the condition is false or 0.0), other line should work fine

previously i was doing the same thing like this

if_stmt: 
    tok_if '(' expression ')''{' root '}' {
        debugBison(17);
        if ($3!= 0.0) {
         yyerror("Hello");
        } else {
        yyerror("Not Hello");
        }
    }
        ;
    

this is also not true , i even try it like this if_stmt: tok_if '(' condition ')' true_block | tok_if '(' condition ')' false_block ;

true_block:
    '{' root '}' { debugBison(17); }  // Executes root if condition is true
    ;

false_block:
    /* empty */ { debugBison(18); }  // Skips the block if condition is false
    ;

condition:
    expression  { $$ = ($1 != 0.0); }  // Condition returns 1 if true, 0 if false.
    ;

the problem with the above two approaches is that it first executes the grammer and then it checks the condition in the {}.So any one can help me with the first approach ??your text

Upvotes: 0

Views: 68

Answers (0)

Related Questions