• Keine Ergebnisse gefunden

Theory of Computer Science B7. Context-free Languages: Normal Form and PDA Gabriele R¨oger

N/A
N/A
Protected

Academic year: 2022

Aktie "Theory of Computer Science B7. Context-free Languages: Normal Form and PDA Gabriele R¨oger"

Copied!
51
0
0

Wird geladen.... (Jetzt Volltext ansehen)

Volltext

(1)

Gabriele R¨oger

University of Basel

March 29, 2021

(2)

Context-free Grammars

(3)

Definition (Context-free Grammar)

Acontext-free grammar is a 4-tuplehV,Σ,R,Si with

1 V finite set of variables,

2 Σ finite alphabet of terminal symbols (with V ∩Σ =∅),

3 R ⊆V ×(V ∪Σ) finite set of rules,

4 S ∈V start variable.

(4)

Short-hand Notation for Rule Sets

We abbreviate several rules with the same left-hand side variable in a single line, using “|” for separating the right-hand sides.

For example, we write X→0Y1|XY for:

X→0Y1and X→XY

(5)

We have used the pumping lemma for regular languages to show thatL={anbn|n∈N0}is not regular.

Show that it is context-free by specifying a suitable grammarG with L(G) =L.

(6)

Questions

Questions?

(7)

Chomsky Normal Form

(8)

Chomsky Normal Form: Motivation

As in other kinds of structured objects,normal formsfor grammars are useful:

they show which aspects are critical for defining grammars and which ones are just syntactic sugar

they allow proofs and algorithms to be restricted

to a limited set of grammars (inputs): those in normal form Hence we now consider anormal form for context-free grammars.

(9)

Definition (Chomsky Normal Form)

A context-free grammarG is inChomsky normal form (CNF) if all rules have one of the following three forms:

A→BC with variables A,B,C

andB andC are not the start variable, or A→awith variableA and terminal symbol a, or S →εwith start variable S.

German: Chomsky-Normalform

formally: rule setR ⊆(V ×((V \ {S})(V \ {S})∪Σ))∪ {hS, εi}

(10)

Chomsky Normal Form: Theorem

Theorem

For every context-free grammar G there is a context-free grammar G0 in Chomsky normal form with L(G) =L(G0).

(11)

Theorem

For every context-free grammar G there is a context-free grammar G0 in Chomsky normal form with L(G) =L(G0).

Proof.

The following algorithm converts the rule set ofG =hV,Σ,R,Si into CNF:

Step 1: Add new start variableS0.

Add a new variableS0 which will be the start variable, and add a ruleS0 →S, whereS is the original start variable.

Afterwards, the (new) start variable does not occur on the right-hand side of a rule.

We will writeV0 for the new variable set (V0=V ∪ {S0}) and R0

for the new rule set. . . .

(12)

Chomsky Normal Form: Theorem

Proof (continued).

Step 2: Eliminateε-rules of the form A→ε(A6=S0).

Let Vε be the set of variable from which one can derive the empty word. We find this set Vε by first collecting all variables A∈V0 with rule A→ε∈R0 and then successively adding additional variablesB if there is a rule B →A1A2. . .Ak ∈R0 and the variablesAi are already in the set for all 1≤i ≤k. Add rules that obviate the need forA→εrules:

for every existing ruleB →w ∈R0 withB ∈V0,

w ∈(V0∪Σ)+, letIεbe the set of positions wherew contains a variable A∈Vε. For every non-empty set I0 ⊆Iε, add a new rule B →w0, wherew0 is constructed fromw by removing the variables at all positions in I0.

Remove all rules of the form A→ε (A6=S0).

. . .

(13)

Proof (continued).

Step 2: Eliminateε-rules of the form A→ε(A6=S0).

Let Vε be the set of variable from which one can derive the empty word. We find this set Vε by first collecting all variables A∈V0 with rule A→ε∈R0 and then successively adding additional variablesB if there is a rule B →A1A2. . .Ak ∈R0 and the variablesAi are already in the set for all 1≤i ≤k. Add rules that obviate the need forA→εrules:

