Programming #4B: A Partial Implementation of the Biola Interpreter, Step II

 

 

Specification:

In addition to the handling of read statements, display statements, assignment statements in Programming 4A, we now want to be able to execute if statements and while statements too in Programming 4B.

 

Simplifying Assumptions: 

To make this programming assignment easier, you can assume in the source programs if statements and while statements are all well formatted in one of the following ways.

 

(i) the if keyword and the logic expression appear in one line followed by a block of statement enclosed inside a pair of curly braces like:

 

if  ( … )

{

…;

…;

}

 

 

See, for example, the Biola program in testPrime1.htm.

 

 

 

(i) the if keyword and the logic expression appear in one line followed by a block of statement enclosed inside a pair of curly braces and

then the else keyword appears on a separate line alone followed by a block of statement enclosed inside a pair of curly braces like:

 

if  (… )

{

…;

…;

}

else

{

…;

…;

}

 

 

See, for example, the Biola program in testPrime2.htm.

 

 

 

while  (… )

{

…;

…;

}

           

See, for example, the Biola programs in testPrime1.htm and testPrime2.htm.

 

 

 

Tracking the nested blocks using stacks:

 

You should use a stack of line numbers stackLineNumbers and a stack of keywords stackBlockTypes to help you match the nested curly braces to mark the beginning of a block of statements and to detect the ending of a block of statements. It can also help you manage the control flow (i.e. the order in which the individual statements are executed according to the results of logic expressions) of if statements and while statements.

 

Marking the beginning of a block using the stacks:

1.     When a left curly brace { is encountered on line n immediately following the if keyword and the logic expression on line n-1, (i) push back the line number n-1 into to stackLineNumbers and (ii) push back the if keyword into to stackBlockTypes to signal the beginning of an if block.

2.     When a left curly brace { is encountered on line n immediately following the else keyword on line n-1, (i) push back the line number n-1 into to stackLineNumbers and (ii) push back the else keyword into to stackBlockTypes to signal the beginning of an else block.

3.     When a left curly brace { is encountered on line n immediately following the while keyword and the logic expression on line n-1, (i) push back the line number n-1 into to stackLineNumbers and (ii) push back the while keyword into to stackBlockTypes to signal the beginning of an while block.

 

 Detecting the end of a block using the stacks:

1.     When a right curly brace } is encountered on line m, check whether stackLineNumbers is empty. If it is empty, the curly braces are not balanced and it indicates a syntax error.

2.     When a right curly brace } is encountered on line m and stackLineNumbers is non-empty, retrieve the matching line number from the top of stackLineNumbers n and retrieve the bock type information from the top of stackBlockTypes. (Note that for the end of a while block such information can allow us to go back to the beginning of the while loop.) After that, pop back stackLineNumbers and stackBlockTypes to remove the top elements just retrieved.

 

 

About the Interpreter class and the management of the control flow:

You need to enhance the static member function for executing a Biola source program in Program 4A so that it can execute if statements and while statements:

 

·    if : When encountering the if keyword and the logic expression on line n, first evaluate the logic expression.

1.     If the result is true (i.e. the numerical value is 1), the interpreter just goes on to line n+1 to interpret things in the if block.

2.     If the result is false (i.e. the numerical value is 0), the interpreter should silently scan through the entire block of statements after the if keyword, starting from n+1without actually executing the statements in the bock until the end of this if block is detected. When reaching the end of this if block on line m, the interpreter should go on to interpret the statement on line m+1.

 

·    End of an if block: When the } is detected as the end of an if block on line n, the interpreter should check whether line n+1 is about the else keyword.

1.     If the else keyword is on line n+1, there is an entire block of statements after the else keyword. In this case, the interpreter should silently scan through the entire else block starting from n+1without executing the statements in the bock until the end of the else block is detected. When reaching the end of this else block on line m, the interpreter should go on to interpret the statement on line m+1.

2.     If the else keyword is NOT on line n+1, the interpreter should go on to interpret the next statement on line n+1.

 

·    else : when encountering the else keyword on line n, the interpreter should go on to interpret the next statement in n+1.

·     End of an else block: When the } is detected as the end of an else block on line n, the interpreter should go on to interpret the next statement in n+1.

 

·    while : when encountering the while keyword and the logic expression on line n, first evaluate the value of the logic expression.

1.     If the result is false (i.e. the numerical value is 0), the interpreter should first silently scan through the entire while block starting from n+1without actually executing the statements in the bock until the end of this while block. When reaching the end of the while block on line m, the interpreter should go on to interpret the next statement in m+1.

2.     If the result is true (i.e. the numerical value is 1), then the interpreter should interpreting the next statement on line n+1.

 

·    End of a while block: When the } is detected as the end of an while block, the interpreter should go back to interpret the line containing the while keyword and the logic expression corresponding to this while block.