Context free languages 1. Equivalence of context free grammars 2. Normal forms.

Slides:



Advertisements
Similar presentations
4.5 Inherently Ambiguous Context-free Language For some context-free languages, such as arithmetic expressions, may have many different CFG’s to generate.
Advertisements

C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Lecture # 8 Chapter # 4: Syntax Analysis. Practice Context Free Grammars a) CFG generating alternating sequence of 0’s and 1’s b) CFG in which no consecutive.
About Grammars CS 130 Theory of Computation HMU Textbook: Sec 7.1, 6.3, 5.4.
Fall 2004COMP 3351 Simplifications of Context-Free Grammars.
Prof. Busch - LSU1 Simplifications of Context-Free Grammars.
Conversion of a Chomsky normal form grammar to Greibach normal form
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.
Chap 2 Context-Free Languages. Context-free Grammars is not regular Context-free grammar : eg. G 1 : A  0A1substitution rules A  Bproduction rules B.
Lecture Note of 12/22 jinnjy. Outline Chomsky Normal Form and CYK Algorithm Pumping Lemma for Context-Free Languages Closure Properties of CFL.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
104 Closure Properties of Regular Languages Regular languages are closed under many set operations. Let L 1 and L 2 be regular languages. (1) L 1  L 2.
Normal forms for Context-Free Grammars
How to Convert a Context-Free Grammar to Greibach Normal Form
January 15, 2014CS21 Lecture 61 CS21 Decidability and Tractability Lecture 6 January 16, 2015.
Cs466(Prasad)L8Norm1 Normal Forms Chomsky Normal Form Griebach Normal Form.
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.
Chapter 12: Context-Free Languages and Pushdown Automata
Problem of the DAY Create a regular context-free grammar that generates L= {w  {a,b}* : the number of a’s in w is not divisible by 3} Hint: start by designing.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Formal Grammars Denning, Sections 3.3 to 3.6. Formal Grammar, Defined A formal grammar G is a four-tuple G = (N,T,P,  ), where N is a finite nonempty.
Lecture 16 Oct 18 Context-Free Languages (CFL) - basic definitions Examples.
BİL 744 Derleyici Gerçekleştirimi (Compiler Design)1 Syntax Analyzer Syntax Analyzer creates the syntactic structure of the given source program. This.
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.
Context-Free Grammars – Chomsky Normal Form Lecture 16 Section 2.1 Wed, Sep 26, 2007.
CSCI 2670 Introduction to Theory of Computing September 21, 2004.
Context Free Grammars CIS 361. Introduction Finite Automata accept all regular languages and only regular languages Many simple languages are non regular:
Lecture # 19. Example Consider the following CFG ∑ = {a, b} Consider the following CFG ∑ = {a, b} 1. S  aSa | bSb | a | b | Λ The above CFG generates.
CSCI 2670 Introduction to Theory of Computing September 15, 2005.
Grammars CPSC 5135.
Languages & Grammars. Grammars  A set of rules which govern the structure of a language Fritz Fritz The dog The dog ate ate left left.
Lecture # 9 Chap 4: Ambiguous Grammar. 2 Chomsky Hierarchy: Language Classification A grammar G is said to be – Regular if it is right linear where each.
Context Free Grammar. Introduction Why do we want to learn about Context Free Grammars?  Used in many parsers in compilers  Yet another compiler-compiler,
Phrase-structure grammar A phrase-structure grammar is a quadruple G = (V, T, P, S) where V is a finite set of symbols called nonterminals, T is a set.
Section 12.4 Context-Free Language Topics
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
Chapter 6 Simplification of Context-free Grammars and Normal Forms These class notes are based on material from our textbook, An Introduction to Formal.
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.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
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.
Syntax Analyzer (Parser)
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
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?)
Donghyun (David) Kim Department of Mathematics and Physics North Carolina Central University 1 Chapter 2 Context-Free Languages Some slides are in courtesy.
Transparency No. 1 Formal Language and Automata Theory Homework 5.
Exercises on Chomsky Normal Form and CYK parsing
Chomsky Normal Form.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
Compiler Chapter 5. Context-free Grammar Dept. of Computer Engineering, Hansung University, Sung-Dong Kim.
Computability Joke. Context-free grammars Parsing. Chomsky
Context-Free Grammars: an overview
7. Properties of Context-Free Languages
Simplifications of Context-Free Grammars
Simplifications of Context-Free Grammars
Chapter 7 Regular Grammars
7. Properties of Context-Free Languages
Chapter 6 Simplification of Context-free Grammars and Normal Forms
CHAPTER 2 Context-Free Languages
Context-Free Languages
Presentation transcript:

