context free languages - university of torontoazadeh/page11/page12/material/cfg.pdfidea remember...

28
CONTEXT FREE LANGUAGES

Upload: doankien

Post on 05-May-2018

218 views

Category:

Documents


3 download

TRANSCRIPT

CONTEXT FREE LANGUAGES

CONTEXT-FREE LANGUAGES

REGULAR

CONTEXT-FREE

• Context-Free Grammars

• Push-Down Automata

IDEA

Remember ”balanced parantheses” example from structural induction?

IDEA

Remember ”balanced parantheses” example from structural induction?

S =�

�(S)S

IDEA

Remember ”balanced parantheses” example from structural induction?

S =�

�(S)S

S −→ (S)S | �

If we write it this way:

IDEA

Remember ”balanced parantheses” example from structural induction?

S =�

�(S)S

it is a context-free grammar generating a context-free language con-taining all balanced parantheses strings.

S −→ (S)S | �

If we write it this way:

CONTEXT-FREE GRAMMARS

A context-free grammar is a tuple G = (V, T, P, S) where

• V is finite set of variables or nonterminals.

• T is a finite set of terminals.

• V ∩ T = ∅

• S ∈ V is a unique start symbol.

• P is a finite set of productions (rules).

CONTEXT-FREE GRAMMARS

A context-free grammar is a tuple G = (V, T, P, S) where

• V is finite set of variables or nonterminals.

• T is a finite set of terminals.

• V ∩ T = ∅

• S ∈ V is a unique start symbol.

• P is a finite set of productions (rules).

A −→ α

CONTEXT-FREE GRAMMARS

A context-free grammar is a tuple G = (V, T, P, S) where

• V is finite set of variables or nonterminals.

• T is a finite set of terminals.

• V ∩ T = ∅

• S ∈ V is a unique start symbol.

• P is a finite set of productions (rules).

A −→ α A ∈ Vα ∈ (V ∪ T )∗

EXAMPLE

G = ({S}, {0, 1}, , S)

EXAMPLE

G = ({S}, {0, 1}, , S)

S −→ 0

S −→ 1

S −→ �

S −→ 0S0

S −→ 1S1

EXAMPLE

G = ({S}, {0, 1}, , S)

S −→ 0

S −→ 1

S −→ �

S −→ 0S0

S −→ 1S1

Generates Palindromes!

EXAMPLE

G = ({S}, {0, 1}, , S)

S −→ 0

S −→ 1

S −→ �

S −→ 0S0

S −→ 1S1

Generates Palindromes!

S −→ 0S0 | 1S1 | � | 1 | 0

HOW DO WE DEFINE THE LANGUAGE OF A

GRAMMAR?

DERIVATIONS

DERIVATIONS

Let α1, α2 ∈ (V ∪ T )∗, we say:

α1 =⇒G α2

DERIVATIONS

Let α1, α2 ∈ (V ∪ T )∗, we say:

α1 =⇒G α2

If:

∃A ∈ V ∃β, γ, λ ∈ (V ∪ T )∗

DERIVATIONS

Let α1, α2 ∈ (V ∪ T )∗, we say:

α1 =⇒G α2

If:

∃A ∈ V ∃β, γ, λ ∈ (V ∪ T )∗

such that

α1 = βAγ α2 = βλγ

DERIVATIONS

Let α1, α2 ∈ (V ∪ T )∗, we say:

α1 =⇒G α2

If:

∃A ∈ V ∃β, γ, λ ∈ (V ∪ T )∗

such that

α1 = βAγ α2 = βλγ

A −→ λ

And

is in P

DERIVATIONS

Let α1, α2 ∈ (V ∪ T )∗, we say:

α1 =⇒G α2

If:

∃A ∈ V ∃β, γ, λ ∈ (V ∪ T )∗

such that

α1 = βAγ α2 = βλγ

A −→ λ

And

is in P

α2 can be derived from α1 in a single production application.

LANGUAGE OF A GRAMMAR

Let =⇒∗G be reflexive and transitive closure of =⇒G.

LANGUAGE OF A GRAMMAR

Let =⇒∗G be reflexive and transitive closure of =⇒G.

α1 =⇒∗G α2 iff α2 can be derived from α1 in finite number (even zero)

of applications of productions in P

LANGUAGE OF A GRAMMAR

Let =⇒∗G be reflexive and transitive closure of =⇒G.

α1 =⇒∗G α2 iff α2 can be derived from α1 in finite number (even zero)

of applications of productions in P

L(G) = {w : w ∈ T ∗ and S =⇒∗G w}

LANGUAGE OF A GRAMMAR

Let =⇒∗G be reflexive and transitive closure of =⇒G.

α1 =⇒∗G α2 iff α2 can be derived from α1 in finite number (even zero)

of applications of productions in P

L(G) = {w : w ∈ T ∗ and S =⇒∗G w}

The language generated by G is all strings of terminals derivable fromthe start symbol.

LANGUAGE OF A GRAMMAR

Let =⇒∗G be reflexive and transitive closure of =⇒G.

α1 =⇒∗G α2 iff α2 can be derived from α1 in finite number (even zero)

of applications of productions in P

L(G) = {w : w ∈ T ∗ and S =⇒∗G w}

The language generated by G is all strings of terminals derivable fromthe start symbol.

Context-Free Languages = {L : ∃ grammar (CFG) G s.t. L(G) = L}

EXAMPLE

L = {0n1n| n ≥ 0}

EXAMPLE

S −→ 0S1 | �

L = {0n1n| n ≥ 0}

EXAMPLE

{w : w has equal number of 0’s and 1’s}