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