theoretical computer science (bridging course) context...

93
Theoretical Computer Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Upload: others

Post on 07-Jul-2020

12 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Theoretical Computer Science (Bridging Course)

Gian Diego Tipaldi

Context Free Languages

Page 2: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Topics Covered

Context free grammars

Pushdown automata

Equivalence of PDAs and CFGs

Non-context free grammars

The pumping lemma

Page 3: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Context Free Grammars

Extend regular expressions

First studied for natural languages

Often used in computer languages

Compilers

Parsers

Pushdown automata

Page 4: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Context Free Grammars

Collection of substitution rules

Rules: Symbol -> string

Variable symbols (Uppercase)

Terminal symbols (lowercase)

Start variable

Page 5: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Context Free Grammars

Example grammar G1:

A, B are variables

0,1,# are terminals

A is the start variable

Page 6: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Context Free Grammars

Example string: 000#111

Does it belong to the grammar?

Page 7: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Context Free Grammars

Example string: 000#111

A -> 0A1

0A1 ->00A11

00A11 -> 000A111

000A111 -> 000B111

000B111 -> 000#111

Page 8: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Context Free Grammars

Example string: 000#111

A -> 0A1

0A1 ->00A11

00A11 -> 000A111

000A111 -> 000B111

000B111 -> 000#111

A A A A B

0 0 0 # 1 1 1

Parse tree for 000#111 in 𝐺1

Page 9: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Context Free Grammars

Example string: 000#111

A -> 0A1

0A1 ->00A11

00A11 -> 000A111

000A111 -> 000B111

000B111 -> 000#111

A A A A B

0 0 0 # 1 1 1

Parse tree for 000#111 in 𝐺1

Page 10: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Natural Language Example

A boy sees

The boy sees the flower

A girl with the flower likes the boy

<SENTENCE> → <NOUN-PHRASE><VERB-PHRASE>

<NOUN-PHRASE> → <CMPLX-NOUN>|<CMPLX-NOUN><PREP-PHRASE>

<VERB-PHRASE> → <CMPLX-VERB>|<CMPLX-VERB><PREP-PHRASE>

<PREP-PHRASE> → <PREP><CMPLX-NOUN>

<CMPLX-NOUN> → <ARTICLE><NOUN>

<CMPLX-VERB> → <VERB>|<VERB><NOUN-PHRASE>

<ARTICLE> → a | the

<NOUN> → boy | girl | flower

<VERB> → touches | likes | sees

<PREP> → with

Page 11: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Context Free Grammar

Definition 2.2:

A context-free grammar is a 4-tuple (𝑉,Σ,𝑅,𝑆)

where:

𝑉 is the set of variables

Σ is the set of terminals, Σ ∩ 𝑉 = ∅

𝑅 is the set of rules

𝑆∈𝑉 is the start symbol

Page 12: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Language of a grammar

u,v,w are strings, A->w a rule

uAv yields uwv: uAv uwv

u derives v: u ∗ v if

Language of a grammar

Page 13: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Parsing a string

Consider the following grammar

What are the parse trees of

a + a x a

(a + a) x a

3( , , , }

{ , , }

{ , , , ( , )}

is

|

|

( ) |

G V R E x p r

V E x p r T e rm F a c to r

a

R

E x p r E x p r T e rm T e rm

T e rm T e rm F a c to r F a c to r

F a c to r E x p r a

Page 14: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Parsing a string

Page 15: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Designing Grammars

Harder than designing automata

Few techniques can be used

Union of context free languages

Conversion from DFA (regular)

Exploit linked variables (0n1n)

Exploit recursive structure (trickier)

Page 16: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Union of Different CFGs

1 1

2 2

1 2

0 1 |

1 0 |

|

S S

S S

S S S

1

2

1 2

( ) {0 1 | 0}

( ) {1 0 | 0}

( ) ( ) ( )

n n

n n

L G n

L G n

L G L G L G

Page 17: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Conversion from DFAs

Take the same vocabulary: Σ𝑔 = Σ𝑎

For each state qi insert a variable Ri

For each transition 𝛿 𝑞𝑖 , 𝑎 = 𝑞𝑗 insert

