CS21 Decidability and Tractability

34
January 15, 2016 CS21 Lecture 6 1 CS21 Decidability and Tractability Lecture 6 January 15, 2016

description

CS21 Decidability and Tractability. Lecture 6 January 17, 2014. Outline. equivalence of NPDAs and CFGs non context-free languages deterministic PDAs. Context-Free Grammars. A → 0A1 A → B B → #. terminal symbols. start symbol. non-terminal symbols. production. - PowerPoint PPT Presentation

Transcript of CS21 Decidability and Tractability

Page 1: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 1

CS21 Decidability and Tractability

Lecture 6

January 15, 2016

Page 2: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 2

Outline

• Context-Free Grammars and Languages– ambiguity– normal form

• equivalence of NPDAs and CFGs

• non context-free languages

Page 3: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 3

CFG example

• Arithmetic expressions over {+,*,(,),a}– (a + a) * a – a * a + a + a + a + a

• A CFG generating this language:<expr> → <expr> * <expr>

<expr> → <expr> + <expr>

<expr> → (<expr>) | a

Page 4: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 4

CFG example

• A derivation of the string: a+a*a<expr> <expr> * <expr> <expr> + <expr> * <expr>

a + <expr> * <expr> a + a * <expr> a + a * a

<expr> → <expr> * <expr>

<expr> → <expr> + <expr>

<expr> → (<expr>) | a

Page 5: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 5

Parse Trees

• Easier way to picture derivation: parse tree

• grammar encodes grouping information; this is captured in the parse tree.

<expr>

<expr> <expr>

<expr> <expr>

*

+

a a

a

Page 6: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 6

CFGs and parse trees

• Is this a good grammar for arithmetic expressions?– can group wrong way (+ precedence over *)– can also group correct way (ambiguous)

<expr> → <expr> * <expr>

<expr> → <expr> + <expr>

<expr> → (<expr>) | a

Page 7: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 7

Solution to first problem

• forces correct precedence in parse tree grouping– within parentheses, * cannot occur as

ancestor of + in the parse tree.

<expr> → <expr> + <term> | <term>

<term> → <term> * <factor> | <factor>

<factor> → <term> * <factor>

<factor> → (<expr>) | a

Page 8: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 8

Parse Trees• parse tree for a + a * a in new grammar:

<expr> → <expr> + <term> | <term>

<term> → <term> * <factor> | <factor>

<factor> → <term> * <factor>

<factor> → (<expr>) | a

<expr>

<expr><term>

+

<term> <factor>*

<factor> a

a

<term>

a

<factor>

Page 9: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 9

Ambiguity

• Second problem: ambiguous grammar

• Definitions:– a string is derived ambiguously if it has two

different parse trees– a grammar is ambiguous if its language

contains an ambiguously derived string

• ambiguity sometimes undesirable

• some CFLS are inherently ambiguous

Page 10: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 10

Ambiguity

• Definition in terms of derivations (rather than parse trees):– order in which we replace terminals in

shouldn’t matter (often several orders possible)

– define leftmost derivation to be one in which the leftmost non-terminal is always the one replaced

– a string is ambiguously derived if it has 2 leftmost derivations

Page 11: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 11

Chomsky Normal Form

• Useful to deal only with CFGs in a simple normal form

• Most common: Chomsky Normal Form (CNF)

• Definition: every production has formA → BC or S → ε or

A → a

where A, B, C are any non-terminals (and B, C are not S) and a is any terminal.

Page 12: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 12

Chomsky Normal Form

Theorem: Every CFL is generated by a CFG in Chomsky Normal Form.

Proof: Transform any CFG into an equivalent CFG in CNF. Four steps:– add a new start symbol– remove “ε-productions” A → ε– eliminate “unit productions” A → B– convert remaining rules into proper form

Page 13: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 13

Chomsky Normal Form

• add a new start symbol– add production S0 → S

• remove “ε-productions” A → ε– for each production with A on rhs, add

production with A’s removed: e.g. for each rule R → uAv, add R → uv

• eliminate “unit productions” A → B– for each production with B on lhs: B → u, add

rule A → u

Page 14: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 14

Chomsky Normal Form

• convert remaining rules into proper form– replace production of form:

A → u1U2u3…u

k

with:

A → U1A1 U1 → u1

A1 → U2A2

A2 → U3A3 U3 → u3

:

Ak-2 → Uk-1Uk Uk-1 → uk-1 Uk → uk

U2 already a non-terminal

Page 15: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 15

Some facts about CFLs

• CFLs are closed under– union (proof?)– concatenation (proof?)– star (proof?)

• Every regular language is a CFL– proof?

Page 16: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 16

NPDA, CFG equivalence

Theorem: a language L is recognized by a NPDA iff L is described by a CFG.

Must prove two directions:() L is recognized by a NPDA implies L is

described by a CFG.

() L is described by a CFG implies L is recognized by a NPDA.

Page 17: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 17

NPDA, CFG equivalence

Proof of (): L is described by a CFG implies L is recognized by a NPDA.

0 # 1

q1

0

:

1

A

$

0 # 1

q2

0

:

1

#

$

0 # 1

q3

0

:

1

#

$

A → 0A1

A → #

an idea:

Page 18: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 18

NPDA, CFG equivalence

1. we’d like to non-deterministically guess the derivation, forming it on the stack

2. then scan the input, popping matching symbol off the stack at each step

3. accept if we get to the bottom of the stack at the end of the input.

what is wrong with this approach?

