Front end and back end of the compiler

Front end and back end of the compiler

Front end and back end is the collection of phases of compiler.
Front End : 
1. Lexical Analysis, 
2. Syntax Analysis,
3. Semantic Analysis, 
4. Intermediate Code, 
5. Some amount of Code Optimization.

Back End: 
2. Code Generation

1. Lexical Analysis: It takes source program as input and produce tokens.
What is lexical token ?
Ans. A unit in the grammar of the programming language.
Type token = (id, number)
Punctuation token = (if, return)

2. Syntax Analysis: It takes output of lexical analysis as input and produces tree as output.
For example: Output of Lexical analysis = C = A + B
Input of syntax analysis = C = A + B
Output of syntax analysis =

3. Semantic Analysis : It takes output of syntax analysis as input and produces a tree with type information as output.
It checks for sematic errors.
For example: Output of syntax analysis

Input of semantic analysis = 
Output of semantic analysis = 

4. Intermediate code: It takes output of semantic analysis as input and produces intermediate code as output.

Disadvantages of Front End:
1. Requires large amount of memory to store tokens and trees.
2. Data move from one memory to another which makes it very slow.

Function of Front End:
1. Determine validity of source code.
2. Determine content of source code.
3. Build source code for easy to analyze.

1. Code Optimization : It is the process to modify the program to make it more efficient, faster execution, less resources requirements. 
Levels of Code Optimization:
a. Design level
b. Source code level
c. Compile level
d. Assembly level
e. Run time level

2. Code generation : Knowledge of target architecture helps code generation to determine:
a. Where to store result in memory location or registers.
b. Which instruction is better for type conversion.
c. Which addressing mode to use.
For example:
AX = 5, BX = 2, AX+BX
Assembly code :