𝑅𝑖 → 𝑎𝑅𝑗

For each accept state 𝑞𝑘 insert

𝑅𝑘 → 𝜖

Page 18: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Conversion from DFAs

Take the same vocabulary: Σ = {0,1}

Insert all the variables: V = {𝑅1, 𝑅2} Insert the rules:

q2 q1

0 1 1

0

Page 19: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Designing Linked Strings

Languages of the type

Create rules of the form

For the language above

Page 20: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Designing Recursive Strings

Example are arithmetic expressions

Create the recursive structure <Expr>

Place it where it appear <Factor>

Page 21: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Ambiguity

Generate a string in several ways

E.g., grammar G5:

No usual notion of precedence

Natural language processing

“a boy touches a girl with the flower”

Page 22: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Ambiguity

Consider the string: a + a x a

Page 23: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Ambiguity – Definition

Leftmost derivation: At every step, replace the leftmost variable

A string is generated ambiguously if it has multiple leftmost derivations

A CFG is ambiguous if generates some string ambiguously

Some context free languages are inherently ambiguous

Page 24: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Chomsky Normal Form (CNF)

Definition 2.8: A context-free grammar is in Chomsky normal form if every rule is of the form

𝐴 → 𝐵𝐶 𝐴 → 𝑎

where 𝑎 is any terminal and 𝐴,𝐵, and 𝐶 are any variables—except that 𝐵 and 𝐶

may not be the start variable. In addition we permit the rule 𝑆→𝜀, where 𝑆 is the start variable.

Page 25: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Chomsky Normal Form (CNF)

Theorem 2.9:

Any context-free language is generated by a context-free grammar in Chomsky normal form.

Page 26: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Proof Idea

Rewrite the rules not in CNF

Introduce new variables

Four cases:

Start variable on the right side

Epsilon rules: 𝐴 → ε

Unit rules: 𝐴 → 𝐵

Long and/or mixed rules: 𝐴 → 𝑎𝐴𝑏𝑏𝐵𝑎𝐵

Page 27: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Proof Idea

Start variable on the right side

Introduce a new start and 𝑆1 → 𝑆0

Epsilon rules: 𝐴 → ε

Introduce new rules without A

Unit rules: 𝐴 → 𝐵

Replace B with its production

Long and/or mixed rules: 𝐴 → 𝑎𝐴𝑏𝑏𝐵𝑎𝐵

New variables and new rules

Page 28: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Formal Proof: by Construction

1. Add a new start symbol 𝑆_0 and the rule 𝑆0 → 𝑆, where 𝑆 is the old start

2. Remove all rules 𝐴 → 𝜖 :

For each 𝑅 → 𝑢𝐴𝑣 add 𝑅 → 𝑢𝑣

For each 𝑅 → 𝐴 add 𝑅 → 𝜖

Repeat until all gone (keep 𝑆0 → 𝜖 )

3. Remove all rules 𝐴 → 𝐵 :

For each 𝐵 → 𝑢 add 𝐴 → 𝑢

Repeat until all gone

Page 29: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Formal Proof: by Construction

4. Convert all rules 𝐴 → 𝑢1…𝑢𝑘 , 𝑘 ≥ 3 in:

𝐴 → 𝑢1𝐴1

𝐴1 → 𝑢2𝐴2, …

𝐴𝑘−2 → 𝑢𝑘−1𝑢𝑘

5. Convert all rules 𝐴 → 𝑢1𝑢2:

Replace any terminal 𝑢𝑖 with 𝑈𝑖

Add the rules 𝑈𝑖 → 𝑢𝑖

Be careful of cycles!

Page 30: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

CNF: Example 2.10 from Book

Convert the CFG in CNF

Added rules in bold

Removed rules in stroke

𝑆 → 𝐴𝑆𝐴 | 𝑎𝐵 𝐴 → 𝐵 | 𝑆 𝐵 → 𝑏 | 휀

Page 31: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

CNF: Example 2.10 from Book

Add the new start symbol

𝑺𝟎 → 𝑺 𝑆 → 𝐴𝑆𝐴 | 𝑎𝐵 𝐴 → 𝐵 | 𝑆 𝐵 → 𝑏 | 휀

