deterministic pushdown automata, reductions and normal … · 2003-10-28 · chomsky normal form...

30
Deterministic Pushdown Automata, Reductions and Normal Forms of CFGs Martin Fr ¨ anzle Informatics and Mathematical Modelling The Technical University of Denmark Context-free languages III – p.1/21

Upload: others

Post on 10-Apr-2020

4 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Deterministic Pushdown Automata,Reductions and Normal Forms of CFGs

Martin Franzle

Informatics and Mathematical Modelling

The Technical University of Denmark

Context-free languages III – p.1/21

Page 2: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

What you’ll learn

1. Deterministic pushdown automata:

� Importance

� Definition

� Language recognition capability (i.e. expressiveness)

2. Transformation/reductions of CFGs:

� Eliminating

� useless symbols (terminal and non-terminal)

� �-productions

� unit productions (� � �

)

� Chomsky normal form

Context-free languages III – p.2/21

Page 3: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Pushdown Automata

Deterministic language recognition

Context-free languages III – p.3/21

Page 4: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Motivation

Previous lecture detailed a direct construction of PDAs from CFGs.

� Highly nondeterministic

not practical, because of lack of adequate oracle.

Determistic PDAs are a more practical alternative.

Their study sheds light on what constructs are suitable for use inpractical

� programming languages

� markup languages

� database query languages

�� � �

Context-free languages III – p.4/21

Page 5: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Deterministic PDAs

A PDA is deterministic iff it has no choice between moves whentraversing any given word.

� No choice between two different

� successor states or

� stack updates.

� No choice between a move consuming an input letter and an �

move.

Def: A PDA is called deterministic or aDPDA iff1. and2. impliesfor each , each and each .

Context-free languages III – p.5/21

Page 6: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Deterministic PDAs

A PDA is deterministic iff it has no choice between moves whentraversing any given word.

� No choice between two different

� successor states or

� stack updates.

� No choice between a move consuming an input letter and an �

move.

Def: A PDA

� � �� � � � � � � � � � � � � is called deterministic or a

DPDA iff1.

� � � � � � � � � � �

and2.

� � � � � � � � � �implies

� � � � � � � � �

for each � � � , each � � � � � � �

and each

� � �

.

Context-free languages III – p.5/21

Page 7: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Regular Languages and DPDAs

Thm: If

is regular then

� � � � �

for some DPDA

.

Prf: Given a DFA

� � �� � � � � � � � � �

construct a corresponding DPDA � �� � � � �� � � ��� � � � � � � �

“ignoring” its stack by

�� � � � � � � � �

� � � � � � � � � �

iff � � �

otherwise.

N.B. The same is not true with “ ” instead of “ ”, as a DPDAnever accepts both a word and a proper prefix of byempty stack. Hence, a DPDA cannot even accept byempty stack.

Cor: There are regular languages that cannot be defined by DPDAsby empty stack.

Context-free languages III – p.6/21

Page 8: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Regular Languages and DPDAs

Thm: If

is regular then

� � � � �

for some DPDA

.

Prf: Given a DFA

� � �� � � � � � � � � �

construct a corresponding DPDA � �� � � � �� � � ��� � � � � � � �

“ignoring” its stack by

�� � � � � � � � �

� � � � � � � � � �

iff � � �

otherwise.

N.B. The same is not true with “

� � � ” instead of “

� � �

”, as a DPDAnever accepts both a word � and a proper prefix � of � byempty stack. Hence, a DPDA cannot even accept

� � �� ��

byempty stack.

Cor: There are regular languages that cannot be defined by DPDAsby empty stack.

Context-free languages III – p.6/21

Page 9: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Regular vs. L(DPDA) vs. CFL

Thm: The languages accepted by DPDAs by final state properlyincludes the regular languages and are properly included in theCFLs.

Prf:

DPDAs can simulate DFAs (see previous theorem), henceDPDA-recognizabilty covers regular languages;

DPDAs can recognize the non-regular language�� � � � � � �

IN

;

DPDAs cannot recognize the CFL

��� �

r

� � � �� � � �� �

.

Context-free languages III – p.7/21

Page 10: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Final state vs. empty stack

Def: A language

has the prefix property iff � � �

implies � �� �for

all proper prefixes � of �.

Thm: A language

is

� � �

for some DPDA

iff

�has the prefix

property and

� � � � � �

for some DPDA

� �

.

Prf: Use the “from empty stack to final state” construction, and vice versa.

Cor: If has the prefix property then is accepted by some DPDAby final state iff is accepted by some DPDA by empty stack.

N.B. The prefix-property can always be enforced by adding a specialend-marker (e.g., “EOF”).

Context-free languages III – p.8/21

Page 11: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Final state vs. empty stack

Def: A language

has the prefix property iff � � �

implies � �� �for

all proper prefixes � of �.

