[go: up one dir, main page]

0% found this document useful (0 votes)
266 views6 pages

Phases of A Compiler

The document discusses the phases of a compiler. It is divided into two main phases: the analysis phase and synthesis phase. The analysis phase creates an intermediate representation from the source code. The synthesis phase creates a target program from the intermediate representation. The analysis phase is further divided into three sub-phases: linear analysis, hierarchical analysis, and semantic analysis. The compiler also has two main modules - the front-end and back-end. The front-end includes lexical analysis, syntax analysis, semantic analysis, and intermediate code generation. The back-end takes the intermediate code and generates the target machine code.

Uploaded by

Kashif Kashif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
266 views6 pages

Phases of A Compiler

The document discusses the phases of a compiler. It is divided into two main phases: the analysis phase and synthesis phase. The analysis phase creates an intermediate representation from the source code. The synthesis phase creates a target program from the intermediate representation. The analysis phase is further divided into three sub-phases: linear analysis, hierarchical analysis, and semantic analysis. The compiler also has two main modules - the front-end and back-end. The front-end includes lexical analysis, syntax analysis, semantic analysis, and intermediate code generation. The back-end takes the intermediate code and generates the target machine code.

Uploaded by

Kashif Kashif
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Phases of a Compiler

 The compilation process is a sequence of various phases.


 Each phase takes input from its previous stage, has its own
representation of source program, and feeds its output to the next
phase of the compiler.

We basically have two phases of compilers, namely Analysis phase and


Synthesis phase.
 Analysis phase creates an intermediate representation from the given
source code.
 Synthesis phase creates an equivalent target program from the
intermediate representation.
Symbol Table
 It is a data structure being used and maintained by the compiler throughout
all the phases of a compiler.
 All the identifier's names along with their types are stored here. 
 It helps the compiler to function smoothly by finding the identifiers record
quickly.
 The symbol table is also used for scope management.

The analysis of a source program is divided into mainly three phases. They are:

1. Linear Analysis-
This involves a scanning phase where the stream of characters are
read from left to right. It is then grouped into various tokens having a
collective meaning.
2. Hierarchical Analysis-
In this analysis phase,based on a collective meaning, the tokens are
categorized hierarchically into nested groups.
3. Semantic Analysis-
This phase is used to check whether the components of the source
program are meaningful or not.

The compiler has two modules namely front end and back end.
 Front-end constitutes of the Lexical analyzer, semantic analyzer, syntax
analyzer and intermediate code generator.
 And the rest are assembled to form the back end.

1. Lexical Analyzer 
 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:
<token-name, attribute-value>

 Each lexeme corresponds to a token. (Tokens are defined by


regular expressions which are understood by the lexical
analyzer)
 It also removes lexical errors (for e.g., erroneous characters),
comments and white space.
2. Syntax Analyzer 

 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 (grammatically\
syntactically correct)
 It takes all the tokens one by one and uses Context Free
Grammar to construct the parse tree (The parse tree is also
called the derivation tree).

Parse trees are generally constructed to check for ambiguity in the


given grammar. There are certain rules associated with the derivation
tree.
 Any identifier is an expression
 Any number can be called an expression
 Performing any operations in the given expression will
always result in an expression. For example, the sum of two
expressions is also an expression.
 The parse tree can be compressed to form a syntax tree
Syntax error can be detected at this level if the input is not in
accordance with the grammar.
3. Semantic Analyzer
 It verifies the parse tree, whether it’s meaningful or not /
follows the rules of language.
 The semantic analyzer keeps track of identifiers, their types and
expressions; whether identifiers are declared before use or not
etc. 
 It also does type checking, Label checking and Flow control
checking.
 The semantic analyzer produces an annotated syntax tree as
an output.

4. Intermediate Code Generator 


 It generates intermediate code, that is a form which can be readily
executed by machine
 It represents a program for some abstract machine.
 It is in between the high-level language and the machine language.
 This intermediate code should be generated in such a way that it makes it easier
to be translated into the target machine code.

We have many popular intermediate codes. Example – Three


address code etc.
 Intermediate code is converted to machine language using the last
two phases which are platform dependent.
 Till intermediate code, it is same for every compiler out there, but
after that, it depends on the platform.
 To build a new compiler we don’t need to build it from scratch. We
can take the intermediate code from the already existing compiler
and build the last two parts

5. Machine In-Dependent Code Optimizer 


 Optimization can be assumed as something that removes unnecessary code
lines, and arranges the sequence of statements in order to speed up the
program execution without wasting resources (CPU, memory).
 It transforms the code so that it consumes fewer resources and
produces more speed.
 The meaning of the code being transformed is not altered.
 Optimization can be categorized into two types: machine
dependent and machine independent.

6. Target Code Generator


 The main purpose of Target Code generator is to write a code that
the machine can understand and also register allocation, instruction
selection etc.
 In this phase, the code generator takes the optimized representation of the
intermediate code and maps it to the target machine language.

7. Machine Dependent Code Optimizer


 The output is dependent on the type of assembler.
 This is the final stage of compilation.
 The optimized code is converted into relocatable machine code
which then forms the input to the linker and loader.

 Note
All these six phases are associated with the symbol table
manager and error handler as shown in the above block
diagram.

You might also like