+ All Categories
Home > Documents > CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105...

CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105...

Date post: 06-Jun-2020
Category:
Upload: others
View: 3 times
Download: 0 times
Share this document with a friend
27
CSE 105 THEORY OF COMPUTATION Fall 2016 http://cseweb.ucsd.edu/classes/fa16/cse105-abc/
Transcript
Page 1: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

CSE 105 THEORY OF COMPUTATION

Fall 2016

http://cseweb.ucsd.edu/classes/fa16/cse105-abc/

Page 2: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Today's learning goals Sipser Ch 1.4, 2.1

• Context Free Grammars (CFG)• Parse trees and ambiguity• CFG Design• Push Down Automata

Page 3: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Arithmetic Expressions● (3+12)*(5+7+(5*63^2))● Grammar rules:

S → S+S | S*S | S^N | N | (S)

N → D | DN

D → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9● Derivations

– S → DN → 1N → 1DN→ 12N→ 12D → 123

– S → S+S → 1+S → 1+S*S →* 1+2*S →* 1+2*3

Page 4: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Parse trees and Leftmost derivations● 1+2*3● S → S+S → S+S*S →* S+2*3 →* 1+2*3 ● S → S+S →* 1+S → 1+S*S →* 1+2*3 (leftmost)

S

S+

S

1

S*

S

2 3

Page 5: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Parse trees and Leftmost derivations● 1+2*3● S → S+S → S+S*S →* S+2*3 →* 1+2*3 ● S → S+S →* 1+S → 1+S*S →* 1+2*3 (leftmost)

S

S+

S

1

S*

S

2 3

● For each derivation, there is a unique parse three● For each parse tree, there is a unique leftmost derivation

Page 6: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Ambiguity● S → S+S → S+S+S → 1+2+3● S → S+S → 1+S → 1+S+S → 1+2+3

S

S + 3

1 + 2

S

1 + S

2 + 3

Page 7: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Ambiguity● S → S+S → S+S+S → 1+2+3● S → S+S → 1+S → 1+S+S → 1+2+3

S

S + 3

1 + 2

S

1 + S

2 + 3(1+2)+3

1+(2+3)

Page 8: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Ambiguity● S → S+S → S+S+S → 1+2+3● S → S+S → 1+S → 1+S+S → 1+2+3

S

S * 3

1 + 2

S

1 + S

2 * 3(1+2)*3 = 9

1+(2*3) = 7

Page 9: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Unambiguous Grammars● A CFG G is unambiguous if every w in L(G) has a

unique parse tree (or, equivalently, a unique leftmost derivation)

● A CFG is ambiguous if there exists a w in L(G) such that w has two different parse trees (or, equivalently, two different leftmost derivations)

Page 10: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Test time● Is the following grammar ambiguous?

S → S+S | S*S | 0 | 1

A)YesB)NoC)It depends on the input stringD)There is no answer because the question is

ambiguousE)I don’t know

Page 11: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Unambiguous Grammar for Exp● G = ({E,T,F},{0,…,9,+,*,^,(,)},R,E) where R is the

set of rules:E → E+T | T

T → T*F | F

F → F^N | N | (E)

N → 0 | … | 9

● Parse/Evaluate 1+2*3^2+5

E

N

F

+ TE

5

+ TE

1* TE

F

Page 12: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Recap: Context-free languagesContext-free grammar

One-step derivation

Derivation

Language generated by grammar

Page 13: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

CFGs and Automata● L is a context free language (CFL) if L=L(G) for

some CFG G● What is the relation between CFL and regular

languages?– Can any DFA/Regex be transformed into an equivalent CFG?

– Not all CFL can be recognized by a DFA/NFA!

– How can we extend DFAs/NFAs to make them as powerful as CFGs?

Page 14: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

From Regex to CFG● Approach via closure properties:

– Show that {a}, {ε} and {} are context free languages

– Show that the class of context free languages is closed under union, concatenation and star

– See Haskell 3 for details● It follows that for any regular expression E, there

is a CFG for L(E)

Page 15: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

From DFA to CFG● Claim: For any DFA M=(Q,Σ,δ,s,F) there is a CFG

G=(V,Σ,R,S) such that L(M)=L(G)● Proof:

Idea: trace computation using variables to denote state

– V=Q

– S=s

– R={q → a δ(q,a) | q ∊Q, a∊Σ} U {q → ε | q ∊ F}

Page 16: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Context-free languagesContext-free languages

Regular languages vs. CFL

Regular languagesRegular languages

Page 17: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

An alternative … Sipser p. 109

• NFA + stack

Page 18: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Pushdown automata Sipser p. 109

• NFA + stack

At each step

1. Transition to new state based on current state, letter read, and top letter of stack.

2. (Possibly) push or pop a letter to (or from) top of stack

Page 19: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Pushdown automata Sipser p. 109

• NFA + stack

Accept a string if

there is some sequence of states and some sequence of stack contents which processes the entire input string and ends in an accepting state.

Page 20: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

State diagram for PDA Sipser p. 109

If hand-drawn or in Sipser

State transition labelled a, b → c means

"when machine reads an a from the input and the top symbol of the stack is a b, it may replace the b with a c."

In JFLAP: use ; instead of →

Page 21: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

State diagram for PDA Sipser p. 109

If hand-drawn or in Sipser

State transition labelled a, b → c means

"when machine reads an a from the input and the top symbol of the stack is a b, it may replace the b with a c."What edge label would indicate “Read a 0, don’t pop anything from stack, don’t push anything to the stack”?

A) 0, ε→ε

B) ε, 0→ε

C) ε,ε→0

D) ε→ε,0

E) I don’t know

Page 22: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Useful trick● What would ε,ε → $ mean?

A) Without reading any input or popping any symbol from stack, push $

B) If the input string and stack are both empty, push $

C) At the end of reading the input, push $

D) I don’t know

Why is this useful?

Commonly used from initial state (at start of computation) to record bottom of stack with a special symbol. …. Useful to check if stack becomes empty again.

Page 23: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Formal definition of PDA Sipser Def 2.13 p. 111

Page 24: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Designing a PDAL = { 0i1i+1 | i ≥ 0 }

Informal description of PDA:

Read symbols from the input. As each 0 is read, push it onto the stack. As soon as 1s are seen, pop a 0 off the stack for each 1 read. If the stack becomes empty and there is exactly one 1 left to read, read that 1 and accept the input. If the stack becomes empty and there are either zero or more than one 1s left to read, or if the 1s are finished while the stack still contains 0s, or if any 0s appear in the input following 1s, reject the input.

Page 25: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

Designing/Tracing a PDAL = { 0i1i+1 | i ≥ 0 }

Page 26: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

PDAs and CFGs are equivalently expressive

Theorem 2.20: A language is context-free if and only some nondeterministic PDA recognizes it.

Consequences- Quick proof that every regular language is context free- To prove closure of class of CFLs under a given operation, can choose two

modes of proof (via CFGs or PDAs) depending on which is easier

Page 27: CSE 105 Theory of Computation - Computer Science › ... › Slides › Lect13CSE105ac.pdfCSE 105 THEORY OF COMPUTATION Fall 2016 ... -To prove closure of class of CFLs under a given

For next time● Read Sipser 2.1, 2.2● Design a few CFGs and PDAs on your own


Recommended