REMOVING LEFT RECURSION AND INDIRECT LEFT RECURSION

Slides:



Advertisements
Similar presentations
How to convert a left linear grammar to a right linear grammar
Advertisements

Rules of Matrix Arithmetic
Context free languages 1. Equivalence of context free grammars 2. Normal forms.
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
Exercise 1: Balanced Parentheses Show that the following balanced parentheses grammar is ambiguous (by finding two parse trees for some input sequence)
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.
May 2006CLINT-LN Parsing1 Computational Linguistics Introduction Approaches to Parsing.
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
Lecture # 11 Grammar Problems.
Mathematics as a Second Language Mathematics as a Second Language Mathematics as a Second Language © 2007 Herbert I. Gross An Innovative Way to Better.
Bhaskar Bagchi (11CS10058) Lecture Slides( 9 th Sept. 2013)
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
Courtesy Costas Busch - RPI1 NPDAs Accept Context-Free Languages.
A basis for computer theory and A means of specifying languages
Parsing III (Eliminating left recursion, recursive descent parsing)
Slide 1 Chapter 2-b Syntax, Semantics. Slide 2 Syntax, Semantics - Definition The syntax of a programming language is the form of its expressions, statements.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
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.
Chapter 4 Context-Free Languages Copyright © 2011 The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 1.
Parsing IV Bottom-up Parsing Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
1 Homework #7 (Models of Computation, Spring, 2001) Due: Section 1; April 16 (Monday) Section 2; April 17 (Tuesday) 2. Covert the following context-free.
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.
Discrete Structure Li Tak Sing( 李德成 ) Lectures
ELIMINATING LEFT RECURSIVENESS. Abbreviation. “cfg” stands for “context free grammar” Definition. A cfg is left recursive if it contains a production.
Section 12.4 Context-Free Language Topics
Languages and Grammars. A language is a set of strings. Example: The set of all valid C++ programs is a language. Each program consists of a string of.
Parsing Top-Down.
1 Simplification of Context-Free Grammars Some useful substitution rules. Removing useless productions. Removing -productions. Removing unit-productions.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Top-down Parsing lecture slides from C OMP 412 Rice University Houston, Texas, Fall 2001.
Recursive Data Structures and Grammars Themes –Recursive Description of Data Structures –Grammars and Parsing –Recursive Definitions of Properties of Data.
Grammars Hopcroft, Motawi, Ullman, Chap 5. Grammars Describes underlying rules (syntax) of programming languages Compilers (parsers) are based on such.
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.
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
Parsing and Code Generation Set 24. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program,
GRAMMARS & PARSING. Parser Construction Most of the work involved in constructing a parser is carried out automatically by a program, referred to as a.
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.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Parsing — Part II (Top-down parsing, left-recursion removal)
CS510 Compiler Lecture 4.
Chomsky Normal Form CYK Algorithm
Parsing — Part II (Top-down parsing, left-recursion removal)
Copyright © Cengage Learning. All rights reserved.
Even-Even Devise a grammar that generates strings with even number of a’s and even number of b’s.
Simplifications of Context-Free Grammars
Simplifications of Context-Free Grammars
NPDAs Accept Context-Free Languages
Course 2 Introduction to Formal Languages and Automata Theory (part 2)
Context-Free Languages
CHAPTER 2 Context-Free Languages
Parsing — Part II (Top-down parsing, left-recursion removal)
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
LEFT RECURSION.
Regular Grammars.
Context-Free Languages
Presentation transcript:

REMOVING LEFT RECURSION AND INDIRECT LEFT RECURSION

DEFINITIONS IMMEDIATE LEFT RECURSION. A production is immediately left recursive if its left hand side and the head of its right hand side are the same symbol, e.g. B → Bvt A grammar is called immediately left recursive if it possesses an immediately left recursive production.

INDIRECT LEFT RECURSION INDIRECT LEFT RECURSION. A grammar is said to posess indirect left recursion if it is possible, starting from any symbol of the grammar, to derive a string whose head is that symbol. Example. A → Br B → Cs C → At Here, starting with A, we can derive Atsr

NOTE. Immediate left recursion is a special case of indirect left recursion (in which the derivation involved is just a single step) “Immediate left recursion” is conventionally referred to simply as “left recursion” (leaving out the word “immediate”). But, for our purposes, this is confusing, so we will not follow that practice.

Let G be any grammar in which the only production, if any, which contains a symbol that vanishes is S →ε, where S is the goal symbol, and which contains no useless or unit productions. To remove left recursion from G (i.e. produce an equivalant grammar with the same language as G, but which is not left recursive), do the following: 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® Aa1                      ……….                     A® Aar and the remaining productions in which A occurs as lhs be                     A® b1                      ………….                     A® bs

Let KA denote a symbol which does not already occur in the grammar. Replace the above productions by:                     A ® b1 | . . . | bs | b1KA | . . . | bsKA                     KA ® a1 | ... | ar | a1KA | ... | arKA 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: A ® b | b KA KA ® R | T | R KA | T KA T ® a | a KT KT ® b | b KT Clearly the grammar G’ obtained is equivalent to G, and has the required two properties

REMOVING INDIRECT LEFT RECURSION Let any ordering of the nonterminals of G be A1,...,Am, we will remove indirect left recursion by constructing an equivalant grammar G’ such that If  Ai ® Aja  is any production of G’, then i < j

We can eliminate left recursion in productions (if any) in which A1 occurs as lhs.  Assume that this is true for all i £ t where t<m, and let the grammar so formed from G be denoted as Gt.

Consider any production of Gt with At+1 as LHS and Aj as the head of its rhs, where j < t+1, e.g. At+1 ® Ajb.  By our assumption, all productions of the form Aj ® a  have as their head either a terminal or Ak for some k > j. 

So if we substitute for Aj in  At+1 ® Ajb  all the rhs’s with Aj as the LHS, then we will get productions of the form:        At+1  ®  a rhs with a terminal as head, or   At+1  ®  a rhs with Ak as its head  where k > j

By iterating the above process, we will end up with a grammar Gt’ in which all productions with At+1 as lhs either have a terminal as the head of the rhs or a nonterminal Ak for some k ³ t+1. 

Productions of this kind in which k=t+1 are left recursive, and can be eliminated to produce a grammar which we will call Gt+1.  Proceeding in this way for increasing values of t, we will obtain a grammar G’ equivalent to G, in which if  Ai ® Aja  is any production of G’, then i < j

G’ will in addition contain some productions with subscripted K’s as left hand side. These do not introduce any direct or indirect left recursion, since the corresponding right hand sides (such as e.g. b KA ) start with symbols that do not vanish, and no symbol derives a string starting with a subsripted K

EXAMPLE 1 A1 ® A2 A3 A2 ® A3 A1 | b A3 ® A1 A1 | a Replace A3 ® A1 A1 by A3 ® A2 A3 A1 and then replace this by A3 ® A3 A1 A3 A1 and A3 ® b A3 A1 Eliminating direct left recursion in the above, gives: A3 ® a | b A3 A1 | aK | b A3 A1K k ® A1 A3 A1 | A1 A3 A1K

The resulting grammar is then: A1 ® A2 A3 A2 ® A3 A1 | b A3 ® a | b A3 A1 | aK | b A3 A1K k ® A1 A3 A1 | A1 A3 A1K

EXAMPLE 2 S ® A A | 0 A ® S S | 1 Answer Considering the ordering S, A, we get: A ® AAS | 0S | 1 and removing immediate left recursion, we get A ® 0S | 1 | 0SKA | 1KA KA ® AS | ASKA