- 217 Views
- Uploaded on

Download Presentation
## PowerPoint Slideshow about 'Lecture 11 Context-Free Grammar' - libitha

**An Image/Link below is provided (as is) to download presentation**

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.

- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -

Presentation Transcript

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 xBy and we say that

xAy derivates xBy.

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

- 0 1 in L(G) because S * 0 1 .
- L(G) = {0 1 | n > 0}

n

n

n

n

n

n

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

xn

S x1q1 x1x2q2 ···

x1…xnf x1…xn

f=qn

s

q1

There is a path associated with x

from initial state to a final state.

x in L(M)

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,

draw an edge A → B.

x

x

For each rule A → x, x in Σ*, draw an edge A → f

G = ({S,A}, {0,1}, {S→0S | 10A, A→00}, S)

0

10

00

f

S

A

This string-labeled digraph with initial state S and a final

state f is a state diagram of an NFA M.

There is a path associated with x

from S to f in M.

S * x in Σ*

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 right-linear 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),

construct G = (V, Σ, R , S) where

R = {A→W | A→W in R}.

- G is right-linear. Hence, L(G ) is regular.
- Therefore, L(G) = L(G ) is regular.

R

R

R

R

R

R

R

R

Example 1

G = ({S,A}, {0, 1}, {S→A01, A→10}, S)

G =({S,A}, {0, 1}, {S →10A, A →01}, S)

NFA accepts L(G )

L(G )={1001}

L(G)={1001}

R

R

10

01

S

A

R

Example 2

L(G) = 0*1

L(G ) = 10*

NFA accepts 10*

G = ({S,A}, {0,1}, {S →1A, A →0A|ε}, S)

G = ({S,A}, {0,1}, {S →A1, A →A0|ε}, S)

R

1

S

A

0

R

Download Presentation

Connecting to Server..