250 likes | 271 Views
Explore the theory of computation topics on context-free grammars, regular and context-free languages, and pushdown automata. Learn how to design context-free grammars and trace pushdown automaton computations.
E N D
CSE 105 theory of computation Fall 2019 https://cseweb.ucsd.edu/classes/fa19/cse105-a/
Today's learning goals Sipser 2.1 • Test if a specific string can be generated by a given context-free grammar • Design a context-free grammar to generate a given language • Determine if a context-free grammar is ambiguous • Define push down automata • Trace the computation of a push down automaton
Birds' eye view All languages over Σ Context-free languages over Σ Regular languages over Σ Finite languages over Σ
Designing a CFG Can CFGs describe simple sets? Building a CFG to describe the language { abba } V = {S} Σ = ? R = { S abba } S What's the set of terminals of this CFG? {a,b} V U S U Σ {S, a, b} {a,b, ε} I don't know.
Is every regular language a CFL? • Approach 1: start with an arbitrary DFA M, build a CFG that generates L(M). • Approach 2: build CFGs for {a}, {ε}, {}; then show that the class of CFL is closed under the regular operations (union, concatenation, Kleene star).
Approach 1 Claim: Given any DFA M, there is a CFG whose language is L(M). Construction: Trace computation using variables to denote state Given M = (Q,Σ,δ,q0,F) a DFA, define the CFG V = { Si | qi is in Q } Σ R = { Si aSj | δ(qi,a) = qj } U { Si ε | qi is in F} S = S0 Then prove correctness…
Approach 2 • Build CFGs for {a}, {ε}, {}; then show that the class of CFL is closed under the regular operations (union, concatenation, Kleene star).
Approach 2 If G1 = (V1, Σ, R1, S1) and G2 = (V2, Σ, R2, S2) are CFGs and G1 describes L1, G2 describes L2, how can we combine the grammars so we describe L1 U L2 ? • G = (V1 U V2, Σ, R1 U R2, S1 U S2) • G = (V1 x V2, Σ, R1 x R2, (S1, S2) ) • We might not always be able to: the class of CFG describable languages might not be closed under union. • I don't know.
Approach 2 If G1 = (V1, Σ, R1, S1) and G2 = (V2, Σ, R2, S2) are CFGs and G1 describes L1, G2 describes L2, how can we combine the grammars so we describe L1 U L2 ?
Designing a CFG We know this set is not regular! Building a CFG to describe the language { anbn | n ≥ 0 }
Designing a CFG Building a CFG to describe the language { anbn | n ≥ 0 } One approach: • what is shortest string in the language? • how do we go from shorter strings to longer ones?
Designing a CFG Building a CFG to describe the language { anbn | n ≥ 0 } V = { S } Σ = { a,b } R = S Which rules would complete this CFG? S ε | ab S ε | aS | Sb S ε | aSb We need another variable other than S. I don't know.
Designing a CFG Also not a regular set Building a CFG to describe the language { 0n1m2n | n,m ≥ 0 } Hint: work from the outside in.
Designing a CFG Also not a regular set Building a CFG to describe the language { 0n1m2n | n,m ≥ 0 } Hint: work from the outside in. V = { S, T } Σ = { 0,1,2 } R = { S 0S2 | T | ε , T 1T | ε } S
CFGs in the wild V = {E}, Σ = {1,+,x,(,)}, R = { E E+E | ExE | (E) | 1 }, S=E Describing well-formed arithmetic expressions Which of the followings strings is generated by this CFG? • E • 11 • 1+1x1 • ε • I don't know.
Derivations and parsing E E+E | ExE | (E) | 1 Lots of derivations for 1+1x1
Derivations and parsing E E+E | ExE | (E) | 1 Lots of derivations for 1+1x1
Derivations and parsing E E+E | ExE | (E) | 1 Lots of derivations for 1+1x1 A string is ambiguously derived in a CFG if it has more than one leftmost derivation i.e. more than one parsing tree
Recap: Context-free languages Context-free grammar One-step derivation Derivation Language generated by grammar
An alternative … Sipser p. 109 • NFA + stack
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 popa letter to (or from) top of stack
Pushdown automata Sipser p. 109 • NFA + stack Accept a string if there is some sequence of states and somesequence of stack contents which processes the entire input string and ends in an accepting state.
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."
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"? 0, ε ε ε, 0 ε ε, ε 0 ε ε, 0 I don't know.