ambiguity in context-free grammars, introduction to pushdown automata ·  · 2003-10-27ambiguity...

36
Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Fr ¨ anzle Informatics and Mathematical Modelling The Technical University of Denmark Context-free languages II – p.1/25

Upload: vomien

Post on 12-May-2018

250 views

Category:

Documents


3 download

TRANSCRIPT

Page 1: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Ambiguity in Context-Free Grammars,Introduction to Pushdown Automata

Martin Franzle

Informatics and Mathematical Modelling

The Technical University of Denmark

Context-free languages II – p.1/25

Page 2: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

What you’ll learn

1. Context-free grammars:

� What’s ambiguity

� Why it is a problem

� How to avoid it

� How to build useful CFGs

2. Pushdown automata:

� Machines recognizing CFLs

� Operational approach towards CFLs

� Tools for CFLs

Context-free languages II – p.2/25

Page 3: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Ambiguity in CFGs

Context-free languages II – p.3/25

Page 4: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Pragmatics

The use cases of regular languages and context-free languages aresubtly different:

� For RLs, one is primarily interested in language membership:

� finding strings in a text,

� classifying words/phrases in a text,

�� � �

� With CFLs, one is often also interested in the structuralinformation conveyed by a parse tree:

BE Sthenif

BE Sthenif

Selse

S

Selse

S

BE Sthenif

BE Sthenif

Context-free languages II – p.4/25

Page 5: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

A classical ambiguity

Given

� � if

� �

then

else

� �

if

� �

then

� �

print� � �

� � �

� � � true

false

�� � �

what shall

if false then if false then print true else print false

output?

BE Sthenif

BE Sthenif

Selse

S

Selse

S

BE Sthenif

BE Sthenif

Context-free languages II – p.5/25

Page 6: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

A classical ambiguity

Given

� � if

� �

then

else

� �

if

� �

then

� �

print� � �

� � �

� � � true

false

�� � �

what shall

if false then if false then print true else print false

output?

BE Sthenif

BE Sthenif

Selse

S

Selse

S

BE Sthenif

BE Sthenif

Context-free languages II – p.5/25

Page 7: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

A classical ambiguity — resolved

Given

� � � � �

if

� �

then

� �

print

� � �� � �

� � � if

� �

then

� �

else

� �

print� � �

� � �

� � � true

false

�� � �

what shall

if false then if false then print true else print false

output?

Selse

S

BE thenifSelse

S

BE Sthenif

BE S’thenif S

BE S’thenif

S’

Context-free languages II – p.6/25

Page 8: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Ambiguity

Def: A CFG

is called ambiguous iff there is � � � � � �

s.t. � has(at least) two different parse trees wrt.

.

N.B. It is not the existence of multiple derivations for some string

� � � � � �

that renders

ambiguous!

Context-free languages II – p.7/25

Page 9: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Removing ambiguity

There is no algorithm for resolving ambiguity (in the sense of

automatically deriving an unambiguous grammar from a givengrammar).

There is not even an algorithm for finding out whether a given

CFG is ambiguous.

However, there are standard techniques for writing an

unambiguous grammar that help in most cases.

Context-free languages II – p.8/25

Page 10: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Removing ambiguity

There is no algorithm for resolving ambiguity (in the sense of

automatically deriving an unambiguous grammar from a givengrammar).

There is not even an algorithm for finding out whether a given

CFG is ambiguous.

However, there are standard techniques for writing an

unambiguous grammar that help in most cases.

Context-free languages II – p.8/25

Page 11: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Removing ambiguity

There is no algorithm for resolving ambiguity (in the sense of

automatically deriving an unambiguous grammar from a givengrammar).

There is not even an algorithm for finding out whether a given

CFG is ambiguous.

However, there are standard techniques for writing an

unambiguous grammar that help in most cases.

Context-free languages II – p.8/25

Page 12: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Expression grammars

Ambiguous

Unambiguous

� � � � � � �

� � � � � � � � �

� � � � � � ��

� � � � � � � � � � �

Context-free languages II – p.9/25

Page 13: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Expression grammars

Ambiguous Unambiguous

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

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

� � � � � � ��

� � � � � � � ��

� � �

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

Context-free languages II – p.9/25

Page 14: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Derivations vs. ambiguity

Let

� � �� � � � � � � �

be a grammar and � � ��

.

Thm: The following statements are equivalent:

� � has more than one parse tree

� � has more than one leftmost derivation

� � has more than one rightmost derivation.

Prf. Leftmost (rightmost, resp.) derivations can be understood as a canonicalway of traversing a parse tree and are thus in one-to-one correspondenceto parse trees.