Page 32: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

CNF: Example 2.10 from Book

Remove the empty rule 𝐵 → 휀

𝑆0 → 𝑆 𝑆 → 𝐴𝑆𝐴 𝑎𝐵 𝒂 𝐴 → 𝐵 𝑆 𝜺 𝐵 → 𝑏 | 휀

Page 33: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

CNF: Example 2.10 from Book

Remove the empty rule 𝐴 → 휀

𝑆0 → 𝑆 𝑆 → 𝐴𝑆𝐴 𝑎𝐵 𝑎 𝑺𝑨 𝑨𝑺 | 𝑺 𝐴 → 𝐵 𝑆 휀 𝐵 → 𝑏

Page 34: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

CNF: Example 2.10 from Book

Remove unit rule: 𝑆 → 𝑆

𝑆0 → 𝑆 𝑆 → 𝐴𝑆𝐴 𝑎𝐵 𝑎 𝑆𝐴 𝐴𝑆 | 𝑆 𝐴 → 𝐵 | 𝑆 𝐵 → 𝑏

Page 35: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

CNF: Example 2.10 from Book

Remove unit rule: 𝑆0 → 𝑆

𝑆0 → 𝑆 | 𝑨𝑺𝑨 𝒂𝑩 𝒂 𝑺𝑨 𝑨𝑺 𝑆 → 𝐴𝑆𝐴 𝑎𝐵 𝑎 𝑆𝐴 𝐴𝑆 𝐴 → 𝐵 | 𝑆 𝐵 → 𝑏

Page 36: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

CNF: Example 2.10 from Book

Remove unit rule: 𝐴 → 𝐵

𝑆0 → 𝐴𝑆𝐴 𝑎𝐵 𝑎 𝑆𝐴 𝐴𝑆 𝑆 → 𝐴𝑆𝐴 𝑎𝐵 𝑎 𝑆𝐴 𝐴𝑆 𝐴 → 𝐵 𝑆 𝒃 𝐵 → 𝑏

Page 37: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

CNF: Example 2.10 from Book

Remove unit rule: 𝐴 → 𝑆

𝑆0 → 𝐴𝑆𝐴 𝑎𝐵 𝑎 𝑆𝐴 𝐴𝑆 𝑆 → 𝐴𝑆𝐴 𝑎𝐵 𝑎 𝑆𝐴 𝐴𝑆 𝐴 → 𝑆 𝑏 𝑨𝑺𝑨 𝒂𝑩 𝒂 𝑺𝑨 𝑨𝑺 𝐵 → 𝑏

Page 38: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

CNF: Example 2.10 from Book

Convert the remaining rules

𝑆0 → 𝐴𝑨𝟏 𝑼𝐵 𝑎 𝑆𝐴 𝐴𝑆 𝑆 → 𝐴𝑨𝟏 𝑼𝐵 𝑎 𝑆𝐴 𝐴𝑆 𝐴 → 𝑏 𝐴𝑨𝟏 𝑼𝐵 𝑎 𝑆𝐴 | 𝐴𝑆 𝑨𝟏 → 𝑺𝑨 𝑼 → 𝒂 𝐵 → 𝑏

Page 39: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Pushdown Automata (PDA)

Extend NFAs with a stack

The stack provides additional memory

Equivalent to context free grammars

They recognize context free languages

Page 40: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Finite State Automata

Can be simplified as follow

State control for states and transitions

Tape to store the input string

state control

a a b b

input

Page 41: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Pushdown Automata

Introduce a stack component

Symbols can be read and written there

state control

a a b b

input a

a

b

stack

Page 42: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

What is a Stack?

Stacks are special containers

Symbols are “pushed” on top

Symbols can be “popped” from top

Last in first out principle

Similar to plates in cafeteria

Page 43: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Formal Definition of PDA

A pushdown automata is a 6-tuple (𝑄, Σ, Γ, 𝛿, 𝑞𝑜, 𝐹)

𝑄 is a finite set of states

Σ is a finite set, the input alphabet

Γ is a finite set, the stack alphabet

𝛿: 𝑄 × Σ𝜖 × Γ𝜖 → 𝑃(𝑄 × Γ𝜖) is the transition function

