0% found this document useful (0 votes)
87 views5 pages

Assignment 2: 1 Context-Free Grammars

The document contains solutions to assignment questions on compiler design. It includes: 1. A context-free grammar that describes strings with different numbers of 'a's and 'b's. 2. A grammar for parsing lowercase Roman numerals from 1-99, including a parse tree for 42 and semantic actions to calculate the decimal value. 3. An analysis of why a given grammar is not LL(1) and its conversion to an equivalent LL(1) grammar, along with the complete LL(1) parsing table. 4. The steps to parse an input string using the LL(1) grammar and the initial closure and full LR(1) DFA for another grammar.

Uploaded by

Rahul Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views5 pages

Assignment 2: 1 Context-Free Grammars

The document contains solutions to assignment questions on compiler design. It includes: 1. A context-free grammar that describes strings with different numbers of 'a's and 'b's. 2. A grammar for parsing lowercase Roman numerals from 1-99, including a parse tree for 42 and semantic actions to calculate the decimal value. 3. An analysis of why a given grammar is not LL(1) and its conversion to an equivalent LL(1) grammar, along with the complete LL(1) parsing table. 4. The steps to parse an input string using the LL(1) grammar and the initial closure and full LR(1) DFA for another grammar.

Uploaded by

Rahul Singh
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Assignment 2

Solutions

Compiler Design I (Kompilatorteknik I) 2011

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.

2 Parsing and semantic actions


The following context-free grammar can parse all the lowercase roman numerals from 1-99. The terminal
symbols are { c, l, x, v, i } and the initial symbol is S. If you are unfamiliar with roman numerals, please
have a look at http://en.wikipedia.org/wiki/Roman_numerals.

S → xTU | lX | X
T → c|l
X → xX | U
U → iY | vI | I
Y → x|v
I → iI | ϵ

1. Draw a parse tree for 42: “xlii”


Answer:

. .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:

S → xT U {S.val = T.val − x.val + U.val}


S → lX {S.val = l.val + X.val}
S → X {S.val = X.val}
T → c {T.val = c.val}
T → l {T.val = l.val}
X1 → xX2 {X1 .val = x.val + X2 .val}
X → U {X.val = U.val}
U → iY {U.val = Y.val − i.val}
U → vI {U.val = v.val + I.val}
U → I {U.val = I.val}
Y → x {Y.val = x.val}
Y → v {Y.val = v.val}
I1 → iI2 {I1 .val = i.val + I2 .val}
I → ϵ {I.val = ϵ.val}

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 /.

1. Calculate the closure of the LR(1) item [ S → ( · L ) | $ ]


Answer:
[ S→ ( · L) | $ ]
[ L→ ·L,S | )/, ]
[ L→ ·S | )/, ]
[ S→ ·(L) | )/, ]
[ S→ ·a | )/, ]

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 )

8 [S→ (L) · | $] 12 [ L → L,S · | )/, ] 10 [ S → (L) · | )/, ]

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

4. Show all the steps required to parse the input string: (( a , a ) , a , a )


Answer:
Stack Symbols Input Action
0 (( a , a),a , a )$ shift
0,3 ( (a, a),a , a )$ shift
0,3,4 (( a, a),a , a )$ shift
0,3,4,6 ((a , a),a , a )$ reduce
0,3,4,5 ((S , a),a , a )$ reduce
0,3,4,9 ((L , a),a , a )$ shift
0,3,4,9,11 ((L, a),a , a )$ shift
0,3,4,9,11,6 ((L,a ),a , a )$ reduce
0,3,4,9,11,12 ((L,S ),a , a )$ reduce
0,3,4,9 ((L ),a , a )$ shift
0,3,4,9,10 ((L) ,a , a )$ reduce
0,3,5 (S ,a , a )$ reduce
0,3,7 (L ,a , a )$ shift
0,3,7,11 (L, a , a )$ shift
0,3,7,11,6 (L,a , a )$ reduce
0,3,7,11,12 (L,S , a )$ reduce
0,3,7 (L , a )$ shift
0,3,7,11 (L, a )$ shift
0,3,7,11,6 (L,a )$ reduce
0,3,7,11,12 (L,S )$ reduce
0,3,7 (L )$ shift
0,3,7,8 (L) $ reduce
0,1 S $ ACCEPT!

You might also like