Tải bản đầy đủ (.pdf) (8 trang)

Tutorial 2 solutions

Bạn đang xem bản rút gọn của tài liệu. Xem và tải ngay bản đầy đủ của tài liệu tại đây (34.08 KB, 8 trang )

SOLUTION 2
1.
S à (L) | a
L à L,S | S
a) What are the terminal, nonterminal and start symbol?
Terminals: {‘,’,a,’(‘,’)’};nonterminals: {S, L}; start symbol: S
b) Find parse tree for the following sentences:
(a,a)

(a,(a,a))


(a,((a,a),(a,a)))

c) Construct a leftmost derivation for each sentence given in (b)
S ⇒ (L) ⇒ (L,S) ⇒ (S,S) ⇒ (a,S) ⇒ (a,a)
S ⇒ (L) ⇒ (L,S) ⇒ (S,S) ⇒ (a,S) ⇒ (a,(L)) ⇒ (a,(L,S)) ⇒ (a,(S,S)) ⇒ (a,(a,S))
⇒ (a,(a,a))
S ⇒ (L) ⇒ (L,S) ⇒ (S,S) ⇒ (a,S) ⇒ (a,(L)) ⇒ (a,(L,S)) ⇒ (a,(S,S)) ⇒
(a,((L),S)) ⇒ (a,((L,S),S)) ⇒ (a,((S,S),S)) ⇒ (a,((a,S),S)) ⇒ (a,((a,a),S)) ⇒
(a,((a,a),(L))) ⇒ (a,((a,a),(L,S))) ⇒ (a,((a,a),(S,S))) ⇒ (a,((a,a),(a,S))) ⇒
(a,((a,a),(a,a)))
d) Construct a rightmost derivation for each sentence given in (b)
S ⇒ (L) ⇒ (L,S) ⇒ (L,a) ⇒ (S,a) ⇒ (a,a)
S ⇒ (L) ⇒ (L,S) ⇒ (L,(L)) ⇒ (L,(L,S)) ⇒ (L,(L,a)) ⇒ (L,(S,a)) ⇒ (L,(a,a)) ⇒
(S,(a,a)) ⇒ (a,(a,a))


S ⇒ (L) ⇒ (L,S) ⇒ (L,(L)) ⇒ (L,(L,S)) ⇒ (L,(L,(L,S))) ⇒ (L,(L,(L,a))) ⇒
(L,(L,(S,a))) ⇒ (L,(L,(a,a))) ⇒ (L,(S,(a,a))) ⇒ (L,((L),(a,a))) ⇒ (L,((L,S),(a,a)))
⇒ (L,((L,a),(a,a))) ⇒ (L,((S,a),(a,a))) ⇒ (L,((a,a),(a,a))) ⇒ (S,((a,a),(a,a))) ⇒


(a,((a,a),(a,a)))
e) What is the language generated by this grammar?
A type of expression in which ‘,’ is used as the only binary operator. Operands
can be either a or a nested parenthesized expression.
2. Consider the following grammar
S à aSbS| bSaS | ∈
a. Find a rightmost derivation for abab
S ⇒ aSbS ⇒ aSbaSbS ⇒ aSbaSb ⇒ aSbab ⇒ abab
S ⇒ aSbS ⇒ aSb ⇒ abSaSb ⇒ aSbab ⇒ abab
b. Construct all possible parse trees for abab

c. Is this grammar ambiguous? Why?
The grammar is ambiguous since we can construct more than one parse tree for
abab
d. What is the language generated by this grammar?
L = {w| na(w) = nb(w)}
3. Write a grammar that generates all of boolean expressions. Construct the
corresponding parse tree for not (true or false).
bexp à bexp or bexp | bexp and bexp | not bexp | (bexp) | true | false | ID


4.
S à (L) | a
L à L,S | S
a. Eliminate the left-recursion from the grammar in Exercise 1
L à L,S | S
ó
L à SL'
L’ à ,SL’ | ∈
b. Compute the First, Follow and Select sets for the transformed grammar

Transformed grammar
S à (L) | a
L à SL'
L’ à ,SL’ | ∈
First sets:
First(S) = {(,a}
First(L) = {(,a}
First(L’) = {,,∈}
Follow sets:
Follow(S) = {$,,,)}
Follow(L) = {)}
Follow(L’) = {)}
Select sets:
Select(Sà(L)) = {(}
Select(Sàa) = {a}
Select(LàSL’) = {(,a}
Select(L’à,SL’) = {,}
Select(L’à∈) = {)}


c. Construct a predictive parser for the transformed grammar
S
L
L’

(
Sà (L)
Là SL’

)


a
Sàa
Là SL’

,

$

L’à,SL’

L’à∈

d. Show the behavior of the parser for the sentences given in Exercise 1b
(a,a)
Stack
$S
$)L(
$)L
$)L’S
$)L’a
$)L’
$)L’S,
$)L’S
$)L’a
$)L’
$)
$

