Wa2 Solutions Computation
Wa2 Solutions Computation
1. Give a context-free grammar (CFG) for each of the following languages over the alphabet = {a, b}:
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 |
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 http://en.wikipedia.org/wiki/Roman numerals and
http://literacy.kent.edu/Minigrants/Cinci/romanchart.htm.
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 |
x T U
l v I
i I
i I
(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 S.val. Hint: have a look at the calculator examples presented in class.
S xT U {S.val = T.val 10 + U.val}
S lX {S.val = X.val + 50}
S X {S.val = X.val}
T c {T.val = 100}
T l {T.val = 50}
X1 xX2 {X1 .val = X2 .val + 10}
X U {X.val = U.val}
U iY {U.val = Y.val 1}
U vI {U.val = I.val + 5}
U I {U.val = I.val}
Y x {Y.val = 10}
Y v {Y.val = 5}
I1 iI2 {I1 .val = I2 .val + 1}
I {I.val = 0}
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, +, ), , $}
( ) 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
(c) Show the operation of an LL(1) parser on the input string ab*.
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;}.
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