𝑞0 ∈ 𝑄 is the initial state

𝐹 ⊆ 𝑄 is the set of accept states

Page 44: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Transition Function

Maps (state, in, stk) in (state, stk)

Can include empty symbols

$ is used to indicate the stack end

Input 0 1 є

Stack 0 $ є 0 $ є 0 $ є

q1 {(q2,$)}

q2 {(q2,0)} {(q3,є)}

q3 {(q3,є)} {(q4,є)}

q4

Page 45: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Example PDA

PDA for the language

q2 q1

q4 q3

є,є → $ 0,є → 0

1,0 → є

1,0 → є є,$ → є

Page 46: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Computation of the PDA

Compute keeping track of

String

State

Stack

Page 47: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Computation of the PDA

Compute keeping track of

String

State

Stack

q2 q1

q4 q3

є,є → $ 0,є → 0

1,0 → є

1,0 → є є,$ → є

Page 48: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Computation of the PDA

Compute keeping track of

String

State

Stack

q2 q1

q4 q3

є,є → $ 0,є → 0

1,0 → є

1,0 → є є,$ → є

1

2

2

2

3

3

4

( 0 0 1 1, , )

( 0 0 1 1, , $ )

( 0 1 1, , 0 $ )

(1 1, , 0 0 $ )

(1, , 0 $ )

( , , $ )

( , ) a c c e p t

q

q

q

q

q

q

q

Page 49: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Definition of Computation

0

*

1

0 0

1

L e t b e a p u s h d o w n a u to m a to n ( , , , , , )

L e t . . . . b e a s t r in g o v e r

i f a n d . . . . w h e re a n d a s e q u e n c e o f

s ta te s , . . . , e x is t s t r in g s , . . . ,

a c c e p ts

e x is tss in a n d in

n

n i

n n

M Q q F

w w w

M w w

s s

w w w w

r r Q

0 0

1 1

*

*

0

1

1 .

2 .fo r a ll 0 , . . . , 1

( , ) ( , , ) w h e re = a n d =

fo r s o m e , a n d s o m e

3 .

N o e x p lic i t te s t fo r e m p ty s ta c k a n d e n d o

s u c h th a t

a n d

f in p u t

i i i i i

n

r q

i n

r r w s a t s b

s

b

a

a t

b t

r F

Page 50: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Another Example of PDA

𝐿 = 𝑎𝑖 𝑏𝑗 𝑐𝑘 𝑖, 𝑗, 𝑘 0 𝑎𝑛𝑑 𝑖 = 𝑗 𝑜𝑟 𝑖 = 𝑘}

q4 q3

q5 q6

휀,$ → 휀

q7 q2

q1

b, 휀 → 휀 a, 휀 → a c,a → 휀

b,a → 휀 c, 휀 → 휀

휀,$ → 휀 휀, 휀 → 휀 휀, 휀 → 휀

Page 51: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Another Example of PDA

𝐿 = 𝑤𝑤𝑅 𝑤 ∈ 0,1 ∗}

𝑤𝑅 is 𝑤 written “backwards”

q2 q1

q4 q3

휀, 휀 → $ 0, 휀 → 0 1, 휀 → 1

휀, 휀 → 휀

0,0 → 휀 1,1 → 휀

휀,$ → 휀

Page 52: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Equivalence of PDAs and CFLs

Theorem 2.20: A language is context free if and only if some pushdown automaton recognizes it.

Lemma 2.21: If a language is context free, then some pushdown automaton recognizes it. (Forward direction of proof)

Page 53: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Lemma 2.21: Proof Idea

Construct a PDA P for the grammar

P accepts w if there is a derivation

Non determinism for multiple rules

Represent intermediate strings on PDA

Store the variables on the stack

Page 54: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Lemma 2.21: Proof Idea

Representing 01A1A0

state control

0 1 1 0

A

1

A 0 1 0

$

0 1 A 1 A 0 0 1 A 1 A 0

Page 55: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Proof by Construction

1. Place the marker symbol $ and the

start variable on the stack.

2. Repeat the following steps forever.

There are three possible cases:

