Com Phases
Com Phases
Com Phases
1. Lexical analysis
2. Syntax analysis
3. Semantic analysis
6. Code generator
Phases
Phases of Compiler
All these phases convert the source code by dividing into
tokens, creating parse trees, and optimizing the source code
by different phases.
Phase 1: Lexical Analysis
Lexical Analysis is the first phase when compiler scans the
source code. This process can be left to right, character by
character, and group these characters into tokens.
Here, the character stream from the source program is
grouped in meaningful sequences by identifying the tokens.
It makes the entry of the corresponding tickets into the
symbol table and passes that token to next phase.
The primary functions of this phase are:
Example:
x = y + 10
Tokens
X identifier
= Assignment operator
Y identifier
+ Addition operator
10 Number
Phase 2: Syntax Analysis
Syntax analysis is all about discovering structure in code. It
determines whether or not a text follows the expected
format. The main aim of this phase is to make sure that the
source code was written by the programmer is correct or
not.
Syntax analysis is based on the rules based on the specific
programming language by constructing the parse tree with
the help of tokens. It also determines the structure of source
language and grammar or syntax of the language.
Here, is a list of tasks performed in this phase:
Example
Any identifier/number is an expression
If x is an identifier and y+10 is an expression, then x= y+10
is a statement.
Consider parse tree for the following example
(a+b)*c
In Parse Tree
Example
float x = 20.2;
float y = x*30;
In the above code, the semantic analyzer will typecast the
integer 30 to float 30.0 before multiplication
Example
For example,
t1 := int_to_float(5)
t2 := rate * t1
t3 := count + t2
total := t3
Example:
Consider the following code
a = intofloat(10)
b = c * a
d = e + b
f = d
Can become
b =c * 10.0
f = e+b
MOVF a, R1
MULF #60.0, R2
ADDF R1, R2
Summary