Context-free languages II – p.10/25

Page 15: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Inherent ambiguity

Def: A CFL

is called inherently ambiguous iff all CFGs

with

� � � � � �

are ambiguous.

N.B.

� The mere existence of one ambiguous CFG for

is notsufficient to render

inherently ambiguous.(In fact, any non-empty CFL has an ambiguous CFG.)

� All the previous examples had an unambiguous CFG and arethus not inherently ambiguous.

Context-free languages II – p.11/25

Page 16: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

An inherently ambiguous language

The language

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

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

is inherently ambiguous.

An example grammar:

Context-free languages II – p.12/25

Page 17: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

An inherently ambiguous language

The language

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

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

is inherently ambiguous.

An example grammar:

� � � � � �

� � � � � � �

� � � � � � �

� � � � � � �

� � � � � � �

Context-free languages II – p.12/25

Page 18: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Pushdown Automata

Context-free languages II – p.13/25

Page 19: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Idea of pushdown automaton

Take an �-NFA and

1. add a stack for unbounded storage, yet with access limited to“last-in-first-out”;

2. make transitions dependent on input symbol and stack top;

3. add side effect on stack to transitions:transitions replace stack top by an arbitrary string, thus beingable to

� “pop” stack (replacement by �),

� preserve stack (replace stack top

by

),

� “push” onto stack (replace stack top

by ��� � � � � ��

),

� “pop” and then “push” (replace stack top

by ��� � � � � �).

Context-free languages II – p.14/25

Page 20: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Formal definition

A pushdown automaton (PDA) is a seven-tuple� � � � � � � � � � � ��� � �� � � �

with

� �

being a finite set of states,

� �

being the input alphabet, i.e. the finite set of input symbols,

� �

being the stack alphabet,

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

fin

� � �� �

being the transition function,

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

implies that

can move from to �

when the inputsymbol is � (or arbitrary in case �� �) and the stack top is

.

is thenreplaced by �.

� ��� being the start state,

� �� being the start symbol on the stack (i.e., execution starts withthe stack containing exactely one item, namely

�� ),

� �

being the set of accepting states.

Context-free languages II – p.15/25

Page 21: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Instantaneous descriptions (IDs)

� An instantaneous description (ID) is a triple

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

, where

� denotes the current state,

� � denotes the remaining input,

� � denotes the stack contents (top of the stack

�� first letter of �).

� IDs describe configurations of PDA computations.

is a relation between IDs of PDA formalizing “moves” of :

iff

Context-free languages II – p.16/25

Page 22: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Instantaneous descriptions (IDs)

� An instantaneous description (ID) is a triple

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

, where

� denotes the current state,

� � denotes the remaining input,

� � denotes the stack contents (top of the stack

�� first letter of �).

� IDs describe configurations of PDA computations.

� ��� is a relation between IDs of PDA�

formalizing “moves” of

:

� � � � � � � ���

� � � � � � � � � �

iff

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

� � � � � �

� � � � �

� � � � � �

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

���

Context-free languages II – p.16/25

Page 23: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Translation invariance

Thm: If

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

is a PDA and � � ��

and � � ��then

� � � � � � � ���

��� � � � � �

implies

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

��� � � � � � � �

I.e., PDA behaviours are preserved under stack extensionand/or input extension.

Prf: It is easy to see from the definition that

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

implies� � � �� � � � � � �� � � �� � � �

The theorem follows by induction on the number of steps.

Context-free languages II – p.17/25

Page 24: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Translation invariance

Thm: If

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

is a PDA and � � � � ��

then

� � � � � � � � ���

��� � � � � � �

implies

� � � � � � � ���

��� � � � � �

I.e., PDA behaviour is independent from trailing input.

N.B. The same is not true wrt. trailing stack contents, as the PDAmight well pop off, inspect, and then restore some part of thestack contents.

Prf: It is easy to see from the definition that

� � � �� � � �� �� � � �� �implies

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

The theorem follows by induction on the number of steps.

Context-free languages II – p.18/25

Page 25: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Language of a PDA

Def: The language

� � � �

of a PDA

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

is

� � � � def� � � � �� � � ��� � � � �� � ���

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

This form of language acceptance is often called “acceptance byfinal state”.

Def: The language of a PDA is

def

This form of language acceptance is often called “acceptance byempty stack”.

N.B. In general, .

Context-free languages II – p.19/25

Page 26: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Language of a PDA

Def: The language

� � � �

of a PDA

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

is

� � � � def� � � � �� � � ��� � � � �� � ���

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