a. The top of stack is a variable symbol A;

b. The top of stack is a terminal symbol a;

c. The top of stack is the symbol $

Page 56: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Proof by Construction

The top of stack is a variable symbol A

Non-deterministically select one of the rules for A and substitute A on the stack.

The top of stack is a terminal symbol a

Read the next symbol from the input and compare it to a. If they match, repeat. If they do not match, reject the branch.

Page 57: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Proof by Construction

The top of stack is the symbol $

Enter the accept state. Doing so accepts the input if it has all been read.

Page 58: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Proof by Construction

PDA to substitute a whole string

Page 59: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Proof by Construction

Final PDA to accept the string

Page 60: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Example 2.25 From the Book

Construct a PDA to accept the CFG 𝑆 → 𝑎𝑇𝑏 | 𝑏 𝑇 → 𝑇𝑎 | 휀

Page 61: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Example 2.25 From the Book

Construct a PDA to accept the CFG 𝑆 → 𝑎𝑇𝑏 | 𝑏 𝑇 → 𝑇𝑎 | 휀

Page 62: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Equivalence of PDAs and CFLs

Lemma 2.27: If a pushdown automaton recognizes some languages, then it is context free. (Backward direction of proof)

Assumptions:

1. The PDA has a single accept state

2. The PDA empties the stack before accepting

3. Transitions either push or remove symbols

Page 63: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Lemma 2.27: Assumptions

Assumption 1

Create a new accept state with empty transitions from the previous ones

Assumption 2

Creates dummy transitions to empty the stack before accepting

Page 64: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Lemma 2.27: Assumptions

Assumption 3

Replace each transitions that pushes and pops with two transitions and a new state

Replace each transitions without push and pop with two transitions that push and pop a dummy symbol and a new state

Page 65: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Lemma 2.27: Proof

S a y th a t a n d c o n s tru c t . T h e v a r ia b le s

o f a re T h e s ta r t v a r ia b le is

N o w w e d e s c r ib e ´s ru le s .

F o r e a c h a n d , i f

0 a c c e p t

0 a c c e p t

p q q ,q

P ( Q , , , q ,{ q } ) G

G { A | p ,q Q } . A .

G

p ,q , r , s Q ; t a ,b ( p ,a , )

c o n ta in s a n d c o n ta in s p u t th e

ru le in

F o r e a c h p u t th e ru le in

F in a lly , fo r e a c h p u t th e ru le in

Y o u m a y g a in s

p q r s

p q p r rq

p p

( r ,t ) ( s ,b ,t ) ( q , )

A a A b G .

p ,q , r Q A A A G .

p Q A G .

o m e in tu i t io n fo r th is c o n s tru c t io n f ro m th e fo llo w in g f ig u re s .

Page 66: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Inserting 𝑨𝒑𝒒 → 𝒂𝑨𝒓𝒔𝒃

Page 67: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Inserting 𝑨𝒑𝒒 → 𝑨𝒑𝒓𝑨𝒓𝒒

Page 68: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Lemma 2.27: Proof

We now need to prove that the construction works

𝑨𝒑𝒒 generates 𝒙 iff 𝒙 brings 𝑷 from

𝒑 with an empty stack to 𝒒 with an empty stack

Prove by induction

Page 69: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Lemma 2.27: Proof (Forward)

If 𝑨𝒑𝒒 generates 𝒙 , it brings 𝑷 from

𝒑 with empty stack to 𝒒 with empty stack

Basis: The derivation has 1 step

There is only one rule possible 𝑨𝒑𝒑 → 𝝐

which trivially brings P from p to p.

Page 70: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Lemma 2.27: Proof (Forward)

Induction:

Assume true for k steps, prove for k+1

Case a): 𝐴𝑝𝑞 𝑎𝐴𝑟𝑠𝑏

𝑥 = 𝑎𝑦𝑏 and 𝐴𝑟𝑠∗ 𝑦 in 𝑘 steps with empty

stack (induction assumption).

Now, because 𝐴𝑝𝑞 𝑎𝐴𝑟𝑠𝑏 in G, we have