Page 19: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 19

NPDA, CFG equivalence– only have access to top of stack– combine steps 1 and 2:

• allow to match stack terminals with tape during the process of producing the derivation on the stack

0 # 1

q1

0

1

A

$

0 # 1

q2

1

A

$

0 # 1

q3

#

1

$

A → 0A1

A → #

Page 20: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 20

NPDA, CFG equivalence

• informal description of construction:– place $ and start symbol S on the stack– repeat:

• if the top of the stack is a non-terminal A, pick a production with A on the lhs and substitute the rhs for A on the stack

• if the top of the stack is a terminal b, read b from the tape, and pop b from the stack.

• if the top of the stack is $, enter the accept state.

Page 21: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 21

NPDA, CFG equivalence

ε, ε → S$

ε, A → w

b, b → ε

one transition for each production

A → w

one transition for each terminal b

ε, A → w = w1w2…wk

shorthand for:

ε, A → wk

q r

q r

q1

q2 qk…

ε, ε → wk-1

ε, ε → w1

ε, $ →

Page 22: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 22

NPDA, CFG equivalence

Proof of (): L is recognized by a NPDA implies L is described by a CFG.

– harder direction– first step: convert NPDA into “normal form”:

• single accept state• empties stack before accepting• each transition either pushes or pops a symbol

Page 23: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 23

NPDA, CFG equivalence

– main idea: non-terminal Ap,q generates exactly the strings that take the NPDA from state p (w/ empty stack) to state q (w/ empty stack)

– then Astart, accept generates all of the strings in the

language recognized by the NPDA.

Page 24: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 24

NPDA, CFG equivalence

• Two possibilities to get from state p to q:

stack height

abcabbacacbacbacabacabbabbabaacabbbababaacaccacccccinput

string taking NPDA from p to q

p qr

generated by Ap,r generated by Ar,q

Page 25: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 25

NPDA, CFG equivalence

• NPDA P = (Q, Σ, , δ, start, {accept})

• CFG G:– non-terminals V = {Ap,q : p, q Q}

– start variable Astart, accept

– productions:for every p, r, q Q, add the rule

Ap,q → Ap,rAr,q

Page 26: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 26

NPDA, CFG equivalence

• Two possibilities to get from state p to q:

stack height

abcabbacacbacbacabacabbabbabaacabbbababaacaccacccccinput

string taking NPDA from p to q

p q

r

generated by Ar,s

s

push d pop d

Page 27: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 27

NPDA, CFG equivalence

• NPDA P = (Q, Σ, , δ, start, {accept})

• CFG G:– non-terminals V = {Ap,q : p, q Q}

– start variable Astart, accept

– productions:for every p, r, s, q Q, d , and a, b (Σ {ε})

if (r, d) δ(p, a, ε), and

(q, ε) δ(s, b, d), add the rule

Ap,q → aAr,sb

from state p, read a, push d, move to state r

from state s, read b, pop d,

move to state q

Page 28: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 28

NPDA, CFG equivalence

• NPDA P = (Q, Σ, , δ, start, {accept})

• CFG G:– non-terminals V = {Ap,q : p, q Q}

– start variable Astart, accept

– productions:for every p Q, add the rule

Ap,p → ε

Page 29: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 29

NPDA, CFG equivalence

• two claims to verify correctness:

1. if Ap,q generates string x, then x can take NPDA P from state p (w/ empty stack) to q (w/ empty stack)

2. if x can take NPDA P from state p (w/ empty stack) to q (w/ empty stack), then Ap,q generates string x

Page 30: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 30

NPDA, CFG equivalence

1. if Ap,q generates string x, then x can take NPDA P from state p (w/ empty stack) to q (w/ empty stack)– induction on length of derivation of x. – base case: 1 step derivation. must have only

terminals on rhs. In G, must be production of form Ap,p → ε.

Page 31: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 31

NPDA, CFG equivalence

1. if Ap,q generates string x, then x can take NPDA P from state p (w/ empty stack) to q (w/ empty stack)– assume true for derivations of length at most

k, prove for length k+1.

– verify case: Ap,q → Ap,rAr,q →k x = yz

– verify case: Ap,q → aAr,sb →k x = ayb

Page 32: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 32

NPDA, CFG equivalence

2. if x can take NPDA P from state p (w/ empty stack) to q (w/ empty stack), then Ap,q generates string x

– induction on # of steps in P’s computation– base case: 0 steps. starts and ends at same

state p. only has time to read empty string ε.

– G contains Ap,p → ε.

Page 33: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 33

NPDA, CFG equivalence

2. if x can take NPDA P from state p (w/ empty stack) to q (w/ empty stack), then Ap,q generates string x– induction step. assume true for computations

of length at most k, prove for length k+1. – if stack becomes empty sometime in the

middle of the computation (at state r)• y is read going from state p to r (Ap,r→* y)• z is read going from state r to q (Ar,q→* z)• conclude: Ap,q → Ap,rAr,q →* yz = x

Page 34: CS21  Decidability and Tractability

January 15, 2016 CS21 Lecture 6 34

NPDA, CFG equivalence

2. if x can take NPDA P from state p (w/ empty stack) to q (w/ empty stack), then Ap,q generates string x

– if stack becomes empty only at beginning and end of computation.

• first step: state p to r, read a, push d

• go from state r to s, read string y (Ar,s→* y)

• last step: state s to q, read b, pop d

• conclude: Ap,q → aAr,sb →* ayb = x