BOOLEAN EXPRESSIONS
Boolean expressions have two primary purposes. They are used to compute logical values, but more often they are used as conditional expressions in statements that alter the flow of control, such as if-then-else, or while-do statements.
Boolean expressions are composed of the boolean operators ( and, or, and not ) applied to elements that are boolean variables or relational expressions. Relational expressions are of the form E1 relop E2, where E1 and E2 are arithmetic expressions.
Here we consider boolean expressions generated by the following grammar :
E->EorE | EandE |notE | ( E ) |id relop id | true | false
Methods of Translating Boolean Expressions:
There are two principal methods of representing the value of a boolean expression. They are :
Numerical Representation
Here, 1 denotes true and 0 denotes false. Expressions will be evaluated completely from left to right, in a manner similar to arithmetic expressions.
For example :
t2 : = b and t1
t3 : = a or t2
which can be translated into the three-address code sequence (aga statement numbers at 100) :
100 : if a < b goto 103 101 : t : = 0
102 : goto 104
103 : t : = 1
104 :
Translation scheme using a numerical representation for booleans
Short-Circuit Code:
We can also translate a boolean expression into three-address code without generating code for any of the boolean operators and without having the code necessarily evaluate the entire expression. This style of evaluation is sometimes called “short-circuit” or “jumping” code. It is possible to evaluate boolean expressions without generating code for the boolean operators and, or, and not if we represent the value of an expression by a position in the code sequence.
Translation of a < b or c < d and e < f
100 : if a < b goto
103 101 : t1 : = 0
102 : goto 104 103 : t1 : = 1
104 : if c < d goto
107 105 : t2 : = 0
106 : goto 108
107 : t2 : = 1
Flow-of-Control Statements
We now consider the translation of boolean expressions into three address code in the context of if-then, if-then-else, and while-do statements such as those generated by the following grammar:
S->ifEthenS1
| if E then S1 else S2
| while E do S1
In each of these productions, E is the Boolean expression to be translated. In the translation, we assume that a three-address statement can be symbolically labeled, and that the function newlabel returns a new symbolic label each time it is called.
1. What is intermediate code generation in the context of boolean expressions? |
2. How does intermediate code generation help in computer science and IT engineering? |
3. What are some common techniques used in intermediate code generation for boolean expressions? |
4. How does intermediate code generation differ from machine code generation? |
5. Can you explain the significance of boolean expressions in computer science and IT engineering? |