homework
play

Homework Homework #3 returned Chomsky Normal Form Homework #4 due - PDF document

Homework Homework #3 returned Chomsky Normal Form Homework #4 due today Homework #5 Pg 169 -- Exercise 4 Pg 183 -- Exercise 4c,e,i (use JFLAP) Pg 184 -- Exercise 10 Pg 195 Exercise 5 Pg 196 Exercise 15


  1. Homework  Homework #3 returned Chomsky Normal Form  Homework #4 due today  Homework #5  Pg 169 -- Exercise 4  Pg 183 -- Exercise 4c,e,i (use JFLAP)  Pg 184 -- Exercise 10  Pg 195 – Exercise 5  Pg 196 – Exercise 15  Due 10 / 14 Announcements Before We Start  Final Exam Dates have been  Exam 1 will be returned next class announced  Tuesday, November 11  12:30 – 2:30 pm  Any questions?  Room TBA  Conflicts? Let me know. Plan for today Exercises to discuss  1st half  For after class  Chomsky Normal Form  CFG from last time  2nd half  Pumping Lemma from exam  Will discuss next time.  Pushdown Automata  Algorithm from HW#3. 1

  2. Languages The Language Bubble  Recall.  What is a language? Context Free Languages  What is a class of languages? Regular Languages Finite Languages Context Free Languages Grammars  Context Free Languages(CFL) is the  Let’s formalize this a bit: next class of languages outside of  A grammar is a 4-tuple: (V, T, P, S) where Regular Languages:  V is a set of variables  T is a set of terminals  Language / grammar: Context Free Grammar  P is a set of production rules  V and T are disjoint (I.e. V ∩ T = ∅ )  Machine for accepting: Pushdown  S ∈ V, is your start symbol Automata General Grammars Grammars  Production Rules  Let’s formalize this a bit:  Production rules  Of the form A → B  We say that γ can be derived from α in one step:  A is a string of terminals and variables  A → β is a rule  α = α 1 A α 2  B is a string of terminals and variables  γ = α 1 β α 2  To apply a rule, replace any occurrence of A  α ⇒ γ with the string B.  We write α ⇒ * γ if γ can be derived from α in zero or more steps. 2

  3. Context Free Grammars Context Free Grammars  Production Rules  The language generated by a grammar  Of the form A → B  Let G = (V, T, P, S)  A is a variable  The language generated by G, L(G)  B is a string, combining terminals and variables  L(G) = { x ∈ T * | S ⇒ * x}  To apply a rule, replace an occurrence of A with the string B.  A language L is a Context Free  We say that the grammar is context-free since this Language (CFL) iff there is a CFG G, substitution can take place regardless of where A is. such that  L = L(G) Chomsky Normal Form Theory Hall of Fame  Noam Chomsky  Chomsky Normal Form  The Grammar Guy  A context free grammar is in Chomsky  1928 – Normal Form (CNF) if every production is  b. Philadelphia, PA of the form:  A → BC  PhD – UPenn (1955)  A → a  Linguistics  Prof at MIT (Linguistics) (1955 - present)  Where A,B, and C are variables and a is a terminal. http://www.chomsky.info  Probably more famous for his leftist political views. Chomsky Normal Form Chomsky Normal Form  If we can put a CFG into CNF, then we 3 Step process:  can calculate the “depth” of the longest branch of a parse tree for the derivation Remove λ - Productions 1. of a string. Remove Unit Productions 2. Remove Useless Symbols A 3. At most 2 branches at every node B C a 3

  4. Removing λ -Productions Removing λ -Productions  A λ -Productions is a production of the  We must be a bit careful here form  If λ is in a CFL, then the production S → λ  A → λ must be in the production set.  Basic idea  The algorithm to be described will generate  Find the set of all variables A such that A ⇒ * λ L – { λ } (set of nullable variables)  For all productions that contain a nullable variable on the right hand side, add a production that eliminates the nullable from the right hand side Removing λ -Productions Removing λ -Productions  Step 1: Find the set of nullable variables:  Step 2: Remove nullable variables  Example:  For all productions A → β where β contains  S → AB nullable variables, add a new production  A → aAA | λ with each nullable removed from β  B → bBB | λ  All variables are nullable  A and B are nullable since A → λ and B → λ  S is nullable since S → AB and A and B are nullable Removing λ -Productions Removing λ -Productions Step 2: Remove nullable variables  Step 2: Remove nullable variables Example: Example:  S → AB  Consider: S → AB  A → aAA | λ  Add to P: S → A and S → B  B → bBB | λ  Consider: A → aAA  Add to P: A → aA and A → a  All variables are nullable  Consider: B → bBB  Add to P: B → bB and B → b 4

  5. Removing λ -Productions Removing λ -Productions  Step 3: Remove your λ -Productions  Step 2: Remove nullable variables  Example:  Our grammar now looks like:  Remove A → λ and B → λ  S → AB | A | B  Our final grammar looks like:  A → aAA | aA | a | λ  S → AB | A | B  B → bBB | bB | b | λ  A → aAA | aA | a  B → bBB | bB | b  Questions? Removing Unit Productions Removing Unit Productions  A Unit Productions is a production of the form  Step 0: Remove λ -Productions using the  A → B where A and B are variable previous algorithm.  Basic idea  Very similar to removing λ productions  For each variable A, find the set of all variables B such that A ⇒ * B by just following unit productions (A- derivable)  For all variables B that are A derivable and for all productions B → α , add the production A → α Removing Unit Productions Removing Unit Productions Step 1: For all variables A find the set  Step 1: For all variables A find the set of A-  derivable variables: of A-derivable variables:  Example: Recursive definition of A-derivable   S → S + T | T If A → B then B is A-derivable 1.  T → T * F | F If C is A derivable and C → B (and B ≠ A),  F → (S) | a 2. then B is A derivable No other variables are A-derivable.  Let’s find the set of S-derivable variables: 3.  T is S derivable since S → T  F is S derivable since T → F and T is S derivable 5

  6. Removing Unit Productions Removing Unit Productions  Step 1: For all variables A find the set of A-  Step 2: For each variable A, if B is A- derivable variables: derivable, for each non-unit production  Example: B → β , add the production A → β  S → S + T | T  T → T * F | F  F → (S) | a  S-derivable = {T, F}  T-derivable = {F}  F-derivable = ∅ Removing Unit Productions Removing Unit Productions  Step 2:  Step 2:  Example:  Our new grammar now looks like:  S → S + T | T  S → S + T | T * F | (S) | a | T  T → T * F | F  F → (S) | a  T → T * F | (S) | a | F  F → (S) | a  S-derivable = {T, F}  T-derivable = {F}  Add to P: S → T * F, S → (S) | a  : T → (S) | a Removing Unit Productions Removing Useless Symbols  A symbol X is useful for a grammar G = (V, T,  Step 3: Remove Unit Productions P, S) if  Our final grammar looks like:  S ⇒ * α X β ⇒ * w where w ∈ L(G)  Our new grammar now looks like:  In other words, a useful symbol will be used  S → S + T | T * F | (S) | a somewhere in the derivation of a string in the  T → T * F | (S) | a language.  F → (S) | a  Any symbol that is not useful is useless.  Remove S → T, T → F  Useless symbols do not add to the language generated by a grammar, so it’s okay to  Questions remove them. 6

  7. Removing Useless Symbols Removing useless symbols  Definitions: Algorithm:   We say a symbol X is generating if: Eliminate all non generating symbols 1.  X ⇒ * w for some w ∈ L(G) Eliminate all non reachable symbols from 2.  We say a symbol X is reachable if: resultant grammar.  S ⇒ * α X β for some α , β  Symbols that are useful must be both generating and reachable.  Such symbols (and assoc. productions) can be removed Removing useless symbols Removing useless symbols Finding generating symbols Finding reachable symbols   All symbols in T are generating S is reachable 1. 1. If A → α and all symbols in α are If A is reachable, and A → α , then all 2. 2. generating, then A is generating. variables in α are reachable. No other symbols are generating. 3. Removing Useless Symbols Removing useless symbols  Example:  Example: S → a S → AB | a A → b A → b  Now A is not reachable, eliminate it! B is useless since it is not generating S → a Eliminate it Note that you must eliminate non-generating symbols before non-reachable symbols. 7

Recommend


More recommend