Sie sind auf Seite 1von 10

PUNJAB UNIVERSITY COLLEGE OF INFORMATION

TECHNOLOGY
University of the Punjab

Sheet No.:

Invigilator Sign:

Date: 14-12-2012

BSCS Fall 2009


Mid Term Examination–December 2012
Student ID: Student Name:

Section: Morning / Afternoon Student Signature:

Course Code: CS 420 Course Title: Compiler Construction


Maximum Time: 90 Minutes Maximum Marks: 185

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

Examiner Signature: Marks Out of 185:

DO NOT OPEN UNTIL YOU ARE TOLD TO DO SO

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

Mark each question as true (T) or false (F).


Marks: 25

1. The input of lexical analyzer is source program.


2. The output of lexical analyzer is a tree.
3. The input of intermediate code optimizer is an intermediate representation.
4. The input of semantic analyzer is a list of tokens.
5. Usually a program using JIT executes faster than a compiled program
6. Regular languages are more powerful than languages expressed using NFA.
7. All languages expressed using NFAs can also be expressed using DFAs.
8. LL(k) parsers are used for bottom up parsing.
9. LR(k) parsers read input from left to right.
10. A NFA can have many start states.
11. A CFG is more powerful (i.e. can express more languages) than a Regular language.
12. LL(k) parsers can accept more languages than LR(k) parsers.
13. LR(k) parsers can use CFG with left recursion.
14. LR(k) parsers can use ambiguous grammars.
15. DFA can have multiple final states.
16. DFA transition table will never have a null/empty entry.
17. Java bytecode is language dependent but machine independent.
18. Interpreter is usually easier to port on a new machine as compared to a compiler.
19. Each NFA can be expressed using a CFG
20. Context sensitive grammars are less powerful than context free grammars.
21. Recursive descent parsing is used for top down parsing.
22. LL(k) parser can be based on either rightmost or leftmost derivations.
23. The Java code a = = b + c has an error that a lexical analyzer cannot find.
24. The Java code a = = b + c has an error that syntax analyzer cannot find.
25. Compiler's Front-end is machine dependent.

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) =

Q 6: Create LL(1) parsing table for the CFG of Q5.


Marks 15
a b ( ) [ ] , $
E
T
L
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)

Stack Input Pointer (remove input


characters as pointer moves ahead)
1 $ (id+id)
2 $E (id+id)
3 $E'T (id+id)
4 $E'T'F (id+id)
5
6

Page 6 of 10
7
8
9
10
11

Q8: Remove indirect left recursion from the following CFG.


Marks 15
A → Ba| b
B → Cd | e
C → Df | g
D → Df | Aa | Cg

Q9: Transform the following grammar by apply left factoring.


Marks 10
Z→X
X → Y aec | Y aed | f
Y→a|b

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