Thm: A language

is

� � �

for some DPDA

iff

�has the prefix

property and

� � � � � �

for some DPDA

� �

.

Prf: Use the “from empty stack to final state” construction, and vice versa.

Cor: If

has the prefix property then�

is accepted by some DPDAby final state iff

is accepted by some DPDA by empty stack.

N.B. The prefix-property can always be enforced by adding a specialend-marker (e.g., “EOF”).

Context-free languages III – p.8/21

Page 12: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

PDPA vs. ambiguity

Thm: If

� � � � �

or

� � � � �

for some DPDA

then

has anunambiguous grammar.

Prf: The “PDA to grammar construction” yields an unambiguous grammar if

isdeterministic.

N.B. There are nevertheless CFLs with unambiguous grammars thatare not DPDA-definable, e.g. r which has theunambiguous grammar

Cor: The languages accepted by DPDAs by final state are properlyincluded in the unambiguous ( not inherently ambiguous)CFLs.

Context-free languages III – p.9/21

Page 13: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

PDPA vs. ambiguity

Thm: If

� � � � �

or

� � � � �

for some DPDA

then

has anunambiguous grammar.

Prf: The “PDA to grammar construction” yields an unambiguous grammar if

isdeterministic.

N.B. There are nevertheless CFLs with unambiguous grammars thatare not DPDA-definable, e.g.

� � � r � � � �� � � �� �

which has theunambiguous grammar

� � � � � � � � � � � �

Cor: The languages accepted by DPDAs by final state are properlyincluded in the unambiguous ( not inherently ambiguous)CFLs.

Context-free languages III – p.9/21

Page 14: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

PDPA vs. ambiguity

Thm: If

� � � � �

or

� � � � �

for some DPDA

then

has anunambiguous grammar.

Prf: The “PDA to grammar construction” yields an unambiguous grammar if

isdeterministic.

N.B. There are nevertheless CFLs with unambiguous grammars thatare not DPDA-definable, e.g.

� � � r � � � �� � � �� �

which has theunambiguous grammar

� � � � � � � � � � � �

Cor: The languages accepted by DPDAs by final state are properlyincluded in the unambiguous ( � not inherently ambiguous)CFLs.

Context-free languages III – p.9/21

Page 15: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

A proper inclusion hierarchy

Accepted

by finalby a DPDA

state

RegularlanguageAccepted

by emptyby a DPDA

stack

CFL

UnambiguousCFL

Context-free languages III – p.10/21

Page 16: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Context-free Grammars

Eliminations and normal forms

Context-free languages III – p.11/21

Page 17: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Chomsky normal form

Def: A context-free grammar

� � � � � � � � � �

is said to be in Chomskynormal form iff all its productions are of the two forms

� � � � �

, where

� � � � � � �

, i.e. are all variables,

� � � �, with � � �

and

has no “useless” symbols.

A symbol (terminal or non-terminal) is considered to be useless if it occursin no derivation from

to a string of the language.

Which CFLs can be expressed in Chomsky normal form?

Context-free languages III – p.12/21

Page 18: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Finding non-generating symbols

Def: A symbol

� � � � �

of a grammar

� � � � � � � � � �

is generatingiff

� ��� � for some � � � �

.

N.B. Each terminal symbol

� � �

is by definition generating.

Lem: The algorithm

Base: Gen ,Recursion: Iterate

Gen

Gen if there is with Gen

and every symbol of being in Gen

Gen otherwiseuntil Gen Gen

finds exactely the generating symbols of .

Context-free languages III – p.13/21

Page 19: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Finding non-generating symbols

Def: A symbol

� � � � �

of a grammar

� � � � � � � � � �

is generatingiff

� ��� � for some � � � �

.

N.B. Each terminal symbol

� � �

is by definition generating.

Lem: The algorithm

Base: Gen � � �

,Recursion: Iterate

Gen �� � � �����

Gen � � � � �

if there is

� � � � �

with

� ��

Gen �

and every symbol of being in Gen �

Gen � otherwiseuntil Gen �� � � Gen �

finds exactely the generating symbols of

.

Context-free languages III – p.13/21

Page 20: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Eliminating non-generating symbols

Thm: If

� � � �

is a non-generating symbol of

� � � � � � � � � � then� � � � � � �

� �

for

�� � � � � � � � � � � � � � � � �� � � �

with

�� � � � � � � � � � � � � �

and all its symbols are

� � � ��

N.B.

itself can’t be non-generating, unless� � � � �

. Hence, wecan safely remove all non-generating symbols from anygrammar

with

� � � � � �

.

Context-free languages III – p.14/21

Page 21: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Finding reachable symbols

Def: A symbol

� � � � �

of a grammar

� � � � � � � � � �

is reachable iff

� �� � � �

for some � � � � � � � � �

.

Lem: The algorithm