Context free languages 1. Equivalence of context free grammars 2. Normal forms

Context-free grammars In a context free grammar, all productions are of the form A -> w, where A is a nonterminal or the start symbol S, and w is a string from (N  T)*

Handles, recursive productions In the production A -> xw, prefix x, if a single symbol, is called the handle of the production, whether x is in N or T A production A -> Aw is called left-recursive The production A -> wA is called right recursive

Repeated sentential forms In a derivation, the sentential form wAx S-> … -> wAx -> … -> wAx -> … is called a repeated sentential form. All the intervening steps are wasted steps.

Leftmost derivations Minimal leftmost derivations A derivation is a leftmost derivation if at each step only the leftmost nonterminal symbol is replaced using some rule of the grammar. A leftmost derivation is called minimal if no sentential form is repeated in the derivation

Weak equivalence Two context-free grammars G1 and G2 are called weakly equivalent if L(G1) = L(G2)

Example of weak equivalence G1: S -> S01; S -> 1 L(G1) = { 1(01)* } G2: S -> S0S; S -> 1 L(G2) = { 1(01)* }

Strong equivalence Two CFGs G1 and G2 are called strongly equivalent if they are weakly equivalent, and for each string w of terminals in L(G1) = L(G2), and the minimal left-most derivations of w in G1 and the minimal left-most derivations of w in G2 are exactly the same in number, and so can be put into one-to-one correspondence.

Strong equivalence Thus G1 and G2 must both be unambiguous, or must both be ambiguous in exactly the same number of ways, for each string w in T*

Weakly equivalent but not strongly equivalent G1: Grammar of expressions S: S -> T | S + T; T -> F | T * F; F -> a | ( S ); G2: Grammar of expressions S: S -> E; E -> E + E | E * E | (E) | a; L(G1) = L(G2) = valid expressions using a, +, *, (, and ). G1 has operator precedence.

Example: Strong equivalence G1: S->A; A->1B; A->1; B->0A L(G1) = { (10)*1 } G2: S->B; B->A1; B->1; A->B0 L(G2) = { 1(01)* }

Elementary transformations of context free grammars substitution expansion removal of useless productions removal of non-generative productions removal of left recursive productions

Substitution If G has the A-rule, A->uBv, and all the B-rules are: B->w 1, B->w 2,..., B->w k, then 1. Remove the A-rule A->uBv 2. Add the A-rules: A->uw 1 v, A->uw 2 v,..., A->uw k v 3. Keep all the other rules of G, including the B-rules

Example of substitution G1: S->H; H->TT; T->S; T->aSb; T->c G2: S->H; H->ST; H->aSbT; H->cT; T->S; T->aSb; T->c;

Strong equivalence after substitution The grammar G, and the grammar G’ obtained by substitution of B into the A- rule, are strongly equivalent if steps 2 and 3 do not introduce duplicate rules.

Expansion If a grammar has the A-rule, A->uv Remove this A-rule, and replace it with the two rules A->Xv; X->u; or with A->uY; Y->v where X (or Y) is a new non-terminal symbol of the grammar.

Strong equivalence after expansion If G is context free, and G’ is obtained from G by expansion, then G and G’ are strongly equivalent.

Useful production A production A->w of a cfg G is useful if there is a string x from T* such that S->.. -> uAv -> uwv ->.. -> x Otherwise the production, A->w is useless Thus, a production that is never used to derive a string of terminals is useless

