Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
Switch statements
2.1.
Translation of Switch statements
3.
Syntax Directed Translation of Switch Statements
4.
Frequently Asked Questions
4.1.
What is compiler design?
4.2.
List down the types of compilers.
4.3.
What are switch statements in compiler design?
4.4.
What is an assembler?
4.5.
What is bootstrapping in compiler design?
5.
Conclusion
Last Updated: Mar 27, 2024

Switch Statements

Compiler Design

Introduction

A compiler translates the code written in one language into code written in another without changing the meaning of the programme. In terms of both time and space, a compiler should make the target code efficient and optimised.

The principles of compiler design provide an in-depth look at the translation and optimising of the process. The design of a compiler includes basic translation techniques as well as error detection and recovery. It comprises front-end lexical, syntax, and semantic analysis, as well as back-end code production and optimisation.

Also see, Phases of a Compiler ,Lexical Analysis in Compiler Design

Why Learn Compiler design?

Computers are made up of a well-balanced combination of software and hardware. Hardware is just a mechanical item whose operations are controlled by software that is compatible with it.

Hardware understands instructions in the form of an electronic charge, which is the software programming equivalent of binary language.

There are just two alphabets in binary: 0 and 1. The hardware codes must be encoded in binary format, which is just a series of 1s and 0s, in order to instruct.

Writing such codes would be a tough and time-consuming operation for computer programmers, which is why we have compilers.

Also read About, Specifications of Tokens in Compiler Design

Switch statements

The switch statement is available in a variety of languages. Our switch-statement syntax is shown below. There's a selection expression E to evaluate, then ‘n’ constant values C1, C2,..., Cn that the expression could take, possibly including a default "value" that always matches the expression if no other value does.

Switch Statements

Translation of Switch statements

There's a selection expression to evaluate, then ‘n’ constant values the expression could take, including a default "value" that always matches the expression if no other value does.

A switch's intended translation is code to:

Step 1: Consider the expression.

Step 2: Determine which value in the list of situations is the same as the expression's value.

Step 3: Execute the statement that corresponds to the value discovered.

Step 2 can be carried out in a variety of ways:

  • If the number of cases is modest, a series of conditional goto statements can be used.
  • By constructing a database of pairs, each pair contains a value and a label for the statement's code. The compiler creates a loop to compare the expression's value to each of the table's values. If no match is detected, the default (last) entry will almost certainly be correct.
  • It is more efficient to create a hash table if the number of cases is huge.
  • There is a particularly common scenario in which the n-way branch can be implemented efficiently. We can create a sequence 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 occupied; otherwise, if the values all fall within a small range, say imin to imax, and the count of various values is a reasonable portion of imax - imin. To make the switch, evaluate the expression to get the value of j, make sure the value is within the range, and then move to the table item at offset j-imin.

Also See, Top Down Parsing

Syntax Directed Translation of Switch Statements

Consider the following switch statement:

SDT of Switch Statements

This case statement is converted to intermediate code that looks like this:

Translation of Case Statement

To translate into the above form :  

  • When the keyword switch is encountered, two new labels, test and next, are created, as well as a new temporary t.
  • The code to convert E to t is generated as the expression E is parsed. The jump goto test is generated after processing E.
  • A new label Fi is produced and placed into the symbol table for each case keyword that occurs. A stack is created with reference to this symbol-table entry and the value Vi of the case constant (used only to store cases).
  • Each Ci: Si statement case is handled by emitting the newly produced label Fi, then the code for Si, and finally the jump goto next.
  • The code for the n-way branch can then be created after the keyword end terminating the body of the switch is located. We may build a sequence of three-address statements of the form by reading the pointer-value pairs on the case stack from bottom to top.

illustration image

Where t is the name of the variable that holds the value of the selection expression E, and Fn is the default statement's label.

Frequently Asked Questions

What is compiler design?

Ans. The basic translation method is covered by compiler design. Error detection and recovery are also covered. It covers lexical, syntactic, and semantic analysis, just like the front end. As a back-end, it also offers code generation and optimization.

List down the types of compilers.

Ans. A compiler is a computer program that converts source code written in a high-level language into low-level machine language.

The following are some examples of compilers:

  • Compilers with a single pass
  • Compilers with two passes
  • Compilers with multiple passes

What are switch statements in compiler design?

Ans. A switch or case statement is a form of selection control mechanism that uses search and map to modify the control flow of programme execution based on the value of a variable or expression

What is an assembler?

An assembler is a computer program that converts assembly language software into machine code that the computer can understand.

What is bootstrapping in compiler design?

Bootstrapping is a method in compiler design in which a simple language is utilized to convert progressively advanced programs. This difficult software can handle even more difficult programs, and so on.

Conclusion

In this article, we learned about the switch statements in compiler design. We also learned about the structure of switch statements. We also saw the translation of switch statements in compiler design.

Recommended Reading: 

Do check out The Interview guide for Product Based Companies as well as some of the Popular Interview Problems from Top companies like Amazon, Adobe, Google, Uber, Microsoft, etc. on Coding Ninjas Studio.

Also check out some of the Guided Paths on topics such as Data Structure and Algorithms, Competitive Programming, Operating Systems, Computer Networks, DBMS, System Design, etc. as well as some Contests, Test Series, Interview Bundles, and some Interview Experiences curated by top Industry Experts only on Coding Ninjas Studio.

Do upvote our blog to help other ninjas grow.

Happy Learning!

Live masterclass