Base: Reach ,Recursion: Iterate

Reach

Reach if there is

with Reach andReach for some

Reach otherwiseuntil Reach Reach

finds exactely the reachable symbols of .

Context-free languages III – p.15/21

Page 22: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Finding reachable symbols

Def: A symbol

� � � � �

of a grammar

� � � � � � � � � �

is reachable iff

� �� � � �

for some � � � � � � � � �

.

Lem: The algorithm

Base: Reach � � � � �

,Recursion: Iterate

Reach �� � � �������������

�������

Reach � � � � �� � � � � � if there is� � � � � � � � � �

with

� �

Reach � and

��

Reach � for some

Reach � otherwiseuntil Reach �� � � Reach �

finds exactely the reachable symbols of

.

Context-free languages III – p.15/21

Page 23: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Eliminating unreachable symbols

Thm: If

� � � �

is an unreachable symbol of

� � � � � � � � � �

then� � � � � � �� �

for

�� � � � � � � � � � � � � � � � �� � � �

with

�� � � � � � � � � � � � � �

and all its symbols are

� � � ��

N.B.

itself is reachable. Hence, we can safely remove allunreachable symbols from any grammar

.

Context-free languages III – p.16/21

Page 24: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Finding nullable symbols

Def: A symbol

� � �

of a grammar

� � � � � � � � � �

is nullable iff� ��� �.

N.B. Nullability of

does not imply that there must be a production� � � in P.

Lem: The algorithm

Base: Null ,Recursion: Iterate

Null

Null if there is withall symbols of being in Null and

Null

Null otherwiseuntil Null Null

finds exactely the nullable symbols of .

Context-free languages III – p.17/21

Page 25: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Finding nullable symbols

Def: A symbol

� � �

of a grammar

� � � � � � � � � �

is nullable iff� ��� �.

N.B. Nullability of

does not imply that there must be a production� � � in P.

Lem: The algorithm

Base: Null � � � � � � � � �� � �

,Recursion: Iterate

Null �� � � �������

����

Null � � � � �if there is

� � � � �

withall symbols of being in Null � and� ��

Null �

Null � otherwiseuntil Null �� � � Null �

finds exactely the nullable symbols of

.

Context-free languages III – p.17/21

Page 26: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Eliminating � productions

Lem: If

� � � � � � � � � �

then

� � � � � � � � � � �� �

for

�� � � � � � � � �� � � �

with

�� � �

����

���� � �� � � � � ���

� � � � � �

� � � �� �� � � � � � �� � � � � �

��� � � � � ��� � � � � and

��� is nullable for each

����

����

I.e., the new productions are obtained by removing an (almost)arbitrary number of nullable symbols from the production body.Resulting productions of the form

� � � are, however, notpermitted.

Context-free languages III – p.18/21

Page 27: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Finding unit pairs

Def: A pair

� � � � � �

of symbols in a grammar

� � � � � � � � � � is a

unit pair iff

� ���

.

Lem: The algorithm

Base: UPair ,Recursion: Iterate

UPair

UPair if there is withUPair and

and all symbolsin and are nullable and

UPair

UPair otherwiseuntil UPair UPair

finds exactely the unit pairs of .

Context-free languages III – p.19/21

Page 28: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Finding unit pairs

Def: A pair

� � � � � �

of symbols in a grammar

� � � � � � � � � � is a

unit pair iff

� ���

.

Lem: The algorithm

Base: UPair � � � � � � � � � � � � �

,Recursion: Iterate

UPair �� � � ��������������

����������

UPair � � � � � � � � �if there is

� � �

with� � � � � �

UPair � and� � � � � � �

and all symbolsin and

are nullable and� � � � � ��

UPair � �

UPair � otherwiseuntil UPair �� � � UPair �

finds exactely the unit pairs of

.

Context-free languages III – p.19/21

Page 29: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Eliminating unit productions

Lem: If

� � � � � � � � � �

then

� � � � � � �� �� ��

for

�� �� �� � � � � � � �� �� � � � �

with

�� �� � � �

����

���� � �

� � � � � �

� � � �

is a unit pair and� � � � � �

and

is not a single variable

����

����

Context-free languages III – p.20/21

Page 30: Deterministic Pushdown Automata, Reductions and Normal … · 2003-10-28 · Chomsky normal form Def: A context-free grammar is said to be in Chomsky normal form iff all its productions

Converting to Chomsky normal form

Thm: Any nonempty CFL not containing � has a Chomsky normalform grammar.

Prf: Take an arbitrary grammar for the language and1. eliminate �-productions,2. eliminate unit productions,3. eliminate non-generating symbols,4. eliminate unreachable symbols,

5. chain productions

� � with

� � � �by introducing helper variables:

� � � �� �

� � ��

� � � �

with a fresh variable

�.

Context-free languages III – p.21/21