Professional Documents
Culture Documents
com
o True
o False
o True
o False
Question No. 4 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
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.
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
o True
o False
Question No. 12 Marks : 05
o True
o False
Question No. 12 Marks : 05
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
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
Find the First and Follow sets for the following grammars:
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)
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
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]
True
False
None of the others
Making a grammar with a FIRST/FIRST conflict LL(1) requires the application of left factoring.
True
False
Insufficient Information
NFAs are converted to DFAs because the former cannot be implemented directly.
True
False
Insufficient Information
S →a |A
A → Aa | a
True
False
None of the others
True
False
None of the others
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* {} {} {}
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: ______________________________
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)*
A shift reduce parser performs reductions in the reverse order specified by a left-most derivation:
► False
► True
► None of these
► 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
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
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
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