Way to create productions for boolean operands in Bison

114 views Asked by At

I'm creating a parser in Bison, and I am trying to convert the rules:

E := E op E
op := + | - | > | & 

There are other rules for E, such as negation, and of course, E can be a number. I was wondering what the best way would be to create these productions. I have seen calculator examples where people are writing out $$ = $1 '+' $3 etc, for each operand.

My lex file returns the operands as {return yytext[0];} but I am finding it difficult to find resources to create the other productions (comparisons, && and negation). I have the yacc & lex book by Levine et al but I'm quite confused on how to go about this still.

I believe that I can do exp '>' exp {$$ = $1 > $3} and exp '&' exp {$$ = $1 & $3}, and return a bool value, which i can define as a type, but how would I do this?

Would love if there was an elegant way of doing this, and if there are any resources which can help me write production rules. I know that I can write C-code but I'm very new at this and I'm quite unsure

0

There are 0 answers