Western Michigan University CS6800 Advanced Theory of Computation Spring 2014 By Abduljaleel Alhasnawi & Rihab Almalki.

Slides:



Advertisements
Similar presentations
Closure Properties of CFL's
Advertisements

Simplifying CFGs There are several ways in which context-free grammars can be simplified. One natural way is to eliminate useless symbols those that cannot.
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
1 Chomsky Normal Form of CFG’s Definition Purpose Method of Constuction.
Fall 2004COMP 3351 Simplifications of Context-Free Grammars.
Prof. Busch - LSU1 Simplifications of Context-Free Grammars.
Dept. of Computer Science & IT, FUUAST Automata Theory 2 Automata Theory VII.
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
CS 3240 – Chapter 6.  6.1: Simplifying Grammars  Substitution  Removing useless variables  Removing λ  Removing unit productions  6.2: Normal Forms.
Chapter 4 Normal Forms for CFGs Chomsky Normal Form n Defn A CFG G = (V, , P, S) is in chomsky normal form if each rule in G has one of.
CS5371 Theory of Computation
CS 310 – Fall 2006 Pacific University CS310 Pushdown Automata Sections: 2.2 page 109 October 11, 2006.
1 CSC 3130: Automata theory and formal languages Tutorial 4 KN Hung Office: SHB 1026 Department of Computer Science & Engineering.
CS Master – Introduction to the Theory of Computation Jan Maluszynski - HT Lecture 4 Context-free grammars Jan Maluszynski, IDA, 2007
Introduction to the Theory of Computation John Paxton Montana State University Summer 2003.
Normal forms for Context-Free Grammars
1 Module 32 Chomsky Normal Form (CNF) –4 step process.
Context-Free Grammars Chapter 3. 2 Context-Free Grammars and Languages n Defn A context-free grammar is a quadruple (V, , P, S), where  V is.
CS 3813 Introduction to Formal Languages and Automata Chapter 6 Simplification of Context-free Grammars and Normal Forms These class notes are based on.
INHERENT LIMITATIONS OF COMPUTER PROGRAMS CSci 4011.
Chapter 12: Context-Free Languages and Pushdown Automata
Context-Free Grammars
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
CONVERTING TO CHOMSKY NORMAL FORM
Pushdown Automata (PDA) Intro
Context-free Grammars Example : S   Shortened notation : S  aSaS   | aSa | bSb S  bSb Which strings can be generated from S ? [Section 6.1]
Context-Free Grammars Normal Forms Chapter 11. Normal Forms A normal form F for a set C of data objects is a form, i.e., a set of syntactically valid.
Normal Forms for Context-Free Grammars Definition: A symbol X in V  T is useless in a CFG G=(V, T, P, S) if there does not exist a derivation of the form.
The Pumping Lemma for Context Free Grammars. Chomsky Normal Form Chomsky Normal Form (CNF) is a simple and useful form of a CFG Every rule of a CNF grammar.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
CSCI 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Ambiguity.
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
CS 3240: Languages and Computation Context-Free Languages.
Membership problem CYK Algorithm Project presentation CS 5800 Spring 2013 Professor : Dr. Elise de Doncker Presented by : Savitha parur venkitachalam.
Grammar G = (V N, V T, P, S) –V N : Nonterminal symbols –V T : Terminal symbols V N  V T = , V N ∪ V T = V – P : a finite set of production rules α 
Section 12.4 Context-Free Language Topics
Chapter 6 Simplification of Context-free Grammars and Normal Forms These class notes are based on material from our textbook, An Introduction to Formal.
CSCI 3130: Formal languages and automata theory Tutorial 4 Chin.
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
Closure Properties Lemma: Let A 1 and A 2 be two CF languages, then the union A 1  A 2 is context free as well. Proof: Assume that the two grammars are.
CS 208: Computing Theory Assoc. Prof. Dr. Brahim Hnich Faculty of Computer Sciences Izmir University of Economics.
1 Chapter 6 Simplification of CFGs and Normal Forms.
Introduction Finite Automata accept all regular languages and only regular languages Even very simple languages are non regular (  = {a,b}): - {a n b.
CSC 3130: Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Normal forms.
Structure and Ambiguity Removing Ambiguity Chomsky Normal Form Pushdown Automata Intro (who is he foolin', thinking that there will be time to get to this?)
Transparency No. 1 Formal Language and Automata Theory Homework 5.
Context-Free Grammars Normal Forms Chapter 11. Normal Forms A normal form F for a set C of data objects is a form, i.e., a set of syntactically valid.
Exercises on Chomsky Normal Form and CYK parsing
Chomsky Normal Form.
Dept. of Computer Science & IT, FUUAST Theory of Computation 2 Regular Expressions Regular Expression.
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
Theory of Computation Automata Theory Dr. Ayman Srour.
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Normal Forms (Chomsky and Greibach) Pushdown Automata (PDA) Intro PDA examples MA/CSSE 474 Theory of Computation.
Normal Forms for CFG’s Eliminating Useless Variables Removing Epsilon
Ambiguity Parsing algorithms
Complexity and Computability Theory I
7. Properties of Context-Free Languages
CSC312 Automata Theory Grammatical Format Chapter # 13 by Cohen
Simplifications of Context-Free Grammars
NORMAL FORMS FDP ON THEORY OF COMPUTING
7. Properties of Context-Free Languages
Chapter 6 Simplification of Context-free Grammars and Normal Forms
Structure and Ambiguity
Theory of Computation Lecture #
Normal forms and parsing
Answer Questions about Exam2 problems
MA/CSSE 474 Theory of Computation
Context-Free Languages
Presentation transcript:

Western Michigan University CS6800 Advanced Theory of Computation Spring 2014 By Abduljaleel Alhasnawi & Rihab Almalki

» Grammar: G = (V, , R, S)  = { a, b } Terminals V = {A, B, C, a,b, c} Terminals and Nonterminals S S Start Symbol R = S → A Rules

» Rules: X  Y, where: » X  V-  » Ex: S  aS » Y : , a, A, aB » G1: R= S  aS | ......? » G2: R= S  aSa | bSb | ....? » G3: R= S  SS | (S) | ......?

» Rules: X  Y, where: » X  V-  and |X| = 1 » Ex: S  aS » Y  V* » Ex: S   S  aSb » A  a A  bBBa » A  B S  SS » A  aB S  ABCD

» A grammar G is ambiguous iff there is at least one string in L(G) for which G produces more than one parse tree. » Ex: » Bal = { w  {),(}*: the parentheses are balanced} » G = {{S,),(}, {),(}, R, S}, where: » R = S  SS | (S) | 

For a string (())(), we have more than one parse trees

» Chomsky Normal Form (CNF) » Def.: A context-free grammar G = (V, Σ, R, S) is said to be in Chomsky Normal Form (CNF), iff every rule in R is of one of the following forms: » X  a where a  , or » X  BC where B and C  V-  » Greibach Normal Form (GNF) » Def.: GNF is a context free grammar G = (V, , R, S), where all rules have one of the following forms: » X  a  where a   and   (V-  )*

» There exists 5-steps algorithm to convert a CFG G into a new grammar Gc such that: L(G) = L(Gc) – {  } » convertChomsky(G:CFG) = » 1- G ' = removeEps(G:CFG) S   » 2- G '' = removeUnits(G':CFG) A  B » 3- G ''' = removeMixed(G '' :CFG) A  aB » 4- G' v = removeLong(G''' :CFG) S  ABCD » 5- G' v : L(G) = L(G' v ) – {  } G v = atmostoneEps(G' v :CFG) S*  S, S  

» Find the set N of nullable variables in G. » X is nullable iff either X   or » (X  A, A   ) : X   » Ex1: G: S  aACa » A  B | a » B  C | c » C  cC |  » Now, since C  , C is nullable » since B  C, B is nullable » since A  B, A is nullable » Therefore N = { A,B,C}

» According to N, the rules will be: » S  aACa » A  B | a |  » B  C | c |  » C  cC |  » Now, removeEps returns G' : » S  aACa | aAa | aCa | aa » A  B | a » B  C | c » C  cC | c

» Def.: unit production is a rule whose right hand side consists of a single nonterminal symbol. Ex: A  B » Remove any unit production from G '. » Consider the remain rules » Ex1(continue), G‘: » S  aACa | aAa | aCa | aa » A  B | a » B  C | c » C  cC | c

» Now by apply removeUnits(G':CFG) : » Remove A  B But B  C | c, so Add A  C | c » Remove B  C Add B  cC (B  c, already there) » Remove A  C Add A  cC (A  c, already there) » So removeUnits returns G'' : » S  aACa | aAa | aCa | aa » A  a | c | cC » B  c | cC » C  cC | c

» Def.: mixed is a rule whose right hand side consists of combination of terminals or terminals with nonterminal symbol. » Create a new nonterminal T a for each terminal a   » For each T a, add the rule T a  a » Ex1(continue), G'' : » S  aACa | aAa | aCa | aa » A  a | c | cC » B  c | cC » C  cC | c

» Now, by apply removeMixed(G '' :CFG), G ''' : » S  T a ACT a | T a AT a | T a CT a | T a T a » A  a | c | T c C » B  c | T c C » C  T c C | c » T a  a » T c  c

» Def.: long is a rule whose right hand side consists of more than two nonterminal symbol. » R: A  BCDE » By remove long, it will be: » A  BM 2 » M 2  CM 3 » M 3  DE » Ex1(continue), G'' : » S  aACa | aAa | aCa | aa » A  a | c | cC » B  c | cC » C  cC | c

» Ex1(continue), G''' : » S  T a ACT a | T a AT a | T a CT a | T a T a » A  a | c | T c C » B  c | T c C » C  T c C | c » T a  a » T c  c

» Now, by apply removeLong(G''' :CFG), G' v : » S  T a S 1 |T a S 3 |T a S 4 |T a T a » S 1  AS 2 S 2  CT a S 3  AT a S 4  CT a » A  a | c | T c C » B  c | T c C » C  T c C | c » T a  a » T c  c

» Finally, atmostoneEps(G' v :CFG) does not apply in Ex1, G v = G' v since L does not contain  (S is not nullable). » S  T a S 1 |T a S 3 |T a S 4 |T a T a » S 1  AS 2 S 2  CT a S 3  AT a S 4  CT a » A  a | c | T c C » B  c | T c C » C  T c C | c » T a  a » T c  c

» Convert the following CFG to CNF: » S  ABC » A  aC | D » B  bB |  | A » C  Ac |  | Cc » D  aa

» N = {A,B,C} So » add S  AB|BC|AC, A  a, B  b, C  c » delete B  , C   » The result is: » S  ABC| AB|BC|AC » A  aC | D|a » B  bB | A|b » C  Ac | Cc| c » D  aa

» S  ABC| AB|BC|AC » A  aC | D |a (A  D, D  aa) remove A  D add A  aa » B  bB | A|b (B  A, A  aC|aa|a) remove B  A add B  aC|aa|a » C  Ac | Cc|c » D  aa

» S  ABC| AB|BC|AC » A  aC | aa |a » B  bB | aC|aa|a|b » C  Ac | Cc|c » D  aa

» S  ABC| AB|BC|AC » A  T a C | T a T a |a » B  T b B | T a C| T a T a |a|b » C  AT c | CT c |c » D  T a T a

» S  AM 2 |M 2 | AC|AB » M 2  BC » A  T a C | T a T a |a » B  T b B | T a C| T a T a |a |b » C  AT c | CT c |c » D  T a T a » T a  a » T b  b » T c  c

» S  AM 2 |M 2 | AC|AB » M 2  BC » A  T a C | T a T a |a » B  T b B | T a C| T a T a |a |b » C  AT c | CT c |c » D  T a T a » T a  a » T b  b » T c  c

» Convert the following CFG to CNF: » A → BAB | B | ε » B → 00 | ε » 1- remove epsilon: » A → BAB | B | BB | AB | BA » B → 00 » 2- remove units: » A → BAB | 00 | BB | AB | BA » B → 00

» 3- remove mixed » A → BC | T 0 T 0 | BB | AB | BA » C → AB, B → T 0 T 0, T 0 → 0 » 4- Add epsilon » A → BC | T 0 T 0 | BB | AB | BA |  » C → AB » B → T 0 T 0 » T 0 → 0

» Which one is Chomsky Normal Form ? Why?

Not Chomsky Normal Form Chomsky Normal Form

» Convert the following CFG to CNF:

Introduce new variables for the terminals:

Introduce new intermediate variable to break first production:

Introduce intermediate variable:

Final grammar in Chomsky Normal Form: Initial grammar

» Convert the following CFG to CNF: » E  E + T » E  T » T  T  F » T  F » F  (E) » F  id

» Remove E  T, add E  T  F |F » Remove E  F, add E  (E) | id » Remove T  F, add T  (E) | id » The result: » E  E + T | T  F | (E) | id » T  T  F | (E) | id » F  (E) | id

» E  E T + T | T T  F | T ( E T ) | id » T  T T  F | T ( E T ) | id » F  T ( E T ) | id » T (  ( » T )  ) » T +  + » T *  *

» E  E M 2 | T M 3 | T ( M 4 | id » T  T M 3 | T ( M 4 | id » F  T ( M 4 | id » M 2  T + T » M 3  T * F » M 4  E T ) » T (  ( » T )  ) » T +  + » T *  *

» E  E M 2 | T M 3 | T ( M 4 | id » T  T M 3 | T ( M 4 | id » F  T ( M 4 | id » M 2  T + T » M 3  T * F » M 4  E T ) » T (  ( » T )  ) » T +  + » T *  *

» Elaine A. Rich (2008) Automata, Computability, and Complexity: Theory and Applications, Pearson Prentice Hall. » entations.htm entations.htm