3. Lexical Analysis
The first phase of scanner
works as a text scanner.
This phase scans the
source code as a stream of
characters and converts it
into meaningful lexemes.
Lexical analyzer represents
these lexemes in the form of
tokens as:
4. Syntax Analysis
The next phase is called the
syntax analysis or parsing. It
takes the token produced by
lexical analysis as input and
generates a parse tree (or
syntax tree). In this phase,
token arrangements are checked
against the source code
grammar, i.e. the parser checks
if the expression made by the
tokens is syntactically correct
5. Semantic Analysis
Semantic analysis checks
whether the parse tree
constructed follows the rules of
language. For example,
assignment of values is between
compatible data types, and
adding string to an integer. Also,
the semantic analyzer keeps
track of identifiers, their types
and expressions; whether
identifiers are declared before
use or not etc.
6. Intermediate Code generator
Compiler Design. Intermediate Code Generation. In the
analysis-synthesis model of a compiler, the front end of
acompiler translates a source program into an
independentintermediate code, then the back end of the
compiler uses this intermediate code to generate the target
code (which can be understood by the machin
7. Intermediate
Code Optimizer
Compiler Design - Code
Optimization. Optimization is a
program transformation technique,
which tries to improve the code by
making it consume less resources
(i.e. CPU, Memory) and deliver
high speed. In optimization, high-
level general programming
constructs are replaced by very
efficient low-level programming
8. Target Code Generator
Code generation can be considered as the final phase of compilation. Through
post code generation, optimization process can be applied on the code, but that
can be seen as a part of code generation phase itself. The code generated by the
compiler is an object code of some lower-level programming language, for
example, assembly language. We have seen that the source code written in a
higher-level language is transformed into a lower-level language that results in a
lower-level object code, which should have the following minimum properties