for every existing ruleB →w ∈R0 withB ∈V0,

w ∈(V0∪Σ)+, letIεbe the set of positions wherew contains a variable A∈Vε. For every non-empty set I0 ⊆Iε, add a new rule B →w0, wherew0 is constructed fromw by removing the variables at all positions in I0.

Remove all rules of the form A→ε (A6=S0).

. . .

(14)

Chomsky Normal Form: Theorem

Proof (continued).

Step 2: Eliminateε-rules of the form A→ε(A6=S0).

Let Vε be the set of variable from which one can derive the empty word. We find this set Vε by first collecting all variables A∈V0 with rule A→ε∈R0 and then successively adding additional variablesB if there is a rule B →A1A2. . .Ak ∈R0 and the variablesAi are already in the set for all 1≤i ≤k. Add rules that obviate the need forA→εrules:

for every existing ruleB →w ∈R0 withB ∈V0,

w ∈(V0∪Σ)+, letIεbe the set of positions wherew contains a variable A∈Vε. For every non-empty set I0 ⊆Iε, add a new rule B →w0, wherew0 is constructed fromw by removing the variables at all positions in I0.

Remove all rules of the form A→ε (A6=S0).

. . .

(15)

Proof (continued).

Step 2: Eliminateε-rules of the form A→ε(A6=S0).

Let Vε be the set of variable from which one can derive the empty word. We find this set Vε by first collecting all variables A∈V0 with rule A→ε∈R0 and then successively adding additional variablesB if there is a rule B →A1A2. . .Ak ∈R0 and the variablesAi are already in the set for all 1≤i ≤k. Add rules that obviate the need forA→εrules:

for every existing ruleB →w ∈R0 withB ∈V0,

w ∈(V0∪Σ)+, letIεbe the set of positions wherew contains a variable A∈Vε. For every non-empty set I0 ⊆Iε, add a new rule B →w0, wherew0 is constructed fromw by removing the variables at all positions in I0.

Remove all rules of the form A→ε (A6=S0).

. . .

(16)

Step 2: Example

ConsiderG =h{X,Y,Z,S},{a,b},R,Si with rules:

S→ε|XY

X→aXYbX|YZ|ab Y→ε|b

Z→ε|a

(17)

Proof (continued).

Step 3: Eliminate rules of the form A→B with variablesA,B. If there are sets of variables{B1, . . . ,Bk} with rules

B1 →B2,B2 →B3, . . . ,Bk−1 →Bk,Bk →B1, then replace these variables by a new variableB.

We useV00 to denote the resulting set of variables.

Define a strict total order<on the variables such that a rule A→B implies that A<B. Iterate from the largest to the smallest variableAand eliminate all rules of the form A→B while adding rulesA→w for every ruleB →w withw ∈(V00∪Σ)+. . . .

(18)

Step 3: Example

ConsiderG =h{X,Y,Z,S},{a,b},R,Si with rules:

S→ε|X

X→aZbY|Y|ab Y→Z|b

Z→Y|bXa

(19)

Context-free Grammars Chomsky Normal Form Push-Down Automata Summary

Chomsky Normal Form: Theorem

Proof (continued).

Step 4: Eliminate rules with terminal symbols on the right-hand side that do not have the formA→a.

For every terminal symbola∈Σ add a new variable Aa

and the ruleAa →a.

Replace all terminal symbols in all rules that do not have

the formA→awith the corresponding newly added variables. . . .

(20)

Chomsky Normal Form: Theorem

Proof (continued).

Step 5: Eliminate rules of the form A→B1B2. . .Bk with k >2 For every rule of the formA→B1B2. . .Bk with k >2, add new variablesC2, . . . ,Ck−1 and replace the rule with

A→B1C2

C2→B2C3 ...

Ck−1→Bk−1Bk

(21)

(Example taken from textbook by Sipser)

ConsiderG =h{A,B,S},{a,b},R,Si with rules:

S→ASA|aB A→B|S B→ε|b

Specify a grammar G0 in CNF with L(G0) =L(G).

