exam-1-sol
exam-1-sol
exam-1-sol
October 1, 2014
Name: ______________________________________
X___________________________________________
2 18
3 14
4 24
5 34
6 15
Total 115
Part 1: Short answers (10 points)
LLVM may have a better optimizing back end — even if the same front-end is used,
LLVM can do a better job of producing optimized code
Some people thought that LLVM was a true virtual machine with a JIT compiler; I gave
credit for reasonable answers along those lines.
2) One nice thing about maintaining a common ISA is that when a new processor
is released, you don’t have to recompile any code, or change the compiler.
Nevertheless, when Intel releases new x86 processors, companies update their
compilers anyway. Why might they want to do that? (5 points)
Even if the ISA is the same, the implementation of that ISA might be different—some
instructions may be more efficient—so it can be important to change the backend to
account for these differences. Also, newer x86 processors may support instruction set
extensions (SIMD, x86-64) and compiler backends need to be changed to generate
code targeting those extensions.
Part 2: Regular expressions, finite automata and scanners (18 points)
1) Consider the following NFA. Fill in the transition table below with its
corresponding DFA using the subset construction. You may not use all of the
rows (16 points):
2 c 4 a 6
a a c
1 b
b b
a
3 c 5 c 7
State Final? a b c
1 No 2, 3 X X
2, 3 No X 3 4, 5
3 No X X 5
4, 5 No 3, 6 4 7
5 No X 4 7
3, 6 Yes X X 5
4 No 3, 6 4 7
7 Yes X X X
2 pts/row
2) List which states (if any) should be merged when you reduce the DFA you just
derived (2 points):
{4, 5} and 4
Note that 3 and {3, 6} can’t be merged because one is final and the other isn’t.
Part 3: Grammars (14 points)
Terminals: (, ), x, $
Non-terminals: S, A, B
A B $
( A B ) x
λ x
Part 4: LL parsers (24 points)
Answer the questions in this part using the same grammar from Part 3.
1) Give the First sets for each non-terminal in the grammar (6 points)
2) Give the Follow sets for each non-terminal in the grammar (6 points)
Follow(S) : { } [1 point]
Follow(A) : {x, (, $, )} [4 points]
Follow(B) : { ) } [1 point
3) Give the Predict sets for each production in the grammar (5 points)
Predict(1) : {(, $}
Predict(2) : { ( }
Predict(3) : { x, (, $, ) }
Predict(4) : { ( }
Predict(5) : { x }
( ) x $
S 1 1
A 2, 3 3 3 3
B 4 5
State 3 ) A → ( • AB) A → (A • B)
A→(•) B → • (A)
A → () • ( A → • (AB) B → • ()
A → • ()
(
B
State 6
B → () • (
State 8
)
State 7 A → (AB • )
State 9 B → ( • A)
A )
B→(•)
B → (A • )
A → • (AB)
) A → • () State 10
A → (AB) •
State 11
B → (A) •
2) List the shift and reduce states in the above CFSM (6 points)
Shift: 0, 1, 4, 5, 7, 8, 9
Reduce: 2 (accept), 3, 6, 10, 11
Shift 4
04574
Reduce 2 goto 5
045
Parse error
Shift 3
045743
Part 6 (573 only): LR(1) Parsers (15 points):
1) For the following grammar, show what the first state of the LR(1) machine
would be (10 points):
S ! AB$
A ! xy
S → • AB$, { }
A ! Bz A → • xy, {w, $}
A → • Bz, {w, $}
B ! Cz B → • Cz, {z}
B! B → λ •, {z}
C → • w, {z}
C!w
2) Give the action table entries for the first state (i.e., for each token that could be
coming up, say whether the parser would shift or reduce). For reduce actions, say
what rule would be reduced. (5 points)
Lookahead Action
x Shift
y Error
z Reduce 5
w Shift
$ Error