Professional Documents
Culture Documents
BSCS Fall 2009 Mid Term Examination-December 2012: University of The Punjab
BSCS Fall 2009 Mid Term Examination-December 2012: University of The Punjab
BSCS Fall 2009 Mid Term Examination-December 2012: University of The Punjab
TECHNOLOGY
University of the Punjab
Sheet No.:
Invigilator Sign:
Date: 14-12-2012
Instructions
• There is only one instruction: We have zero tolerance to cheating. F grade will be
given to a cheater without any prior warning.
Question # 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Total
Total Marks 25 10 10 10 10 15 15 15 15 10 10 10 10 10 10 185
Obtained
Page 1 of 10
This page is intentionally left blank.
You may use it for any rough work.
Page 2 of 10
Midterm Examination
CS420: Compiler Construction
Instructor: Prof. Dr. Faisal Aslam
Total Marks: 185
Page 3 of 10
Q1: Write a regular expression for all possible strings of 0’s and 1’s with at most single 1. (e.g. 10000, 010000,
0010000 and 000001)
Marks 10
+
Q2: Write a NFA for the regular expression ab? | ((b|a) c)+
Marks 10
Q3: Write transition table of the NFA created in Q2. Show only first five states.
Marks 10
a b c ε
1
2
3
4
5
Q4: Show that the following grammar is ambiguous using the input string aaa. (Hint: you will need to create
two parse trees)
Marks 10
R→ R R
R→R|R
R→R*
R→(R)
R→a
R→b
Page 4 of 10
This page is intentionally left blank.
You may use it for any rough work.
Page 5 of 10
Q5: Find FIRST and FOLLOW of each non-terminal.
E → abT
T → (L)T | [E]T | a | ε
L → ER
R→ , ER |b
Marks 15
FIRST(E) = FOLLOW(E) =
FIRST(T) = FOLLOW(T) =
FIRST(L) = FOLLOW(L) =
FIRST(R) = FOLLOW(R) =
Q7: Given the following parsing table (M) use the LL(1) parsing algorithm to validate the input (id+id). Show
next 7 stack state and input processed at each stage.
Marks 15
id + * ( ) $
E TE' TE'
E' +TE' ε ε
T FT' FT'
T' ε *FT' ε ε
F id (E)
Page 6 of 10
7
8
9
10
11
Page 7 of 10
Q10: Above is a finite automaton for LR(k) parser. Show only next 5 steps to verify the input string (id+id)
using LR(k) parser.
Marks 10
Stack
Step # States Symbols Input Action Handle
1 0 $ (id+id) shift to 4 -
2 04 $( id+id) shift to 5 -
3 045 $(id +id) reduce using 3 id
4
5
6
7
8
Q11: Write CFG for the given NFA.
Marks 10
Page 8 of 10
Q12: Given the following stack of the LR(k) construct parse tree. Also write separately the input and the start
symbol.
LR(k) Stack Trace
x
F
T
T*
T*(
T*(y
T*(F
T*(T
T*(E
T*(E)
T*F
T
E
Marks 1+2+7
Start Symbol:
Input Sentence:
Parse Tree:
Page 9 of 10
Q13: Mark the last five values (nodes) added in the following tree created using recursive descent parsing. No
marks will be given if you have marked more than five values. (For example you can mark "Program, Block,
Var and y")
Marks 10
Q14: Subset construction algorithm is used to create DFA from the NFA. Show next five function and subsets
created using the NFA of Q11.
Marks 10
function subset
1 ε-closure(q0) {q0}
2
3
4
5
6
Page 10 of 10