(22)

Chomsky Normal Form: Length of Derivations

Observation

LetG be a grammar in Chomsky normal form,

and letw ∈ L(G) be a non-empty word generated byG.

Then all derivations ofw have exactly 2|w| −1 derivation steps.

Proof.

Exercises

(23)

Questions?

(24)

Push-Down Automata

(25)

q0 0 q1 q2

0,1

0

Language Lis regular.

⇐⇒ There is a finite automaton that acceptsL.

What information can a finite automaton “store”

about the already read part of the word?

Infinite memory would be required for

L={x1x2. . .xnxn. . .x2x1|n>0,xi ∈ {a,b}}.

therefore: extension of the automata model with memory

(26)

Limitations of Finite Automata

q0 0 q1 q2

0,1

0

Language Lis regular.

⇐⇒ There is a finite automaton that acceptsL.

What information can a finite automaton “store”

about the already read part of the word?

Infinite memory would be required for

L={x1x2. . .xnxn. . .x2x1|n>0,xi ∈ {a,b}}.

therefore: extension of the automata model with memory

(27)

q0 0 q1 q2

0,1

0

Language Lis regular.

⇐⇒ There is a finite automaton that acceptsL.

What information can a finite automaton “store”

about the already read part of the word?

Infinite memory would be required for

L={x1x2. . .xnxn. . .x2x1|n>0,xi ∈ {a,b}}.

therefore: extension of the automata model with memory

(28)

Stack

Astack is a data structure following thelast-in-first-out (LIFO) principle supporting the following operations:

push: puts an object on top of the stack

pop: removes the object at the top of the stack

Pop Push

German: Keller, Stapel

(29)

Input tape

I n p u t

Read head

Push-down automaton

Stack access

Stack

German: Kellerautomat, Eingabeband, Lesekopf, Kellerzugriff

(30)

Push-down Automaton for {a

n

b

n

| n ∈ N

0

}: Idea

As long as you read symbols a, push anA on the stack.

As soon as you read a symbol b, pop an Aoff the stack as long as you read b.

If reading the input is finished exactly when the stack becomes empty, accept the input.

If there is no Ato pop when reading a b, or there is still an A on the stack after reading all input symbols, or if you read an afollowing a bthen reject the input.

(31)

PDAs arenon-deterministic and can allow several next transitions from a configuration.

Like NFAs, PDAs can have transitions that do not read a symbol from the input.

Similarly, there can be transitions that do not pop and/or push a symbol off/to the stack.

Deterministic variants of PDAs are strictly less expressive, i. e. there are languages that can be recognized by a (non-deterministic) PDA but not the deterministic variant.

(32)

Push-down Automata: Non-determinism

PDAs arenon-deterministic and can allow several next transitions from a configuration.

Like NFAs, PDAs can have transitions that do not read a symbol from the input.

Similarly, there can be transitions that do not pop and/or push a symbol off/to the stack.

Deterministic variants of PDAs are strictly less expressive, i. e. there are languages that can be recognized by a (non-deterministic) PDA but not the deterministic variant.

(33)

q0 ε,ε# q1 q2 q3

a,εA b,Aε

b,Aε ε,#ε

(34)

Push-down Automata: Definition

Definition (Push-down Automaton)

Apush-down automaton (PDA) is a 6-tuple M =hQ,Σ,Γ, δ,q0,Fi with

Q finite set of states Σ the input alphabet Γ the stack alphabet

δ :Q×(Σ∪ {ε})×(Γ∪ {ε})→ P(Q×(Γ∪ {ε})) the transition function

q0∈Q the start state

F ⊆Q is the set of accept states

German: Kellerautomat, Eingabealphabet, Kelleralphabet, Uberf¨¨ uhrungsfunktion

(35)

LetM =hQ,Σ,Γ, δ,q0,Fi be a push-down automaton.

What is the Intuitive Meaning of the Transition Functionδ?

hq0,Bi ∈δ(q,a,A): If M is in stateq, reads symbola and has Aas the topmost stack symbol,

