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

ProductionSemantic 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: 05-Nov-2021

4K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements