In this article, we are going to cover an overview that how we can each compiler phase works individually with the help of an example. Let’s discuss one by one.
Pre-requisite – Introduction to compiler phases
You will see how compiler phases like lexical analyzer, Syntax analyzer, Semantic Analyzer, Intermediate code generator, code Optimizer, and Target code generation. let’s consider an example.
x = a+b*50
The symbol table for the above example is given below. In symbol table are clearly mentions the variable name and variable types.
|S.No.||Variable name||Variable type|
Now, here you will see how you can execute the compiler phase at each level and how it works.
- Lexical Analyzer :
In this phase, you will see how you can tokenize the expression.
x -> Identifier- (id, 1) = -> Operator - Assignment a -> Identifier- (id, 2) + -> Operator - Binary Addition b -> Identifier- (id, 3) * -> Operator - Multiplication 50 -> Constant - Integer
Now, the final tokenized expression is given below.
(id, 1) = (id, 2) + (Id, 3)*50
- Syntax Analyzer :
In this phase, you will see how you can check the syntax after tokenized the expression.
S -> Id = E E -> E+T | T T -> T*F | F F -> Id | Integer constant
SDT for the above expression is given below.
- Semantic Analyzer :
In this phase, you will see how you can check the type and semantic action for the syntax tree. Given below is the diagram of the semantic analyzer.
- Intermediate Code Generator :
In this phase as an input, you will give a modified parse tree and as output after converting into Intermediate code will generate 3 -Address Code. Given below is an expression of the above-modified parse tree.
3 Address Code –
t1 = b * 50.0 t2 = a+t1 x = t2
- Code Optimizer :
In this phase, you will see as an input will give 3 address code and as an output, you will see optimize code. Let’s see how it will be converted.
t1 = b* 50.0 x = a+ t1
- Target Code Generator :
It is the last phase and In this, you will see how you can convert the final expression into assembly code. so, that it will be easy to understand for the processor.
Mul Add Store
Attention reader! Don’t stop learning now. Get hold of all the important CS Theory concepts for SDE interviews with the CS Theory Course at a student-friendly price and become industry ready.
- Phases of a Compiler
- Difference between Native compiler and Cross compiler
- Incremental Compiler in Compiler Design
- Working of Lexical Analyzer in compiler
- Compiler Theory | Set 1
- Compiler Theory | Set 2
- Introduction to Syntax Analysis in Compiler Design
- Why FIRST and FOLLOW in Compiler Design?
- Runtime Environments in Compiler Design
- Syntax Directed Translation in Compiler Design
- Intermediate Code Generation in Compiler Design
- Peephole Optimization in Compiler Design
- Language Processors: Assembler, Compiler and Interpreter
- Code Optimization in Compiler Design
- Introduction of Object Code in Compiler Design
- Introduction of Compiler Design
- Compiler Design | Syntax Directed Definition
- Error detection and Recovery in Compiler
- Symbol Table in Compiler
- Error Handling in Compiler Design
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.