You are on page 1of 18

www.vujannat.ning.

com

CS410 Visual Programming


Final Term Examination – Spring 2005
Time Allowed: 150 Minutes

Please read the following instructions carefully before


attempting any of the questions:
1. The duration of this examination is 150 Mins.
2. This examination is closed book, closed notes, closed neighbors.
3. Do not ask any questions about the contents of this examination from
anyone.
a. If you think that there is something wrong with any of the
questions, attempt it to the best of your understanding.
b. If you believe that some essential piece of information is
missing, make an appropriate assumption and use it to solve
the problem.
4. Some of the examination consists of multiple-choice questions.
Choose only one choice as your answer.
a. If you believe that two (or more) of the choices are correct for a
particular question, choose the best one.
b. On the other hand, if you believe that all of the choices provided
for a particular question are wrong then select the one that
appears to you as being the least wrong.

**WARNING: Please note that Virtual University takes serious


action against unfair means. Anyone found involved in
cheating will get an `F` grade in this course.

Total Marks: 160 Total Questions: 21

Question No. 1 Marks : 05


When generating a lexical analyzer from a token description, the item sets (states) are
constructed by two types of "moves": character moves and ε-moves. ε-moves do not
consume any input character, but merely expand non-basic items with repetition and
composition operators.

o True
o False

Question No. 2 Marks : 30

Consider the following augmented grammar

(a) Derive the canonical sets of LR( 1) items


(b) Build the LR(1) parse table (Action/Goto)

Question No. 3 Marks : 05

The following two items


{A→ P • P,B → Q • Q}
can coexist in an LR item set.

o True
o False
Question No. 4 Marks : 05

A recursive descent parser consists of a set of (recursive) routines, each routine


corresponding closely to a rule in the grammar.
- an empty production (N → ε) translates to a routine returning true without inspecting any
token.
o True
o False

Question No. 5 Marks : 05

The dependency graph associated with the attribute evaluation rule of some production
rule (N→ α) captures how values flow from one attribute to another, hence, which
attribute depends on which others.
- An inherited attribute can depend on a synthesized attribute.
o True
o False

Question No. 6 Marks : 05

Dotted items (T→ α • β) record which part of a token has already been matched. There are
two kinds of basic items: shift items and reduce items.
integer → (•[0 - 9])+
- This is a reduce item.

o True
o False
Question No. 7 Marks : 05

When dealing with attribute grammars it is important to detect cycles in the evaluation
rules to prevent the evaluator to loop endlessly. In the case of dynamic cycle detection,
the AST is traversed multiple times until either all attributes have obtained a value, or a
maximum number of traversals is reached (in which case a cycle can be reported).
- For an AST with N attributes, cycles can be detected dynamically by performing (at
most) N evaluation traversals.

o True
o False
Question No. 8 Marks : 25

Consider the productions and semantic rules for the expression grammar
a) Write the three-address code that would be generated from the semantic rules
shown for the program fragment (input string)
x:=(a+b)* (c+a)
b) How many temporary variables are used to implement this?

c) The programming language designer has decided to add the pre-increment operator
"++" to the grammar with the production "E →++id". This new operator increments
id, and then returns the value (for example after the code, x=5; y=++x; is
executed then x is 6 and y is 7). Add the semantic rule that will generate code
for this new operator.

Question No. 9 Marks : 05

The regular expressions (a |b) + and a+ |b+ describe the same set of strings

o True
o False
Question No. 10 Marks : 05

In a CFG (Context Free Grammar) the set of terminal and non-terminal symbols may
overlap.

o True
o False
Question No. 11 Marks : 05

The LR(1) parsing technique reduces a handle (the right-hand side of a production N → α)
only when the current input token is an element of FOLLOW(N).

o True
o False
Question No. 12 Marks : 15

Consider the following ACTION/GOTO tables:


Show the contents of the stack and input buffer for the shift-reduce parse of input "a",
assuming state 0 is the start state:

Question No. 12 Marks : 05

Register allocation by graph coloring uses a register interference graph.


- If a node in the graph is connected to N other nodes, then at at least N + 1 registers
(colors) are needed.

o True
o False
Question No. 12 Marks : 05

Grammars with LL(1) conflicts can be made LL(1) by applying left-factoring,


substitution, and left-recursion removal.
- Left-factoring takes care of FIRST/FIRST conflicts.

o True
o False
Question No. 12 Marks : 05

Code generation consists of three tasks:


1. instruction selection
2. register allocation
3. instruction ordering
- For generating "optimal" code these three tasks must be considered at once, because
they dependent on each other.

o True
o False
Question No. 12 Marks : 05
A lexical analyzer generated by lex is essentially a PDA (Push Down Automaton).

o True
o False
Question No. 12 Marks : 05

A lexical analyzer transforms a stream of characters into a stream of tokens.

o True
o False
Question No. 12 Marks : 05

The symbol table is used to pass information only between consecutive stages (lexical
analysis, semantics analysis, etc.) in the compiler pipeline.