Input

(a,a)$
(a,a)$
a,a)$
a,a)$
a,a)$
,a)$
,a)$
a)$
a)$
)$
)$
S

Stack
$S
$)L(
$)L
$)L’S
$)L’a
$)L’
$)L’S,
$)L’S
$)L’)L(
$)L’)L
$)L’)L’S

Input
(a,(a,a))$
(a,(a,a))$
a,(a,a))$

a,(a,a))$
a,(a,a))$
,(a,a))$
,(a,a))$
(a,a))$
(a,a))$
a,a))$
a,a))$

Ouput
Sà(L)
LàSL’
Sàa
L’à,SL’
Sàa
L’à∈

(a,(a,a))
Ouput
Sà (L)
Là SL’
Sàa
L’à,SL’
Sà (L)
Là SL’


$)L’)L’a
$)L’)L’
$)L’)L’S,

$)L’)L’S
$)L’)L’a
$)L’)L’
$)L’)
$)L’
$)
$

a,a))$
,a))$
,a))$
a))$
a))$
))$
))$
)$
)$
$

Sàa
L’à,SL’
Sàa
L’à∈
L’à∈

(a,(a,a),(a,a)))
Stack
$S
$)L(
$)L

$)L’S
$)L’a
$)L’
$)L’S,
$)L’S
$)L’)L(
$)L’)L
$)L’)L’S
$)L’)L’)L(
$)L’)L’)L
$)L’)L’)L’S
$)L’)L’)L’a
$)L’)L’)L’
$)L’)L’)L’S,
$)L’)L’)L’S
$)L’)L’)L’a
$)L’)L’)L’
$)L’)L’)
$)L’)L’
$)L’)L’S,
$)L’)L’S
$)L’)L’)L(
$)L’)L’)L

Input
(a,((a,a), (a,a)))$
(a,((a,a), (a,a)))$
a,((a,a), (a,a)))$
a,((a,a), (a,a)))$
a,((a,a), (a,a)))$

,((a,a), (a,a)))$
,((a,a), (a,a)))$
((a,a), (a,a)))$
((a,a), (a,a)))$
(a,a), (a,a)))$
(a,a), (a,a)))$
(a,a), (a,a)))$
a,a), (a,a)))$
a,a), (a,a)))$
a,a), (a,a)))$
,a), (a,a)))$
,a), (a,a)))$
a), (a,a)))$
a), (a,a)))$
), (a,a)))$
), (a,a)))$
, (a,a)))$
, (a,a)))$
(a,a)))$
(a,a)))$
a,a)))$

Ouput
Sà (L)
Là SL’
Sàa
L’à,SL’
Sà (L)
Là SL’
Sà (L)

Là SL’
Sàa
L’à,SL’
Sàa
L’à∈
L’à,SL’
Sà (L)


$)L’)L’)L’S
$)L’)L’)L’a
$)L’)L’)L’
$)L’)L’)L’S,
$)L’)L’)L’S
$)L’)L’)L’a
$)L’)L’)L’
$)L’)L’)
$)L’)L’
$)L’)
$)L’
$)
$

a,a)))$
a,a)))$
,a)))$
,a)))$
a)))$
a)))$
)))$

)))$
))$
))$
)$
)$
$

Là SL’
Sàa
L’à,SL’
Sàa
L’à∈
L’à∈
L’à∈

5. Eliminate left-recursion and left-factoring for the grammar constructed in
Exercise 3, if present.
bexp à bexp or bexp | bexp and bexp | not bexp | (bexp) | true | false | ID
- Eliminate left-factoring
bexp à bexp bexp_tail | not bexp | (bexp) | true | false | ID
bexp_tail à or bexp | and bexp
- Eliminate left-recursion
bexp à bexp_head bexp_tail’
bexp_head à not bexp | (bexp) | true | false | ID
bexp_tail’ à bexp_tail bexp_tail’ | ∈
bexp_tail à or bexp | and bexp


TUTORIAL 3
1. Consider the following grammar

E à T+E | T
T à id
a. Construct the collection of sets of LR(0) items for this grammar
b. Construct the parsing table using SLR method
c. Show the moves according to the table constructed above on the inputs of
id+id and id+id+id
2. Show that the following grammar is LL(1) but not SLR(1). Also, show that the
grammar is LR(1) as well.
S à AaAb | BbBa
Aà∈
Bà∈
Hint: Note that S à •∈ is concurrently equivalent to S à ∈•, or we can write S
à•
3. Show that the following grammar is LR(1) but not SLR(1)
S à Aa | bAc | dc | bda
Aàd
4. Show that the following grammar is LR(1) but not LALR(1)
S à As | bAc | Bc | bBa
Aàd
Bàd



Tài liệu bạn tìm kiếm đã sẵn sàng tải về

Tải bản đầy đủ ngay
×