87
Boolean expressions
Boolean expressions have two primary purposes. They are used for computing the logical values. They are also used as conditional expression using if-then-else or while-do.
Consider the grammar
The relop is denoted by <, >, <, >.
The AND and OR are left associated. NOT has the higher precedence then AND and lastly OR.
Production rule | Semantic actions |
---|---|
E → E1 OR E2 | {E.place = newtemp(); Emit (E.place ‘:=’ E1.place ‘OR’ E2.place) } |
E → E1 + E2 | {E.place = newtemp(); Emit (E.place ‘:=’ E1.place ‘AND’ E2.place) } |
E → NOT E1 | {E.place = newtemp();  Emit (E.place ‘:=’ ‘NOT’ E1.place) } |
E → (E1) | {E.place = E1.place} |
E → id relop id2 | {E.place = newtemp();  Emit (‘if’ id1.place relop.op id2.place ‘goto’  nextstar + 3);  EMIT (E.place ‘:=’ ‘0’)  EMIT (‘goto’ nextstat + 2)  EMIT (E.place ‘:=’ ‘1’) } |
E → TRUE | {E.place := newtemp();  Emit (E.place ‘:=’ ‘1’) } |
E → FALSE | {E.place := newtemp();  Emit (E.place ‘:=’ ‘0’) } |
The EMIT function is used to generate the three address code and the newtemp( ) function is used to generate the temporary variables.
The E → id relop id2 contains the next_state and it gives the index of next three address statements in the output sequence.
Here is the example which generates the three address code using the above translation scheme:
Next TopicFlow altering statements