ELIMINATING LEFT RECURSIVENESS. Abbreviation. “cfg” stands for “context free grammar” Definition. A cfg is left recursive if it contains a production.

Slides:



Advertisements
Similar presentations
Completeness and Expressiveness
Advertisements

REMOVING LEFT RECURSION AND INDIRECT LEFT RECURSION
4.5 Inherently Ambiguous Context-free Language For some context-free languages, such as arithmetic expressions, may have many different CFG’s to generate.
Chapter 5 Pushdown Automata
1 Pushdown Automata (PDA) Informally: –A PDA is an NFA-ε with a stack. –Transitions are modified to accommodate stack operations. Questions: –What is a.
Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
Closure Properties of CFL's
Grammars, constituency and order A grammar describes the legal strings of a language in terms of constituency and order. For example, a grammar for a fragment.
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.
SIMPLIFYING GRAMMARS Definition: A useless symbol of a context-free grammar is one which does not occur in the derivation of any sentence of that grammar.
SIMPLIFYING GRAMMARS Definition: A useless symbol of a context-free
FORMAL LANGUAGES, AUTOMATA, AND COMPUTABILITY
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.
Introduction to Computability Theory
Lecture 15UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 15.
Foundations of (Theoretical) Computer Science Chapter 2 Lecture Notes (Section 2.1: Context-Free Grammars) David Martin With some.
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
Parsing III (Eliminating left recursion, recursive descent parsing)
1 The Pumping Lemma for Context-Free Languages. 2 Take an infinite context-free language Example: Generates an infinite number of different strings.
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.
Costas Busch - RPI1 The Pumping Lemma for Context-Free Languages.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
Normal forms for Context-Free Grammars
Transparency No. P2C1-1 Formal Language and Automata Theory Part II Pushdown Automata and Context-Free Languages.
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.
Chapter 12: Context-Free Languages and Pushdown Automata
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
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.
Classification of grammars Definition: A grammar G is said to be 1)Right-linear if each production in P is of the form A  xB or A  x where A and B are.
Section 12.4 Context-Free Language Topics
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 11 Midterm Exam 2 -Context-Free Languages Mälardalen University 2005.
Context Free Grammars CFGs –Add recursion to regular expressions Nested constructions –Notation expression  identifier | number | - expression | ( expression.
Context Free Grammars 1. Context Free Languages (CFL) The pumping lemma showed there are languages that are not regular –There are many classes “larger”
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.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data.
Grammars CS 130: Theory of Computation HMU textbook, Chap 5.
Top-down Parsing. 2 Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production.
Pumping Lemma for CFLs. Theorem 7.17: Let G be a CFG in CNF and w a string in L(G). Suppose we have a parse tree for w. If the length of the longest path.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 9 Mälardalen University 2006.
1 Chapter 6 Simplification of CFGs and Normal Forms.
Grammars A grammar is a 4-tuple G = (V, T, P, S) where 1)V is a set of nonterminal symbols (also called variables or syntactic categories) 2)T is a finite.
Context-Free Languages
Classifications LanguageGrammarAutomaton Regular, right- linear Right-linear, left-linear DFA, NFA Context-free PDA Context- sensitive LBA Recursively.
CSC312 Automata Theory Lecture # 26 Chapter # 12 by Cohen Context Free Grammars.
How YACC is constructed. How Yacc works To construct a parsing machine for arithmetic expressions, a special case considered to simplify the account of.
Exercises on Chomsky Normal Form and CYK parsing
About Grammars Hopcroft, Motawi, Ullman, Chap 7.1, 6.3, 5.4.
1 Context-Free Languages & Grammars (CFLs & CFGs) Reading: Chapter 5.
1 Section 4.4 Inductive Proof What do we believe about nonempty subsets of N? Since  N, <  is well-founded, and in fact it is linear, it follows that.
Context-Free Languages & Grammars (CFLs & CFGs) (part 2)
Context-Free Grammars: an overview
NPDAs Accept Context-Free Languages
NPDAs Accept Context-Free Languages
Course 2 Introduction to Formal Languages and Automata Theory (part 2)
Context-Free Languages
Context-Free Grammars
Definition: Let G = (V, T, P, S) be a CFL
CHAPTER 2 Context-Free Languages
Context-Free Grammars 1
Finite Automata and Formal Languages
COSC 3340: Introduction to Theory of Computation
Properties of Context-Free Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 2 Context-Free Language - 01
COSC 3340: Introduction to Theory of Computation
Presentation transcript:

ELIMINATING LEFT RECURSIVENESS

Abbreviation. “cfg” stands for “context free grammar” Definition. A cfg is left recursive if it contains a production of the form A  A α i.e. a production in which the lhs occurs as the head of the rhs.

Theorem 1. Eliminating left recursivenes. For any cfg G we can construct an equivalent cfg G’ such that G’ is not left-recursive

PROOF. For each nonterminal A that occurs as the lhs of a left-recursive production of G, do the following:

Let the left-recursive productions in which A occurs as lhs be A  A  1 ………. A  A  r and the remaining productions in which A occurs as lhs be A   1 …………. A   s

Let K A denote a symbol which does not already occur in the grammar. Replace the above productions by: A   1 K A |... |  s K A K A  ε |  1 K A |... |  r K A Clearly the grammar G' produced is equivalent to G.

EXAMPLE. S  R a | A a | a R  a b A  A  R | A T | b T  T b | a A non-left recursive grammar equiv. to the above is: S  R a | A a | a R  a b K A  ε | R K A | T K A A  b K A T  a K T K T  ε | b K T Clearly the grammar G’ obtained is equivalent to G, and has the required two properties

Definition. A grammar is indirectly left recursive if, for some production with left hand side (say) A, it is possible to derive a string whose head is A. Example. A  Db D  Ecc E  A t

Theorem 2. Eliminating indirect left recursiveness. For any cfg G we can construct an equivalent cfg G’ such that G’ is not indirectly left-recursive

Note. The theorem 1 assertion is a subset of that of theorem 2. Theorem 2 follows directly from the following lemma.

LEMMA. For any cfg G, and any ordering of the nonterminals of G, denoted here as A 1,...,A m, we can construct an equivalent cfg G’ such that: If A i  A j  is any production of G’, then i < j

PROOF. The lemma is true for i  1, since we can eliminate left recursion in productions (if any) in which A 1 occurs as lhs. Assume that the lemma is true for all i  t where t<m, and let the grammar so formed from G be denoted as G t.

Consider any production of G t with A t+1 as LHS and A j as the head of its rhs, where j < t+1, e.g. A t+1  A j . By the inductive assumption, all productions of the form A j   have as their head either a terminal or A k for some k > j.

So if we substitute for A j in A t+1  A j  all the rhs’s with A j as the LHS, then we will get productions of the form: A t+1  a rhs with a terminal as head, or A t+1  a rhs with A k as its head where k > j

By iterating the above process, we will end up with a grammar G t ’ in which all productions with A t+1 as lhs either have a terminal as the head of the rhs or a nonterminal A k for some k  t+1. The lemma follows by induction

Productions of this kind in which k=t+1 are left recursive, and can be eliminated to produce a grammar which we will call G t+1.

EXAMPLE GRAMMARS G 1 S  A A | 0 A  S S | 1 G 2 A 1  A 2 A 3 A 2  A 3 A 1 | b A 3  A 1 A 1 | a