Mid1 Soln spr11
Mid1 Soln spr11
Mid1 Soln spr11
b. (3 pts) Explain how a LR parser can generate a right-most derivation even though
it scans input from left to right
S AB | Cd FIRST FOLLOW
S a, b, c, d, f $
A a | Bf
A a, b, f b, $
B b | ε B b, ε f, $
C c, ε d
C c | ε
b. (10 pts) Using the FIRST and FOLLOW information provided, construct the LL(1)
parse table for the following grammar.
S aA | A FIRST FOLLOW a b $
S a, b, ε $ S S aA S A S A
A bA | ε A b, ε $ A A bA A ε
c. (2 pts) Is the grammar in part 2(b) LL(1)? Explain.
d. (8 pts) Using the following LL(1) parse table, parse the input “ba”. You only
need to show the stack and remaining input at each stage of the parse.
a b $ Stack Input
S S ba S A S C $S ba$
A A ba A bBa A ε $A ba$
B B ε B a B ε $aBb ba$
C C ab C ba C ε $aB a$
$a a$
$ $
3. (12 pts) Shift-reduce parsing
Given the following ACTION/GOTO table, show the parse if the current stack
contents are 0 b 4 B 1 (i.e., current state is 1) and the remaining input is “d$”.
S’ S [ S’ → • S, $ ] [ S → a • b, $ ]
[ S → • Aha, $ ] a [ A → a • Ac, h ]
S Aha | ab
[ S → • ab, $ ] [ A → • aAc, c ]
A aAc | d
[ A → • aAc, h ] [ A → • d, c ]
B b [ A → • d, h ]
5. (12 pts) LR(1) parse table construction
Given the following sets of LR(1) items, construct the corresponding entries in the
LR(1) parse table.
Unable to handle as many grammars (i.e., may have conflicts not found in
LR(1) parser).