1 / 60

Complexity and Computability Theory I

Complexity and Computability Theory I. Lecture #10 Instructor: Rina Zviel-Girshin Lea Epstein. Overview. Regular grammars Examples Conversion of an NFA to a Regular Grammar Conversion of a Regular Grammar to an NFA Chomsky grammar hierarchy Operations over grammars

ita
Download Presentation

Complexity and Computability Theory I

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. Complexity and Computability Theory I Lecture #10 Instructor: Rina Zviel-Girshin Lea Epstein

  2. Overview • Regular grammars • Examples • Conversion of an NFA to a Regular Grammar • Conversion of a Regular Grammar to an NFA • Chomsky grammar hierarchy • Operations over grammars • Chomsky normal form Rina Zviel-Girshin @ASC

  3. Regular grammar A grammar is called regular if each production has one of the following forms: Sw or SwT where w* and S,TV. Rina Zviel-Girshin @ASC

  4. Regular grammar example • Example S  012 S  0A A  0A A  0 Rina Zviel-Girshin @ASC

  5. Examples of regular grammars • Construct a regular grammar for the following regular expression: 0* • Regular grammar: S 0S |  Rina Zviel-Girshin @ASC

  6. Examples of regular grammars • Construct a regular grammar for the following regular expression: (0+1)+ • Regular grammar: S 0S | 1S | 0 | 1 Rina Zviel-Girshin @ASC

  7. Examples of regular grammars • Construct a regular grammar for the following regular expression: 0*+1* • Regular grammar: S | A | B A 0A | 0 B 1B | 1 Rina Zviel-Girshin @ASC

  8. Examples of regular grammars • Construct a regular grammar for the following regular expression: (01)+ • Regular grammar: S 01S | 01 Rina Zviel-Girshin @ASC

  9. Conversion of NFA to Regular Grammars • A regular grammar can be constructed for any NFA. The basic idea: translation of transition functions of an automaton to rules of the resulting regular grammar. Rina Zviel-Girshin @ASC

  10. Algorithm of conversion 1. Rename all states of NFA to a set of capital letters. 2. Name the start state of NFA S. 3. Translate each transition (A,)=B into the rule AB and (A,)=B into the rule AB. 4. Add the rule A for each final state A of the NFA. Rina Zviel-Girshin @ASC

  11. Example of conversion of NFA to RG We name q0: S and q1: A The regular grammar is: S 0S | 0A | 1A |  A 0A | 1S Rina Zviel-Girshin @ASC

  12. Conversion of Regular Grammars to NFA • A NFA can be constructed for any regular grammar G. The basic idea: construction of an NFA that accepts the language of the given regular grammar. Rina Zviel-Girshin @ASC

  13. A B A B   Algorithm 1. Transform all rules of the grammar to following form: Ax or AxB where x is either  or  and BV (by adding variables to the grammar). 2. The start state of NFA is the grammar's start symbol S. 3. For each rule: AB construct a state transition from A to B labeled . AB construct a state transition from A to B labeled . Rina Zviel-Girshin @ASC

  14. A F  Algorithm (cont.) 4. Add a new state F for each rule A and construct a state transition from A to the new state F labeled . 5. The final states of the NFA are F together with all A such that there is a rule A. Rina Zviel-Girshin @ASC

  15. Example S 0S | 11A A 1A | 0 New grammar is: S 0S | 1B B 1A A 1A | 0 Rina Zviel-Girshin @ASC

  16. Example (cont.) • The resulting NFA is: Rina Zviel-Girshin @ASC

  17. Chomsky grammar hierarchy • The Chomsky hierarchy is an hierarchy of classes of formal grammars that generate formal languages. • A formal grammar consists of: • a finite set of terminal symbols, • a finite set of nonterminal symbols, • a set of production rules with a left- and a right-hand side consisting of a word of these symbols, • a start symbol. Rina Zviel-Girshin @ASC

  18. Chomsky hierarchy • Chomsky hierarchy consist of 4 types of grammars: • Regular (type 3) • Context-free (type 2) • Context-sensitive (type 1) • Recursively enumerable (type 0) Rina Zviel-Girshin @ASC

  19. Regular grammars • Restricted to rules as: Sa or SaT where a and S,TV (different from our definition - a*) • Generates regular languages. • These languages are exactly all languages that can be decided by a FA . Rina Zviel-Girshin @ASC

  20. Context-free grammars • Restricted to rules as: Ab, AV and b(VU)* • Generates context-free languages. • These languages are exactly all languages that can be decided by a nondeterministic pushdown automaton PDA. Rina Zviel-Girshin @ASC

  21. Context-sensitive grammars • Restricted to rules as: α Aβ  α γ β AV and α , β, γ (VU)* • Generates context-sensitive languages. • These languages are exactly all languages that can be decided by a linear-bounded nondeterministic Turing machine BTM. Rina Zviel-Girshin @ASC

  22. S a S B C a a b C B C a a b B C a a b b C a a b b Example • L={anbn| n>=1} CSG G: S  aSBC S  abC CB  BC bB  bb bC  b Rina Zviel-Girshin @ASC

  23. Recursively enumerablegrammar • Another name unrestricted grammar. • No restrictions on the rules. • Generates recursively enumerable languages RE. • These languages are exactly all languages that can be recognized by Turing machine. Rina Zviel-Girshin @ASC

  24. Relations between types • Every regular language is context-free. • Every context-free language is context-sensitive. • Every context-sensitive language is recursively enumerable. • These are all proper inclusions. Rina Zviel-Girshin @ASC

  25. Operations over grammars We can perform the • union • concatenation • Kleene star operations over grammars. Rina Zviel-Girshin @ASC

  26. Union Given two languages and their grammars: • L1 with G1= (V1,1,S1,R1) and • L2 with G2 = (V2,2,S2,R2) we construct their union by merging their grammars: • G = (V1V2S, 12, S, R1R2{SS1| S2}) Rina Zviel-Girshin @ASC

  27. Union (cont.) • The rule SS1 | S2 • means that a string w in L(G) can be derived either from S1 or from S2. Rina Zviel-Girshin @ASC

  28. Concatenation Given two languages and their grammars: • L1 with G1= (V1,1,S1,R1) and • L2 with G2 = (V2,2,S2,R2) we construct their concatenation : • G = (V1V2S, 12, S, R1R2{SS1S2}) Rina Zviel-Girshin @ASC

  29. Concatenation (cont.) • The rule SS1S2 • means that a string w in L(G) is a concatenation of two parts w=uv, • such that u is derived from S1 and v is derived from S2. Rina Zviel-Girshin @ASC

  30. Kleene star • Given a language and its grammar: • L1 with G1= (V1,1,S1,R1) • we construct L1* by the grammar: • G = (V1 S, 1, S, R1 {SS1S|}) Rina Zviel-Girshin @ASC

  31. Kleene star (cont.) • The rule SS1S • means that a word w in L(G) is built of two parts w=uv such that u is derived from S1 and v is derived from S. • The rule S • means a final derivation of S or derivation of the  string. Rina Zviel-Girshin @ASC

  32. Simplified grammars • Every context-free grammar can be rewritten in a simplified form. • A simplified form of the grammar is a grammar that • doesn't have  rules and • doesn't have unit rules. •  rule is a rule of the form: A. • Unit rule is a rule of the form: AB. Rina Zviel-Girshin @ASC

  33. Removing  rules • A context-free language that does not contain  can be written with a CFG without  rules. • If L then remove  from L. • Build a CFG without  rules. • Add a S'S |  rule to it. Rina Zviel-Girshin @ASC

  34. How to find the set of all variables A such that A*? Variable A is called nullable if *? Rina Zviel-Girshin @ASC

  35. Algorithm for removing  rules 1. Find the set of all variables A such that A*. 2. For each rule of G of the form Bw where w(V)* create all possible new rules Bw' where w' is obtained by deleting one or more occurrences of variables found in step 1. ( If a variable on the right side can derive  then create another rule where this variable on the right side is deleted). Rina Zviel-Girshin @ASC

  36. Algorithm for removing  rules 3. The resulting grammar consists of the original rules together with new one constructed at step 2 minus -rules. Rina Zviel-Girshin @ASC

  37. A Simple Example S aA A A  is an  rule. By rule 2 of the algorithm to the rule: S aA we add the rule S a. By rule 3 of the algorithm we delete A. Rina Zviel-Girshin @ASC

  38. Simple Example (cont.) • The resulting grammar is: S aA | a • It is obvious that the first rule can’t be used to derive any word. • So it can be deleted. • The minimized grammar is: S a Rina Zviel-Girshin @ASC

  39. Example S aBaC B bB | C C cC |  Variables C and B can derive . • C • BC Rina Zviel-Girshin @ASC

  40. Example (cont.) • So we will delete B and C from the right side of the rules: Original grammarNew rules S aBaC SaaC | aBa | aa B bB Bb B C B C cC Cc C none Rina Zviel-Girshin @ASC

  41. Example (cont.) • The resulting grammar after removing -rules is: S aBaC | aaC | aBa | aa B bB | b | C C cC | c Rina Zviel-Girshin @ASC

  42. Removing unit rules • A context-free grammar that contains unit rules can be rewritten without unit rules. • Unit rule: A  B Rina Zviel-Girshin @ASC

  43. Algorithm for removing unit rules 1. For each unit rule AB , delete this rule from the grammar and add all productions of B to A. ( If Bw then after deleting AB a rule Aw should be added to A rules.) 2. Repeat step 1 until all unit rules are removed. Rina Zviel-Girshin @ASC

  44. Example SA | b A B | b B bB | a First we will eliminate AB unit rule. • Ab (old rule) • AbB | a (rules of B) • AbB | a | b (union of old and new rules) Rina Zviel-Girshin @ASC

  45. Example (cont.) • The resulting grammar is: SA | b A bB | a | b B bB | a Rina Zviel-Girshin @ASC

  46. Example (cont.) • Now we will eliminate SA unit rule. Sb (old rule) S bB | a | b (rules of A) SbB | a | b (union of old and new rules) The resulting grammar is: S bB | a | b A bB | a | b B bB | a Rina Zviel-Girshin @ASC

  47. Example (cont.) • Now A is not reachable from S and can be removed. (A is not used in any right-hand side production). • The resulting grammar is: S bB | a | b B bB | a Rina Zviel-Girshin @ASC

  48. Example (cont.) • The resulting grammar is: S bB | a | b B bB | a • What is the language of G? L(G) = a + b + b+a Rina Zviel-Girshin @ASC

  49. Chomsky normal form • Any context-free grammar can be written in a special form called Chomsky form. • A CFG is in Chomsky normal form if every rule of it written a following form: ABC or A where  and B,CV. • If a language contains  then the S rule is allowed. Rina Zviel-Girshin @ASC

  50. Chomsky normal form (cont.) • The Chomsky normal form has several uses. • Any string of length n can be derived in 2n-1 steps. • The derivation tree is a binary tree. Rina Zviel-Girshin @ASC

More Related