Tải bản đầy đủ (.ppt) (37 trang)

Chapter 5: Context - Free Grammar

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 (97.62 KB, 37 trang )

Chapter 5: Context-Free Grammar
Quan Thanh Tho

2
Non-regular Languages

Not all languages are regular.

L
1
= {a
n
b
n
| n ≥ 0} is not regular.
L
2
= {(), (()), ((())), } is not regular.

some properties of programming languages
3
Context-Free Grammars
G = (V, T, S, P)
Productions are of the form:
A → x
A ∈ V and x ∈ (V ∪ T)
*
4
CFG and Regular Language

A regular language is also a context-free language.


• Why the name?
5
Example 5.1

G = ({S}, {a, b}, S, P)
P = { S → aSa
S → bSb

S → λ
}
S ⇒ aSa ⇒ aaSaa ⇒ aabSbaa ⇒ aabbaa
L(G) = {ww
R
| w ∈ {a, b}*}
6
Example 5.2

G = ({S, A, B}, {a, b}, S, P)
P = { S → abB
A → aaBb

B → bbAa
A → λ
}
S ⇒ abB ⇒ abbbAa ⇒ abbbaaBba ⇒ abbbaabbAaba ⇒ abbbaabbaba
7
Example 5.3

L = {a
n

b
m
| n ≠ m} is context-free.
G = (?, {a, b}, S, ?)
P = { S → AS
1
| S
1
B
S
1
→ aS
1
b | λ
A → aA | a

B → bB | b
}
8
Example 5.4

G = ({S}, {a, b}, S, P)
P = { S → aSb | SS | λ}
S ⇒ aSb ⇒ aaSbb ⇒ aaSSbb ⇒ aaabSbb ⇒ aaababbb
L(G) = ?
9
Derivations

G = ({S, A, B}, {a, b}, S, {S → AB, A → aaA, A → λ, B → Bb, B → λ})
1 2 3 4 5


S ⇒ AB ⇒ aaAB ⇒ aaB ⇒ aaBb ⇒ aab
S ⇒ AB ⇒ ABb ⇒ aaABb ⇒ aaAb ⇒ aab
1 2 3 4 5
1 4 2 5 3
10
Leftmost and Rightmost Derivations

Leftmost: in each step the leftmost variable in the
sentential form is replaced.

Rightmost: in each step the rightmost variable in the
sentential form is replaced.
11
Example 5.5

G = ({S, A, B}, {a, b}, S, {S → AB, A → aaA, A → λ, B → Bb, B → λ})
1 2 3 4 5

S ⇒ AB ⇒ aaAB ⇒ aaB ⇒ aaBb ⇒ aab leftmost
S ⇒ AB ⇒ ABb ⇒ aaABb ⇒ aaAb ⇒ aab
1 2 3 4 5
1 4 2 5 3
12
Ordered Tree for a Production
A → abABc
A
ba cBA
ordered tree
13

Derivation Tree

Let G = (V, T, S, P) be a context-free grammar.
An ordered tree is a derivation tree iff:
1. The root is labeled S.
2. Every leaf has a label from T ∪ {λ}.
3. Every interior vertex has a label from V.
4. A vertex has label A∈V and its children are labeled a
1
, a
2
, , a
n
iff
P contains the production A → a
1
a
2
a
n
.
5. A leaf labeled λ has no siblings.
14
Partial Derivation Tree

Let G = (V, T, S, P) be a context-free grammar.
An ordered tree is a partial derivation tree iff:
1. The root is labeled S.
2. Every leaf has a label from T ∪ {λ}.
Every leaf has a label from V ∪ T ∪ {λ}.

1. Every interior vertex has a label from V.
2. A vertex has label A∈V and its children are labeled a
1
, a
2
, , a
n
iff
P contains the production A → a
1
a
2
a
n
.
5. A leaf labeled λ has no siblings.
15
Yield of a Tree
The string of symbols obtained by reading the leaves of a tree from left to
right (omitting any λ's encountered) is called the yield of the tree.
16
Example 5.6
S → aAB
A → bBb
B → A | λ
yield: abbbb
S
b
a
B

b
A
B
λ
b b
A
B
λ
17
Example 5.7
S → aAB
A → bBb
B → A | λ
S
b
a
B
b
A
B
λ
b b
A
B
λ
partial derivation tree
18
Sentential Forms & Derivation Trees

Let G = (V, T, S, P) be a context-free grammar.


w∈L(G) iff there exists a derivation tree of G whose yield is w.

If t
G
is a partial derivation tree of G whose root label is S,
then the yield of t
G
is a sentential form of G.
19
Membership and Parsing

Membership algorithm: tells if w∈L(G).
• Parsing: finding a sequence of productions by which
w∈L(G) is derived.
20
Example 5.8
S → SS | aSb | bSa | λ w = aabb
21
Exhaustive Search Parsing

Top-down parsing

S → SS | aSb | bSa | λ w = aabb
1. S ⇒ SS S ⇒ SS ⇒ SSS S ⇒ aSb ⇒ aSSb
2. S ⇒ aSb S ⇒ SS ⇒ aSbS S ⇒ aSb ⇒ aaSbb
3. S ⇒ bSa S ⇒ SS ⇒ bSaS S ⇒ aSb ⇒ abSab
4. S ⇒ λ S ⇒ SS ⇒ S S ⇒ aSb ⇒ ab
S ⇒ aSb ⇒ aaSbb ⇒ aabb
22

Exhaustive Search Parsing
Disadvantages

It is not efficient.

If w ∉ L(G) then it may never terminate, due to:
A → B
A → λ
23
Restricted Grammar for Exhaustive
Search Parsing
Suppose G = (V, T, S, P) is a context-free grammar which
does not have any rule of the form A → B or A → λ.
Then the exhaustive search parsing method can decide if
w∈L(G) or not.
24
Theorem 5.1

For every context-free grammar G, there exists an
algorithm that parses any w∈L(G) in a number of steps
proportional to |w|
3
.

Not satisfactory as linear time parsing algorithm (proportional
to the length of a string)
25
Simple Grammars (S-Grammars)
G = (V, T, S, P)
Productions are of the form:

A → ax
A ∈ V, a ∈ T, and x ∈ V
*
, and any pair (A,a) can occur
in at most one rule.

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

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