What are Boolean Expressions?



Boolean means True or False. It can also be represented by 1 or 0.

Boolean Expression is the expression that returns true or false. Boolean Expression can be represented in two ways−

  • Conditional Expressions

For example, If a > b{
    cout<<"a is greater than b";
}

Here a > b is a conditional expression that can be either True or False.

  • Logical Expression

These are expressions that contain logical operators, i.e., OR, AND, NOT applied on operands.

For example−

  • ? ?? ? →It returns true if any of the expressions is true.
  • ? ??? ? → It returns true if both of the expression is true.
  • ??? ? → It returns true if the expression ? is false and returns false if ? is true.


Precedence wise NOT has the highest precedence, then AND, then OR has Lowest Precedence.

Translation of Boolean Expression

Consider the following Grammar

? → ? ?? ?                                     ? → ????

? → ? ??? ?                                  ? → ???????? ???

? → ??? ?                                     ? → (?)

? → ?????

Syntax Directed Translation For Boolean Expressions

Production Semantic Rule
E → E(1)?? E(2) {
E. PLACE = newtemp( ); GEN(E. PLACE = E(1). PLACE ?? E(2). PLACE)
}
E → E(1)??? E(2) {
E. PLACE = newtemp( ); GEN(E. PLACE = E(1). PLACE ??? E(2). PLACE)
}
E → ??? E(1) {
E. PLACE = newtemp( ); GEN(E. PLACE = ??? E(1). PLACE)
}
E → (E(1)) {
E. PLACE = E(1). PLACE
}
E → id1????? id2 {
E. PLACE = newtemp( ); GEN(if id1. PLACE ????? id2. PLACE goto nextquad + 3); GEN(E. PLACE = 0); GEN(goto nextquad + 2); GEN(E. PLACE = 1)
}
E → TRUE {
E. PLACE = newtemp( ); GEN(E. PLACE = 1)
}
E → FALSE {
E. PLACE = newtemp( ); GEN(E. PLACE = 0)
}

???????( ) will create a new temporary variable.

?. ????? will represent the name that will hold the value of an expression.

????????? ??? (?????????) will generate three address code for the particular statement and will insert the operator & operands occurred in a statement into a Quadruple.

'nexquad' is the index of the next three address codes statement (quadruple array).

Substituting newtemp( ) into E. PLACE will refresh the value of E. PLACE.

Example1− Convert the following expression into three address code sequences.

(a) X AND Y OR C

(b) X OR Y AND NOT C

Solution

  • ? ??? ? ?? ?

T1 = X AND Y

T2 = T1 OR C

  • ? ?? ? ??? ??? ?

T1 = NOT C

T2 = Y AND T1

T3 = X OR T2

∴ NOT has highest precedence, then AND, then OR has the lowest precedence.

Example2− Write three address code for

If (A < B OR X > Y)

A = B + C

Solution

(1) If A < B goto(4)

(2) If X > Y goto(4)

(3) goto(6)

(4) T = B + C

(5) A = T

Example3− Write three address code for a > b ??? c < d ??? e < f.

Solution

(1) If a > b goto(4)

(2) t1 = 0

(3) goto(5)

(4) t1 = 1

(5) If c < d goto(8)

(6) t2 = 0

(7) goto(9)

(8) t2 = 1

(9) If e < f goto(12)

(10) t3 = 0

(11) goto(13)

(12) t3 = 1

(13) t4 = t1 ??? t2

(14) t5 = t4 ??? t3

Updated on: 2021-11-05T11:58:53+05:30

5K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started