1. Consider the following grammar. Note that the terminals include x, [, ], (, and ).
S
→
[ST] | T
T
→
(TF) | F
F
→
x |
ε
(a) Compute the First set for all nonterminals in the grammar.
First(S) = { [, (, x,
ε
}
First(T) = { (, x,
ε
}
First(F) = { x,
ε
}
(b) Compute the First set for each production (i.e., the right side of the production).
First(S
→
[ST]) = { [ }
First(S
→
T) = { (, x,
ε
}
First(T
→
(TF)) = { ( }
First(T
→
F) = { x,
ε
}
First(F
→
x) = { x }
First(F
→
ε
) = {
ε
}
(c) Compute the Follow set for all non-terminals in the grammar.
Follow(S) = { ], (, x, $}
Follow(T) = { ], (, ), x, $ }
Follow(F) = { ], (, ), x, $ }
(d) Based on the First and Follow sets you obtained in (a), (b), and (c), determine whether the
grammar is LL(1). Explain how your decision is made.
The grammar is not LL(1). Because there is an overlap between the first set of one nonempty
production and the follow set of the left side non-terminal of that production.
F
→
ε
, First(F
→
x) = { x }, Follow(F) = { ], (, ), x, $ }
b
First(F
→
x)
∩
Follow(F)
≠
φ
(e) Build an LL(1)
parsing table
for the grammar.
x
[
]
(
)
$
S
S
→
T
S
→
[ST]
S
→
T
S
→
T
S
→
T
T
T
→
F
T
→
F
T
→
(TF)
T
→
F
T
→
F
T
→
F
F
F
→
x
F
→
ε
F
→
ε
F
→
ε
F
→
ε
F
→
ε
(f) Parse the string [[x()](((xx))x)].
When there are conflicts, choose the one you know that
would work.
Show the stack, the input, and the action taken.