Parsing II Top-down parsing. Comp 412

Similar documents
Parsing II Top-down parsing. Comp 412

Parsing. Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice.

Syntax Analysis, III Comp 412

Syntax Analysis, III Comp 412

Introduction to Parsing. Comp 412

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

CS 406/534 Compiler Construction Parsing Part I

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

CSCI312 Principles of Programming Languages

Introduction to Parsing

Syntax Analysis, V Bottom-up Parsing & The Magic of Handles Comp 412

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Parsing Part II (Top-down parsing, left-recursion removal)

EECS 6083 Intro to Parsing Context Free Grammars

Syntactic Analysis. Top-Down Parsing

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Syntax Analysis, VI Examples from LR Parsing. Comp 412

3. Parsing. Oscar Nierstrasz

Computer Science 160 Translation of Programming Languages

Syntax Analysis, VII One more LR(1) example, plus some more stuff. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. target code.

Syntactic Analysis. Top-Down Parsing. Parsing Techniques. Top-Down Parsing. Remember the Expression Grammar? Example. Example

Compilers. Yannis Smaragdakis, U. Athens (original slides by Sam

Types of parsing. CMSC 430 Lecture 4, Page 1

CS 314 Principles of Programming Languages

Parsing III. (Top-down parsing: recursive descent & LL(1) )

Building a Parser Part III

Part 5 Program Analysis Principles and Techniques

Syntax. In Text: Chapter 3

Generating Code for Assignment Statements back to work. Comp 412 COMP 412 FALL Chapters 4, 6 & 7 in EaC2e. source code. IR IR target.

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Prelude COMP 181 Tufts University Computer Science Last time Grammar issues Key structure meaning Tufts University Computer Science

CSE 3302 Programming Languages Lecture 2: Syntax

Syntax Analysis Check syntax and construct abstract syntax tree

Front End. Hwansoo Han

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Spring UW CSE P 501 Spring 2018 C-1

Parsing #1. Leonidas Fegaras. CSE 5317/4305 L3: Parsing #1 1

Homework & Announcements

Chapter 3. Parsing #1

Derivations vs Parses. Example. Parse Tree. Ambiguity. Different Parse Trees. Context Free Grammars 9/18/2012

CMPS Programming Languages. Dr. Chengwei Lei CEECS California State University, Bakersfield

Principles of Programming Languages COMP251: Syntax and Grammars

CS415 Compilers. Lexical Analysis

CA Compiler Construction

Chapter 4: LR Parsing

Building a Parser II. CS164 3:30-5:00 TT 10 Evans. Prof. Bodik CS 164 Lecture 6 1

CSE P 501 Compilers. Parsing & Context-Free Grammars Hal Perkins Winter /15/ Hal Perkins & UW CSE C-1

Defining syntax using CFGs

Bottom-up Parser. Jungsik Choi

Compilers - Chapter 2: An introduction to syntax analysis (and a complete toy compiler)

COMP 181. Prelude. Next step. Parsing. Study of parsing. Specifying syntax with a grammar

Computing Inside The Parser Syntax-Directed Translation. Comp 412 COMP 412 FALL Chapter 4 in EaC2e. source code. IR IR target.

More on Syntax. Agenda for the Day. Administrative Stuff. More on Syntax In-Class Exercise Using parse trees

Plan for Today. Regular Expressions: repetition and choice. Syntax and Semantics. Context Free Grammars

Lexical Analysis. Introduction

Handling Assignment Comp 412

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

Chapter 3. Describing Syntax and Semantics ISBN

Syntax/semantics. Program <> program execution Compiler/interpreter Syntax Grammars Syntax diagrams Automata/State Machines Scanning/Parsing

Lexical Analysis - An Introduction. Lecture 4 Spring 2005 Department of Computer Science University of Alabama Joel Jones

COP 3402 Systems Software Syntax Analysis (Parser)

Arrays and Functions

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

Syntax Analysis. COMP 524: Programming Language Concepts Björn B. Brandenburg. The University of North Carolina at Chapel Hill

Parsing. source code. while (k<=n) {sum = sum+k; k=k+1;}

CMSC 330: Organization of Programming Languages

Where We Are. CMSC 330: Organization of Programming Languages. This Lecture. Programming Languages. Motivation for Grammars

Context-Free Grammar. Concepts Introduced in Chapter 2. Parse Trees. Example Grammar and Derivation

Programming Language Syntax and Analysis

Habanero Extreme Scale Software Research Project

Compiler Design Concepts. Syntax Analysis

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Comp 411 Principles of Programming Languages Lecture 3 Parsing. Corky Cartwright January 11, 2019

EDAN65: Compilers, Lecture 04 Grammar transformations: Eliminating ambiguities, adapting to LL parsing. Görel Hedin Revised:

Syntax Analysis. The Big Picture. The Big Picture. COMP 524: Programming Languages Srinivas Krishnan January 25, 2011

Intermediate Representations

Downloaded from Page 1. LR Parsing

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

Intermediate Representations

Top down vs. bottom up parsing

Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38

Syntax Analysis Part I

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

3.5 Practical Issues PRACTICAL ISSUES Error Recovery

MIT Top-Down Parsing. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

MIT Specifying Languages with Regular Expressions and Context-Free Grammars. Martin Rinard Massachusetts Institute of Technology

CS 406/534 Compiler Construction Parsing Part II LL(1) and LR(1) Parsing

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Academic Formalities. CS Modern Compilers: Theory and Practise. Images of the day. What, When and Why of Compilers

Outline. Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

CSE450 Translation of Programming Languages. Lecture 4: Syntax Analysis

Context-free grammars

CMSC 330: Organization of Programming Languages. Architecture of Compilers, Interpreters

Principles of Programming Languages COMP251: Syntax and Grammars

CMSC 330: Organization of Programming Languages

Context-free grammars (CFG s)

CMSC 330: Organization of Programming Languages

Administrativia. PA2 assigned today. WA1 assigned today. Building a Parser II. CS164 3:30-5:00 TT 10 Evans. First midterm. Grammars.

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

Grammars. CS434 Lecture 15 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Transcription:

COMP 412 FALL 2018 Parsing II Top-down parsing Comp 412 source code IR Front End Optimizer Back End IR target code Copyright 2018, Keith D. Cooper & Linda Torczon, all rights reserved. Students enrolled in Comp 412 at Rice University have explicit permission to make copies of these materials for their personal use. Faculty from other educational institutions may use these materials for nonprofit educational purposes, provided this copyright notice is preserved. Chapter 3 in EaC2e

Ambiguity Review Definitions A context-free grammar G is ambiguous if there exists has more than one leftmost derivation for some sentence in L(G) A context-free grammar G is ambiguous if there exists has more than one rightmost derivation for some sentence in L(G) The leftmost and rightmost derivations for a sentential form may differ, even in an unambiguous grammar However, they must have the same parse tree 1

Ambiguity Review We talked about syntactic ambiguity Syntactic ambiguity in the context-free grammar Classic example is the if-then-else grammar 0 Stmt if Expr then Stmt 1 if Expr then Stmt else Stmt 2 other statements Fix ambiguity in context-free grammar by re-writing the grammar Semantic ambiguity in the underlying language When one set of characters has two possible meanings a = f(17,21) Fix ambiguity in meaning by changing the language (e.g., C s [ ] ), or Manage ambiguity by accepting language and deferring disambiguation until the compiler has enough context (e.g., type information) 2

Ambiguity Semantic ambiguity in the underlying language One syntax with two meanings Classic example arose in Algol-like languages a = f(17,21) Is it a call to a function f? or a reference to an element of an array f? Disambiguating this kind of confusion requires context Need the value of the declaration for f The ambiguity is an issue of type, not syntax Requires either: 1. An extra-grammatical solution Manage the ambiguity by accepting the imprecise language and deferring disambiguation until the compiler has enough context (e.g., type information) 2. A different syntax Fix ambiguity in meaning by changing the language (e.g., C s [ ] or BCPL s! ) 4

Order of Operations or Precedence Consider again the derivation of x 2 * y 0 Expr Expr Op Value 1 Value 2 Value number 3 identifier 4 Op plus 5 minus 6 times 7 I divide Rule Sentential Form Expr 0 Expr Op Value 0 Expr Op Value Op Value 1 Value Op Value Op Value 3 <id,x> Op Value Op Value 5 <id,x> Value Op Value 2 <id,x> <num,2> Op Value 6 <id,x> <num,2> * Value 3 <id,x> <num,2> * <id,y> Leftmost Derivation Not a good expression grammar 5

Order of Operations The leftmost derivation is unique, but it specifies the wrong precedence Rule Sentential Form Expr 0 Expr Op Value 0 Expr Op Value Op Value 1 Value Op Value Op Value 3 <id,x> Op Value Op Value 5 <id,x> Value Op Value 2 <id,x> <num,2> Op Value 6 <id,x> <num,2> * Value 3 <id,x> <num,2> * <id,y> Evaluates (x - 2) * y Expr Expr Op Expr Op Value Value <id,x> times minus <num,2> Value <id,y> Eliminating ambiguity does not necessarily produce the desired meaning. It COMP produces 412, a consistent Fall 2018 meaning, but that meaning can be consistently wrong. 6

Order of Operations How do you add precedence to a grammar? To add precedence Decide how many levels of precedence the grammar needs Create a nonterminal for each level of precedence Isolate the corresponding part of the grammar Force the parser to recognize high precedence subexpressions first For algebraic expressions, including (), +,, *, and / Parentheses first (level 1 ) Multiplication and division, next (level 2) Subtraction and addition, last (level 3) 7

Derivations and Precedence Adding the standard algebraic precedence produces: level 3 level 2 level 1 0 Goal Expr 1 Expr Expr + Term 2 Expr Term 3 Term 4 Term Term * Factor 5 Term / Factor 6 Factor 7 Factor ( Expr ) 8 number 9 id The Classic Expression Grammar The new grammar is larger (7 vs. 9) Takes more rewriting to reach some of the terminal symbols Encodes expected precedence Produces same parse tree under leftmost & rightmost derivations Correctness trumps the speed of the parser Let s see how it derives x 2 * y Both parentheses & precedence are beyond the power of an RE This COMP is 412, the Fall good 2018 expression grammar. Figure 7.7 on p. 351 of EaC2e shows this grammar extended to include Boolean & relational operators. 8

Derivations and Precedence Rule Sentential Form Goal 0 Expr 2 Expr Term 3 Term Term 6 Factor Term 9 <id,x> Term 4 <id,x> Term * Factor 6 <id,x> Factor * Factor 8 <id,x> <num,2> * Factor 9 <id,x> <num,2> * <id,y> The leftmost derivation E G E T T T * F F <id,x> F <num,2> <id,y> Parse tree for x 2 * y The classic expression grammar derives x ( 2 * y ) with the parse tree shown.. Both the leftmost and rightmost derivations give the same parse tree and value, because the grammar directly and explicitly encodes the desired precedence. 9

Derivations and Precedence Rule Sentential Form Goal 0 Expr 2 Expr Term 4 Expr Term * Factor 9 Expr Term * <id,y> 6 Expr Factor * <id,y> 8 Expr <num,2> * <id,y> 3 Term <num,2> * <id,y> 6 Factor <num,2> * <id,y> 9 <id,x> <num,2> * <id,y> The rightmost derivation The classic expression grammar derives x ( 2 * y ) with the parse tree shown.. Both the leftmost and rightmost derivations give the same parse tree and value, because the grammar directly and explicitly encodes the desired precedence. E T F <id,x> G E T F <num,2> Parse tree for x 2 * y T * F <id,y> 10

Derivations and Associativity The classic expression grammar is also left associative Rule Sentential Form Goal 0 Expr 1 Expr + Term 1 Expr + Term + Term 3 Term + Term + Term 6 Factor + Term + Term 9 id + Term + Term 6 id + Factor + Term 9 id + id + Term 6 id + id + Factor 9 id + id + id Leftmost derivation of x + y + z E G E + T E + T F T F <id,x> F <id,y> <id,z> Parse tree evaluates as (x + y) + z COMP Corresponds 412, Fall to 2018 left-to-right evaluation. We will want to preserve this property. 11

Parsing Techniques Top-down parsers (LL(1), recursive descent) Start at the root of the parse tree and grow toward leaves Pick a production & try to match the input Bad pick Þ may need to backtrack Large class of grammars are backtrack-free E G E - T Bottom-up parsers (LR(1), operator precedence) Start at the leaves and grow toward root As input is consumed, encode possibilities in an internal state Start in a state valid for legal first tokens We can make the process deterministic T T * F F F <id,y> <id,x> <num,2> Parse tree for x - 2 * y COMP Bottom-up 412, Fall parsers 2018 can recognize a larger class of grammars than can top-down parsers. 12

Top-Down Parsing We will examine two ways of implementing top-down parsers Scanner <word, category> pairs Recursive Descent Parser IR Recursive-Descent Parser Highly efficient, highly flexible form of parser Typically implemented as a hand-coded parser Set of mutually-recursive routines Works well for any backtrack free or predictively parsable grammar Easy to understand, easy to implement 13

Top-Down Parsing We will examine two ways of implementing top-down parsers Scanner <word, category> pairs Skeleton LL Parser Table IR Knowledge encoded in tables to drive skeleton specifications (as a CFG) LL(1) Parser Generator Table-driven LL(1) Parser LL(1) Parser Generator takes as input a CFG that is backtrack free Skeleton Parser interprets the table produced by the generator In Lab 2, you will implement an LL(1) table generator Your table generator will use a recursive-descent parser as its front end 14

Back to the Meta Question Two kinds of top-down parsers Scanner Tokens <word, category> Recursive Descent Parser IR Design Time Write scanner & parser Build time Compile scanner & parser Compile time Run scanner & parser Scanner Tokens <word, category> Skeleton LL Parser Table IR Design Time Write scanner & CFG Debug CFG CFG for input language LL(1) Parser Generator Knowledge encoded in tables to drive skeleton Build time Invoke parser generator Compile scanner & parser Compile time Run scanner & parser 15

Top-down Parsing The Algorithm A top-down parser starts with the root of the parse tree The root node is labeled with the goal symbol of the grammar Construct the root node of the parse tree Repeat until lower fringe of the parse tree matches the input string 1. At a node labeled with NT A, select a production with A on its LHS and, for each symbol on its RHS, construct the appropriate child 2. When a terminal symbol is added to the fringe and it doesn t match the fringe, backtrack 3. Find the next node to be expanded (label Î NT) The key is picking the right production in step 1 That choice should be guided by the input string 16

The Classic Expression Grammar Consider the Classic Expression Grammar 0 Goal Expr 1 Expr Expr + Term 2 Expr - Term 3 Term 4 Term Term * Factor 5 Term / Factor 6 Factor 7 Factor ( Expr ) 8 number 9 id and the input x 2 * y Classic Expression Grammar 17

Example Let s try to derive x 2 * y : I am going to make choices for pedagogical reasons, rather than for consistency Goal Rule Sentential Form Input Goal x - 2 * y Lower fringe of the partially completed parse tree is the position in the input buffer Build a leftmost derivation, to work with a left-to-right scanner. 0 Goal Expr 1 Expr Expr + Term 2 Expr Term 3 Term 4 Term Term * Factor 5 Term / Factor 6 Factor 7 Factor ( Expr ) 8 number 9 18 identifier

Example Let s try to derive x 2 * y : Goal Rule Sentential Form Input Goal x - 2 * y 0 Expr x - 2 * y 1 Expr + Term x - 2 * y 3 Term + Term x - 2 * y 6 Factor + Term x - 2 * y 9 <id,x> + Term x - 2 * y <id,x> + Term x - 2 * y is the position in the input buffer Expr Expr + Term Fact. <id,x> This worked well, except that doesn t match + The parser must backtrack to here Term 0 Goal Expr 1 Expr Expr + Term 2 Expr Term 3 Term 4 Term Term * Factor 5 Term / Factor 6 Factor 7 Factor ( Expr ) 8 number 9 19 identifier

Example Continuing with x 2 * y : Goal Rule Sentential Form Input Goal x - 2 * y 0 Expr x - 2 * y 2 Expr -Term x - 2 * y 3 Term -Term x - 2 * y 6 Factor -Term x - 2 * y 9 <id,x> - Term x - 2 * y <id,x> -Term x - 2 * y <id,x> -Term x - 2 * y Expr Expr Term Term Fact. <id,x> Þ Now, we need to expand Term - the last NT on the fringe 0 Goal Expr 1 Expr Expr + Term 2 Expr Term 3 Term 4 Term Term * Factor Now, - and - match Now we can expand Term to match 2 5 Term / Factor 6 Factor 7 Factor ( Expr ) 8 number 9 20 identifier

Example Trying to match the 2 in x 2 * y : Rule Sentential Form Input <id,x> - Term x - 2 * y 6 <id,x> - Factor x - 2 * y 8 <id,x> - <num,2> x - 2 * y <id,x> - <num,2> x - 2 * y Where are we? 2 matches 2 Expr Term Fact. <id,x> We have more input, but no NTs left to expand The expansion terminated too soon Þ Need to backtrack Goal Expr Term Fact. <num,2> 0 Goal Expr 1 Expr Expr + Term 2 Expr Term 3 Term 4 Term Term * Factor 5 Term / Factor 6 Factor 7 Factor ( Expr ) 8 number 9 21 identifier

Example Trying again with 2 in x 2 * y : Rule Sentential Form Input <id,x> - Term x - 2 * y Goal Expr 4 <id,x> - Term * Factor x - 2 * y 6 <id,x> - Factor * Factor x - 2 * y Expr Term 8 <id,x> - <num,2> * Factor x - 2 * y Term Term * Fact. <id,x> - <num,2> * Factor x - 2 * y <id,x> - <num,2> * Factor x - 2 * y 9 <id,x> - <num,2> * <id,y> x - 2 * y <id,x> - <num,2> * <id,y> x - 2 * y Fact. <id,x> Fact. <num,2> <id,y> 0 Goal Expr 1 Expr Expr + Term 2 Expr Term 3 Term This time, we matched & consumed all the input ÞSuccess! 4 Term Term * Factor 5 Term / Factor 6 Factor 7 Factor ( Expr ) 8 number 9 22 identifier

Example Trying again with 2 in x 2 * y : Rule Sentential Form Input <id,x> - Term x - 2 * y 4 <id,x> - Term * Factor x - 2 * y 6 <id,x> - Factor * Factor x - 2 * y 8 <id,x> - <num,2> * Factor x - 2 * y <id,x> - <num,2> * Factor x - 2 * y <id,x> - <num,2> * Factor x - 2 * y 9 <id,x> - <num,2> * <id,y> x - 2 * y <id,x> - <num,2> * <id,y> x - 2 * y The Point: Goal Expr Expr Term Term Term Fact. Fact. <id,x> <num,2> * Fact. <id,y> 0 Goal Expr 1 Expr Expr + Term 2 Expr Term 3 Term 4 Term Term * Factor 5 Term / Factor For efficiency, the parser must make the correct choice when it expands an NT. Wrong choices lead to wasted effort. 6 Factor 7 Factor ( Expr ) 8 number 9 23 identifier

Another possible parse Other choices for expansion are possible Rule Sentential Form Input Goal x - 2 * y 0 Expr x - 2 * y 1 Expr +Term x - 2 * y 1 Expr + Term +Term x - 2 * y 1 Expr + Term +Term + Term x - 2 * y 1 and so on. x - 2 * y This expansion doesn t terminate Wrong choice of expansion leads to non-termination Non-termination is a bad property for a parser to have Parser must make the right choice Consumes no input! 0 Goal Expr 1 Expr Expr + Term 2 Expr Term 3 Term 4 Term Term * Factor 5 Term / Factor 6 Factor 7 Factor ( Expr ) 8 number 9 24 identifier

The Classic Expression Grammar 0 Goal Expr 1 Expr Expr + Term 2 Expr - Term 3 Term 4 Term Term * Factor 5 Term / Factor 6 Factor 7 Factor ( Expr ) 8 number 9 id Classic Expression Grammar The possibility of an infinite sequence of expansions in a parser is bad. disastrous The problem arises from left recursion in the grammar and a leftmost derivation 1 LHS symbol cannot appear at start of the RHS Cannot derive from it in multiple steps, either Top down parsers build leftmost derivations, so grammars with left recursion are not suitable for topdown parsing 1 COMP Similar 412, problem Fall 2018arises with right recursion and a rightmost derivation. 25

Left Recursion Top-down parsers cannot handle left-recursive grammars Formally, A grammar is left recursive if $ A Î NT such that a derivation A Þ + Aa exists, for some string a Î (NT ÈT ) + Our classic expression grammar is left recursive This can lead to non-termination in a top-down parser In a top-down parser, any recursion must be right recursion We would like to convert the left recursion to right recursion Non-termination is always a bad property in a compiler Fortunately, we can eliminate left recursion in an algorithmic way. COMP Right 412, recursion Fall 2018 is defined in a symmetric way. 26

Eliminating Left Recursion To remove left recursion, we can transform the grammar Consider a grammar fragment of the form Fee Fee a b where neither a nor b start with Fee Language is b followed by 0 or more a s We can rewrite this fragment as Fee b Fie Fie a Fie e where Fie is a new non-terminal The new grammar defines the same language as the old grammar, using only right recursion. New Idea: the e production Added a reference to the empty string 27

Eliminating Left Recursion The expression grammar contains two cases of left recursion Expr Expr + Term Term Term * Factor Expr - Term Term * Factor Term Factor Applying the transformation yields Expr Expr Term Expr + Term Expr - Term Expr e Term Factor Term Term * Factor Term / Factor Term e These fragments use only right recursion 28

Eliminating Left Recursion Substituting them back into the grammar yields 0 Goal Expr 1 Expr Term Expr 2 Expr + Term Expr 3 - Term Expr 4 e 5 Term Factor Term 6 Term * Factor Term 7 / Factor Term 8 e 9 Factor ( Expr ) 10 number 11 id Right-recursive expression grammar This grammar is correct, if somewhat counter-intuitive. A top-down parser will terminate using it. A top-down parser may need to backtrack with it. It is left associative, as was the original Why didn t we just rewrite it so Expr was at the right end of the RHS, rather than the left end? 29

Eliminating Left Recursion Substituting them back into the grammar yields 0 Goal Expr 1 Expr Term Expr 2 Expr + Term Expr 3 - Term Expr 4 e 5 Term Factor Term 6 Term * Factor Term 7 / Factor Term 8 e 9 Factor ( Expr ) 10 number 11 id Right-recursive expression grammar This grammar is correct, if somewhat counter-intuitive. NOTE: This technique eliminates direct left A top-down recursion parser when a will production s terminate RHS using begins it. with its own LHS. It A does top-down not eliminate parser indirect may need left to recursion. backtrack We with will it. get there, in the next lecture It is left associative, as was the original Why didn t we just rewrite it so Expr was at the right end of the RHS, rather than the left end? 30

Associativity Eliminating Left Recursion Notice that we do not use the naïve right-recursive form Expr Term Expr Expr Term + Expr Expr + Term Expr - Term Expr e Term - Expr Term Transformed grammar fragment Naïve right-recursive form The naïve right-recursive form generates a different associativity (and parse tree) than did the original grammar. The transformed grammar fragment generates the same parse tree as the original grammar did. (See 3.5.3 in EaC2e.) 31

Associativity Eliminating Left Recursion The naïve right-recursive form changes the associativity Expr Term Expr Expr Term + Expr Expr + Term Expr - Term Expr e Term - Expr Term Transformed grammar fragment Naïve right-recursive form + + + z w + + y x + w x y z Abstracted Parse Trees for w + x + y + z 32

Parsing with the RR CEG 0 Goal Expr 1 Expr Term Expr 2 Expr + Term Expr 3 - Term Expr 4 e 5 Term Factor Term 6 Term * Factor Term 7 / Factor Term 8 e 9 Factor ( Expr ) 10 number 11 id Right Recursive CEG Rule Sentential Form x 2 * y, again Goal 0 Expr 1 Term Expr 5 Factor Term Expr 11 <id,x> Term Expr 8 <id,x> Expr 3 <id,x> - Term Expr 5 <id,x> - Factor Term Expr 10 <id,x> - <num,2> Term Expr 6 <id,x> - <num,2> * Factor Term Expr 11 <id,x> - <num,2> * <id,y> Term Expr 8 <id,x> - <num,2> * <id,y> Expr 4 <id,x> - <num,2> * <id,y> 33

Parsing with RR CEG Goal x 2 * y (again) Rule Text of slide Sentential Form Expr Goal 0 Expr Term Expr 1 Term Expr 5 Factor Term Expr minus Term Expr 11 <id,x> Term Expr 8 <id,x> Expr Factor Term e 3 <id,x> - Term Expr 5 <id,x> - Factor Term Expr <num,2> 10 <id,x> - <num,2> Term Expr 6 <id,x> - <num,2> * Factor Term Expr times Factor Term 11 <id,x> - <num,2> * <id,y> Term Expr 8 <id,x> - <num,2> * <id,y> Expr 4 <id,x> - <num,2> * <id,y> Factor Term <id,y> e <id,x> e It is ugly, but it is 34 left associative.