1 / 25

CSE 105 theory of computation

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.

latimore
Download Presentation

CSE 105 theory of computation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSE 105 theory of computation Fall 2019 https://cseweb.ucsd.edu/classes/fa19/cse105-a/

  2. 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

  3. Birds' eye view All languages over Σ Context-free languages over Σ Regular languages over Σ Finite languages over Σ

  4. 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.

  5. 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).

  6. 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…

  7. Approach 2 • Build CFGs for {a}, {ε}, {}; then show that the class of CFL is closed under the regular operations (union, concatenation, Kleene star).

  8. 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.

  9. 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 ?

  10. Designing a CFG We know this set is not regular! Building a CFG to describe the language { anbn | n ≥ 0 }

  11. 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?

  12. 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.

  13. 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.

  14. 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

  15. 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.

  16. Derivations and parsing E  E+E | ExE | (E) | 1 Lots of derivations for 1+1x1

  17. Derivations and parsing E  E+E | ExE | (E) | 1 Lots of derivations for 1+1x1

  18. 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

  19. Recap: Context-free languages Context-free grammar One-step derivation Derivation Language generated by grammar

  20. An alternative … Sipser p. 109 • NFA + stack

  21. 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

  22. 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.

  23. 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."

  24. 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.

  25. Formal definition of PDA Sipser Def 2.13 p. 111

More Related