Exercises
- Appel 3.15 ("Yacc" means "bison").
- We have considered a grammar for expressions with rules for '+', '-', '*', '/', id, num, and parentheses. Expand this grammar with boolean operators (and, or, not) and comparisons (==, <=, etc.) in such a way that you get the usual precedence and associativity. Use Tiny Expressions as your staring point.
-
Currently in SCIL, minus is implemented as a binary operation only (as defined
by the grammar). Extend SCIL with unary minus using so-called
syntactic sugar. This means that you do not have to take care of the
construction all the way through the compiler. Instead, you simply handle it
in the parsing phase: You extend the grammar to allow for unary minus,
but when you recognize a unary minus,
MINUS expression
, you build the parser tree pretending that you had just recognized0 MINUS expression
; and the compiler already know how to deal with that. Extend the functionp_expression
, adding a new right-hand side,expression_unary
. Then write a new functionp_expression_unary
that recognizes unary minus and handles it as described. Test it by running the compiler on input using unary minus in different ways.