You are on page 1of 19

MODULE-2

COMPILERS
Translators or Language Processors

A language processor, or translator is a


computer program that translates source code
from one programming language to another.
Ex: Complier, Interpreter, Assembler
Compiler:
A compiler is a program that can read a program in one language the
source language (high level language ) and translate it in to an
equivalent program in another language the target language(assembly
level language or machine language)
An important role of the compiler is to report errors in the source
program that it detects during the translation process
If the target program is a executable machine code it then calls the
user to process inputs and produce outputs
Ex: C, C++, java
Interpreter
• A n i n t e r p r e t e r i s a n o t he r k i nd o f l a n g u a g e p r o c e s s o r
which translates high level language program to machine
language line by line
• The Machine language target program produced by a compiler is
much faster than an interpreter at mapping inputs to outputs.
• It takes less memory than compiler.
• It takes more time than compiler.
• Interpreter gives better error diagnostics than a compiler,because
it executes the source program statement by statement
Ex: python, perl
A Typical Language-Processing System
The Structure of Compiler

• Explain the various phasesof complier


with an example?(10M)
• With the help of diagram explain the various
phases of compiler?
Compiler basically does two things
• Analysis: Compiling source code & detecting errors in it.

• Synthesis: Translating the source code into object


code depending upon the type of machine.

• Program errors are difficult to track if the compiler is wrongly


designed.
• Hence compiler construction is a very tedious and
long process.
Front End- Phase
(also known as Analysis Part)
• Lexical Analysis
• Syntax Analysis
• Semantic Analysis
• Intermediate Code Generation
• * => Front end is machine independent.
Back End -Phases
(also known as Synthesis Part)
• Code Optimization
• Code Generation
• * => Back end is machine dependent.
• Constructs the target code from the syntax tree, and from the
information in the symbol table.
• Here, code optimization offers efficiency of code
generation
with least use of resources.
Symbol table
• The symbol table is a data structure containing a record for
each variable name, with fields for the attributes of the name
(storage allocated for a name, its type, its scope), and for
procedure names (number and types of its arguments, the
method of passing each argument and the type returned ).

• The data structure should be designed to allow the compiler to


find the record for each name quickly and to store or retrieve
data from that record quickly.
Lexical Analysis
• The first phase of a compiler is called lexical analysis or
scanning.
• The LA reads the streams of character from source program
and group the characters into meaningful sequences called
lexemes.
• For each lexemes , the lexical analyser produces as output a
token of form
• <Token-name, attribute-value>
• In the token, 1st component token-name is an abstract symbol
in the programming language, that is used during syntax
analysis .
• In the token ,2nd componets attribute-value points to an entry
in the symbol table for this token.
• Ex: Variable, keyword, delimiters or digits.
• Information in the symbol table entry is need for
semantic analysis and code generation.
• Blanks separating the lexemes would be discarded by
the
lexical analyser.
Ex: Position = initial + rate *
60 After lexical analysis
<id,1><=>
<id,2><+><id,3><*><60>
• Position is a lexeme that would be mapped into token
<id,1>, where id is an abstract symbol standing for identifier
and 1 points to the symbol- table entry for position.
• The symbol table entry for an identifier holds
information about the identifier such as its name and
type.
Syntax Analysis
• The second phase of the compiler is syntax analysis or parsing.
• The parser uses the first components of the tokens produced by
the lexical analyser to create a tree like intermediate
representation that depicts the grammatical structure of the
token stream.
• A typical representation is a syntax tree in which each interior
node represents an operation and children of the node
represents the argument of the operation.

=
<id, 1> +

<id,2> *
<id,3> <60>
Semantic Analysis
• The semantic analyser uses the syntax tree and the information in the
symbol table to check the source program for semantic Consistency
with the language definition.

• It also gathers type information and saves it in either the syntax tree or
the symbol table for subsequent use during intermediate code generation.
• An important part of semantic analysis is type checking , where
the
compiler checks that each operator has matching operands.

Ex: Many programming language definitions require an array index to be an


integer , compiler must report an error if a floating point number is used to
index an array.
Intermediate Code Generation
• In the process of translating a source program into target code, a compiler
may construct one or more intermediate representation which can have a
variety of forms.
• Syntax tree are form of intermediate representation.
• Many compiler generate an explicit low level or machine like intermediate
representation .It should have two important properties.
 It should be easy to produce
 It should be easy to translate into target machine.
• We can write intermediate code using “3-address code”, which consists of
3 operands per instruction. Each operand act like register.
t1 = int to float
(60) t2 = id3*t1
t3 = id2+t2
id1 = t3
Code Optimization
• The machine independent code optimization phase attempts to
improve the intermediate code so that better(faster/ shorter)
target code will be results.
• The optimizer can deduce that the conversion of 60 from
integer to floating point can be done once and for all at
compile time , so the int to float operation can be eliminated
by replacing the integer 60 by the floating point number 60.0.
t1=id3 * 60.0
id1=id2 + t1
• There are simple optimization that significantly improve
the running time of the target program without slowing
compilation too much.
Code Generation
• The code generator takes as input an
representation of the source program and maps it into the
intermediate
target language.
• If the target language is machine code, register or memory
locations are selected for each of the variables used by the
program.
• The intermediate instruction are translated into sequences of
machine instructions that perform the same task.

You might also like