Download Practice Problems 2 Solutions - Introduction to Compilers | CMSC 430 and more Exams Computer Science in PDF only on Docsity!
CMSC 430, Practice Problems 2 Solutions
1. Bottom up parsing
a. Describe the difference between bottom-up and top-down parsing with respect
to a grammar production A → β
Bottom up parsers perform reductions of β to A, while top-down
parsers perform expansions of A to β.
b. Define handle
The right-hand side of the next production to be reduced in the right-
most derivation, and its position in the current sentential form.
c. Explain why bottom-up parsers result in right-most derivations in reverse.
Because input is processed from left to right and unprocessed input is
composed only of terminals, a right-most derivation is used so the
sentential form to the right of the handle is composed entirely of
terminals.
d. Given the following ACTION/GOTO table, perform a parse of “a”, assuming
0 is the start state. Show the contents of the stack, remaining input, and action
performed at each step of the shift-reduce parse.
2. LR parsing
a. What is the canonical set of LR(k) items for a grammar?
A deterministic finite automaton (DFA) for recognizing handles
(right-hand sides of productions) for the grammar.
b. What are ACTION/GOTO tables of an LR(k) parser for a grammar?
Tables encoding the DFA for recognizing handles for the grammar.
c. Given an LR(k) item [ A → α•γ, δ] from a production A → β, describe the
role of A, α, γ, δ
A is the nonterminal that is potentially the result of a reduction. α is
the portion of ββββ that the parser has just seen. γγγγ is the portion of ββββ the
parser will need to see before performing the reduction to A. δδδδ is k
terminals that must be the k lookaheads for the reduction to be
performed.
d. Given an LR(k) item [ A → α•γ, δ], what lookahead allows a shift to be
performed (ignoring any other LR(k) items added to the state by closure)?
The terminal at the beginning of γγγγ.
e. Given an LR(k) item [ A → α•γ, δ], what LR(k) items must be added to the
closure?
Any production for the nonterminal B at the beginning of γγγγ , with • at
the beginning of the right-hand side of the production, and FIRST( γγγγ ’ δδδδ )
as the lookahead token, where γγγγ ’ is what remains in γγγγ after removing
the first B.
f. If the parser is in a state containing an LR(k) item [ A → α•γ, δ], what must be
at the top of the stack, and why?
The top of the stack must be α , since the parser just saw α.
g. Describe what constitutes a shift/reduce conflict
A DFA state contains both LR items allowing shift and reduce for the
same lookahead.
h. Give an example of two LR(1) items causing a reduce/reduce conflict.
[A →→→→ α •, δδδδ ] and [B →→→→ α •, δδδδ ]
i. Explain why there are no shift/shift conflicts
Because no decision needs to be made about which right-hand side of
a production is the handle.
3. LALR parsing.
Consider the following sets of LR(1) items in the states of a LR(1) parser.
a. Find all shift/reduce and reduce/reduce conflicts. List the LR(1) items and
lookaheads causing conflicts.
Shift/reduce conflict is caused by [A →→→→ •a, c] and [B →→→→ a•, a] on
lookahead a in state 2.
b. What states would be merged in a LALR(1) parser?
States 0 and 2.
c. Are any new reduce/reduce conflicts introduced in the LALR(1) parser?
Reduce/reduce conflict is caused by [A →→→→ a•, b] and [B →→→→ a•, b] on
lookahead b in the merged state.
d. Explain why LALR(1) parsers will not introduce new shift/reduce conflicts.
Because the core of all merged states must be identical, the LR item
for the shift must have been in all of the states prior to the merge. Any
d. Explain why the grammar is or is not SLR(1)
It is SLR(1) since the SLR(1) ACTION/GOTO table has no conflicts.
e. Use the ACTION/GOTO table to parse the string “abc”
Stack Remaining Input Action Comment
$ 0 a b c $ Shift 3
$ 0 a 3 b c $ Shift 4
$ 0 a 3 b 4 c $ Shift 2
$ 0 a 3 b 4 c 2 $ Reduce S → c
$ 0 a 3 b 4 S $ Goto 6 Intermediate Step
$ 0 a 3 b 4 S 6 $ Reduce S → abS
$ 0 S $ Goto 1 Intermediate Step
$ 0 S 1 $ Accept
5. Consider the following grammar: S → Sa | Sc | c
a. Compute the canonical set of LR(1) items for the grammar
State 0 [S’ →→→→ • S, $ ] [S →→→→ • Sa, {$,a,c} ] [S →→→→ • Sc, {$,a,c} ] [S →→→→ • c, {$,a,c} ]
State 1 [S →→→→ c • , {$,a,c} ]
State 2 [S’ →→→→ S •, $ ] [S →→→→ S • a, {$,a,c} ] [S →→→→ S • c, {$,a,c} ]
State 3 [S →→→→ Sa •, {$,a,c} ]
State 4 [S →→→→ Sc •, {$,a,c} ]
S
a c c
State 0 [S’ →→→→ • S, $ ] [S →→→→ • Sa, {$,a,c} ] [S →→→→ • Sc, {$,a,c} ] [S →→→→ • c, {$,a,c} ]
State 1 [S →→→→ c • , {$,a,c} ]
State 2 [S’ →→→→ S •, $ ] [S →→→→ S • a, {$,a,c} ] [S →→→→ S • c, {$,a,c} ]
State 3 [S →→→→ Sa •, {$,a,c} ]
State 4 [S →→→→ Sc •, {$,a,c} ]
S
a c c
b. Build the ACTION/GOTO table for the grammar
Action Goto
State a c $ S
0 Shift 1 2
1 Reduce
S → c
Reduce
S → c
Reduce
S → c
2 Shift 3 Shift 4 Accept
3 Reduce
S → Sa
Reduce
S → Sa
Reduce
S → Sa
4 Reduce
S → Sc
Reduce
S → Sc
Reduce
S → Sc
c. Explain why the grammar is or is not LR(1)
It is LR(1) since the LR(1) ACTION/GOTO table has no conflicts.
d. Use the ACTION/GOTO table to parse the string “caca”
Stack Remaining Input Action Comment
$ 0 c a c a $ Shift 1
$ 0 c 1 a c a $ Reduce S → c
$ 0 S a c a $ Goto 2 Intermediate Step
$ 0 S 2 a c a $ Shift 3
$ 0 S 2 a 3 c a $ Reduce S → Sa
$ 0 S c a $ Goto 2 Intermediate Step
$ 0 S 2 c a $ Shift 4
$ 0 S 2 c 4 a $ Reduce S → Sc
$ 0 S a $ Goto 2 Intermediate Step
$ 0 S 2 a $ Shift 3
$ 0 S 2 a 3 $ Reduce S → Sa
$ 0 S $ Goto 2 Intermediate Step
$ 0 S 2 $ Accept
6. Consider the following grammar: S → S + S | S * S | (S) | num
a. Compute the canonical set of LR(1) items for the grammar
State 0 [S’ →→→→ • S, $ ] [S (^) →→→→ • S+S, {$,+,} ] [S* →→→→ • SS, {$,+,} ] [S** →→→→ • (S), {$,+,} ] [S* →→→→ • n, {$,+,} ]*
State 1 [S →→→→ n • , {$,+,}] S*
State 3 [S’ →→→→ S •, $ ] [S →→→→ S • +S, {$,+,} ] [S* →→→→ S • S, {$,+,} ]
State 2 [S →→→→ ( • S), { $,+,} ] [S* →→→→ • S+S, { ),+,} ] [S* →→→→ • SS, { ),+,} ] [S** →→→→ • (S), { ),+,} ] [S* →→→→ • n, { ),+,} ]*
State 14 [S →→→→ S+ • S, {$,+,} ] [S* →→→→ • S+S, {$,+,} ] [S* →→→→ • SS, {$,+,} ] [S** →→→→ • (S), {$,+,} ] [S* →→→→ • n, {$,+,} ]*
State 15 [S →→→→ S • S, {$,+,} ] [S** →→→→ • S+S, {$,+,} ] [S* →→→→ • SS, {$,+,} ] [S** →→→→ • (S), {$,+,} ] [S* →→→→ • n, {$,+,} ]*
State 5 [S →→→→ (S • ), {$,+,} ] [S* →→→→ S • +S, { ),+,} ] [S* →→→→ S • S, { ),+,} ]
State 6 [S →→→→ ( • S), { ),+,} ] [S* →→→→ • S+S, { ),+,} ] [S* →→→→ • SS, { ),+,} ] [S** →→→→ • (S), { ),+,} ] [S* →→→→ • n, { ),+,} ]*
State 8 [S →→→→ (S • ), { ),+,} ] [S* (^) →→→→ S • +S, { ),+,} ] [S* →→→→ S • S, { ),+,} ]
State 7 [S →→→→ (S) •, {$,+,} ]*
State 9 [S →→→→ (S) •, { ),+,} ]*
State 16 [S →→→→ S+S •, {$,+,} ] [S* →→→→ S • +S, {$,+,} ] [S* →→→→ S • S, {$,+,} ] State 17 [S →→→→ SS •, {$,+,} ] [S** →→→→ S • +S, {$,+,} ] [S* (^) →→→→ S • S, {$,+,} ]
State 10 [S →→→→ S+ • S, { ),+,} ] [S* →→→→ • S+S, { ),+,} ] [S* →→→→ • SS, { ),+,} ] [S** →→→→ • (S), { ),+,} ] [S* →→→→ • n, { ),+,} ]*
State 12 [S →→→→ S+S •, { ),+,} ] [S* →→→→ S • +S, { ),+,} ] [S* →→→→ S • S, { ),+,} ]
State 13 [S →→→→ SS •, { ),+,} ] [S** →→→→ S • +S, { ),+,} ] [S* →→→→ S • S, { ),+,} ]
State 4 [S →→→→ n • , { ),+,} ]*
State 11 [S →→→→ S • S, { ),+,} ] [S** →→→→ • S+S, { ),+,} ] [S* →→→→ • SS, { ),+,} ] [S** →→→→ • (S), { ),+,} ] [S* →→→→ • n, { ),+,} ]*
S
S
S
S
S
S
n
n
n
n
n n n
+
+
+
+
+
+
**+ ***
(
(
(
(
(
( (
)
)
State 0 [S’ →→→→ • S, $ ] [S (^) →→→→ • S+S, {$,+,} ] [S* →→→→ • SS, {$,+,} ] [S** →→→→ • (S), {$,+,} ] [S* →→→→ • n, {$,+,} ]*
State 1 [S →→→→ n • , {$,+,}] S*
State 3 [S’ →→→→ S •, $ ] [S →→→→ S • +S, {$,+,} ] [S* →→→→ S • S, {$,+,} ]
State 2 [S →→→→ ( • S), { $,+,} ] [S* →→→→ • S+S, { ),+,} ] [S* →→→→ • SS, { ),+,} ] [S** →→→→ • (S), { ),+,} ] [S* →→→→ • n, { ),+,} ]*
State 14 [S →→→→ S+ • S, {$,+,} ] [S* →→→→ • S+S, {$,+,} ] [S* →→→→ • SS, {$,+,} ] [S** →→→→ • (S), {$,+,} ] [S* →→→→ • n, {$,+,} ]*
State 15 [S →→→→ S • S, {$,+,} ] [S** →→→→ • S+S, {$,+,} ] [S* →→→→ • SS, {$,+,} ] [S** →→→→ • (S), {$,+,} ] [S* →→→→ • n, {$,+,} ]*
State 5 [S →→→→ (S • ), {$,+,} ] [S* →→→→ S • +S, { ),+,} ] [S* →→→→ S • S, { ),+,} ]
State 6 [S →→→→ ( • S), { ),+,} ] [S* →→→→ • S+S, { ),+,} ] [S* →→→→ • SS, { ),+,} ] [S** →→→→ • (S), { ),+,} ] [S* →→→→ • n, { ),+,} ]*
State 8 [S →→→→ (S • ), { ),+,} ] [S* (^) →→→→ S • +S, { ),+,} ] [S* →→→→ S • S, { ),+,} ]
State 7 [S →→→→ (S) •, {$,+,} ]*
State 9 [S →→→→ (S) •, { ),+,} ]*
State 16 [S →→→→ S+S •, {$,+,} ] [S* →→→→ S • +S, {$,+,} ] [S* →→→→ S • S, {$,+,} ] State 17 [S →→→→ SS •, {$,+,} ] [S** →→→→ S • +S, {$,+,} ] [S* (^) →→→→ S • S, {$,+,} ]
State 10 [S →→→→ S+ • S, { ),+,} ] [S* →→→→ • S+S, { ),+,} ] [S* →→→→ • SS, { ),+,} ] [S** →→→→ • (S), { ),+,} ] [S* →→→→ • n, { ),+,} ]*
State 12 [S →→→→ S+S •, { ),+,} ] [S* →→→→ S • +S, { ),+,} ] [S* →→→→ S • S, { ),+,} ]
State 13 [S →→→→ SS •, { ),+,} ] [S** →→→→ S • +S, { ),+,} ] [S* →→→→ S • S, { ),+,} ]
State 4 [S →→→→ n • , { ),+,} ]*
State 11 [S →→→→ S • S, { ),+,} ] [S** →→→→ • S+S, { ),+,} ] [S* →→→→ • SS, { ),+,} ] [S** →→→→ • (S), { ),+,} ] [S* →→→→ • n, { ),+,} ]*
S
S
S
S
S
S
n
n
n
n
n n n
+
+
+
+
+
+
**+ ***
(
(
(
(
(
( (
)
)
c. Explain how to use the ACTION/GOTO table so that
Lookahead
State + *
S+S on stack
Reduce + on left or
shift + on right onto stack?
Depends on associativity of +
(shift if right assoc, reduce if left)
Reduce + on left or
shift * on right onto stack?
Depends on precedence of + vs *
(shift if * > +, reduce if * < +)
S*S on stack
Reduce * on left or
shift + on right onto stack?
Depends on precedence of * vs +
(shift if + > *, reduce if + < *)
Reduce * on left or
shift * on right onto stack?
Depends on associativity of *
(shift if right assoc, reduce if left)
i. * has higher precedence than + and *, + are both left associative
Resolve the shift/reduce conflicts as follows (same for 12 & 13)..
Lookahead
State + *
16 Reduce S → S+S Shift 15
17 Reduce S → SS Reduce S → SS
ii. * has higher precedence than +, * is left associative, and + is right
associative
Resolve the shift/reduce conflicts as follows (same for 12 & 13)..
Lookahead
State + *
16 Shift 14 Shift 15
17 Reduce S → SS Reduce S → SS
iii. + has higher precedence than * and *, + are both right associative
Resolve the shift/reduce conflicts as follows (same for 12 & 13).
Lookahead
State + *
16 Shift 14 Reduce S → S+S
17 Shift 14 Shift 15
7. Consider the following grammar: S → AS | b A → SA | a
a. Compute the canonical set of LR(0) items for the grammar
b. Build the ACTION/GOTO table for the grammar
c. Compute the canonical set of LR(1) items for the grammar
d. Build the ACTION/GOTO table for the grammar
See solution for problem 8 at
http://www.cs.umd.edu/class/spring2006/cmsc430/p1-f03-soln.txt
8. Consider the following grammar: S → S ; a | a
a. Show the grammar is LR(1) but not SLR(1)
SLR(1) parser?
FOLLOW(S’) = { $ }
FOLLOW(S) = { $, ; }
Canonical set of LR(0) items =
State 0 [S’ →→→→ • S ] [S →→→→ • S;a ] [S →→→→ • a ]
State 1 [S (^) →→→→ a • ]
State 2 [S’ (^) →→→→ S • ] [S →→→→ S • ;a ]
State 3 [S →→→→ S ; • a ]
S
a
a ;
State 4 [S →→→→ S ; a • ]
State 0 [S’ →→→→ • S ] [S →→→→ • S;a ] [S →→→→ • a ]
State 1 [S (^) →→→→ a • ]
State 2 [S’ (^) →→→→ S • ] [S →→→→ S • ;a ]
State 3 [S →→→→ S ; • a ]
S
a
a ;
State 4 [S →→→→ S ; a • ]
Action Goto
State a ; $ S
0 Shift 1 2
1 Reduce
S → a
Reduce
S → a
2 Shift 3 Accept
3 Shift 4
4 Reduce
S → S;a
Reduce
S → S;a
No conflicts in any state, grammar is SLR(1)
LR(1) parser?
Canonical set of LR(1) items =
State 0 [S’ →→→→ • S, $ ] [S →→→→ • S;a, {$,;} ] [S →→→→ • a, {$,;} ]
State 1 [S →→→→ a •, {$,;} ]
State 2 [S’ →→→→ S •, $ ] [S →→→→ S • ;a, {$,;} ]
State 3 [S →→→→ S ; • a, {$,;} ]
S
a
a ;
State 4 [S →→→→ S ; a •, {$,;} ]
State 0 [S’ →→→→ • S, $ ] [S →→→→ • S;a, {$,;} ] [S →→→→ • a, {$,;} ]
State 1 [S →→→→ a •, {$,;} ]
State 2 [S’ →→→→ S •, $ ] [S →→→→ S • ;a, {$,;} ]
State 3 [S →→→→ S ; • a, {$,;} ]
S
a
a ;
State 4 [S →→→→ S ; a •, {$,;} ]
No conflicts in any state, grammar is LR(1)
Alternative proof: Since grammar is SLR(1), it must be also LR(1)
since LR(1) is a superset of SLR(1).
10. Consider the following grammar: S → Aa | bAc | Bc | bBa A → d B → d
a. Show the grammar is LR(1) but not LALR(1)
LR(1) parser?
Canonical set of LR(1) items =
State 0 [S’ →→→→ • S, $ ] [S →→→→ • Aa, $ ] [S →→→→ • bAc, $ ] [S →→→→ • Bc, $ ] [S →→→→ • bBa, $ ] [A (^) →→→→ • d, a ] [B →→→→ • d, c ]
State 1 [S →→→→ b • Ac, $ ] [S →→→→ b • Ba, $ ] [A →→→→ • d, c ] [B →→→→ • d, a ]
State 6 [A →→→→ d •, c ] [B →→→→ d •, a ]
State 2 [S →→→→ bA • c, $ ]
d
c
d
State 3 [S →→→→ bAc •, $ ]
State 4 [S →→→→ bB • a, $ ]
State 5 [S →→→→ bBa •, $ ]
State 12 [S’ →→→→ S •, $ ]
a
A
B
b
S State 8 [S (^) →→→→ A • a, $ ]
a (^) State 9 [S →→→→ Aa •, $ ]
State 10 [S →→→→ B • c, $ ]
State 11 [S →→→→ Bc •, $ ]
c
A
B
State 7 [A →→→→ d •, a ] [B →→→→ d •, c ]
State 0 [S’ →→→→ • S, $ ] [S →→→→ • Aa, $ ] [S →→→→ • bAc, $ ] [S →→→→ • Bc, $ ] [S →→→→ • bBa, $ ] [A (^) →→→→ • d, a ] [B →→→→ • d, c ]
State 1 [S →→→→ b • Ac, $ ] [S →→→→ b • Ba, $ ] [A →→→→ • d, c ] [B →→→→ • d, a ]
State 6 [A →→→→ d •, c ] [B →→→→ d •, a ]
State 2 [S →→→→ bA • c, $ ]
d
c
d
State 3 [S →→→→ bAc •, $ ]
State 4 [S →→→→ bB • a, $ ]
State 5 [S →→→→ bBa •, $ ]
State 12 [S’ →→→→ S •, $ ]
a
A
B
b
S State 8 [S (^) →→→→ A • a, $ ]
a (^) State 9 [S →→→→ Aa •, $ ]
State 10 [S →→→→ B • c, $ ]
State 11 [S →→→→ Bc •, $ ]
c
A
B
State 7 [A →→→→ d •, a ] [B →→→→ d •, c ]
No conflicts in any state, grammar is LR(1)
LALR(1) parser?
States 6 & 7 merged since they have identical cores, yielding
State 0 [S’ →→→→ • S, $ ] [S →→→→ • Aa, $ ] [S →→→→ • bAc, $ ] [S →→→→ • Bc, $ ] [S →→→→ • bBa, $ ] [A →→→→ • d, a ] [B (^) →→→→ • d, c ]
State 1 [S →→→→ b • Ac, $ ] [S →→→→ b • Ba, $ ] [A →→→→ • d, c ] [B →→→→ • d, a ]
State 2 [S →→→→ bA • c, $ ]
d
c
d
State 3 [S →→→→ bAc •, $ ]
State 4 [S →→→→ bB • a, $ ]
State 5 [S →→→→ bBa •, $ ]
State 12 [S’ →→→→ S •, $ ]
a
A
B
b
S State 8 [S →→→→ A • a, $ ]
a State 9 [S →→→→ Aa •, $ ]
State 10 [S (^) →→→→ B • c, $ ]
State 11 [S →→→→ Bc •, $ ]
c
A
B
State 6 [A →→→→ d •, {a,c} ] [B →→→→ d •, {a,c} ]
State 0 [S’ →→→→ • S, $ ] [S →→→→ • Aa, $ ] [S →→→→ • bAc, $ ] [S →→→→ • Bc, $ ] [S →→→→ • bBa, $ ] [A →→→→ • d, a ] [B (^) →→→→ • d, c ]
State 1 [S →→→→ b • Ac, $ ] [S →→→→ b • Ba, $ ] [A →→→→ • d, c ] [B →→→→ • d, a ]
State 2 [S →→→→ bA • c, $ ]
d
c
d
State 3 [S →→→→ bAc •, $ ]
State 4 [S →→→→ bB • a, $ ]
State 5 [S →→→→ bBa •, $ ]
State 12 [S’ →→→→ S •, $ ]
a
A
B
b
S State 8 [S →→→→ A • a, $ ]
a State 9 [S →→→→ Aa •, $ ]
State 10 [S (^) →→→→ B • c, $ ]
State 11 [S →→→→ Bc •, $ ]
c
A
B
State 6 [A →→→→ d •, {a,c} ] [B →→→→ d •, {a,c} ]
Not LALR(1), reduce/reduce error in State 6 on lookaheads a,c