Забыли?

?

# Lecture 11 Context

код для вставкиСкачать
```Lecture 11 Context-Free
Grammar
Definition
вЂў A context-free grammar (CFG) G is a
quadruple (V, ОЈ, R, S) where
вЂў V: a set of non-terminal symbols
вЂў ОЈ: a set of terminals (V в€© ОЈ = Зѕ)
вЂў R: a set of rules (R: V в†’ (V U ОЈ)*)
вЂў S: a start symbol.
Example
вЂў V = {q, f,}
вЂў ОЈ = {0, 1}
вЂў R = {q в†’ 11q, q в†’ 00f,
f в†’ 11f, f в†’ Оµ
}
вЂў S=q
вЂў (R= {q в†’ 11q | 00f, f в†’ 11f | Оµ })
How do we use rules?
вЂў If A в†’ B, then xAy
xAy derivates xBy.
xBy and we say that
вЂў If s В·В·В· t, then we write s * t.
вЂў A string x in ОЈ* is generated by G=(V,ОЈ,R,S)
if S * x.
вЂў L(G) = { x in ОЈ* | S * x}.
Example
вЂў
вЂў
вЂў
вЂў
G = ({S}, {0,1}. {S в†’ 0S1 | Оµ }, S)
Оµ in L(G) because S Оµ .
01 in L(G) because S 0S1 01.
0011 in L(G) because
S 0S1 00S11 0011.
n n
n n
вЂў 0 1 in L(G) because S * 0 1 .
n n
вЂў L(G) = {0 1 | n > 0}
Context-Free Language (CFL)
вЂў A language L is context-free if there exists
a CFG G such that L = L(G).
Theorem
вЂў For every regular set, there exists a CFG
G such that L=L(G).
Proof.
Let L=L(M) for a DFA M=(Q, ОЈ, Оґ, s, F).
Construct a CFG G=(V, ОЈ, R, S) as follows.
V = Q,
ОЈ = ОЈ,
R = { q в†’ ap | Оґ(q,a) = p } U { f в†’ Оµ | f in F},
S = s.
x1
s
xn
q1
S
f=qn
x q x x q В·В·В·
x1вЂ¦xnf x1вЂ¦xn
1
1
1
2
2
x in L(M)
There is a path associated with x
from initial state to a final state.
S
*x
Therefore, L(M) = L(G).
Corollary
вЂў Every regular language is a CFL.
вЂў The class of regular languages is a proper
subclass of CFLs.
CFL
Regular
Why, proper?
Regular Grammar
вЂў Regular grammar is a CFG (V, ОЈ, R, S)
such that every rule is in form
Vв†’ ОЈ*(V+Оµ)
вЂў Example
G = ({S, A}, {0, 1}, {S в†’1A, A в†’ 00}, S)
вЂў Remark: Every regular language can be
generated by a regular grammar.
Theorem
вЂў Every regular grammar generates a
regular language.
Proof.
Consider a regular grammar G=(V, ОЈ, R, S).
Construct a string-labeled digraph with vertex set V U {f}
as follows:
For each rule A в†’ xB, x in ОЈ* and B in V,
x
draw an edge A в†’
B.
x
For each rule A в†’ x, x in ОЈ*, draw an edge A в†’
f
G = ({S,A}, {0,1}, {Sв†’0S | 10A, Aв†’00}, S)
Example
0
10
S
A
00
f
This string-labeled digraph with initial state S and a final
state f is a state diagram of an NFA M.
S
* x in ОЈ*
There is a path associated with x
from S to f in M.
Therefore, L(G) = L(M).
Corollary
A language L is regular if and only if L can
be generated by a regular grammar.
Right-Linear and Left-Linear
вЂў The regular grammar is also called a rightlinear grammar.
вЂў A grammar G=(V, ОЈ, R, S) is left-linear if
every rule is in form
V в†’ (V+Оµ)ОЈ*.
(e.g., ({S,A}, {0, 1}, {Sв†’A01, Aв†’10}, S)
Remark: Every language generated from a left-linear
grammar is regular.
Why?
Why?
вЂў For left-linear grammar G = (V, ОЈ, R, S),
R
R
construct G = (V, ОЈ, R , S) where
R
R
R = {Aв†’W | Aв†’W in R}.
R
R
вЂў G is right-linear. Hence, L(G ) is regular.
R R
вЂў Therefore, L(G) = L(G ) is regular.
Example 1
G = ({S,A}, {0, 1}, {Sв†’A01, Aв†’10}, S)
R
G =({S,A}, {0, 1}, {S в†’10A, A в†’01}, S)
R
NFA accepts L(G )
10
S
R
L(G )={1001}
L(G)={1001}
01
A
Example 2
L(G) = 0*1
R
L(G ) = 10*
NFA accepts 10*
1
S
A
0
G = ({S,A}, {0,1}, {S в†’1A, A в†’0A|Оµ}, S)
G = ({S,A}, {0,1}, {S в†’A1, A в†’A0|Оµ}, S)
R
```
###### Документ
Категория
Презентации
Просмотров
7
Размер файла
327 Кб
Теги
1/--страниц
Пожаловаться на содержимое документа