Introduction To Compiler
Introduction To Compiler
Introduction to compiler
COMPILER
Compiler is a translator program that translates a program written in (HLL) the source
program and translate it into an equivalent program in (MLL) the target program. As an important
part of a compiler is error showing to the programmer.
Error
Executing a program written n HLL programming language is basically of two parts. the source program
must first be compiled translated into a object program. Then the results object program is loaded into a
memory executed.
1.5 INTERPRETER: An interpreter is a program that appears to execute a source program as if it were
machine language.
Languages such as BASIC, SNOBOL, LISP can be translated using interpreters. JAVA also uses interpreter.
The process of interpretation can be carried out in following phases. 1. Lexical analysis
2. Synatx analysis
3. Semantic analysis
4. Direct Execution
Advantages:
Modification of user program can be easily made and implemented as execution proceeds.
Type of object that denotes a various may change dynamically.
Debugging a program and finding errors is simplified task for a program used for interpretation.
The interpreter for the language makes it machine independent.
Disadvantages:
“A loader is a program that places programs into memory and prepares them for execution.”
It would be more efficient if subroutines could be translated into object form the loader
1.6 TRANSLATOR
A translator is a program that takes as input a program written in one language and produces
as output a program in another language. Beside program translation, the translator performs
another very important role, the error-detection. Any violation of d HLL specification would be
detected and reported to the programmers. Important role of translator are:
INTERPRETOR
COMPILER
PREPROSSESSOR
Phases of a compiler: A compiler operates in phases. A phase is a logically interrelated operation that
takes source program in one representation and produces output in another representation. The phases
of a compiler are shown in below There are two phases of compilation.
a. Analysis (Machine Independent/Language Dependent)
b. Synthesis(Machine Dependent/Language independent)
Compilation process is partitioned into no-of-sub processes called ‘phases’.
Lexical Analysis:-
Definition
Grouping stream of characters, read from the source program in
left-to-right fashion, to form meaningful sequences/components called
lexeme . This phase is also sometimes called scanning.
Example
For the following assignment statement
Lexical analysis is also responsible to generate output for the scanned lexemes called tokens that is
passed to the subsequent phase i.e., syntax analysis. These tokens normally takes the following form.
htokenName, attributeValuei
where tokenName is the abstract symbol to represent the lexeme, and tokenValue is token’s position in the
symbol table.
Symbol table
Basic action
1. Tokenaziation
2. Remove comment
Syntax analyzer
The second phase of compiler that is responsible to check the syntax/grammar of the token stream generated by
lexical analysis and create a grammatical structure in tree-like representation called syntax tree.
In a syntax tree:
The syntax tree of our assignment statement example is given bellow that is generated by the token stream of
lexical analysis:
The tree above have an interior node labeled ‘∗’ with left child as hid, 3i and a right child hid, 4i.
id, 3i represents the identifier quantity and hid, 4i represents the identifier rate.
This node also makes it explicit that we must first multiply quantity with rate.
Then the node labeled ‘+’ indicates that we must add the result of multiplication to the tax.
Finally, the node labeled ‘=’ indicates that we must store the result of addition into the location for
identifier total.
This ordering is similar to the arithmetic order of operation, where multiplication have higher precedence than
addition.
This grammatical structure is then used by subsequent phases to analyze the source program to generate
the target program
Semantic analyzer
The semantic analysis uses syntax tree generated during syntax analysis and the information in the symbol table
to check the source program for semantics defined in the language definition. It also maintains type information
and store it in either syntax tree or symbol table for further use in the sub-sequent phases.
Type checking is also an important role of this phase where it is checked that each operator have matching
operands.
Some language specifications may allow type conversions automatically also called coercions.
Main purpose
1. Type checking
Int +float
The intermediate code generation uses the structure produced by the syntax analyzer to create a stream
of simple instructions. Many styles of intermediate code are possible. One common style uses instruction
with one operator and a small number of operands.
The output of the syntax analyzer is some representation of a parse tree. the intermediate code
generation phase transforms this parse tree into an intermediate language representation of the source
program.
Code optimizer:
This is optional phase described to improve the intermediate
code so that the output runs faster and takes less space. Its
output is another intermediate code program that does the
some job as the original, but in a way that saves time and / or
spaces.
Code Optimizer
Code Generator
MOVF id4, r2
Lexical Analyzer
Syntsx Analyzer
id1 +
id2 *
id3 id4
Semantic Analyzer
id1 +
id2 *
id3 60
Code Optimizer
Code Generator
MOVF id3, r2
MULF *60.0, r2
MOVF id2, r2
ADDF r2, r1
MOVF r1, id1