I’m currently in a CSCI class, compiler at my college. I have to write a parser for the compiler and I’ve already done Adding subtracting multiplying dividing and the assignment statement. My question is we now have to do the less than equal (<=) and the greater than equal (>=) and I’m not sure how to write the rule for it…
I was thinking something like…
expr LESSTHAN expr { $1 <= $3 }
expr GREATERTHAN expr { $1 >= $3 }
any suggestions?
You should include a more precise question. Here are some general suggestions though.
The structure of the rule for relational operations should be the same as of the arithmetic operations. In both cases you have binary operators. The difference is that one returns a number, the other returns a boolean value. While
1 + 1 >= 3usually is valid syntax, other combinations like1 >= 2 => 5is most likely invalid. Of course there are exceptions. Some languages allow it as syntactic sugar for multiple operations. Others simply define that boolean values are just integers (0and1). It’s up to you (or your assignment) what you want the syntax to look like.Anyway, you probably don’t simply want append those rules to
expr, but create a new rule. This way you distinguish between relational and arithmetical expressions.