1 / 18

CSCI 2670 Introduction to Theory of Computing

CSCI 2670 Introduction to Theory of Computing. September 21, 2004. Agenda. Last week Pumping lemma Context-free grammars Examples, definition, strategies for building This week More on CFG’s Ambiguity, Pushdown automata, pumping lemma for CFG’s Next week Midterm (Chapters 1 & 2).

zona
Download Presentation

CSCI 2670 Introduction to Theory of Computing

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. CSCI 2670Introduction to Theory of Computing September 21, 2004

  2. Agenda • Last week • Pumping lemma • Context-free grammars • Examples, definition, strategies for building • This week • More on CFG’s • Ambiguity, Pushdown automata, pumping lemma for CFG’s • Next week • Midterm (Chapters 1 & 2)

  3. Announcement • Homework due next Tuesday (9/28) • 2.8, 2.9, 2.12, 2.13, 2.15, 2.18 d, 2.22

  4. Ambiguity • Consider the CFG ({S},{0,1},R,S), where the rules of R are S  0 | 1 | S + S | S * S • Derive the string 0 * 1 + 1

  5. S S + * S S 1 0 S S + * S S 1 0 1 1 Ambiguity S  0 | 1 | S + S | S * S • 0 * 1 + 1 S S Different parse trees!

  6. Leftmost derivation & ambiguity • A derivation of a string w in a grammar G is a leftmost derivation if every step of the derivation replaced the leftmost variable • A string is derived ambiguously in CFG G if it has two or more different leftmost derivations • The grammar G is ambiguous if it generates some string ambiguously • Some grammars are inherently ambiguous

  7. Chomsky normal form • Method of simplifying a CFG Definition: A context-free grammar is in Chomsky normal form if every rule is of one of the following forms A  BC A  a where a is any terminal and A is any variable, and B, and C are any variables or terminals other than the start variable the rule S  ε is permitted, where S is the start variable

  8. CFG’s and Chomsky normal form Theorem: Any context-free language is generated by a context-free grammar in Chomsky normal form. Proof idea: Convert any CFG to one in Chomsky normal form by removing or replacing all rules in the wrong form • Add a new start symbol • Eliminate ε rules of the form A  ε • Eliminate unit rules of the form A  B • Convert remaining rules into proper form

  9. Convert a CFG to Chomsky normal form • Add a new start symbol • Create the following new rule S0 S where S is the start symbol and S0 is not used in the CFG

  10. Convert a CFG to Chomsky normal form • Eliminate all ε rules A  ε, where A is not the start variable • For each rule with an occurrence of A on the right-hand side, add a new rule with the A deleted R  uAv becomes R  uAv | uv R  uAvAw becomes R  uAvAw | uvAw | uAvw | uvw • If we have R  A, replace it with R  ε unless we had already removed R  ε

  11. Convert a CFG to Chomsky normal form • Eliminate all unit rules of the form A  B • For each rule B  u, add a new rule A  u, where u is a string of terminals and variables, unless this rule had already been removed • Repeat until all unit rules have been replaced

  12. Convert a CFG to Chomsky normal form • Convert remaining rules into proper form • What’s left? • Replace each rule A  u1u2…uk, where k  3 and ui is a variable or a terminal with k-1 rules A  u1A1 A1  u2A2 … Ak-2  uk-1uk

  13. Example S  S1 | S2 S1 S1b | Ab | ε A  aAb | ab S2 S2a | Ba | ε B  bBa | ba Step 1: Add a new start symbol

  14. Example S0  S S  S1 | S2 S1 S1b | Ab A  aAb | ab | ε S2 S2a | Ba B  bBa | ba | ε Step 2: Eliminate ε rules

  15. Example S0  S S  S1 | S2 S1 S1b | Ab | b A  aAb | ab S2 S2a | Ba | a B  bBa | ba Step 3: Eliminate all unit rules

  16. Example S0  S1b | Ab | b | S2a | Ba | a S  S1b | Ab | b | S2a | Ba | a S1 S1b | Ab | b A  aAb | ab S2 S2a | Ba | a B  bBa | ba Step 4: Convert remaining rules to proper form

  17. Example S0  S1b | Ab | b | S2a | Ba | a S  S1b | Ab | b | S2a | Ba | a S1 S1b | Ab | b A  aA1 | ab A1  Ab S2 S2a | Ba | a B  bB1| ba B1  Ba

  18. Tomorrow • Pushdown automata

More Related