january 15, 2014cs21 lecture 61 cs21 decidability and tractability lecture 6 january 16, 2015

29
January 15, 2014 CS21 Lecture 6 1 CS21 Decidability and Tractability Lecture 6 January 16, 2015

Upload: justina-freeman

Post on 21-Dec-2015

217 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 1

CS21 Decidability and Tractability

Lecture 6

January 16, 2015

Page 2: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 2

Outline

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

• equivalence of NPDAs and CFGs

• non context-free languages

Page 3: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 3

Context-Free Grammars

A → 0A1

A → B

B → #

start symbol

terminal symbols

non-terminal symbols

production

Page 4: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 4

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 5: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 5

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 6: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 6

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 7: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 7

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 8: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 8

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 9: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 9

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 10: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 10

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 11: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 11

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 12: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 12

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 13: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 13

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 14: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 14

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 15: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 15

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 16: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 16

Some facts about CFLs

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

• Every regular language is a CFL– proof?

Page 17: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 17

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 18: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 18

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 19: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 19

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 20: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 20

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 21: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 21

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 22: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 22

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 23: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 23

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 24: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 24

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 25: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 25

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 26: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 26

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 27: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 27

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 28: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 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, 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 29: January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015

January 15, 2014 CS21 Lecture 6 29

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 → ε