CSE 105
THEORY OF COMPUTATION
Fall 2016 http://cseweb.ucsd.edu/classes/fa16/cse105-abc/
CSE 105 THEORY OF COMPUTATION Fall 2016 - - PowerPoint PPT Presentation
CSE 105 THEORY OF COMPUTATION Fall 2016 http://cseweb.ucsd.edu/classes/fa16/cse105-abc/ Today's learning goals Sipser Ch 1.4, 2.1 Context Free Grammars (CFG) Parse trees and ambiguity CFG Design Push Down Automata Arithmetic
Fall 2016 http://cseweb.ucsd.edu/classes/fa16/cse105-abc/
Sipser Ch 1.4, 2.1
S → S+S | S*S | S^N | N | (S) N → D | DN D → 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9
–
S → DN → 1N → 1DN→ 12N→ 12D → 123
–
S → S+S → 1+S → 1+S*S →* 1+2*S →* 1+2*3
S S + S 1 S * S 2 3
S S + S 1 S * S 2 3
S S + 3 1 + 2 S 1 + S 2 + 3
S S + 3 1 + 2 S 1 + S 2 + 3 (1+2)+3 1+(2+3)
S S * 3 1 + 2 S 1 + S 2 * 3 (1+2)*3 = 9 1+(2*3) = 7
unique parse tree (or, equivalently, a unique leftmost derivation)
such that w has two difgerent parse trees (or, equivalently, two difgerent leftmost derivations)
S → S+S | S*S | 0 | 1
A)Yes B)No C)It depends on the input string D)There is no answer because the question is ambiguous E)I don’t know
set of rules:
E → E+T | T T → T*F | F F → F^N | N | (E) N → 0 | … | 9
1+2*3^2+5
E N F + T E 5 + T E 1 * T E F
Context-free grammar One-step derivation Derivation Language generated by grammar
some CFG G
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?
–
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
is a CFG for L(E)
G=(V,Σ,R,S) such that L(M)=L(G)
Idea: trace computation using variables to denote state
–
V=Q
–
S=s
–
R={q → a δ(q,a) | q ∊Q, a∊Σ} U {q → ε | q ∊ F}
Context-free languages Context-free languages
Regular languages Regular languages
Sipser p. 109
Sipser p. 109
At each step
based on current state, letter read, and top letter of stack.
letter to (or from) top of stack
Sipser p. 109
Accept a string if there is some sequence of states and some sequence
processes the entire input string and ends in an accepting state.
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 →
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
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
again.
Sipser Def 2.13 p. 111
L = { 0i1i+1 | i ≥ 0 } Informal description of PDA: Read symbols from the input. As each 0 is read, push it onto the
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,
0s appear in the input following 1s, reject the input.
L = { 0i1i+1 | i ≥ 0 }
Theorem 2.20: A language is context-free if and only some nondeterministic PDA recognizes it.
Consequences
modes of proof (via CFGs or PDAs) depending on which is easier