This form of language acceptance is often called “acceptance byfinal state”.

Def: The language

� � � �

of a PDA

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

is

� � � � def� � � � �� � � ��� � � � �� � ���

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

This form of language acceptance is often called “acceptance byempty stack”.

N.B. In general,� � � � � � � � � �

.Context-free languages II – p.19/25

Page 27: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Expressiveness of the acceptance criteria

Thm: If

� � � � � �

for some pushdown automaton

then there is apushdown automaton

� �

with

� � � � � � �

.(Given

, the construction of

� �

is completely mechanic.)

Thm: If for some pushdown automaton then there is apushdown automaton with .(Given , the construction of is completely mechanic.)

Both forms of acceptance are thus equally “expressive”.

Context-free languages II – p.20/25

Page 28: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Expressiveness of the acceptance criteria

Thm: If

� � � � � �

for some pushdown automaton

then there is apushdown automaton

� �

with

� � � � � � �

.(Given

, the construction of

� �

is completely mechanic.)

Thm: If

� � � � � �

for some pushdown automaton�

then there is apushdown automaton

� �

with

� � � � � � �.

(Given

, the construction of

� �

is completely mechanic.)

Both forms of acceptance are thus equally “expressive”.

Context-free languages II – p.20/25

Page 29: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

PDAs vs. CFGs

Acceptedby a PDAby empty

stack

Definableby aCFG

Acceptedby a PDAby final

state

Claim: PDAs and CFGs define the same languages.

Context-free languages II – p.21/25

Page 30: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Proof of the claim

� Have already shown equivalence between

� being accepted by some PDA by empty stack

� being accepted by some PDA by final state.

Suffices to show equivalence of

� being definable by a CFGto one of the two forms of PDA definability.

� We will show equivalence of CFG-definabilty to being acceptedby some PDA by empty stack.

Context-free languages II – p.22/25

Page 31: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

From CFG to PDA

Thm: If

� � � � � �

for some contextfree grammar

then there is apushdown automaton

with

� � � � � �

.(Given

, the construction of

is completely mechanic.)

Prf: (Essence of) Given , a corresponding 1-state PDA can bedefined by

forforotherwise.

Then show that for any and any

iff lm

Context-free languages II – p.23/25

Page 32: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

From CFG to PDA

Thm: If

� � � � � �

for some contextfree grammar

then there is apushdown automaton

with

� � � � � �

.(Given

, the construction of

is completely mechanic.)

Prf: (Essence of) Given

� � � �� �� � � � �

, a corresponding 1-state PDA

can bedefined by

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

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

for

�� �

� � � �� � � � � � � � � �for �� �

� � � �� � � � �

otherwise.

Then show that for any �� ��and any �� � � � � � �

� � �� � � �� � � � �� � � iff

� lm �� � � �

Context-free languages II – p.23/25

Page 33: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

From PDA to CFG

Thm: If

� � � � � �

for some pushdown automaton

then there is acontextfree grammar

with

� � � � � �

.(Given

, the construction of

is completely mechanic.)

Prf: Given , a corresponding CFG can be defined by

Then show that for any , any , and any

iff

Context-free languages II – p.24/25

Page 34: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

From PDA to CFG

Thm: If

� � � � � �

for some pushdown automaton

then there is acontextfree grammar

with

� � � � � �

.(Given

, the construction of

is completely mechanic.)

Prf: Given

� � � � � �� �� �� ��� � � � , a corresponding CFG�

can be defined by

� � � �� �� �� � �

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

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

��

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

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

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

�����

Then show that for any � � �

, any �� ��

, and any �� �

� � � �� � iff

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

Context-free languages II – p.24/25

Page 35: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Stack symbols can simulate states

Cor: For each PDA

there is a PDA

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

with

� � � � �

such that

� � � � � � � � � �

.

Prf: 1. Convert

to an equivalent CFG

using the construction of theprevious theorem,

2. convert

to a one-state PDA

� �

using the construction of thesecond-last theorem.

Prize is blowup in the size of the stack alphabet!

Context-free languages II – p.25/25

Page 36: Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata ·  · 2003-10-27Ambiguity in Context-Free Grammars, Introduction to Pushdown Automata Martin Franzle¨ Informatics

Stack symbols can simulate states

Cor: For each PDA

there is a PDA

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

with

� � � � �

such that

� � � � � � � � � �

.

Prf: 1. Convert

to an equivalent CFG

using the construction of theprevious theorem,

2. convert

to a one-state PDA

� �

using the construction of thesecond-last theorem.

Prize is blowup in the size of the stack alphabet!

Context-free languages II – p.25/25