Computer Science Engineering (CSE) Exam  >  Computer Science Engineering (CSE) Notes  >  Case Statements - Intermediate Code Generation, Computer Science and IT Engineering

Case Statements - Intermediate Code Generation, Computer Science and IT Engineering - Computer Science Engineering (CSE) PDF Download

CASE STATEMENTS

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

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 codeof 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

begin

case V1 : S1 case V2 : S2

case Vn-1 : Sn-1

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

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

L1

case V2

L2

case

Vn-1 Ln-1

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.

The document Case Statements - Intermediate Code Generation, Computer Science and IT Engineering - Computer Science Engineering (CSE) is a part of Computer Science Engineering (CSE) category.
All you need of Computer Science Engineering (CSE) at this link: Computer Science Engineering (CSE)

FAQs on Case Statements - Intermediate Code Generation, Computer Science and IT Engineering - Computer Science Engineering (CSE)

1. What is the purpose of case statements in intermediate code generation?
Ans. Case statements in intermediate code generation are used to handle multiple branching conditions efficiently. They provide a way to execute different code blocks based on the value of a variable or an expression.
2. How are case statements represented in intermediate code?
Ans. In intermediate code, case statements are typically represented by a series of conditional branches. Each branch compares the value of the variable or expression against a set of constant values and jumps to the corresponding code block when a match is found.
3. Can case statements handle floating-point values in intermediate code generation?
Ans. In most programming languages, case statements in intermediate code generation are designed to handle only integral or enumerated types. Floating-point values are not directly supported. However, some languages provide workarounds such as converting floating-point values to integers before using them in case statements.
4. How do case statements handle default cases in intermediate code generation?
Ans. In intermediate code generation, case statements typically include a default case that is executed when none of the constant values match the variable or expression being evaluated. This default case serves as a fallback option to ensure that the program doesn't encounter any unexpected behavior.
5. Are case statements limited to a specific number of branches in intermediate code generation?
Ans. The number of branches in case statements in intermediate code generation is usually limited by the programming language or compiler's specifications. Some languages impose a maximum limit, while others may allow unlimited branches. It is important to consider the limitations of the language or compiler being used to avoid potential issues with large or complex case statements.
Download as PDF

Top Courses for Computer Science Engineering (CSE)

Related Searches

study material

,

video lectures

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

past year papers

,

Summary

,

pdf

,

Objective type Questions

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

Important questions

,

Semester Notes

,

Free

,

Case Statements - Intermediate Code Generation

,

shortcuts and tricks

,

Exam

,

Computer Science and IT Engineering - Computer Science Engineering (CSE)

,

Sample Paper

,

practice quizzes

,

Extra Questions

,

Viva Questions

,

Previous Year Questions with Solutions

,

Case Statements - Intermediate Code Generation

,

MCQs

,

mock tests for examination

,

ppt

,

Case Statements - Intermediate Code Generation

;