CASE STATEMENTS
The “switch” or “case”
statement is available in a variety of languages. The switchstatement syntax
is as shown below :
Switchstatement syntax s
witch expression
begin
case value : statement
case value : statement
..
case value : statement
default : statement
end
There is a selector
expression, which is to be evaluated, followed by n constant values that the
expression might take, including a default “value” which always matches the
expression if no other value does. The intended translation of a switch is code
to:
1.
Evaluate the expression.
2.
Find which value in the list of cases is
the same as the value of the expression.
3.
Execute the statement associated with
the value found.
Step (2) can be implemented in one of several ways :
By a sequence
of conditional goto statements, if the number of cases is small.
* By
creating a table of pairs, with each pair consisting of a value and a label for
the code
of the corresponding statement. Compiler generates a
loop to compare the value of the expression with each value in the table. If no
match is found, the default (last) entry is sure to match.
* If
the number of cases s large, it is efficient to construct a hash table.
* There
is a common special case in which an efficient implementation of the nway
branch exists. If the values all lie in some small range, say imin to imax, and
the number of different values is a reasonable fraction of imax  imin, then we
can construct an array of labels, with the label of the statement for value j
in the entry of the table with offset j  imin and the label for the default in
entries not filled otherwise. To perform switch, evaluate the expression to
obtain the value of j , check the va transfer to the table entry at offset
jimin .
SyntaxDirected Translation of Case
Statements:
Consider the following switch statement:
switch E
begin
case V1 : S1 case V2 : S2
case Vn1 : Sn1
default : Sn
end
This case statement is
translated into intermediate code that has the following form : Translation of
a case statement
code to evaluate E into t goto test
L1 : code for S1 goto next
L2 : code for S2 goto next
Ln1 : code for Sn1 goto next
Ln : code for Sn goto next
test : if t = V1 goto L1 if t = V2 goto L2
if t = Vn1 goto Ln1 goto Ln
next
:
To translate into above form :
* When keyword switch is seen, two new
labels test and next, and a new temporary t are generated.
As
expression E is parsed, the code to evaluate E into t is generated. After
processing E , the jump goto test is generated.
* As
each case keyword occurs, a new label Li is created and entered into the symbol
table. A pointer to this symboltable entry and the value Vi of case constant
are placed on a stack (used only to store cases).
* Each
statement case Vi : Si is processed by emitting the newly c
by
the code for Si , followed by the jump goto next.
* Then when the keyword end terminating
the body of the switch is found, the code can be generated for the nway branch.
Reading the pointervalue pairs on the case stack from the bottom to the top,
we can generate a sequence of threeaddress statements of the form
case V1 
L1 

case V2 
L2 

case 
Vn1
Ln1 

case 
t
Ln 

label
next
where t is the name
holding the value of the selector expression E, and Ln is the label for the
default statement.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 20182023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.