then M cantransition toq0 in the next step

poppingA off the stack and pushing B on the stack.

q a,AB q0

special casea=εis allowed (spontaneous transition) special caseA=εis allowed (no pop)

special caseB =εis allowed (no push)

(36)

Push-down Automata: Transition Function

LetM =hQ,Σ,Γ, δ,q0,Fi be a push-down automaton.

What is the Intuitive Meaning of the Transition Functionδ?

hq0,Bi ∈δ(q,a,A): If M is in stateq, reads symbola and has Aas the topmost stack symbol,

then M cantransition toq0 in the next step

poppingA off the stack and pushing B on the stack.

q a,AB q0

special casea=εis allowed (spontaneous transition) special caseA=εis allowed (no pop)

special caseB =εis allowed (no push)

(37)

q0 ε,ε# q1 q2 q3

a,εA b,Aε

b,Aε ε,#ε

M =h{q0,q1,q2,q3},{a,b},{A,#}, δ,q0,{q0,q3}iwith

δ(q0,a,A) = δ(q0,b,A) = δ(q0, ε,A) =

δ(q0,a,#) = δ(q0,b,#) = δ(q0, ε,#) =

δ(q0,a, ε) = δ(q0,b, ε) = δ(q0, ε, ε) ={(q1,#)}

δ(q1,a,A) = δ(q1,b,A) ={(q2, ε)} δ(q1, ε,A) =

δ(q1,a,#) = δ(q1,b,#) = δ(q1, ε,#) =

δ(q1,a, ε) ={(q1,A)} δ(q1,b, ε) = δ(q1, ε, ε) = δ(q2,a,A) = δ(q2,b,A) ={(q2, ε)} δ(q2, ε,A) = δ(q2,a,#) = δ(q2,b,#) = δ(q2, ε,#) ={(q3, ε)}

δ(q2,a, ε) = δ(q2,b, ε) = δ(q2, ε, ε) = andδ(q3,x,y) =for allx ∈ {a,b, ε},y ∈ {A,#, ε}

(38)

Context-free Grammars Chomsky Normal Form Push-Down Automata Summary

Push-down Automata: Accepted Words

Definition

A PDAM =hQ,Σ,Γ, δ,q0,Fi accepts inputw

if it can be written asw =w1w2. . .wm where eachwi ∈Σ∪ {ε}

and sequences of statesr0,r1, . . . ,rm ∈Q and stringss0,s1, . . . ,sm ∈Γ exist

that satisfy the following three conditions:

1 r0=q0 ands0

2 For i = 0, . . . ,m−1, we have (ri+1,b)∈δ(ri,wi+1,a), wheresi =at andsi+1=bt for somea,b∈Γ∪ {ε}and t ∈Γ.

3 rm∈F

(39)

Definition

A PDAM =hQ,Σ,Γ, δ,q0,Fi accepts inputw

if it can be written asw =w1w2. . .wm where eachwi ∈Σ∪ {ε}

and sequences of statesr0,r1, . . . ,rm ∈Q and stringss0,s1, . . . ,sm ∈Γ exist

that satisfy the following three conditions:

1 r0=q0 ands0

2 For i = 0, . . . ,m−1, we have (ri+1,b)∈δ(ri,wi+1,a), wheresi =at andsi+1=bt for somea,b∈Γ∪ {ε}and t ∈Γ.

3 rm∈F

The stringssi represent the sequence of stack contents.

(40)

Push-down Automata: Accepted Words

Definition

A PDAM =hQ,Σ,Γ, δ,q0,Fi accepts inputw

if it can be written asw =w1w2. . .wm where eachwi ∈Σ∪ {ε}

and sequences of statesr0,r1, . . . ,rm ∈Q and stringss0,s1, . . . ,sm ∈Γ exist

that satisfy the following three conditions:

1 r0=q0 ands0

2 For i = 0, . . . ,m−1, we have (ri+1,b)∈δ(ri,wi+1,a), wheresi =at andsi+1=bt for somea,b∈Γ∪ {ε}and t ∈Γ.

3 rm∈F

The stringssi represent the sequence of stack contents.

(41)

Definition

A PDAM =hQ,Σ,Γ, δ,q0,Fi accepts inputw

if it can be written asw =w1w2. . .wm where eachwi ∈Σ∪ {ε}

and sequences of statesr0,r1, . . . ,rm ∈Q and stringss0,s1, . . . ,sm ∈Γ exist

that satisfy the following three conditions:

1 r0=q0 ands0

2 For i = 0, . . . ,m−1, we have (ri+1,b)∈δ(ri,wi+1,a), wheresi =at andsi+1=bt for somea,b∈Γ∪ {ε}and t ∈Γ.

3 rm∈F

The stringssi represent the sequence of stack contents.

(42)

Push-down Automata: Accepted Words

Definition

A PDAM =hQ,Σ,Γ, δ,q0,Fi accepts inputw

if it can be written asw =w1w2. . .wm where eachwi ∈Σ∪ {ε}

and sequences of statesr0,r1, . . . ,rm ∈Q and stringss0,s1, . . . ,sm ∈Γ exist

that satisfy the following three conditions:

1 r0=q0 ands0

2 For i = 0, . . . ,m−1, we have (ri+1,b)∈δ(ri,wi+1,a), wheresi =at andsi+1=bt for somea,b∈Γ∪ {ε}and t ∈Γ.

3 rm∈F

The stringssi represent the sequence of stack contents.

(43)

q0 q1 q2 q3

ε,ε# a,εA

b,Aε b,Aε

ε,#ε

The PDA accepts inputaabb.

(44)

Acceptance: Exercise

q0 ε,ε# q1 q2 q3

a,εA, b,εB

ε,εε a,Aε b,Bε

ε,#ε

Show that this PDA accepts inputabba.

(45)

Definition (Language Recognized by an NFA) LetM be a PDA with input alphabet Σ.

Thelanguage recognized by M is defined as L(M) ={w ∈Σ |w is accepted by M}.

(46)

Recognized Language: Exercise

q0 ε,ε# q1 q2 q3

a,εA, b,εB

ε,εε a,Aε b,Bε

ε,#ε

What language does this PDA recognize?

(47)

Theorem

A language L is context-free if and only if L is recognized by a push-down automaton.

(48)

PDAs: Exercise (if time)

Assume you want to have a possible transition from state q to stateq0 in your PDA that

processes symbolcfrom the input word, can only be taken if the top stack symbol is A, doesnotpopAoff the stack, and

pushesB.

What problem do you encounter? How can you work around it?

(49)

Questions?

(50)

Summary

(51)

Every context-free language has a grammar in Chomsky normal form. All rules have form

ABC with variables A,B,C (B,C not start variable), or Aawith variable A, terminal symbola, or

S εwith start variableS.

Push-down automata (PDAs) extend NFAs with memory.

The languages recognized by PDAsare exactly thecontext-free languages.

Referenzen

ÄHNLICHE DOKUMENTE

I DFAs, NFAs and regular grammars can all describe exactly the regular languages.. I Are there other concepts with the

B7.1 Context-free Grammars B7.2 Chomsky Normal Form B7.3 Push-Down Automata B7.4 Summary.. Gabriele R¨ oger (University of Basel) Theory of Computer Science March 29, 2021 2

B7.1 Context-free Grammars B7.2 Chomsky Normal Form B7.3 Push-Down Automata B7.4 Summary... B7.1

Picture courtesy of imagerymajestic / FreeDigitalPhotos.net Gabriele R¨ oger (University of Basel) Theory of Computer Science March 31, 2021 4 / 26... Closure under

for 2.: identical to (1), only that M B only recognizes B and therefore the simulation does not necessarily terminate if y 6∈ B. Since y 6∈ B iff x 6∈ A, the procedure still

Pumping Lemma Minimal Automata Properties

Pumping Lemma Minimal Automata Properties

The word problem, emptiness problem and finiteness problem for the class of context-free languages are decidable. The equivalence problem and intersection problem for the class