o True
o False
Question No. 12 Marks : 05

The stack used in a bottom-up parser contains an alternating sequence of states and
grammar symbols.

o True
o False
Question No. 12 Marks : 05

When generating code at the basic block level, a dependency graph is used instead of an
AST.
- The nodes in the graph represent the basic blocks, and the edges capture the ordering
dependencies that must be obeyed at execution time.

o True
o False
Question No. 12 Marks : 05

A top-down parser creates the nodes in the AST (Abstract Syntax Tree) in preorder.

o True
o False
www.vujannat.ning.com

CS606 Compiler Construction


Mid Term Examination – Spring 2005
Time Allowed: 90 Minutes

Maximum Time Allowed: (1.5 Hour)


Please read the following instructions carefully before
attempting any of the questions:
1. The duration of this examination is 90 Mins.
2. This examination is closed book, closed notes, closed neighbors.
3. Do not ask any questions about the contents of this examination from
anyone.
a. If you think that there is something wrong with any of the
questions, attempt it to the best of your understanding.
b. If you believe that some essential piece of information is
missing, make an appropriate assumption and use it to solve
the problem.
4. Some of the examination consists of multiple-choice questions.
Choose only one choice as your answer.
a. If you believe that two (or more) of the choices are correct for a
particular question, choose the best one.
b. On the other hand, if you believe that all of the choices provided
for a particular question are wrong then select the one that
appears to you as being the least wrong.

**WARNING: Please note that Virtual University takes serious


action against unfair means. Anyone found involved in
cheating will get an `F` grade in this course.

Total Marks: 55 Total Questions: 4

Question No. 1 Marks : 20

Given the following grammar and input string "bool id , id , id"


1. Decl → Type IdList
2. Type → bool | int | float
3. IdList → IdList , id | id

a. show the leftmost derivation of the input string


b. show the parse tree of the input string
c. create a new equivalent grammar that has left recursion removed
d. show the parse tree for the above input string on the new grammar

Question No. 2 Marks : 10

Find the First and Follow sets for the following grammars:

1. Exp → – Exp | (Exp) | Var ExpTail


2. ExpTail → – Exp | ε
3. Var → id VarTail
4. VarTail → (Exp) | ε

Question No. 3 Marks : 15

Consider the following grammar of lists. Note that there are four different terminals: "(",
")", "a", and ","
1. S → ()
2. | a
3. | (A)
4. A → S
5. | A,S

a) Show that the grammar is not LL(1) hint: you should be able to do this by
computing some of the
FIRST sets
b) Rewrite the grammar so that it is suitable for recursive decent parsing
c) If you were going to build a table driven top-down parser for this grammar
from (b), how big
would the table be? (how many entries would it have)

Question No. 4 Marks : 10

Use the subset construction to create a DFA that is equivalent to the following NFA that
uses the alphabet a,b. Show your work.
Note: The states with '*' are final states.
www.vujannat.ning.com

CS606 Compiler Construction


Mid Term Examination – Spring 2006
Time Allowed: 90 Minutes

Please read the following instructions carefully before


attempting any of the questions:
1. The duration of this examination is 90 Minutes.
2. This examination is closed book, closed notes, closed neighbors.
3. Do not ask any questions about the contents of this examination from
anyone.
a. If you think that there is something wrong with any of the
questions, attempt it to the best of your understanding.
b. If you believe that some essential piece of information is
missing, make an appropriate assumption and use it to solve
the problem.
4. Some of the examination consists of multiple-choice questions.
Choose only one choice as your answer.
a. If you believe that two (or more) of the choices are correct for a
particular question, choose the best one.
b. On the other hand, if you believe that all of the choices provided
for a particular question are wrong then select the one that
appears to you as being the least wrong.

**WARNING: Please note that Virtual University takes serious action


against unfair means. Anyone found involved in cheating will get an `F`
grade in this course.

Question No. 1 Marks : 25

Consider the context-free grammar


S→ XS|dS|ε
X→ Y|Zb|aY
Y→ cZ
Z→ e

The symbols S, X, Y and Z are non-terminals with S as the start symbol while a, b, c, d, e are terminal
symbols.
a) Give the FOLLOW and FIRST sets for each non-terminal symbol.
[12 marks]
b) Construct the parsing table for a non-recursive predictive parser for this grammar. [11
marks]
c) Is the grammar LL(1)? [02 marks]

Question No. 2 Marks : 2

A bottom-up parser creates the nodes in the AST in pre-order.

 True
 False
 None of the others

Question No. 3 Marks : 2

Making a grammar with a FIRST/FIRST conflict LL(1) requires the application of left factoring.

 True
 False
 Insufficient Information

Question No. 4 Marks : 2

NFAs are converted to DFAs because the former cannot be implemented directly.

 True
 False
 Insufficient Information

Question No. 5 Marks : 2

S →a |A
A → Aa | a

This grammar is ambiguous.

 True
 False
 None of the others

Question No. 6 Marks : 2

A recursive descent parser consists of a set of (recursive) routines, each routine


