Home | | Compiler Design | Case Statements

Chapter: Principles of Compiler Design : Intermediate Code Generation

Case Statements

The “switch” or “case” statement is available in a variety of languages.


The “switch” or “case” statement is available in a variety of languages. The switch-statement syntax is as shown below :


Switch-statement syntax s

witch expression



case value : statement

case value : statement



case value : statement

default : statement



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 n-way 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 j-imin .


Syntax-Directed Translation of Case Statements:


Consider the following switch statement:

switch E



case V1 : S1 case V2 : S2


case Vn-1 : Sn-1

default : Sn



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


Ln-1 : code for Sn-1 goto next


Ln : code for Sn goto next


test : if t = V1 goto L1 if t = V2 goto L2


if t = Vn-1 goto Ln-1 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 symbol-table 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 n-way branch. Reading the pointer-value pairs on the case stack from the bottom to the top, we can generate a sequence of three-address statements of the form

case V1


case V2



Vn-1 Ln-1


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.

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Principles of Compiler Design : Intermediate Code Generation : Case Statements |

Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.