Assignment 2: 1 Context-Free Grammars
Assignment 2: 1 Context-Free Grammars
Solutions
1 Context-free grammars
Give the definition of a context free grammar over the alphabet Σ = {a, b} that describes all strings that
have a different number of ’a’s and ’b’s.
Answer:
S → U |V
U → T aU |T aT
V → T bV |T bT
T → aT bT |bT aT |ϵ
The intuition is that the string will have either more ’a’s (non-terminal U ) or more ’b’s (non-terminal V ).
Non-terminal T produces a string with balanced ’a’s and ’b’s.
S → xTU | lX | X
T → c|l
X → xX | U
U → iY | vI | I
Y → x|v
I → iI | ϵ
. .x
.
.T .l
.S .
. .i
.
.U .I
. .i
.I .
.I .ϵ
1
2. Is this grammar ambiguous?
Answer: No
3. 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 their value. The final value should be returned in
S.val.
Answer:
Given c.val = 100, l.val = 50, x.val = 10, v.val = 5, i.val = 1 and ϵ.val = 0:
3 LL(1) Parsers
In the following context-free grammar, the symbols 0, 1, 2 and 3 are terminals and S is the initial symbol.
S → 0|1S 2S 3|1A3
A → S |AS
1. Explain briefly why this grammar is not LL(1).
Answer:
This grammar cannot be parsed by a recursive descent parser. This can be shown by the following two
examples:
• If the parser has to expand an S non-terminal and the next token is 1, it is not possible to choose
between the 2 productions from S that start with 1 with just this information. However LL(1)
languages allow for just one look-ahead symbol.
• If the parser were to make use of the A → AS production, for some look-ahead symbol, then in
the new state it would still have to expand the new A with the same look-ahead, leading to an
infinite loop.
2. Convert this grammar to an equivalent that is LL(1).
Answer:
• Factorize the S productions and eliminate immediate left recursion from the A productions:
S → 0 | 1 S′
S′ → S 2 S 3 | A 3
A → S A′
A′ → S A′ | ϵ
2
• Inline singular A production rule to uncover another possible factorization:
S → 0 | 1 S′
S′ → S 2 S 3 | S A′ 3
A′ → S A′ | ϵ
• Factorize the S ′ production and inline the new singular S ′ it in S’s production:
S → 0 | 1 S S ′′
′′
S → 2 S 3 | A′ 3
′
A → S A′ | ϵ
3. For the grammar of the previous subtask, construct the complete LL(1) parsing table.
Answer:
First(S) = {0, 1} Follow (S) = {0, 1, 2, 3, $}
First(S ′′ ) = {0, 1, 2, 3} Follow (S ′′ ) = {0, 1, 2, 3, $}
First(A′ ) = {0, 1, ϵ} Follow (A′ ) = {3}
0 1 2 3 $
S S→0 S → 1 S S ′′
S ′′ S ′′ → A′ 3 S ′′ → A′ 3 S ′′ → 2 S 3 S ′′ → A′ 3
A′ A′ → S A′ A′ → S A′ A′ → ϵ
4. Show all the steps required to parse the input string: 1 1 0 2 0 3 0 1 0 3 3
Answer:
Stack Input Action
S $ 110 2 0301 0 3 3 $ S → 1 S S ′′
1 S S ′′ $ 110 2 0301 0 3 3 $ terminal
S S ′′ $ 10 2 0301 0 3 3 $ S → 1 S S ′′
1 S S ′′ S ′′ $ 10 2 0301 0 3 3 $ terminal
S S ′′ S ′′ $ 0 2 0301 0 3 3 $ S→0
0 S ′′ S ′′ $ 0 2 0301 0 3 3 $ terminal
S ′′ S ′′ $ 2 0301 0 3 3 $ S ′′ → 2 S 3
2 S 3 S ′′ $ 2 0301 0 3 3 $ terminal
S 3 S ′′ $ 0301 0 3 3 $ S→0
0 3 S ′′ $ 0301 0 3 3 $ terminal
3 S ′′ $ 301 0 3 3 $ terminal
S ′′ $ 01 0 3 3 $ S ′′ → A′ 3
′
A 3 $ 01 0 3 3 $ A′ → S A′
S A′ 3 $ 01 0 3 3 $ S→0
0 A′ 3 $ 01 0 3 3 $ terminal
A′ 3 $ 1 0 3 3 $ A′ → S A′
S A′ 3 $ 1 0 3 3 $ S → 1 S S ′′
1 S S ′′ A′ 3 $ 1 0 3 3 $ terminal
S S ′′ A′ 3 $ 0 3 3 $ S→0
0 S ′′ A′ 3 $ 0 3 3 $ terminal
S ′′ A′ 3 $ 3 3 $ S ′′ → A′ 3
A 3 A′ 3
′
$ 3 3 $ A′ → ϵ
3 A′ 3 $ 3 3 $ terminal
A′ 3 $ 3 $ A′ → ϵ
3 $ 3 $ terminal
$ $ ACCEPT
3
4 LR(1) Parsers
In the following context-free grammar, the symbols (, a, ) and , are terminals. and S is the initial symbol.
(1) S → (L)
(2) S → a
(3) L → L,S
(4) L → S
Because , is a symbol of the language we are going to use | as a separator between the core of the LR(1)
items and the lookahead symbols. Lookaheads with the same core can be separated as usual with /.
2. Construct the full LR(1) DFA, showing all items in each state.
Answer:
New unique initial production: (0) E → S
[ S→ ( · L) | $ ]
[E→ ·S| $] ( [ L→ ·L,S | )/, ]
S
start 0 [S→ · .( L ) | $] 3 [ L→ ·S | )/, ] 5 [L→ S · | )/, ]
[S→ ·a| $] [ S→ ·(L) | )/, ]
[ S→ ·a | )/, ]
( S
S
a a
1 [E→ S · | $] [ S→ ( · L) | )/, ]
[ L→ ·L,S | )/, ]
a
L 6 [S→ a · | )/, ] 4 [ L→ ·S | )/, ] (
[ S→ ·(L) | )/, ]
2 [S→ a · | $] [ S→ ·a | )/, ]
a (
L
[L→ L, · S| )/, ]
[S→ (L · )| $] , , [S→ (L · )| )/, ]
7 11 [ S → ·(L)| )/, ] 9
[L→ L · ,S| )/, ] [L→ L · ,S| )/, ]
[S→ ·a| )/, ]
) S )
4
3. Construct the LR(1) parsing table using the DFA. For the reduce actions, please use the provided
enumeration of the productions in the grammar.
Answer:
State ( a ) , $ S L
0 s3 s2 1
1 ACCEPT
2 r2
3 s4 s6 5 7
4 s4 s6 5 9
5 r4 r4
6 r2 r2
7 s8 s11
8 r1
9 s10 s11
10 r1 r1
11 s4 s6 12
12 r3 r3