600 likes | 766 Views
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
E N D
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 • Chomsky normal form Rina Zviel-Girshin @ASC
Regular grammar A grammar is called regular if each production has one of the following forms: Sw or SwT where w* and S,TV. Rina Zviel-Girshin @ASC
Regular grammar example • Example S 012 S 0A A 0A A 0 Rina Zviel-Girshin @ASC
Examples of regular grammars • Construct a regular grammar for the following regular expression: 0* • Regular grammar: S 0S | Rina Zviel-Girshin @ASC
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
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
Examples of regular grammars • Construct a regular grammar for the following regular expression: (01)+ • Regular grammar: S 01S | 01 Rina Zviel-Girshin @ASC
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
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 AB and (A,)=B into the rule AB. 4. Add the rule A for each final state A of the NFA. Rina Zviel-Girshin @ASC
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
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
A B A B Algorithm 1. Transform all rules of the grammar to following form: Ax or AxB where x is either or and BV (by adding variables to the grammar). 2. The start state of NFA is the grammar's start symbol S. 3. For each rule: AB construct a state transition from A to B labeled . AB construct a state transition from A to B labeled . Rina Zviel-Girshin @ASC
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
Example S 0S | 11A A 1A | 0 New grammar is: S 0S | 1B B 1A A 1A | 0 Rina Zviel-Girshin @ASC
Example (cont.) • The resulting NFA is: Rina Zviel-Girshin @ASC
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
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
Regular grammars • Restricted to rules as: Sa or SaT where a and S,TV (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
Context-free grammars • Restricted to rules as: Ab, AV 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
Context-sensitive grammars • Restricted to rules as: α Aβ α γ β AV 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
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
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
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
Operations over grammars We can perform the • union • concatenation • Kleene star operations over grammars. Rina Zviel-Girshin @ASC
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 = (V1V2S, 12, S, R1R2{SS1| S2}) Rina Zviel-Girshin @ASC
Union (cont.) • The rule SS1 | S2 • means that a string w in L(G) can be derived either from S1 or from S2. Rina Zviel-Girshin @ASC
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 = (V1V2S, 12, S, R1R2{SS1S2}) Rina Zviel-Girshin @ASC
Concatenation (cont.) • The rule SS1S2 • 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
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 {SS1S|}) Rina Zviel-Girshin @ASC
Kleene star (cont.) • The rule SS1S • 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
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: AB. Rina Zviel-Girshin @ASC
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
How to find the set of all variables A such that A*? Variable A is called nullable if *? Rina Zviel-Girshin @ASC
Algorithm for removing rules 1. Find the set of all variables A such that A*. 2. For each rule of G of the form Bw where w(V)* create all possible new rules Bw' 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
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
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
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
Example S aBaC B bB | C C cC | Variables C and B can derive . • C • BC Rina Zviel-Girshin @ASC
Example (cont.) • So we will delete B and C from the right side of the rules: Original grammarNew rules S aBaC SaaC | aBa | aa B bB Bb B C B C cC Cc C none Rina Zviel-Girshin @ASC
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
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
Algorithm for removing unit rules 1. For each unit rule AB , delete this rule from the grammar and add all productions of B to A. ( If Bw then after deleting AB a rule Aw should be added to A rules.) 2. Repeat step 1 until all unit rules are removed. Rina Zviel-Girshin @ASC
Example SA | b A B | b B bB | a First we will eliminate AB unit rule. • Ab (old rule) • AbB | a (rules of B) • AbB | a | b (union of old and new rules) Rina Zviel-Girshin @ASC
Example (cont.) • The resulting grammar is: SA | b A bB | a | b B bB | a Rina Zviel-Girshin @ASC
Example (cont.) • Now we will eliminate SA unit rule. Sb (old rule) S bB | a | b (rules of A) SbB | 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
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
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
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: ABC or A where and B,CV. • If a language contains then the S rule is allowed. Rina Zviel-Girshin @ASC
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