1 / 23

Introduction to the Theory of Computation

Introduction to the Theory of Computation. John Paxton Montana State University Summer 2003. Humor.

oistin
Télécharger la présentation

Introduction to the 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. Introduction to the Theory of Computation John Paxton Montana State University Summer 2003

  2. Humor • A foreign visitor was being given a tour of Washington, D.C. one day by an American friend of hers. She was amazed at the size of the Monuments, the Congressional Buildings, and so forth. Finally she gazed upon the White House itself.       "My, that's an incredibly large building!" she remarked.       "Yes, it's pretty big, alright." said her friend.       "Big? It's huge!! About how many people work in there?" she asked.       "Oh... about half."

  3. Chapter 2: Context-Free Languages • Applications: Capture most human language syntax, programming language syntax, the parser in a compiler • More powerful model of computation

  4. 2.1 Context-Free Grammars • A grammar consists of • production rules • variables • terminals • a starting variable A => 0A1 | #

  5. Derivation • A => 0A1 => 00A11 => 00#11

  6. Parse Tree A 0 A 1 0 A 1 #

  7. Natural Language • <sentence> => <noun phrase> <verb phrase> • <noun phrase> => <noun> | <determiner> <noun> • <verb phrase> => <verb> • <det> => a | the • <noun> => boy | girl • <verb> => runs | plays

  8. Context Free Grammar Definition A CFG is a 4-tuple (V, S, R, S) where • V is a finite set called the variables • S is a finite set, disjoint from V, called the terminals • R is a finite set of rules, with each rule being a variable and a string of variables and terminals • S is the start symbol

  9. Exercise • Identify (V, S, R, S) for A => 0A1 | # • Describe the language

  10. More Definitions • uAv yields uwv if A => w is a rule • uAv =>* uwv if a sequence of 1 or more steps exist such that A => w • The language of the grammer is { w  S* | S =>* w }

  11. Another Grammar • <expr> => <expr> + <term> | <term> • <term> => <term> x <factor> | <factor> • <factor> => ( <expr> ) | a • Exercise: Parse tree for a + a x a • Exercise: Parse tree for (a + a) x a

  12. Exercises • Design a context free grammar for the language {0n1n | n >= 0} U {1n0n | n >= 0} • Design a context free grammar for the below NFA 0 1 e

  13. Exercises • Give a CFG that generates the language {w | w contains at least three 1s} over the alphabet {0,1}. • Repeat the above question for {w | w contains more 1s than 0s} • Repeat the above question for {w | w contains twice as many a’s as b’s} over the alphabet {a, b}

  14. Ambiguity • <expr> => <expr> + <expr> | <expr> x <expr> | a • A string w is derived ambiguously in a CFG if it has two or more different leftmost derivations. A CFG is ambiguous if it generates some string ambiguously. • Exercise: Show a + a x a is ambiguous.

  15. Chomsky Normal Form All rules are in one of the following forms: • S => e is allowed (S is the start symbol) • A => B C (B and C aren’t S) • A => a

  16. Theorem • Any context-free language is generated by a context-free grammar in Chomsky normal form.

  17. Proof • Add new start symbol S0 • Add S0 => S • Eliminate an e rule, adjust other rules. • Repeat above step until all e rules eliminated except for possible S0 => e Example: A => e, B => aAaAa becomes B => aAaAa | aaAa | aAaa

  18. Proof • Remove a unit rule A => B. Example: A => B, C => AA becomes C => BB • Repeat until all unit rules removed.

  19. Proof • Convert all remaining rules into proper form. Example: A => aBcD becomes A => U1U2 U1 => a U2 => BU3 U3 => U4D U4 => c

  20. Example • S => ASA | b | e • A => a • S0 => S (add new start rule) • S => ASA | b | e • A => a

  21. Example • S0 => S | e • S => AA | ASA | b (eliminate S => e) • A => a • S0 => AA | ASA | b | e (eliminate S0 => S) • S => AA | ASA | b • A => a

  22. Example • S0 => AA | AU1 | b | e (change S0 => ASA) • S => AA | AU1 | b (change S => ASA) • A => a • U1 => SA

  23. Exercise • Convert the following grammar to Chomsky Normal Form: • A => BAB | B | e • B -> 00 | e

More Related