Removing useless productions T-marking S-marking Productions that are both T-marked and S-marked are useful. All other productions can be removed.

T-marking Construct a sequence P0, P1, P2,..., of subsets of P, and a sequence N0, N1, N2,... of subsets of N as follows: P0 = empty, N0 = empty, j = 0 P[j+1] = { A->w|w in (N[j] + T)* } N[j+1] = { A in N | P[j+1] contains a rule A->w } Continue until P[j] = P[j+1] = P[T]

S-marking Construct a sequence Q1, Q2, Q3,... of subsets of P[T] as follows: Q1 = {S->w in P[T]} Q[j+1] = Q[j] + {A->w in P[T] | Q[j] contains a rule B->uAv } Continue until Q[j] = Q[j+1] = P[S] P[S] are now the useful productions.

Example: T/S-marking RuleT markS mark 1. S->H21 2. H->AB 3. H->aH22 4. H->a12 5. B->Hb2 6. C->aC Thus only 1,3,4 are useful

Strong equivalence after removal of useless productions If grammar G’ is obtained from grammar G after removal of useless productions of grammar G, then G and G’ are strongly equivalent.

Removing non-generative productions

Removing left-recursive rules Let all the X-rules of grammar G be: X->u 1 | u 2 |... | u k X->Xw 1 | Xw 2 |... | Xw h Then these rules may be replaced by the following: X->u 1 | u 2 |... | u k X->u 1 Z | u 2 Z |... | u k Z Z->w 1 | w 2 |... | w h Z->w 1 Z | w 2 Z |... | w h Z where Z is a new non-terminal symbol

Example: Removing left- recursive rules S->E;S->E; E->T | aT | bT;E->T | aT | bT; E->EaT | EbT;E->TG | aTG | bTG; T->F;G->aT | bT; T->TcF | TdF;G->aTG | bTG; F->n | xEyT->F; T->FH; H->cF | dF; H->cFH | dFH; F->n | xEy

Strong equivalence after removal of left-recursive rules If grammar G’ is obtained from grammar G by replacing the left- recursive rules of G by right recursive rules to get G’, then G and G’ are strongly equivalent.

Well-formed grammars A context free grammar G=(N,T,P,S) is well-formed if each production has one of the forms: S->  S->A A->w where A  N and w  (N+T)* - N and each production is useful.

Example of well-formed grammars Parenthesis grammar S->A; A->AA; A->(A); A->();

Chomsky Normal form A context free grammar G=(N,T,P,S) is in normal form (Chomsky normal form) if each production has one of the forms: S->  S->A A->BC A->a where A,B,C  N and a  T.

Example of Chomsky normal form grammar Parenthesis grammar S->A;S->A; A->AA;A->AA; A->(A);A->BC; B-> (; C->AD; D->); A->();A->BD;

Chomsky Normal Form Theorem From any context free grammar, one can construct a strongly equivalent grammar in Chomsky normal form.

Greibach normal form (standard form) A context free grammar G=(N,T,P,S) is in standard form (Greibach normal form) if each production has one of the forms: S->  S->A A->aw where A  N, a  T, and w  (N+T)*.

Example: converting to Greibach standard form First remove left-recursive rules: S->E;S->E; E->T;E->T; E->EaT;E->TF; T->n;F->aT; T->xEy;F->aTF; T->n; T->xEy;

Converting to Greibach: then substitute to get nonterminal handles S->E;S->E; E->T;E->n | xEy; E->TF;E->nF | xEyF; F->aT;F->aT; F->aTF;F->aTF; T->n;T->n; T->xEy;T->xEy;

Standard Form Theorem From any context free grammar, one can construct a strongly equivalent grammar in standard form (Greibach normal form).

Pumping Lemma for context free languages If L is a context free language, then there exists a positive integer p such that: if w  L and |w| > p, then w = xuyvz, with uv and y nonempty and xu k yv k z  L for all k  0.