Extend the compiler from MP5 to support loops and if statements. them. Unlike the previous calculator examples, the language should be a civilized one based on the following grammar (this grammar is a slight extension of the grammar from problem 3 of HW8):
program ::= { statement } end_line statement ::= P expression | V ( expression ) = expression | ( statement { statement } ) | L boolean statement | I boolean statement [ E statement ]Here, we have added 3 new kinds of statements to the grammar of MP5, the first two of these additions you have seen in problem 3 of HW8, "begin end" blocks and while loops. The third new feature is the if statement. If the boolean expression is true, the following statement gets executed; otherwise, it gets skipped. The square brackets above surround the optional else clause of the if statement. If the bracketed E is present, the following statement is the else clause of the if statement. This grammar rests on a new kind of expression, the boolean expression:
boolean ::= expression rop expression rop ::= > | = | <A boolean expression consists of a relational operator separating a pair of expressions. Good ideas for how to do this assignment can be gleaned from Homework 9! Here is an example illustrating the use of these calculator extensions:
V(1)=7 LV(1)>1 (IV(1)%2=0 V(1)=V(1)/2 EV(1)=V(1)*3+1 PV(1))