25 Control Flow statements and Boolean Expressions

Rajeswari Sridhar

In this module we learn to generate three address code for control flow statements. We will also try to incorporate short circuit information in Boolean expression’s 3-address code generation. As the flow in control flow’s statements involve Boolean expressions, we will look at the semantic rules for generating three-address code involving Boolean expressions with Control flow.

 

25.1 Control Flow statements

 

We have seen semantic rules that would help generate three-address code for declarations, array accesses, arithmetic expressions, Boolean expressions and to keep track of scope information. Thus, a context free grammar is used to define every programming construct and we write semantic rules based on this context free grammar to generate three-address code.

 

Control flow statements are essential components of programming languages that allows executing code based on a decision. The typically available contro l flow statements are if-then, if- then-else, while-do statement and do – while statements. The control flow statements have an expression that needs to be evaluated and based on the true or false value of the expression the appropriate branching is taken. The context free grammar for defining control flow statements can be defined as

 

S à if E then S1 | if E then S1 else S2 | while E do S1 | do S1 while E

 

The LHS symbol ‘S’ stands for statement. The production defines a statement could be a simple “if Expression then Statement” or “if Expression then statement else alternate statement”, “while Expression is true do the statements repeatedly” or do a statement repeatedly while the expression is true. In all these statements “E” corresponds to a Boolean expression or sometimes it could be an assignment statement. Thus to generate three-address code for a control flow statement, the first step is to generate code for the expression. This expression could be a sequence of expressions combined with relational and logical operators. Let us discuss each type of control flow statements and the semantic rules used for generating three-address code in the subsequent sections.

 

25.1.1 Three-address code for if-then statements

 

The schematic for generating three-address code for if- then statements is given in figure 25.1. As discussed already the expression E needs to be executed first and hence the code corresponding to E is generated first. The value of the expression is evaluated and if it is found to be “true” the statement corresponding to the body of the if- then is executed followed by the statement following the if-then statement. If the expression is false, the body of the if- then is skipped and directly we go to the statement that is following the if-then statement’s body.

 

The sections 25.1.1 to 25.1.4 discussed the various semantic rules for, if- then, if-then-else, while, do-while loops. A ‘for’ loop construct may be considered as a ‘while’ construct and this can be used to generate 3-address code

 

25.2 Control flow with Boolean expression

 

In the previous module we discussed about how to generate three-address code for Boolean expressions. We had generated two new variable and one was set to value ‘0’ if the expression is false and other set to ‘1’ if the expression is true. However, we could avoid generating code for the Boolean expression based on the logical operators if they are connected by one. Avoiding generation of code for some expression in a Boolean expression is referred to as short circuit based code generation.

 

Short circuit avoids computing the full expression involving logical operators. Consider the example where the expression is given by “a > b” . The corresponding code for this expression would be

 

100 If a > b goto E.true

 

101 goto E.false

 

This is different from creating a temporary variable and assigning it 0 / 1. In other words if an expression E is given as E1 or E2 where ‘or’ is a logical operator, then if E1 is true, then this can directly be associated to E.true and we could skip generating code for E2. However, if E1 is false then E2 need to be evaluated. The table 25.5 summarizes the various semantic rules associated by incorporating this short circuit information to generate three-address code.