Home Â» Boolean expressions

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: