CS 143 Compilers Handout 7
Solutions to Written Assignment 2
1. Give a context-free grammar (CFG) for each of the following languages over the alphabet = {a, b}:
(a) All strings in the language L : {an bm a2n |n, m 0}
S aSaa | B
B bB |
(b) All nonempty strings that start and end with the same symbol.
S aXa | bXb | a | b
X aX | bX |
(c) All strings with more as than bs.
S Aa | M S | SM A
A Aa |
M | M M | bM a | aM b
(d) All palindromes (a palindrome is a string that reads the same forwards and backwards).
S aSa | bSb | a | b |
2. A history major taking CS143 decided to write a rudimentary CFG to parse the roman nu-
merals 1-99 (i,ii,iii,iv,v,. . . ,ix,x,. . . ,xl,. . . ,lxxx,. . . ,xc,. . . ,xcix). If you are unfamiliar with ro-
man numerals, please have a look at [Link] numerals and
[Link]
Consider the grammar below, with terminals {c, l, x, v, i}. c = 100, l = 50, x = 10, v = 5, i = 1.
Notice that we use lowercase characters here to represent the numerals, to distinguish them from the
non-terminals.
S xT U | lX | X
T c|l
X xX | U
U iY | vI | I
Y x|v
I iI |
(a) Draw a parse tree for 47: xlvii.
See Figure 1.
(b) Is this grammar ambiguous?
No
Fall 2010/2011 page 1 of 5
CS 143 Compilers Handout 7
x T U
l v I
i I
i I
Figure 1: Question 2a: Parse tree for 47: xlvii
(c) Write semantic actions for each of the 14 rules in the grammar (remember X A | B is short
for X A and X B) to calculate the decimal value of the input string. You can associate a
synthesized attribute val to each of the non-terminals to store its value. The final value should
be returned in [Link]. Hint: have a look at the calculator examples presented in class.
S xT U {[Link] = [Link] 10 + [Link]}
S lX {[Link] = [Link] + 50}
S X {[Link] = [Link]}
T c {[Link] = 100}
T l {[Link] = 50}
X1 xX2 {X1 .val = X2 .val + 10}
X U {[Link] = [Link]}
U iY {[Link] = [Link] 1}
U vI {[Link] = [Link] + 5}
U I {[Link] = [Link]}
Y x {[Link] = 10}
Y v {[Link] = 5}
I1 iI2 {I1 .val = I2 .val + 1}
I {[Link] = 0}
3. (a) Left factor the following grammar:
E int | int + E | int E | E (E)
Solution:
E int E 0 | E (E)
E0 | + E | E
(b) Eliminate left-recursion from the following grammar:
AA+B | B
B int | (A)
Fall 2010/2011 page 2 of 5
CS 143 Compilers Handout 7
Solution:
A BA0
A0 +BA0 |
B int | (A)
4. Consider the following LL(1) grammar, which has the set of terminals T = {a, b, ep, +, *, (, )}. This
grammar generates regular expressions over {a, b}, with + meaning the RegExp OR operator, and
ep meaning the symbol. (Yes, this is a context free grammar for generating regular expressions!)
E T E0
E 0 +E |
T FT0
T0 T |
F PF0
F 0 *F 0 |
P (E) | a | b | ep
(a) Give the first and follow sets for each non-terminal.
The First and Follow sets of the non-terminals are as follows.
First(E) = {(, a, b, ep} Follow(E) = {), $}
First(E 0 ) = {+, } Follow(E 0 ) = {), $}
First(T ) = {(, a, b, ep} Follow(T ) = {+, ), $}
First(T 0 ) = {(, a, b, ep, } Follow(T 0 ) = {+, ), $}
First(F ) = {(, a, b, ep} Follow(F ) = {(, a, b, ep, +, ), $}
First(F 0 ) = {, } Follow(F 0 ) = {(, a, b, ep, +, ), $}
First(P ) = {(, a, b, ep} Follow(P ) = {(, a, b, ep, +, ), , $}
(b) Construct an LL(1) parsing table for the left-factored grammar.
Here is an LL(1) parsing table for the grammar.
( ) a b ep + $
E TE 0 0 0
TE TE TE 0
E0 +E
T FT 0 0 0
FT FT FT 0
T0 T T T T
F PF 0 0 0
PF PF PF 0
F0 F 0
P (E) a b ep
Fall 2010/2011 page 3 of 5
CS 143 Compilers Handout 7
(c) Show the operation of an LL(1) parser on the input string ab*.
Stack Input Action
E$ ab $ T E0
T E0$ ab $ FT0
F T 0E0$ ab $ PF0
P F 0T 0E0$ ab $ a
aF 0 T 0 E 0 $ ab $ terminal
F 0T 0E0$ b$
T 0E0$ b$ T
T E0$ b$ FT0
F T 0E0$ b$ PF0
P F 0T 0E0$ b$ b
bF 0 T 0 E 0 $ b$ terminal
F 0T 0E0$ $ F 0
F 0 T 0 E 0 $ $ terminal
F 0T 0E0$ $
T 0E0$ $
E0$ $
$ $ ACCEP T
5. Consider the following CFG, which has the set of terminals T = {stmt, {, }, ;}. This grammar describes
the organization of statements in blocks for a fictitious programming language. Blocks can have zero
or more statements and other nested blocks, separated by semicolons, where the last semicolon is
optional. (P is the start symbol here.)
P S
S stmt | {B
B } | S} | S;B
(a) Construct a DFA for viable prefixes of this grammar using LR(0) items.
Figure 2 shows a DFA for viable prefixes of the grammar. Note that for simplicity we omitted
adding an extra state S 0 P .
(b) Identify any shift-reduce and reduce-reduce conflicts in this grammar under the SLR(1) rules.
There are no conflicts.
(c) Assuming that an SLR(1) parser resolves shift-reduce conflicts by choosing to shift, show the
operation of such a parser on the input string {stmt;}.
Fall 2010/2011 page 4 of 5
CS 143 Compilers Handout 7
4
S
1 P->.S P->S.
S->.stmt
S->.{B 5
stmt
S->stmt. stmt
{
stmt 9
6
2 S->{.B } } B->S;.B
B->.} B->}. B->.}
B->.S} { B->.S}
{
B->.S;B B->.S;B
S->.stmt 7 B->S.} ; S->.stmt
S
S->.{B B->S.;B S->.{B
S
B } B
3 S->{B. 8 B->S}. 10 B->S;B.
Figure 2: A DFA for viable prefixes of the grammar in Question 5a
Configuration DFA Halt State Action
| {stmt; }$ 1 shif t
{| stmt; }$ 2 shif t
{stmt |; }$ 5 0 ;0 F ollow(S) reduce S stmt
{S |; }$ 7 shif t
{S; |}$ 9 shif t
{S; } | $ 6 0 $0 F ollow(B) reduce B }
{S; B | $ 10 0 $0 F ollow(B) reduce B S; B
{B | $ 3 0 $0 F ollow(S) reduce S {B
S|$ 4 0 $0 F ollow(S 0 ) reduce P S
P |$ ACCEP T
Fall 2010/2011 page 5 of 5