Header Ads Widget

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

  1. E  →  E OR E  
  2. E  →  E AND E  
  3. E  →  NOT E   
  4. E  →  (E)  
  5. E →  id relop id  
  6. E  →  TRUE  
  7. E  →  FALSE  

The relop is denoted by <, >, <, >.

The AND and OR are left associated. NOT has the higher precedence then AND and lastly OR.

Production ruleSemantic 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:

  1. p>q AND r<s OR u>r  

  2.      100if p>q goto 103  
  3.      101: t1:=0  
  4.      102goto 104  
  5.      103: t1:=1  
  6.      104if r<s goto 107  
  7.      105: t2:=0  
  8.      106goto 108  
  9.      107: t2:=1  
  10.      108if u>v goto 111  
  11.      109: t3:=0  
  12.      110goto 112  
  13.      111: t3:= 1  
  14.      112: t4:= t1 AND t2  
  15.      113: t5:= t4 OR t3  

Post a Comment

0 Comments