cse 105 theory of computation - computer science › ... › slides › lect13cse105ac.pdfcse 105...
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/1.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/2.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/3.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/4.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/5.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/6.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/7.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/8.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/9.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/10.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/11.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/12.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/13.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/14.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/15.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/16.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/17.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/18.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/19.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/20.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/21.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/22.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/23.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/24.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/25.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/26.jpg)
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](https://reader035.vdocument.in/reader035/viewer/2022062603/5f03244e7e708231d407beba/html5/thumbnails/27.jpg)
For next time● Read Sipser 2.1, 2.2● Design a few CFGs and PDAs on your own