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

sliek môn máy tự động chương 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 (100.67 KB, 36 trang )

Chapter 5: Context-Free Grammar
November 9, 2009
Chapter 5: Context-Free Grammar
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 are not regular.
Chapter 5: Context-Free Grammar
Context-Free Grammars
G = (V, T, S, P)
Productions are of the form:
A → x
A ∈ V and x ∈ (V ∪ T )

Chapter 5: Context-Free Grammar
CFG and Regular Language
A regular language is also a context-free language.
Why the name?
Chapter 5: Context-Free Grammar
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}

}
Chapter 5: Context-Free Grammar
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
Chapter 5: Context-Free Grammar
Example 5.3
L = {a
n
b
m
| n = m} is context-free.
S → AS
1

| S
1
B
S
1
→ aS
1
b | λ
A → aA | a
B → bB | b
Chapter 5: Context-Free Grammar
Example 5.4
G = ({S}, {a, b}, S, P)
P = { S → aSb | SS | λ}
S ⇒ aSb ⇒ aaSbb ⇒ aaSSbb ⇒ aaabSbb ⇒ aaababbb
L(G) = ?
Chapter 5: Context-Free Grammar
Derivations
G = ({S, A, B}, {a, b}, S, {S → AB, A → aaA, A → λ, B →
Bb, B → λ})
S ⇒ AB ⇒ aaAB ⇒ aaB ⇒ aaBb ⇒ aab
S ⇒ AB ⇒ ABb ⇒ aaABb ⇒ aaAb ⇒ aab
Chapter 5: Context-Free Grammar
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.
Chapter 5: Context-Free Grammar
Example 5.5

G = ({S, A, B}, {a, b}, S, {S → AB, A → aaA, A → λ, B →
Bb, B → λ})
S ⇒ AB ⇒ aaAB ⇒ aaB ⇒ aaBb ⇒ aab leftmost
S ⇒ AB ⇒ ABb ⇒ aaABb ⇒ aaAb ⇒ aab
Chapter 5: Context-Free Grammar
Ordered Tree for a Production
A → abABc
A
a
b A B
c
Chapter 5: Context-Free Grammar
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. 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.
Chapter 5: Context-Free Grammar
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 (not need).
2. Every leaf has a label from V ∪ T ∪ {λ}.
3. Every interior vertex has a label from V.
4. 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.
Chapter 5: Context-Free Grammar
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.

Chapter 5: Context-Free Grammar
Example 5.6
S
a
A
b B b
λ
B
A
b B b
λ
yield: abbbb
S → aAB
A → bBb
B → A | λ
Chapter 5: Context-Free Grammar
Example 5.7
S
a
A
b B b
λ
B
A
b B b
λ
partial derivation tree
S → aAB
A → bBb
B → A | λ

Chapter 5: Context-Free Grammar
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 tG is a sentential form of G.
Chapter 5: Context-Free Grammar
Membership and Parsing
Membership algorithm: tells if w ∈ L(G).
Parsing: finding a sequence of productions by which w ∈ L(G)
is derived.
Chapter 5: Context-Free Grammar
Example 5.8
S → SS | aSb | bSa | λ
w = aabb
Chapter 5: Context-Free Grammar
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
Chapter 5: Context-Free Grammar
Exhaustive Search Parsing Disadvantages
It is not efficient.
If w /∈ L(G) then it may never terminate, due to:

A → B
A → λ
Chapter 5: Context-Free Grammar
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.
Chapter 5: Context-Free Grammar
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)
Chapter 5: Context-Free Grammar
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.
Chapter 5: Context-Free Grammar

×