System Programming Unit-3
System Programming Unit-3
1. Introduction to compilers:-
A compiler is a program that reads a program written in one language (source language (or)
high level language) and translates it into an equivalent program in another language. (Target
language (or) low level language)
Compiler:- It converts the high level language into an equivalent low level language program.
PHASES OF COMPILER
Source Program
Lexical Analyzer
Syntax Analyzer
Semantic Analyzer
Symbol Table Manager Error Handler
Code Optimizer
Code Generator
Target Program
Analysis Phase:-
The analysis phase breaks up the source program into constituent pieces. The analysis phase
of a compiler performs,
1. Lexical analysis
2. Syntax Analysis
3. Semantic Analysis
24
1.Lexical Analysis (or) Linear Analysis (or) Scanning:-
The lexical analysis phase reads the characters in the program and groups them into
tokens that are sequence of characters having a collective meaning.
Such as an Identifier, a Keyboard, a Punctuation, character or a multi character operator like ++.
“ The character sequence forming a token is called lexeme”
For Eg. Pos = init + rate * 60
Lexeme Token Attribute value
rate ID Pointer to symbol table
+ ADD
60 num 60
init ID Pointer to symbol table
Syntax analysis processes the string of descriptors (tokens), synthesized by the lexical
analyzer, to determine the syntactic structure of an input statement. This process is known as
parsing.
ie, Output of the parsing step is a representation of the syntactic structure of a statement.
Example:-
pos = init + rate * 60
=
pos +
init *
rate 60
3. Semantic Analysis:-
The semantic analysis phase checks the source program for semantic errors.
Processing performed by the semantic analysis step can classified into
a. Processing of declarative statements
b. Processing of executable statements
During semantic processing of declarative statements items of information are added to the
lexical tables.
Example:- (symbol table or lexical table)
real a, b;
id A real length ……
id B real length …..
25
Synthesis Phase:-
1. Intermediate code generation
2. Code optimization
3. Code Generator
Example:-
pos = init + rate * 60
pos = init + rate * int to real (60)
id1 +
id2 *
id3 60
2. Code Optimization:-
The code optimization phase attempts to improve the intermediate code, so that faster
running machine code will result.
3. Code Generation:-
The final phase of the compiler is the generation of the target code or machine code or
assembly code.
Memory locations are selected for each of the variables used by the program. Then intermediate
instructions are translated into a sequence of machine instructions that perform the same task.
Example:-
MOV F id3, R2
MUL F #60.0, R2
MOV F id2, R1
ADD F R2, R1
MOV F R1,id1
26
Translation of a statement pos = init + rate * 60
Lexical Analyzer
Syntax Analyzer
id1 +
id2 *
id3 60
Semantic Analyzer
id1 +
id2 *
Code Optimizer
Code Generator
MOV F id3, R2
MUL F #60.0, R2
MOV F id2, R1
ADD F R2, R1
MOV F R1,id1
27
Role of Lexical Analyzer:-
The main task is to read the input characters and produce as output a sequence of
tokens that the parser uses for syntax analysis.
Source Token
Program Lexical Analyzer Parser
Get next Token
Symbol Table
After receiving a “get next token” command from the parser, the lexical analyzer
reads input characters until it can identify a next token.
Token:-
Token is a sequence of characters that can be treated as a single logical entity. Typical
tokens are,
(a) Identifiers
(b) Keywords
(c) Operators
(d) Special symbols
(e) Constants
Pattern:-
A set of strings in the input for which the same token is produced as output, this set of
strings is called pattern.
Lexeme:-
A lexeme is a sequence of characters in the source program that is matched by the
pattern for a token.
Finite Automata
Definition:-
A recognizer for a language is a program that takes as input a string x and answers
“yes” if x is a sentence of the language and “no” otherwise.
A better way to convert a regular expression to a recognizer is to construct a
generalized transition diagram from the expression. This diagram is called finite automation.
28
1. Non – deterministic Finite Automata:- [ NFA]
A NFA is a mathematical model that consists of,
1. a set of states S
2. a set of input symbol Σ
3. a transition function δ
4. a state S0 that is distinguished as start state
5. a set of states F distinguished as accepting state. It is indicated by double circle.
Example:-
The transition graph for an NFA that recognizes the language (a/b)* a
start a
0 1
Input Symbol
State
a b
0 0,1 0
1 - -
PROBLEM:-
r5
r4 *
( r3 )
r1 / r2
a b
29
For r1 construct NFA start a
2 3
For r1 construct NFA start b
2 3
a
ε 2 3 ε
NFA for r3 = r1/r2 start 6
1 ε ε
b
4 5
NFA for r4, that is (r3) is the same as that for r3.
30
2. Construct a non – deterministic finite automata for a regular expression (a/b)*abb
Solution;-
r = (a/b)*
Decomposition of (a/b)* abb (parse tree)
r11
r9 r10
r7 r8 b
r5 r6 b
r4 * a
( r3 )
r1 / r2
a b
a
ε 2 3 ε
NFA for r3 = r1/r2 start 6
1 ε ε
b
4 5
NFA for r4, that is (r3) is the same as that for r3.
31
NFA for r6=a start a
7
8
a
3
ε
2
start ε ε ε ε a 8
0 1 ε ε 6 7
b
4 5
ε
NFA for r8 = b
start b
8
9
a
3 ε
2
start ε ε ε 7 a 8
b 9 b 1
0
0 1 ε ε 6
b
4 5
ε
32
CONVERSION OF NFA INTO DFA
Since A is the start state and state C is the only accepting state then, the transition table is,
Input symbol
State
a b
A B C
B B C
C B C
a a b
Start a b
A B C
33
2. Convert the NFA (a/b)*abb into DFA?
Solution:
The NFA for (a/b)*abb is,
a
3 ε
2
start ε ε ε 7 a 8
b 9 b 1
0
0 1 ε ε 6
b
4 5
ε
Since A is the start state and state E is the only accepting state then, the transition table is,
Input symbol
State
a b
A B C
B B D
C B C
D B E
E B C
34
b
C
b
b a
a
start A a B c D b E
a
a
MINIMIZATION OF STATES
Problem 1: Construct a minimum state DFA for a regular expression (a/b)* abb
Solution:-
1. The NFA of (a/b)*abb is
a
3 ε
2
start ε ε ε 7 a 8
b 9 b 1
0
0 1 ε ε 6
b
4 5
ε
2. Construct a DFA:
Since A is the start state and state E is the only accepting state then, the transition table is,
Input symbol
State
a b
A B C
B B D
C B C
D B E
E B C
Let Π = ABCDE
The initial partition Π consists of two groups.
Π1 = ABCD ( that is the non – accepting states)
Π2 = E ( that is the accepting state)
AB
a a
A B B B
b b
A C B D
AC
a a
A B C B
b b
A C C C
36
AD
a a
A B D B
b b
A C D E
On input “a” each of these states has a transition to B, so they could all remain in one group as
far as input a is concerned.
On input “b” A,B,C go to members of the group Π1 (ABCD) while D goes to Π2 (E) . Thus Π1
group is split into two new groups.
Π1 = ABC Π2 = D , Π 3 = E
So, (ABC) (D) (E)
AB
a a
A B B B
b b
A C B D
Here B goes to Π2. Thus Π1 group is again split into two new groups. The new groups are,
Π1 = AC Π2 = B , Π3 = D, Π4 = E
So, (AC) (B) (D) (E)
Here we cannot split any of the groups consisting of the single state. The only possibility is try to
split only (AC)
For AC
a a
A B C B
b b
A C C C
But A and C go the same state B on input a, and they go to the same state C on input b.
Hence after this,
(AC) (B) (D) (E)
Here we choose A as the representative for the group AC.
Thus A is the start state and state E is the only accepting state.
37
So the minimized transition table is,
Input symbol
State
a b
A B A
B B D
D B E
E B A
b
b
a
start A a B b D b E
a
a
_____________________________________________________________________________
_
SYNTAX ANALYSIS
Notational Conventions:-
38
PARSER:
A parser for a grammar G is a program that takes a string W as input and produces either a
parse tree for W, if W is a sentence of G or an error message indicating that W is not a sentence
of G as output.
1. Bottom up Parser:-
The bottom up parser build parse trees from bottom (leaves) to the top (root). The input
to the parser is being scanned from left to right, one symbol at a time. This is also called as
“Shift Reduce Parsing” because it consist of shifting input symbols onto a stack until the right
side of a production appears on top of the stack.
id + * $
Id - > > >
+ < > < >
* < > > >
$ < < < -
+ - * / id ( ) $
+ > > < < < < < > >
- > > < < < < < > >
* > > > > < < < > >
/ > > > > < < < > >
> > > > < < < > >
id > > > > > - - > >
( < < < < < < < = -
) > > > > > - - > >
$ < < < < < < < - -
Derivations:-
The central idea is that a production is treated as a rewriting rule in which the non-
terminal in the left side is replaced by the string on the right side of the production.
For Ex, consider the following grammar for arithmetic expression,
EE+E | E*E | (E) | -E |id
That is we can replace a single E by –E. we describe this action by writing
E => -E , which is read “E derives –E”
E(E) tells us that we could also replace by (E).
So, E*E => (E) * E or E*E => E* (E)
We can take a single E and repeatedly apply production in any order to obtain sequence of
replacements.
E => -E
E => -(E)
E => -(id)
We call such sequence of replacements is called derivation.
40
Ex.
E => -E E
- E
E => -(E) E
- E
( E )
E => -(E+E) E
- E
( E )
E + E
E => -(id+E) E
- E
( E )
E + E
id
E => -(id+E) E
- E
( E )
E + E
id id
A compiler while translating a source program into a functionally equivalent object code
representation may first generate an intermediate representation.
Advantages of generating intermediate representation
1. Ease of conversion from the source program to the intermediate code
2. Ease with which subsequent processing can be performed from the intermediate code
41
Parser Parse Tree Intermediate code Intermediate Code Code
Generator Generator
INTERMEDIATE LANGUAGES:
There are three kinds of Intermediate representation. They are,
1. Syntax Trees
2. Postfix Notation
3. Three address code
1. Syntax Tree:-
A syntax tree depicts the natural hierarchical structure of a source program. A DAG
(Direct Acyclic Graph) gives the same information but in a more compact way because common
sub expressions are identified.
A syntax tree and dag for the assignment statement a:= b* -c + b* -c
assign
a +
Syntax Tree
* *
b uminus b uminus
c c
assign
a +
DAG
b uminus
c
2. Postfix notation:-
Post fix notation is a linearized representation of a syntax tree. It is a list of nodes of the
tree in which a node appears immediately after its children.
The postfix notation for the syntax tree is,
a b c uminus * b c uminus * + assign
3. Three Address Code:-
Three Address code is a sequence of statements of the general form
x := y op z
where x,y and z are names, constants or compiler generated temporaries.
42
op stands for any operator such as a fixed or floating point arithmetic operator or a logical
operator on a Boolean valued data.
The Three Address Code for the source language expression like x+y*z is,
t1:= y * z
t2 := x + t1
Where t1 and t2 are compiler generated temporary names
So, three address code is a linearized representation of a syntax tree or a dag in which explicit
names correspond to the interior nodes of the graph.
Three Address Code Corresponding to the syntax tree and DAG is,
Code for Syntax Tree
t1 := -c
t2 := b * t1
t3 := -c
t4 := b * t3
t5 := t2 + t4
a := t5
Code for DAG
t1 := -c
t2 := b * t1
t5 := t2 + t2
a := t5
Types of Three Address Statements:-
1. Assignment statement of the form x := y op z
2. Assignment instructions of the form x := op z
where op is a unary operation.
3. Copy statements of the form x := y
where, the value of y is assigned to x.
4. The Unconditional Jump GOTO L
5. Conditional Jumps such as if x relop y goto l
6. param x and call p, n for procedure calls and return y.
7. Indexed assignments of the form x := y[i] and x[i] := y
8. Address and pointer assignments, x :=&y, x := *y and *x := y
43