Left Recursion Lecture 7 Fri, Feb 4, 2005.

Slides:



Advertisements
Similar presentations
Parsing 4 Dr William Harrison Fall 2008
Advertisements

Translator Architecture Code Generator ParserTokenizer string of characters (source code) string of tokens abstract program string of integers (object.
1 Parsing The scanner recognizes words The parser recognizes syntactic units Parser operations: Check and verify syntax based on specified syntax rules.
Lecture # 11 Grammar Problems.
Top-Down Parsing.
Prof. Bodik CS 164 Lecture 81 Grammars and ambiguity CS164 3:30-5:00 TT 10 Evans.
Parsing III (Eliminating left recursion, recursive descent parsing)
ISBN Chapter 4 Lexical and Syntax Analysis The Parsing Problem Recursive-Descent Parsing.
Prof. Bodik CS 164 Lecture 61 Building a Parser II CS164 3:30-5:00 TT 10 Evans.
1 Predictive parsing Recall the main idea of top-down parsing: Start at the root, grow towards leaves Pick a production and try to match input May need.
1 Foundations of Software Design Lecture 23: Finite Automata and Context-Free Grammars Marti Hearst Fall 2002.
Parsing — Part II (Ambiguity, Top-down parsing, Left-recursion Removal)
1 The Parser Its job: –Check and verify syntax based on specified syntax rules –Report errors –Build IR Good news –the process can be automated.
CS 536 Spring Ambiguity Lecture 8. CS 536 Spring Announcement Reading Assignment –“Context-Free Grammars” (Sections 4.1, 4.2) Programming.
Parsing II : Top-down Parsing Lecture 7 CS 4318/5331 Apan Qasem Texas State University Spring 2015 *some slides adopted from Cooper and Torczon.
Bottom-up parsing Goal of parser : build a derivation
(2.1) Grammars  Definitions  Grammars  Backus-Naur Form  Derivation – terminology – trees  Grammars and ambiguity  Simple example  Grammar hierarchies.
Compiler Principles Winter Compiler Principles Exercises on scanning & top-down parsing Roman Manevich Ben-Gurion University.
Compiler Construction 1. Objectives Given a context-free grammar, G, and the grammar- independent functions for a recursive-descent parser, complete the.
1 Introduction to Parsing Lecture 5. 2 Outline Regular languages revisited Parser overview Context-free grammars (CFG’s) Derivations.
Syntax and Semantics Structure of programming languages.
Review: –How do we define a grammar (what are the components in a grammar)? –What is a context free grammar? –What is the language defined by a grammar?
Top-Down Parsing - recursive descent - predictive parsing
4 4 (c) parsing. Parsing A grammar describes the strings of tokens that are syntactically legal in a PL A recogniser simply accepts or rejects strings.
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
10/13/2015IT 3271 Tow kinds of predictive parsers: Bottom-Up: The syntax tree is built up from the leaves Example: LR(1) parser Top-Down The syntax tree.
CS 461 – Oct. 7 Applications of CFLs: Compiling Scanning vs. parsing Expression grammars –Associativity –Precedence Programming language (handout)
Lesson 3 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
Parsing Lecture 5 Fri, Jan 28, Syntax Analysis The syntax of a language is described by a context-free grammar. Each grammar rule has the form A.
4 4 (c) parsing. Parsing A grammar describes syntactically legal strings in a language A recogniser simply accepts or rejects strings A generator produces.
Top Down Parsing - Part I Comp 412 Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University.
1 COMP313A Programming Languages Syntax Analysis (2)
CPS 506 Comparative Programming Languages Syntax Specification.
COP4020 Programming Languages Parsing Prof. Xin Yuan.
Comp 311 Principles of Programming Languages Lecture 3 Parsing Corky Cartwright August 28, 2009.
Recursive Descent Parsers Lecture 6 Mon, Feb 2, 2004.
Muhammad Idrees, Lecturer University of Lahore 1 Top-Down Parsing Top down parsing can be viewed as an attempt to find a leftmost derivation for an input.
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.
Parsing — Part II (Top-down parsing, left-recursion removal) Copyright 2003, Keith D. Cooper, Ken Kennedy & Linda Torczon, all rights reserved. Students.
Chapter 3 Context-Free Grammars and Parsing. The Parsing Process sequence of tokens syntax tree parser Duties of parser: Determine correct syntax Build.
Top-Down Parsing CS 671 January 29, CS 671 – Spring Where Are We? Source code: if (b==0) a = “Hi”; Token Stream: if (b == 0) a = “Hi”; Abstract.
Transforming Grammars. CF Grammar Terms Parse trees. – Graphical representations of derivations. – The leaves of a parse tree for a fully filled out tree.
Syntax Analysis - Parsing Compiler Design Lecture (01/28/98) Computer Science Rensselaer Polytechnic.
1 Context free grammars  Terminals  Nonterminals  Start symbol  productions E --> E + T E --> E – T E --> T T --> T * F T --> T / F T --> F F --> (F)
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 Compiler Construction (CS-636) Muhammad Bilal Bashir UIIT, Rawalpindi.
Syntax Analyzer (Parser)
CSE 5317/4305 L3: Parsing #11 Parsing #1 Leonidas Fegaras.
1 Introduction to Parsing. 2 Outline l Regular languages revisited l Parser overview Context-free grammars (CFG ’ s) l Derivations.
Lesson 4 CDT301 – Compiler Theory, Spring 2011 Teacher: Linus Källberg.
COMP 3438 – Part II-Lecture 5 Syntax Analysis II Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
1 Topic #4: Syntactic Analysis (Parsing) CSC 338 – Compiler Design and implementation Dr. Mohamed Ben Othman ( )
1 CMPSC 160 Translation of Programming Languages Fall 2002 slides derived from Tevfik Bultan, Keith Cooper, and Linda Torczon Lecture-Module #6 Parsing.
Compiler Syntactic Analysis r Two general classes of parsing techniques m Bottom-up (Operator-Precedence parsing) Begin with the terminal nodes.
Lecture # 10 Grammar Problems. Problems with grammar Ambiguity Left Recursion Left Factoring Removal of Useless Symbols These can create problems for.
CMSC 330: Organization of Programming Languages Pushdown Automata Parsing.
Syntax Analysis By Noor Dhia Syntax analysis:- Syntax analysis or parsing is the most important phase of a compiler. The syntax analyzer considers.
Parsing #1 Leonidas Fegaras.
Parsing — Part II (Top-down parsing, left-recursion removal)
Programming Languages Translator
Lecture #12 Parsing Types.
Parsing — Part II (Top-down parsing, left-recursion removal)
FIRST and FOLLOW Lecture 8 Mon, Feb 7, 2005.
Parsing Techniques.
Syntax-Directed Definition
LR Parsing – The Tables Lecture 11 Wed, Feb 16, 2005.
ENERGY 211 / CME 211 Lecture 15 October 22, 2008.
Predictive Parsing Lecture 9 Wed, Feb 9, 2005.
Parsing — Part II (Top-down parsing, left-recursion removal)
Presentation transcript:

Left Recursion Lecture 7 Fri, Feb 4, 2005

A Problem with Recursive Descent Parsers Suppose the grammar were S  A B | C D A  B A | C A B  C A | A D C  B A | A D | a D  A C | B D | b How could a top-down parser decide which production for S to use?

Another Problem with Recursive Descent Parsers Suppose the grammar were S  S S | a How could the parser decide how many times to use the production S  S S before using the production S  a?

Futile Attempt #1 void S() { if (token == a) match(a); else S(); }

Futile Attempt #2 void S() { if (token != EOF) S(); }

Left Recursion The method of recursive descent does not work if the grammar is left recursive. A grammar is left recursive if there is a derivation A + A for some nonterminal A and string . In particular, a production is left recursive if it is of the form A  A.

Left Recursion Applying the method of recursive descent would lead to the function void A() { A(); // Process  } which leads to infinite recursion.

Left Recursion Recall that in the earlier example, we added the production S'  S S' | , not the production S'  S' S |  Why? Are they equivalent as far as the language of the grammar is concerned?

Eliminating Left Recursion Left recursion in a production may be removed by transforming the grammar in the following way. Replace A  A |  with A  A' A'  A' | .

Eliminating Left Recursion Under the original productions, a derivation of  is A  A  A  A  . Under the new productions, a derivation of  is A  A'  A'  A'  A'  .

Example: Eliminating Left Recursion Consider the left recursive grammar E  E + T | T T  T * F | F F  (E) | id Apply the transformation to E: E  T E' E'  + T E' | .

Example: Eliminating Left Recursion Then apply the transformation to T: T  F T' T'  * F T' | .

Example: Eliminating Left Recursion Now the grammar is E  T E' E'  + T E' | . T  F T' T'  * F T' | . F  (E) | id

Example: Eliminating Left Recursion The function for E' would be void Eprime() { if (token == PLUS) match(PLUS); T(); Eprime(); } return;

Advantages of Left Recursion A left recursive grammar is often more intuitive than the transformed grammar. A left recursive grammar will match expressions earlier, leading to shallow recursion. Consider parsing a + b + c + d + e. Bottom-up parsing takes advantage of the benefits of left recursion.

Example Consider the simple grammar E  E + E | num Convert it to E  num E' E'  + E E' |  Example: SimpleParser

Exercise The grammar R  R  R | RR | R* | (R) | a | b generates all regular expressions on the alphabet {a, b}. Rewrite the grammar to reflect the precedence rules. Eliminate left recursion.