Explain front end and back end of compiler in detail.
-
The phases are collected into a front end and back end :
Front End
-
The front end consist of those phases, that depends primarily on source language and largely independent of the target machine.
-
Front end includes lexical analysis, syntax analysis, semantic analysis, intermediate code generation and creation of symbol table.
-
Certain amount of code optimization can be done by front end.
-
It includes following phases:
-
Lexical analysis
-
The lexical analyzer is the first phase of compiler.
- Its Main task is to read the input characters and produce as output a sequence of tokens that the parser uses for syntax analysis.
- It is implemented by making lexical analyzer be a subroutine
- Upon receiving a “get next token” command from parser, the lexical analyzer reads the input character until it can identify the next token
- It may also perform secondary task at user interface
- One such task is stripping out from the source program comments and white space in the form of blanks, tabs, and newline characters.
- The scanner is responsible for doing simple task while lexical analysis does the more complex task
-
Syntax analysis
- Syntax analysis is also called hierarchical analysis or parsing.
- The syntax analyzer checks each line of the code and spots every tiny mistake that the programmer has committed while typing the code.
- If code is error free then syntax analyzer generates the tree
-
Semantic analysis
-
Semantic analyzer determines the meaning of a source string.
-
For example matching of parenthesis in the expression, or matching of if..else statement or performing arithmetic operation that are type compatible, or checking the scope of operation
-
Intermediate code generation
-
The intermediate representation should have two important properties, it should be easy to produce and easy to translate into target program.
-
We consider intermediate form called “three address code”.
-
Three address code consist of a sequence of instruction, each of which has at most three operands.
-
Creation of symbol table
-
A symbol table is a data structure used by a language translator such as a compiler or interpreter.
-
It is used to store names encountered in the source program, along with the relevant attributes for those names.
-
Information about following entities
-
-
Variable/Identifier
-
Procedure/function
-
Keyword
-
Constant
-
Class name
-
Label name
Back end
-
The back end consists of those phases, that depends on target machine and do not depend on source program.
-
Back end includes code optimization and code generation phase with necessary error handling and symbol table operation.
-
It includes following phases:
-
Code optimization
- The code optimization phase attempt to improve the intermediate code.
- This is necessary to have a faster executing code or less consumption of memory.
- Thus by optimizing the code the overall r improved.
- t1= id3 * 2.0
- t2= id2 * t1
- id1 = id1 + t2
-
Code generation phase
- In code generation phase the target code gets generated.
- The intermediate code instructions are translated into sequence of machine instruction.
- MOV id3, R1
- MUL #2.0, R1
- MOV id2, R2
- MUL R2, R1
- MOV id1, R2
- ADD R2, R1
- MOV R1, id1
-
Error handling and symbol table operation