Basics of Compiler Design - Solutions
Basics of Compiler Design - Solutions
1 Introduction
This document provides solutions for selected exercises from “Basics of Compiler
Design”.
Note that in some cases there can be several equally valid solutions, of which
only one is provided here. If your own solutions differ from those given here, you
should use your own judgement to check if your solution is correct.
1
Exercise 2.2
a)
2
ε a ε - 5 a
N
ε- a- a-
- 1 3 6 7 8
ε b
- 4
b)
A = ε-closure({1}) = {1, 2, 3, 4, 5}
B = move(A, a) = ε-closure({1, 6}) = {1, 6, 2, 3, 4, 5}
C = move(A, b) = ε-closure({6}) = {6}
D = move(B, a) = ε-closure({1, 6, 7}) = {1, 6, 7, 2, 3, 4, 5}
move(B, b) = ε-closure({6}) = C
E = move(C, a) = ε-closure({7}) = {7}
move(C, b) = ε-closure({}) = {}
F = move(D, a) = ε-closure({1, 6, 7, 8}) = {1, 6, 7, 8, 2, 3, 4, 5}
move(D, b) = ε-closure({6}) = C
G = move(E, a) = ε-closure({8}) = {8}
move(E, b) = ε-closure({}) = {}
move(F, a) = ε-closure({1, 6, 7, 8}) = F
move(F, b) = ε-closure({6}) = C
move(G, a) = ε-closure({}) = {}
move(G, b) = ε-closure({}) = {}
States F and G are accepting since they contain the accepting NFA state 8.
In diagram form, we get:
2
a- a- a- a
- A B D F
@ b Y
b
b
@ b
R
@@ ?
a- a-
C E G
Exercise 2.5
We start by noting that there are no dead states, then we divide into groups of
accepting and non-accepting states:
0 = {0}
A = {1, 2, 3, 4}
We now check if group A is consistent:
A a b
1 A −
2 A −
3 A 0
4 A 0
We see that we must split A into two groups:
B = {1, 2}
C = {3, 4}
And we now check these, starting with B:
B a b
1 B −
2 C −
So we need to split B into it individual states. The only non-singleton group left
is C, which we now check:
C a b
3 C 0
4 C 0
This is consistent, so we can see that we could only combine states 3 and 4 into a
group C. The resulting diagram is:
3
?
0
Q
C QQa
b CC s
Q
Q
b 1
C
a C a
CW
a
- C 2
Exercise 2.7
a)
a a a a
U
U
U
U
- 0 b - 1 b - 2 b - 3
b)
a a a
U
U
U
b- b-
- 0 1 2
Y
b
c)
1
3
M
a a
b b
b
N
j
- 0 i 2
a
4
Exercise 2.8
( ( (
R
R R
- 0 1 2 3
I I I
) ) )
Exercise 2.9
a) The number must be 0 or end in two zeroes:
0
1 1
U
?
U
0 - 1
0
-
0 2
Y
1
b) We use that reading a 0 is the same as multiplying by 2 and reading a 1 is the
same as multiplying by two and adding 1. So of we have remainder m, read-
ing a 0 gives us remainder (2m) mod 5 and reading a 1 gives us remainder
(2m + 1) mod 5. We can make the following transition table:
m 0 1
0 0 1
1 2 3
2 4 0
3 1 2
4 3 4
Exercise 2.10
a)
φ|s = s because L(φ) ∪ L(s) = 0/ ∪ L(s) = L(s)
φs = φ because there are no strings in φ to put in front of strings in s
sφ = φ because there are no strings in φ to put after strings in s
φ∗ = ε because φ∗ = ε|φφ∗ = ε|φ = ε
b)
- ε
c) As there can now be dead states, the minimization algorithm will have to
take these into consideration as described in section 2.8.2.
Exercise 2.11
In the following, we will assume that for the regular language L, we have an NFA
N with no dead states.
6
Closure under prefix. When N reads a string w ∈ L, it will at each prefix of w
be at some state s in N. By making s accepting, we can make N accept this prefix.
By making all states accepting, we can accept all prefixes of strings in L.
So an automaton N p that accepts the prefixes of strings in L is made of the same
states and transitions as N, with the modification that all states in N p accepting.
Closure under suffix. When N reads a string w ∈ L, where w = uv, it will after
reading u be in some state s. If we made s the start state of N, N would hence
accept the suffix v of w. If we made all states of N into start states, we would
hence be able to accept all suffixes of strings in L. Since we are only allowed
one start state, we instead add ε-transitions from the original start state to all other
states.
So an automaton Ns that accepts all suffixes of strings in L is made of the same
states and transitions as N, with the modification that we add ε-transitions from
the start state in Ns to all other state in Ns .
Closure under reversal. We assume N has only one accepting state. We can
safely make this assumption, since we can make it so by adding an extra accepting
state f and make ε-transitions from all the original accepting states to f and then
make f the only accepting state.
We can now make N accept the reverses of the strings from L by reversing all
transitions and swap start state and accepting state.
So an automaton Nr that accepts all reverses of strings in L is made the follow-
ing way:
7
3 Exercises for chapter 3
Exercise 3.3
If we at first ignore ambiguity, the obvious grammar is
P →
P → (P)
P → PP
i.e., the empty string, a parentesis around a balanced sequence and a concatenation
of two balanced sequences. But as the last production is both left recursive and
right recursive, the grammar is ambiguous. An unambiguous grammar is:
P →
P → (P)P
which combines the two last productions from the first grammar into one.
Exercise 3.4
a)
S →
S → aSbS
S → bSaS
Explanation: The empty string has the same number of as and bs. If a string
starts with an a, we find a b to match it and vice versa.
b)
A → AA
A → SaS
S →
S → aSbS
S → bSaS
8
c)
D → A
D → B
A → AA
A → SaS
B → BB
B → SbS
S →
S → aSbS
S → bSaS
Explanation: If there are more as than bs, we use A from above and other-
wise we use a similarly constructed B.
d)
S →
S → aSaSbS
S → aSbSaS
S → bSaSaS
Explanation: If the string starts with an a, we find later macthing as and bs,
if it starts with a b, we find two matching as.
Exercise 3.5
a)
B → ε
B → O1 C1
B → O2 C2
O1 → ( B
O1 → [ B ) B
O2 → [ B
O2 → O1 O1
C1 → )B
C1 → (B]B
C2 → ]B
C2 → C1 C1
9
B is “balanced”, O1 /C1 are “open one” and “close one”, and O2 /C2 are “open
two” and “close two”.
b)
B
QQ
B
C2
QQ
@
@
O1 C1 O2 C1 C1
A@
A@ A@
A@ AA A@
A@ AA
[ B ) B ( B ] B [ B ( B ] B ) B
ε ε ε ε ε ε ε ε
Exercise 3.6
The string −id − id has these two syntax trees:
A A
! aa
!! LL a
A L A
QQ L J
A L
L
A J
J
L J
− id − id − id − id
a) : A → A − id b) : A → −A
A → B A → B
B → −B B → B − id
B → id B → id
The trees for the string −id − id with these two grammars are:
10
a) b)
A A
P
PP !
PP !! CC
A A C
C
C
B B C
J QQ C
B J
J
B C
C
J C
− id − id − id − id
Exercise 3.9
We first find the equations for Nullable:
Nullable(A) = true
Nullable(B) = true
which solve to
FIRST(A) = {a, b}
FIRST(B) = {a, b}
Finally, we add the production A0 → $ and set up the constraints for FOLLOW:
11
{$} ⊆ FOLLOW(A)
FIRST(Aa) ⊆ FOLLOW(B)
{a} ⊆ FOLLOW(A)
{c} ⊆ FOLLOW(B)
FIRST(A) ⊆ FOLLOW(A)
FOLLOW(B) ⊆ FOLLOW(A)
which we solve to
FOLLOW(A) = {a, b, c, $}
FOLLOW(B) = {a, b, c}
Exercise 3.10
Exercise 3.11
Nullable for each right-hand side is trivially found to be:
12
FIRST (Exp2 Exp0 ) = {num, (}
FIRST (+ Exp2 Exp0 ) = {+}
FIRST (− Exp2 Exp0 ) = {−}
FIRST () = {}
FIRST (Exp3 Exp20 ) = {num, (}
FIRST (∗ Exp3 Exp20 ) = {∗}
FIRST (/ Exp3 Exp20 ) = {/}
FIRST () = {}
FIRST (num) = {num}
FIRST (( Exp )) = {(}
Exercise 3.12
We get the following constraints for each production (abbreviating FIRST and
FOLLOW to FI and FO and ignoring trivial constraints like FO(Exp) ⊆ FO(Exp1 )):
Exercise 3.13
The table is too wide for the page, so we split it into two, but for layout only (they
are used as a single table).
num + − ∗
Exp0 Exp0
→ Exp $
Exp Exp → num Exp1
Exp1 → + Exp Exp1 Exp1 → − Exp Exp1 Exp1 → ∗ Exp Exp1
Exp1
Exp1 → Exp1 → Exp1 →
13
/ ( ) $
Exp0 Exp0 → Exp $
Exp Exp → ( Exp ) Exp1
Exp1 → / Exp Exp1
Exp1 Exp1 → Exp1 →
Exp1 →
Note that there are several conflicts for Exp1 , which isn’t surprising, as the gram-
mar is ambiguous.
Exercise 3.14
a)
E → num E 0
E0 → E + E0
E0 → E ∗ E0
E0 →
b)
E → num E 0
E0 → E Aux
E0 →
Aux → + E0
Aux → ∗ E0
c)
Nullable FIRST
E → num E 0 f alse {num} FOLLOW
E 0 → E Aux f alse {num} E {+, ∗, $}
E0 → true {} E 0 {+, ∗, $}
Aux → + E 0 f alse {+} Aux {+, ∗, $}
Aux → ∗ E 0 f alse {∗}
d)
num + ∗ $
E E → num E 0
E 0 E 0 → E Aux E 0 → E0 → E0 →
Aux Aux → + E 0 Aux → ∗ E 0
14
Exercise 3.19
a) We add the production T 0 → T .
b) We add the production T 00 → T 0 $ for calculating FOLLOW . We get the
constraints (omitting trivially true constraints):
T 00 → T0$ : $ ∈ FOLLOW (T 0 )
T0 → T : FOLLOW (T 0 ) ⊆ FOLLOW (T )
T → T − >T : − > ∈ FOLLOW (T )
T → T ∗T : ∗ ∈ FOLLOW (T )
T → int :
which solves to
FOLLOW (T 0 ) = {$}
FOLLOW (T ) = {$, − >, ∗}
0: T0 → T
1: T → T − >T
2: T → T ∗T
3: T → int
1
T- ->- T-
C D E F
2
T- *- T-
G H I J
3
int-
K L
15
We then add epsilon-transitions:
ε
A C, G, K
C C, G, K
E C, G, K
G C, G, K
I C, G, K
and convert to a DFA (in tabular form):
state NFA states int -> * T
0 A, C, G, K s1 g2
1 L
2 B, D, H s3 s4
3 E, C, G, K s1 g5
4 I, C, G, K s1 g6
5 F, D, H s3 s4
6 J, D, H s3 s4
and add accept/reduce actions according to the FOLLOW sets:
state NFA states int -> * $ T
0 A, C, G, K s1 g2
1 L r3 r3 r3
2 B, D, H s3 s4 acc
3 E, C, G, K s1 g5
4 I, C, G, K s1 g6
5 F, D, H s3/r1 s4/r1 r1
6 J, D, H s3/r2 s4/r2 r2
d) The conflict in state 5 on -> is between shifting on -> or reducing to pro-
duction 1 (which contains ->). Since -> is right-associative, we shift.
The conflict in state 5 on * is between shifting on * or reducing to production
1 (which contains ->). Since * binds tighter, we shift.
The conflict in state 6 on -> is between shifting on -> or reducing to pro-
duction 2 (which contains *). Since * binds tighter, we reduce.
The conflict in state 6 on * is between shifting on * or reducing to production
2 (which contains *). Since * is left-associative, we reduce.
The final table is:
16
state int -> * $ T
0 s1 g2
1 r3 r3 r3
2 s3 s4 acc
3 s1 g5
4 s1 g6
5 s3 s4 r1
6 r2 r2 r2
Exercise 3.20
The method from section 3.16.3 can be used with a standard parser generator and
with an unlimited number of precedences, but the restructuring of the syntax tree
afterwards is bothersome. The precedence of an operator needs not be known at
the time the operator is read, as long as it is known at the end of reading the syntax
tree.
Method a) requires a non-standard parser generator or modification of a gen-
erated parser, but it also allows an unlimited number of precedences and it doesn’t
require restructuring afterwards. The precedence of an operator needs to be known
when it is read, but this knowledge can be aquired earlier in the same parse.
Method b) can be used with a standard parser generator. The lexer has a rule
for all possible operator names and looks up in a table to find which token to
use for the operator (similar to how, as described in section 2.9.1, identifiers can
looked up in a table to see if they are keywords or variables). This table can
be updated as a result of a parser action, so like method a), precedence can be
declared earlier in the same parse, but not later. The main disadvantage is that the
number of precedence levels and the associativity of each level is fixed in advance,
when the parser is constructed.
Exercise 3.21
a) The grammar describes the language of all even-length palindromes, i.e.,
strings that are the same when read forwards or backwards.
b) The grammar is unambiguous, which can be proven by induction on the
length of the string: If it is 0, the last production is the only that matches. If
greater than 0, the first and last characters in the string uniquely selects the
first or second production (or fails, if none match). After the first and last
characters are removed, we are back to the original parsing problem, but on
a shorter string. By the induction hypothesis, this will have a unique syntax
tree.
17
c) We add a start production A0 → A) and number the productions:
0: A0 → A
1: A → aAa
2: A → bAb
3: A →
We note that FOLLOW (A) = {a, b, $} and make NFAs for each production:
0
A-
A B
1
a- A- a-
C D E F
2
b- A- b-
G H I J
3
K
18
d) Consider the string aa. In state 0, we shift on the first a to state 1. Here
we are given a choice between shifting on the second a or reducing with the
empty reduction. The right action is reduction, so r3 on a in state 1 must be
preserved.
Consider instead the string aaaa. After the first shift, we are left with the
same choice as before, but now the right action is to do another shift (and
then a reduce). So s1 on a in state 1 must also be preserved.
Removing any of these two actions will, hence, make a legal string un-
parseable. So we can’t remove all conflicts.
Some can be removed, though, as we can see that choosing some actions
will lead to states from which there are no legal actions. This is true for the
r3 actions in a and b in state 0, as these will lead to state 3 before reaching
the end of input. The r3 action on b in state 1 can be removed, as this would
indicate that we are at the middle of the string with an a before the middle
and a b after the middle. Similarly, the r3 action on a in state 2 can be
removed. But we are still left with two conflicts, which can not be removed:
state a b $ A
0 s1 s2 r3 g3
1 s1/r3 s2 r3 g4
2 s1 s2/r3 r3 g5
3 acc
4 s6
5 s7
6 r1 r1 r1
7 r2 r2 r2
19