𝛿(𝑝, 𝑎, 휀) ∋ (𝑟, 𝑡) and 𝛿 𝑠, 𝑏, 𝑡 ∋ (𝑞, 휀)

Therefore, 𝑥 can bring 𝑃 from 𝑝 to 𝑞 with empty stack.

Page 71: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Lemma 2.27: Proof (Forward)

Induction:

Assume true for k steps, prove for k+1

Case b): 𝐴𝑝𝑞 𝐴𝑝𝑟𝐴𝑟𝑞

𝑥 = 𝑦𝑧 such that 𝐴𝑝𝑟∗ 𝑦 and 𝐴𝑝𝑟

∗ 𝑧 in at

most 𝑘 steps with empty stack.

Therefore, 𝑥 can bring 𝑃 from 𝑝 to 𝑞 with empty stack.

Page 72: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Lemma 2.27: Proof (Backward)

If 𝒙 brings 𝑷 from 𝒑 with empty stack to 𝒒 with empty stack, then 𝑨𝒑𝒒 generates 𝒙

Basis: The computation has 0 steps

If it has 0 steps, it starts and ends in the same state. P can only read the empty string. The rule 𝑨𝒑𝒑 → 𝝐 generates it.

Page 73: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Lemma 2.27: Proof (Backward)

Induction:

Assume true for k steps, prove for k+1

Case a): Stack is not empty in between

The symbol pushed at the beginning is the same popped at the end, we have therefore 𝐴𝑝𝑞 → 𝑎𝐴𝑟𝑠𝑏 in the grammar.

We have 𝑥 = 𝑎𝑦𝑏, from induction we have

𝐴𝑟𝑠∗ 𝑦, therefore 𝐴𝑝𝑞

∗ 𝑎𝑦𝑏

Page 74: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Lemma 2.27: Proof (Backward)

Induction:

Assume true for k steps, prove for k+1

Case b): Stack is empty in between

There exists a state 𝑟 in between and computations from 𝑝 to 𝑟 and 𝑟 to 𝑞 have at most k steps. We have 𝑥 = 𝑦𝑧, from

induction 𝐴𝑝𝑟∗ 𝑦 and 𝐴𝑟𝑞

∗ 𝑧 .

Since 𝐴𝑝𝑞 → 𝐴𝑝𝑟𝐴𝑟𝑞 is in the grammar, we

have that 𝐴𝑝𝑞∗ 𝑦𝑧

Page 75: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Regular vs. Context Free

Every regular language is context free

NFAs are PDAs without a stack!

regular languages

Page 76: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Pumping Lemma

P u m p in g L e m m a

If is a c o n te x t f re e la n g u a g e , th e n th e r e is a n u m b e r

s u c h th a t i f i s a n y s t r in g in o f le n g th a t le a s t

th e n m a y b e d iv e d in to s u c h th a t

1 . F o r e a c h 0 ;

2 .

i i

A p

s A p

s s u v x y z

i u v x y z A

v

T h e o r e m

0

3 .

y

v x y p

Page 77: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Remember the Parse Tree?

Page 78: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Pumping Lemma: Proof Idea

Let T be the parse tree for A

Show that s can be broken into uvxyz

Prove the conditions holds

T

T

Page 79: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Pumping Lemma: Proof Idea

Let T be the parse tree for A

Show that s can be broken into uvxyz

Prove the conditions holds

T

T

R

u z

Page 80: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Pumping Lemma: Proof Idea

Let T be the parse tree for A

Show that s can be broken into uvxyz

Prove the conditions holds

T

T

R

R

x u v y z

Page 81: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Pumping Lemma: Proof Idea

Let T be the parse tree for A

Show that s can be broken into uvxyz

Prove the conditions holds

T

v y

T

R

R

R

x

u v y z

Page 82: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Pumping Lemma: Proof Idea

Let T be the parse tree for A

Show that s can be broken into uvxyz

Prove the conditions holds

T

T

R

R

x u v y z

Page 83: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Pumping Lemma: Proof Idea

Let T be the parse tree for A

Show that s can be broken into uvxyz

Prove the conditions holds

T

T

R

x u z

Page 84: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Pumping Lemma: Proof

Let 𝑏 be the maximum number of symbols on right hand side of a rule

