0

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

Naz1706
  • 5
  • 3
  • You've got the grammar basics but you need to read a bit more about C. The boolean and operator in C is written `&&`; `&` is a bitwise operator whose results are subtly different. In C boolean values are integers, with 0 being false and everything else true. So you don't need a new type. (You can `#include stdbool`, which will define the type `bool` and the constants `true` and `false`, but it's just cosmetic; behind the scenes nothing changes. – rici Apr 27 '21 at 22:12
  • Thank you, this is really helpful to know that I need to look into C instead of just searching for questions on yacc or Bison! – Naz1706 Apr 27 '21 at 22:54

0 Answers0