corresponding closely to a rule in the grammar.
- an empty production (N→ ε) ‘translates’ to a routine consuming the current input token and
returning true regardless of its (token) value.

 True
 False
 None of the others

Question No. 7 Marks : 10

Use subset construction to build a DFA equivalent to the NFA below. Show your work.
Note: ε is the epsilon symbol.

a b ε
1 {2,5} {1,3} {}
2 {2} {3} {4}
3 {5} {} {}
4 {} {4} {3}
5* {} {} {}

Note: here ‘*’ shows the final state

Question No. 8 Marks : 10

Demonstrate that the following grammar is ambiguous. (HINT: Use parse trees.)
S → id := id ;
S → if (E) then S
S → if (E) then S else S
E → id == id ;
Connecting VU Students
1
FINALTERM EXAMINATION
SPRING 2006 Marks: 75
CS606 - COMPILER CONSTRUCTION (Session - 1 ) Time: 120min

StudentID/LoginID: ______________________________

Student Name: ______________________________

Center Name/Code: ______________________________

Exam Date: Wednesday, August 23, 2006

Please read the following instructions carefully before attempting any


of the questions:
1. This examination is closed book, closed notes, closed neighbors.
2. Do not ask any questions about the contents of this examination from anyone.
a. If you think that there is something wrong with any of the questions,
attempt it to the best of your understanding.
b. If you believe that some essential piece of information is missing, make
an appropriate assumption and use it to solve the problem.
3. Some of the examination consists of multiple-choice questions. Choose only
one choice as your answer.
a. If you believe that two (or more) of the choices are correct for a particular
question, choose the best one.
b. On the other hand, if you believe that all of the choices provided for a
particular question are wrong then select the one that appears to you as
being the least wrong.

**WARNING: Please note that Virtual University takes serious action


against unfair means. Anyone found involved in cheating will get an
`F` grade in this course.

For Teacher's use only


Question 1 2 3 4 5 6 7 8 Total
Marks
Connecting VU Students
2

Question No: 1 ( Marks: 2 ) - Please choose one

Circle the choice that is a regular expression for the set of all strings over the alphabet {a} that has
an even number of a’s.

► aa*

► (aa)*

► aa*a

► a(aa)*

Question No: 2 ( Marks: 2 ) - Please choose one

A shift reduce parser performs reductions in the reverse order specified by a left-most derivation:

► False

► True

► None of these

Question No: 3 ( Marks: 2 ) - Please choose one

Consider the context-free grammar with terminal symbols a, b, c, non-terminal


symbols A and B where A is the start symbol and productions
A →BAB | a
B→b|c|ε
Which of the following strings is not in the language of the grammar?

► abb

► bcacb

► bbccab

► bcab

► bcacba
Connecting VU Students
3
Question No: 4 ( Marks: 2 ) - Please choose one

Dotted items (T → α • β) record which part of a token has already been matched.
There are two kinds of basic items: shift items and reduce items.
integer → (• [0-9]) +
-This is a shift item.

► False

► True

► None of these

Question No: 5 ( Marks: 2 ) - Please choose one

A grammar with a FIRST/FIRST conflict can be made LL(1) by only applying left factoring, that
is, no substitution and left-recursion removal are needed.

► False

► True

► None of these

Question No: 6 ( Marks: 20 )

Consider the grammar


S→aBCd|dCBe
B→bB|ε
C→ca|ac|ε
(a) What are the terminals, non-terminals, and the start symbol for the grammar? [02 Points]
(b) Give the parse tree for the input string abbcad. [02 Points]
(c) Compute the First sets and Follow sets for each of the nonterminals in the grammar.
[04 Points]
(d) Construct an LL(1) parsing table for the grammar. [12 Points]

Question No: 7 ( Marks: 30 )

Consider the following grammar over the terminals + , − (the negation operator) and id.
Connecting VU Students
4
S→E
E → E + E | − E | id
Below is a partial DFA for this grammar.

(a) Complete the above DFA. You need to do the following: [21 points]
• Complete state 0 by performing closure on the item listed.
• Fill in all elements of states 1 and 5, and the lookahead items in states 3, 4 and 6.
• Fill in the missing transition labels on all edges.
• Write the necessary “reduce by . . . ” l a b e l s on states.

(b) For each state with a conflict, list the state, the lookahead token, and the type of conflict
(i.e. shift-reduce c o n f l i c t , or reduce-reduce conflict). [4 points]

c) Show that the given grammar is ambiguous (Hint: use the parse tree) [5 points]
E →E + T | T
T → T + E | id

Question No: 8 ( Marks: 15 )

Add semantic rules to the following grammar to compute the attribute rm, whose value is the
rightmost terminal in the string we parsed. For example, if the string parsed were zxyxy, S.rm
would be y.

Note: subscripts in the grammar below are only to distinguish multiple instances of the same
Connecting VU Students
5
nonterminal.

S →A {S.rm =
A → A1 x y
| B A1 y
| C
B → B1 z
| x
C → w C1
| y C1
| z

You might also like