The number of leaves in a parse tree of height ℎ is at most 𝑏ℎ

Hence, for any string 𝑠 of such parse

tree, its length |s| ≤ 𝑏ℎ

Let 𝑉 be the number of variables and

choose the pumping length 𝑝 = 𝑏 𝑉 +2

Page 85: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Pumping Lemma: Proof

For any 𝑠 ≥ 𝑝: possible parse trees for 𝑠 have height at least 𝑉 + 1

let 𝜏 be the minimum parse tree for 𝑠

It must contain a path P from root to a leaf of length at least 𝑉 + 1

P has at least 𝑉 + 2 nodes: one terminal and the rest variables

P has at least 𝑉 + 1 variables some

variable must be doubled!

Page 86: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Pumping Lemma: Proof Cnd. 1

Divide 𝑠 into 𝑢𝑣𝑥𝑦𝑧 as in picture.

R generates 𝑣𝑥𝑦, with a large subtree, or just 𝑥, with a smaller subtree.

Pumping down gives 𝑢𝑥𝑧; pumping up

gives 𝑢𝑣𝑖𝑥𝑦𝑖𝑧 with 𝑖 ≥ 1

T

R

R

x u v y z

Page 87: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Pumping Lemma: Proof Cnd. 2

Condition states 𝑣𝑦 > 0.

We must be sure 𝑣 and 𝑦 are not 휀.

Assuming they were 휀, substituting smaller for bigger subtree would lead to parse tree with fewer nodes.

Contradiction: 𝜏 chosen to be parse tree with fewest number of nodes

Page 88: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Pumping Lemma: Proof Cnd. 3

Condition states 𝑣𝑥𝑦 ≤ 𝑝

Upper occurrence of R generates 𝑣𝑥𝑦

R chosen such that both occurrences fall within the bottom 𝑉 + 1 variables on the path and longest path

Subtree where R generates 𝑣𝑥𝑦 is at most 𝑉 + 2 high.

A tree of height 𝑉 + 2 can generate

strings of length at most 𝑏 𝑉 +2 = 𝑝

Page 89: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Non Context Free Languages

𝐵 = 𝑎𝑛𝑏𝑛𝑐𝑛 | 𝑛 ≥ 0

Choose 𝑎𝑝𝑏𝑝𝑐𝑝

Find 𝑢𝑣𝑥𝑦𝑧 , either v or y not empty (2)

Two cases:

Contain only one type of symbol: Impossible to respect the equal number

Contain mixed symbols: Impossible to keep the order of symbols

Page 90: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Non Context Free Languages

𝐶 = 𝑎𝑖𝑏𝑗𝑐𝑘 | 0 ≤ 𝑖 ≤ 𝑗 ≤ 𝑘

Choose 𝑎𝑝𝑏𝑝𝑐𝑝

Find 𝑢𝑣𝑥𝑦𝑧 , either v or y not empty (2)

Two cases as before:

Contain only one type of symbol More complex to prove (next slide)

Contain mixed symbols Impossible to keep the order of symbols

Page 91: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Non Context Free Languages

𝐶 = 𝑎𝑖𝑏𝑗𝑐𝑘 | 0 ≤ 𝑖 ≤ 𝑗 ≤ 𝑘

Contain only one type of symbol

a does not appear: we have that 𝑢𝑣0𝑥𝑦0𝑧 ∉ 𝐶 (less b and c)

b does not appear: if a appears, 𝑢𝑣2𝑥𝑦2𝑧 ∉ 𝐶 (more a than b) if c appears, 𝑢𝑣0𝑥𝑦0𝑧 ∉ 𝐶 (more c than b)

c does not appear: we have that 𝑢𝑣2𝑥𝑦2𝑧 ∉ 𝐶 (more a and b)

Page 92: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Example Exam Question

Page 93: Theoretical Computer Science (Bridging Course) Context ...ais.informatik.uni-freiburg.de/teaching/ss15/... · Science (Bridging Course) Gian Diego Tipaldi Context Free Languages

Summary

Context free grammars

Pushdown Automata

Equivalence of PDAs and CFGs

Non-context free grammars

Pumping lemma