Control flow is an order in which statements are executed. The code executes in order from the first line in the file to the last line unless it comes across conditionals or loops that change the order.
Control flow statements include conditional statements, branching statements, and looping statements. Control flow can be decided with the help of a boolean expression (an expression that evaluates to either true or false).
The translation of boolean expressions is connected to the translation of statements like if-else statements and while statements. Boolean expressions are frequently used in programming languages to:-
- Compute the logical values: True or false can be represented as values in a boolean expression. These boolean phrases can be evaluated using three-address instructions and logical operators, just like arithmetic expressions.
- Alter the flow control: Boolean expressions are utilized as conditional expressions in statements that change the flow of control. The value of such boolean expressions is implicit in the program's position. For example, if(E) S, where E is a boolean expression, and 'S' is the statement. Control will reach S only if the boolean expression E evaluates to true.
Also see, Translation of Expressions
Boolean Expressions
As discussed above boolean expressions are expressions that evaluate to either true or false and are used to alter the control flow. Boolean expressions are made of boolean operators(such as &&(AND), ||(OR), !(NOT), etc), elements(boolean variables) and relational expressions. Relational expressions are of the form E1 rel E2, where E1 and E2 are arithmetic expressions.
Example: A→ A1 || A2 | (A && A) | E rel E | true
In the above expression, if we determine whether A1 is true for expression A1 || A2, the whole expression will be true since OR is taken of the two. Similarly, each part of the expression can be calculated independently, and then the result is combined. If the language definition permits portions of the expression to go unevaluated, the compiler can optimize the evaluation of boolean expression by computing only enough to determine its value. Suppose different OR expressions are present and any one of them becomes true, then the whole expression is considered true and the control will jump accordingly.
Another example can be the expression B1 && B2, if any of the B1 is false, then no need to determine the whole expression, the expression is false. If B1 is true then only check for B2, if B2 is also true, only then the expression is true. To check whether all parts of the boolean expressions should be evaluated or not we use semantic definitions. We will be discussing it below.
Also read About, Specifications of Tokens in Compiler Design and,Lexical Analysis in Compiler Design
Short-Circuit Code
The boolean operators &&, I I, and ! translate into jumps in short-circuit (or jumping) code. Instead of the operators appearing in the code, the value of a boolean expression is represented by a position in the code sequence.
For example: Consider the following boolean expression.
If ( a< 7 || a> 9 && a!=b) then a=0;
Here if any of the boolean expressions (a<7) or (a> 9 && a!=b) becomes true, then the expression a=0, will be executed. If the whole expression becomes false then the control will jump to the rest of the code. Let’s denote expression a=0, at Label L2, and the remaining part of the code at label L1. Label can be defined as a position where the control will jump after the evaluation of the expression. The jumping code for the above expression is given in the table below.
If a < 7 goto L2
If FALSE a > 9 goto L1
If FALSE a!=b goto L1
L2: a = 0
L1:
Jumping code
First the expression a<7 is checked if its true control will directly jump to label L2 and expression a=0 will be executed. It is because the expression is in OR with the rest of the expression. But if a<7 comes to false, then the other expression will be checked part-wise. Since there is an &&(and) operation in the second part first the expression a>9 will be checked if it is false, no need to evaluate the remaining expressions, the whole expression will result in false, and control will jump to label L1. If a>9 is true, then the remaining part of the expression is checked (a!=b), if it is false the whole expression again will become false and control will jump to label L1. Else if both expressions( a> 9 && a!=b) are true, then the control will jump to label L2.
Also See, Top Down Parsing