Upload
kamar
View
54
Download
1
Embed Size (px)
Citation preview
Silicon Institute of TechnologyClass Test – I (7th Sem. B. Tech- CS & IT), 2010
Sub : Compiler Design
Time – 60 mins. Max. Marks – 10 Date : 11.08.10
(Answer any Four including Q.1)
Q.1. Short type (any Five) [0.5 x 5 = 2.5]
a) What is a complier and how it is different from an interpreter?
b) Define regular expression.
c) Differentiate DFA & NFA.
d) Differentiate left sentential form and right sentential form of a string.
e) What is a cross compiler and its advantage?
Q.2. Consider the following while statement : [2.5]
While A > B && A <= 2 * B – 5 do
A = A + B
Identify the Tokens. Generate the Parse tree, intermediate code and optimized
code.
Q.3. Show that the following grammar is ambiguous. [2.5]
E ® E+E | E*E | (E) | id
Q.4. Using Thomson Construction Rule construct Î-NFA for the following regular
expressions. [2.5]
a) (a | b)* b) ab (a | b)* c) (a* | b*)* a d) (a | b)* a (a
| b)+ e) (a | b)+ abb
Q5. Give an equivalent DFA for the regular expression (a|b)*abb.
[2.5]
Good Luck
Solutionto
Sub – Compiler DesignClass Test – I, 2010
1)a) Compiler and Interpreter both are translators for high level languages. But they
have differences which are given below.
Compiler
i. Compiler translates the whole source code at a time and produces a list of errors.
ii. After removal of all errors the compiler produces the object code for the given source code.
iii. Compiler is faster.iv. Compiler generates an object
code of the source code.v. The generate d object code is
stored in permanent storage device
vi. The object code is used for execution of the program without any need of the source code.
Interpreter
i. Interpreter translates the source code line -by-line and halts when an error is found.
ii. After remova l of the error the interpreter moves with the translation of the next line of the source code.
iii. Interpreter is slower.iv. Interpreter generates an
executable code of the source code.
v. The generated executable code is stored in temporary storage device i.e. primary memory.
vi. The source code is always needed for execution of the program as the executable code is lost after the execution.
b) Regular Expression:Regular expressions over åcan be defined recursively as follows:
1. Any terminal symbol (i.e. an element of å), L and f are regular expressions.
2. The union of two regular expressions R1 and R2, written as R1 + R2, is also a regular expression.
3. The concatenation of two regular expressions R 1 and R2, written as R1 R2, is also a regular expression.
4. The iteration (or closure) of a regular expression R, written as R *, is also a regular expression.
5. If R is a regular expression, then (R) is also a regular expression.6. A recursive application of the rules 1 -5 once or several times results into a
regular expression.
c) Non-deterministic Finite Automaton (NFA):
A non-deterministic finite automaton is a 5-tuple (Q, å, d, q0, F), wherei) Q is a finite nonempty set of states;ii) å is a finite nonempty set of inputs;
iii) d is the transition function mapping from Q x å into 2Q which is the power set of Q, the set of all subsets of Q;
iv) q0 Î Q is the initial state; andv) F Í Q is the set of final states.
Deterministic Finite Automaton (DFA):
A deterministic finite automaton is a 5-tuple (Q, å, d, q0, F), wherei) Q is a finite nonempty set of states;ii) å is a finite nonempty set of inputs;
iii) d is the transition function which maps Q x å into Q;iv) q0 Î Q is the initial state; andv) F Í Q is the set of final states.
DFA:a) In DFA when an input is
given to a state it transits to a single state.
b) DFA can be i mplemented in a system.
NFA:a) In NFA when an input is
given to a state it may transitto multiple states.
b) NFA can not be implemented in a system.
d) If a Þ b by a step in which the leftmost non-terminal in a is replaced, we write a Þ b. Every leftmost step, has the form wAg Þ wdg in which w consists of terminals only. If a derives b by a leftmost derivation, we write a Þ b. If S Þ a, then we say a is a left sentential form of the given grammar.
Similarly, If a Þ b by a step in which the rightmost non -terminal in a is replaced, we write a Þ b. Every rightmost step, has the form gAw Þ gdw in which w consists of terminals only. If a derives b by a rightmost derivation, we write a Þ b. If S Þ a, then we say a is a right sentential form of the given grammar.
For example: A given grammar is :S ® i C t SS ® i C t S e SS ® aC ® bAnd a string w = i b t i b t a e a can be derived using leftmost deriva tion as follows:
lm lm
lm*
lm*
rm rm
rm*
rm*
S Þ i C t S
Þ i b t S
Þ i b t i C t S e S
Þ i b t i b t S e S
Þ i b t i b t a e S
Þ i b t i b t a e a
It is in the form of S Þ a1 Þ a2 Þ × × × Þ an = w. Here all ai, 1≤ i ≤ n, is a left -sentential
form of the given string.Similarly, the rightmost derivation of the string is as follows:
S Þ i C t S
Þ i C t i C t S e S
Þ i C t i C t S e a
Þ i C t i C t a e a
Þ i C t i b t a e a
Þ i b t i b t a e a
It is in the form of S Þ a1 Þ a2 Þ × × × Þ an = w. Here all ai, 1≤ i ≤ n, is a right -sentential
form of the given string. The rightmost derivation is also known as canonical derivation.
e) A compiler is characterized by three languages: its source language, its object language, and the language in which it is written. These languages may all be quite different.
A compiler may run on one machine and produce object code for another machine. Such a compiler is often called a cross-compiler.
Suppose a new language L is made available to two different machines A
and B. As a first step we may write for machine A, a small compilerCSA
A, that
translates a subset S of language L into the machine or assembly code of A.
lm
lm
lm
lm
lm
lm
lm lm lm lm
rm
rm
rm
rm
rm
rm
rm rm rm rm
Then we write a compiler CLA
S in the simple language S. This program,
when run throughCSA
A, becomesC
LA
A, the compiler for the complete language
L, running on machine A and producing object code for A.
CLA
S® ® C
LA
A
Now suppose we want to have another compiler for L to run on machine B
and to produce code for B. If CLA
S has been designed carefully and machine B
is not that different from machine A, then with little modification we can
convert CLA
Sinto a compiler C
LB
L which produces object code for B.
So, using CLB
Lto produceC
LB
B, a compiler for L on B, is a two step
process which is given below. Here we first run CLB
Lthrough C
LA
A to
produceCLB
A, a cross -compiler for L which runs on machine A but produces
code for machine B. Then we run CLB
Lthrough this cross -compiler to produce
the desired compiler for L t hat runs on machine B and produces object code for B.
CLB
L® ® C
LB
A
CLB
L® ® C
LB
B
Hence, we can see a cross -compiler helps u s to create a compiler for another machine without starting from the scratch. It helps a lot in the form of reduction of amount of coding for a new compiler.
2) The given while statement is while A > B & A ≤ 2*B – 5 do
A := A + B;The list of tokens present in the given string are:
while [id, n1] > [id, n2] & [id, n1] ≤ [const, n3] * [id, n2] – [const, n4] do [id, n1] ¬ [id, n1] + [id, n2];
Here n1, n2, n3 and n4 stand for pointers to the symbol table entries for A, B, 2, and 5, respectively.
The parse tree for the given statement is given below.
CSA
A
CLA
A
CLB
A
statement
while-statement
while condition do statement
condition condition&
relation
exp relop exp
relation
exp exprelop
exp exp-
assignment
location ¬ exp
exp exp+
id(A) id(B) id(A)
id(B)
id(A)
exp exp*
const(2)
> ≤
const(5)
id(B)
id(A)
The intermediate code for the given string is as follows:
L1: if A > B goto L2goto L3
L2: T1 := 2 * BT2 := T1 – 5if A ≤ T2 goto L4goto L3
L4: A := A + Bgoto L1
L3:
In an attempt to code improvement we can have local transformations. Here we are having two instances of jumps over jumps in the intermediate code.
if A > B goto L2goto L3
L2:This sequence of code can be replaced by the single statement
if A ≤ B goto L3.
By applying such replacement the optimized code will be as follows
L1: if A ≤ B goto L2T1 := 2 * BT2 := T1 – 5if A > T2 goto L2A := A + Bgoto L1
L2:
3) The given grammar is E ® E + E | E * E | (E) | id
A given grammar is said to be ambiguous if it produces more than one parse tree for some sentence. In other words, if a string is derivable in more than one ways using a given grammar, then the given grammar is said to be ambiguous.
Let’s have a sentence like id + id * id.We can derive the above string in two ways, which is given below:E Þ E + E
Þ id + EÞ id + E * EÞ id + id * EÞ id + id * id. And the other way is
E Þ E * EÞ E + E * EÞ id + E * EÞ id + id * EÞ id + id * id.
For the above two derivations we can have two different parse trees, which are shown below.
Hence the given grammar is ambiguous.
E
E + E
E * E
idd
idd
idd
E
E * E
idd
E + E
idd
idd
4)a) The regular expression is (a | b)*
The Î-NFA for the given regular expression is as below:
b) The regular expression is ab(a | b)*.The Î-NFA for the given regular expression is as below:
q1
q2
q4
q3
q5
q6q0 q7
a
Î
Î
ÎÎ
Î
b
Î
Î
Î
q1q0a b q3
q4
q6
q5
q7
q8q2q9
a
Î
Î
ÎÎ
Î
b
Î
Î
Î
c) The regular expression is (a* | b*)*aThe Î-NFA for the given regular expression is as below:
q0
q2 q6
q1
q9
q8
q7
q5
q4
q3
q10
q11
q12
a
a
bÎ
Î
Î
Î
Î
Î
Î Î
Î
ÎÎ
Î
ÎÎ
Î
Î
d) The regular expression is (a | b)*a (a | b)+
The Î-NFA for the given regular expression is as below:
q0
q2 q4
q1
q9
q7
q5
q6
q3
q8
q13
a
b
Î
Î
Î
a
a
Î
Î Î
Î
Î
Î
Î Î
Î
b
Î
q10
q11
q12
q17
q16
q15
q14
a b
q19
q18
Î
Î
Î
Î Î
Î Î
q20
e) The regular expression is (a | b)+abb.The Î-NFA for the given regular expression is as below:
q1 q3
q0
q8
q6
q4
q5
q2
q7
q12
a
bÎ
Î
a
a
Î
Î Î
Î Î
b
q9
q10
q11
q14q13b
Î
Î Î
Î Î
q15b
5) The given regular expression is (a | b)*abb. The Î-NFA for the given regular expression is given below:
Here the Î-closure of {q0}={q0, q1, q2, q4, q7}= A. Here all the states in A are equivalent. Now applying ‘a’ as the input to the states in A, we get {q3, q8}. Now to get all the equivalent states of {q3, q8}, we compute the Î-closure of {q3, q8}.Î-closure {q3, q8} = {q1, q2, q3, q4, q6, q7, q8} = B.Applying ‘b’ as the input to the states in A, we get {q5}.Now to get all the equivalent states of {q5}, we compute the Î-closure of {q5}.Î-closure {q5} = {q1, q2, q4, q5, q6, q7} = C.Similarly, applying ‘a’ to B, we get, {q3, q8}.Î-closure {q3, q8} = B.Applying ‘b’ to B, we get, {q5, q9}.Î-closure {q5, q9} = {q1, q2, q4, q5, q6, q7, q9} = D.Now applying ‘a’ to C, we get, {q3, q8}.Î-closure {q3, q8} = B.Applying ‘b’ to C, we get, {q5}.Î-closure {q5} = C.Now applying ‘a’ to D, we get, {q3, q8}.Î-closure {q3, q8} = B.Applying ‘b’ to D, we get, {q5, q10}.Î-closure {q5, q10} = {q1, q2, q4, q5, q6, q7, q10} = E.Now applying ‘a’ to E, we get, {q3, q8}.Î-closure {q3, q8} = B.Applying ‘b’ to E, we get, {q5}.Î-closure {q5} = C.Now the transition table for the DFA is shown below
q7q1
q2
q4
q3
q5
q6q0
a
Î
Î
ÎÎ
Î
b
Î
Î
Î
q8
q9
q10
a
b
b
State Input a b
® A B CB B DC B CD B EE* B C
Now let us minimize the above DFA to get an equivalent minimized DFA.Here the 0-equivalent classes will be given by Q1
0={E}, Q20={A, B, C, D}, which is a
set of all final states and a set of all non-final states.Hence the set of 0-equivalent classes is given byP0={ Q1
0, Q20}.
Now for 1-equivalent classes we got,Q1
1={E}, Q21={A, B, C}, Q3
1={D}.Hence the set of 1-equivalent classes is given byP1={ Q1
1, Q21, Q3
1}, where Q11, Q2
1, Q31 are given above.
Now for 2-equivalent classes we got,Q1
2={E}, Q22={A, C}, Q3
2={B}, Q42={D}.
Hence the set of 2-equivalent classes is given byP2={ Q1
2, Q22, Q3
2, Q42}, where Q1
2, Q22, Q3
2, Q42 are given above.
Now for 3-equivalent classes we got,Q1
3={E}, Q23={A, C}, Q3
3={B}, Q43={D}.
Hence the set of 3-equivalent classes is given byP3={ Q1
3, Q23, Q3
3, Q43}, where Q1
3, Q23, Q3
3, Q43 are given above.
We can see that P2=P3. Hence P2 is the set of equivalence classes.
So, now the transition table for minimized DFA is given below.
State Input a b
® A B AB B DD B EE* B A
The transition diagram for the DFA is shown below.
A B D
E
a
b
b
b
b
aa
a