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

Similar documents
Syntax Analysis, III Comp 412

Syntax Analysis, III Comp 412

Parsing II Top-down parsing. Comp 412

Introduction to Parsing. Comp 412

CS 406/534 Compiler Construction Parsing Part I

CSCI312 Principles of Programming Languages

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

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

Syntactic Analysis. Top-Down Parsing

Introduction to Parsing

Parsing II Top-down parsing. Comp 412

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

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

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

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

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

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

3. Parsing. Oscar Nierstrasz

Computer Science 160 Translation of Programming Languages

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

EECS 6083 Intro to Parsing Context Free Grammars

Types of parsing. CMSC 430 Lecture 4, Page 1

Syntax Analysis, VI Examples from LR Parsing. Comp 412

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

Chapter 3. Parsing #1

CS 314 Principles of Programming Languages

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

CA Compiler Construction

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

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

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

Building a Parser Part III

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

Front End. Hwansoo Han

Part 5 Program Analysis Principles and Techniques

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

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

Syntax Analysis Check syntax and construct abstract syntax tree

Defining syntax using CFGs

CS415 Compilers. Lexical Analysis

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

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

Top down vs. bottom up parsing

Bottom-up Parser. Jungsik Choi

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

Chapter 4: LR Parsing

CMSC 330: Organization of Programming Languages

Lexical Analysis. Introduction

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

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

CSE 3302 Programming Languages Lecture 2: Syntax

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

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

Chapter 3. Describing Syntax and Semantics ISBN

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

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

Monday, September 13, Parsers

Downloaded from Page 1. LR Parsing

Chapter 4. Lexical and Syntax Analysis

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

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

Syntax Analysis Part I

LECTURE 7. Lex and Intro to Parsing

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

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

Syntax. In Text: Chapter 3

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

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

Optimizing Finite Automata

CMSC 330: Organization of Programming Languages. Context Free Grammars

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Fall Compiler Principles Lecture 2: LL parsing. Roman Manevich Ben-Gurion University of the Negev

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

Introduction to Parsing. Lecture 8

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

Introduction to Parsing. Lecture 5

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Wednesday, September 9, 15. Parsers

10/5/17. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntax Analysis

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

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

Intermediate Representations

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

CMSC 330: Organization of Programming Languages. Context Free Grammars

Introduction to Parsing. Lecture 5

Wednesday, August 31, Parsers

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

10/4/18. Lexical and Syntactic Analysis. Lexical and Syntax Analysis. Tokenizing Source. Scanner. Reasons to Separate Lexical and Syntactic Analysis

Ambiguity, Precedence, Associativity & Top-Down Parsing. Lecture 9-10

Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

Administrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:30-5:00 TT 10 Evans.

Principles of Programming Languages COMP251: Syntax and Grammars

Compiler Design Concepts. Syntax Analysis

Programming Language Syntax and Analysis

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

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

Transcription:

Parsing Note by Baris Aktemur: Our slides are adapted from Cooper and Torczon s slides that they prepared for COMP 412 at Rice. Copyright 2010, 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. The Front End Source code Scanner tokens Parser IR Errors Parser Checks the stream of words and their parts of speech (produced by the scanner) for grammatical correctness Determines if the input is syntactically well formed Need a mathematical model of syntax a grammar G Need an algorithm for testing membership in L(G) Need to keep in mind that our goal is building parsers, not studying the mathematics of arbitrary languages 1 1

Limits of Regular Languages Advantages of Regular Expressions Simple & powerful notation for specifying patterns Automatic construction of fast recognizers Many kinds of syntax can be specified with REs Example a regular expression for arithmetic expressions Term [a-za-z] ([a-za-z] [0-9]) * Op + - * / Expr ( Term Op ) * Term ([a-za-z] ([a-za-z] [0-9]) * (+ - * /))* [a-za-z] ([a-za-z] [0-9]) Limitation: Operator precedence? 2 Grammars 0 Expr Expr Op Expr 1 number 2 id 3 Op + 4-5 * 6 / Rule Sentential Form Expr 0 Expr Op Expr 2 <id,x> Op Expr 4 <id,x> - Expr 0 <id,x> - Expr Op Expr 1 <id,x> - <num,2> Op Expr 5 <id,x> - <num,2> * Expr 2 <id,x> - <num,2> * <id,y> Such a sequence of rewrites is called a derivation Process of discovering a derivation is called parsing We denote this derivation: Expr Þ * id num * id 3 2

Derivations The point of parsing is to construct a derivation At each step, we choose a nonterminal to replace Different choices can lead to different derivations Two derivations are of interest Leftmost derivation replace leftmost NT at each step Rightmost derivation replace rightmost NT at each step These are the two systematic derivations (We don t care about randomly-ordered derivations!) 4 The Two Derivations for x 2 * y Rule Sentential Form Expr 0 Expr Op Expr 2 <id,x> Op Expr 4 <id,x> - Expr 0 <id,x> - Expr Op Expr 1 <id,x> - <num,2> Op Expr 5 <id,x> - <num,2> * Expr 2 <id,x> - <num,2> * <id,y> Leftmost derivation Rule Sentential Form Expr 0 Expr Op Expr 2 Expr Op <id,y> 5 Expr * <id,y> 0 Expr Op Expr * <id,y> 1 Expr Op <num,2> * <id,y> 4 Expr - <num,2> * <id,y> 2 <id,x> - <num,2> * <id,y> Rightmost derivation In both cases, Expr Þ* id num * id The two derivations produce different parse trees The parse trees imply different evaluation orders! 5 3

Derivations and Parse Trees Leftmost derivation Rule Sentential Form Expr G 0 Expr Op Expr 2 <id,x> Op Expr E 4 <id,x> - Expr 0 <id,x> - Expr Op Expr 1 <id,x> - <num,2> Op Expr E Op E 5 <id,x> - <num,2> * Expr 2 <id,x> - <num,2> * <id,y> x E Op E This evaluates as x ( 2 * y ) 2 * y 6 Derivations and Parse Trees Rightmost derivation Rule Sentential Form Expr 0 Expr Op Expr 2 Expr Op <id,y> 5 Expr * <id,y> 0 Expr Op Expr * <id,y> 1 Expr Op <num,2> * <id,y> 4 Expr - <num,2> * <id,y> 2 <id,x> - <num,2> * <id,y> E G E Op E E Op E * y This evaluates as ( x 2 ) * y x 2 This ambiguity is NOT good 7 4

Derivations and Precedence These two derivations point out a problem with the grammar: It has no notion of precedence, or implied order of evaluation To add precedence 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 Parentheses first (level 1 ) Multiplication and division, next (level 2) Subtraction and addition, last (level 3) 8 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 This grammar is slightly larger 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 parses x - 2 * y Cannot handle precedence in an RE for expressions Introduced parentheses, too (beyond power of an RE) One form of the classic expression grammar 9 5

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 G E E T F T T * F F <id,y> <id,x> <num,2> Its parse tree It derives x ( 2 * y ), along with an appropriate parse tree. Both the leftmost and rightmost derivations give the same expression, because the grammar directly and explicitly encodes the desired precedence. 10 Ambiguous Grammars Let s leap back to our original expression grammar. It had other problems. 0 Expr Expr Op Expr 1 number 2 id 3 Op + 4-5 * 6 / Rule Sentential Form Expr 0 Expr Op Expr 2 <id,x> Op Expr 4 <id,x> - Expr 0 <id,x> - Expr Op Expr 1 <id,x> - <num,2> Op Expr 5 <id,x> - <num,2> * Expr 2 <id,x> - <num,2> * <id,y> This grammar allows multiple leftmost derivations for x - 2 * y Hard to automate derivation if > 1 choice The grammar is ambiguous Different choice than the first step 11 6

Two Leftmost Derivations for x 2 * y The Difference: Different productions chosen on the second step Rule Sentential Form Expr 0 Expr Op Expr 2 <id,x> Op Expr 4 <id,x> - Expr 0 <id,x> - Expr Op Expr 1 <id,x> - <num,2> Op Expr 5 <id,x> - <num,2> * Expr 1 <id,x> - <num,2> * <id,y> Original choice Rule Sentential Form Expr 0 Expr Op Expr 0 Expr Op Expr Op Expr 2 <id,x> Op Expr Op Expr 4 <id,x> - Expr Op Expr 1 <id,x> - <num,2> Op Expr 5 <id,x> - <num,2> * Expr 2 <id,x> - <num,2> * <id,y> New choice Both derivations succeed in producing x - 2 * y 12 Ambiguous Grammars Definitions If a grammar has more than one leftmost derivation for a single sentential form, the grammar is ambiguous If a grammar has more than one rightmost derivation for a single sentential form, the grammar is ambiguous The leftmost and rightmost derivations for a sentential form may differ, even in an unambiguous grammar However, they must have the same parse tree! Classic example the if-then-else problem Stmt if Expr then Stmt if Expr then Stmt else Stmt other stmts This ambiguity is inherent in the grammar 13 7

Ambiguity This sentential form has two derivations if Expr 1 then if Expr 2 then Stmt 1 else Stmt 2 E 1 if then else E 1 if then Part of the problem is that the structure built by the parser will determine the interpretation of the code, and these two forms have different meanings! if S 2 if E 2 then E 2 then else S 1 S 1 S 2 production 2, then production 1 production 1, then production 2 14 Ambiguity The grammar forces the structure to match the desired meaning. Removing the ambiguity Must rewrite the grammar to avoid generating the problem Match each else to innermost unmatched if (common sense rule) 0 Stmt if Expr then Stmt 1 ½ if Expr then WithElse else Stmt 2 ½ Other Statements 3 WithElse if Expr then WithElse else WithElse 4 ½ Other Statements With this grammar, example has only one rightmost derivation Intuition: once into WithElse, we cannot generate an unmatched else a final if without an else can only come through rule 2 15 8

Ambiguity if Expr 1 then if Expr 2 then Stmt 1 else Stmt 2 Rule Sentential Form Stmt 0 if Expr then Stmt 1 if Expr then if Expr then WithElse else Stmt 2 if Expr then if Expr then WithElse else S 2 4 if Expr then if Expr then S 1 else S 2? if Expr then if E 2 then S 1 else S 2? if E 1 then if E 2 then S 1 else S 2 Other productions to derive Expr s This grammar has only one rightmost derivation for the example 16 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 Some grammars are backtrack-free 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 Bottom-up parsers handle a large class of grammars 17 9

Top-down Parsing Top-down Parsing Top-down parsing algorithm: 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 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 19 10

Remember the expression grammar? We will call this version 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 20 Example Let s try x 2 * y : Goal Rule Sentential Form Input Goal x - 2 * y is the position in the input buffer 21 11

Example Let s try x 2 * y : Goal Rule Sentential Form Input Goal x - 2 * y Expr 0 Expr x - 2 * y 1 Expr +Term x - 2 * y Expr + Term 3 Term +Term x - 2 * y 6 Factor +Term x - 2 * y Term 9 <id,x> +Term x - 2 * y Fact. <id,x> +Term x - 2 * y <id,x> This worked well, except that doesn t match + The parser must backtrack to here is the position in the input buffer 22 Example Continuing with x 2 * y : Rule Sentential Form Input Goal x - 2 * y 0 Expr x - 2 * y 2 Expr -Term x - 2 * y Expr Goal Expr Term 3 Term -Term x - 2 * y 6 Factor -Term x - 2 * y Term 9 <id,x> - Term x - 2 * y <id,x> -Term x - 2 * y Fact. <id,x> -Term x - 2 * y <id,x> Now, - and - match Now we can expand Term to match 2 Þ Now, we need to expand Term - the last NT on the fringe 23 12

Example Trying to match the 2 in x 2 * y : Goal 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 Expr Term Expr - Term Fact. Fact. Where are we? 2 matches 2 <id,x> We have more input, but no NTs left to expand The expansion terminated too soon Þ Need to backtrack <num,2> 24 Example Trying again with 2 in x 2 * y : Goal Rule Sentential Form Input <id,x> - Term x - 2 * y 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 <id,x> - <num,2> * Factor x - 2 * y <id,x> - <num,2> * Factor x - 2 * y Term Fact. Term Fact. * Fact. <id,y> 9 <id,x> - <num,2> * <id,y> x - 2 * y <id,x> - <num,2> * <id,y> x - 2 * y <id,x> <num,2> The Point: This time, we matched & consumed all the input ÞSuccess! The parser must make the right choice when it expands a NT. Wrong choices lead to wasted effort. 25 13

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 Consumes no input! 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 26 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, 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 27 14

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 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. Added a reference to the empty string 28 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 Term Expr Expr + Term Expr - Term Expr e Term Factor Term Term * Factor Term / Factor Term e These fragments use only right recursion Right recursion often means right associativity. In this case, the grammar does not display any particular associative bias. 29 15

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 This grammar is correct, if somewhat non-intuitive. It is left associative, as was the original The naïve transformation yields a right recursive grammar, which changes the implicit associativity A top-down parser will terminate using it. A top-down parser may need to backtrack with it. 30 Picking the Right Production If it picks the wrong production, a top-down parser may backtrack Alternative is to look ahead in input & use context to pick correctly How much lookahead is needed? In general, an arbitrarily large amount Use the Cocke-Younger, Kasami algorithm or Earley s algorithm Fortunately, Large subclasses of CFGs can be parsed with limited lookahead Most programming language constructs fall in those subclasses Among the interesting subclasses are LL(1) and LR(1) grammars We will focus, for now, on LL(1) grammars & predictive parsing 31 16

Predictive Parsing Basic idea Given A a b, the parser should be able to choose between a & b FIRST sets For some rhs aîg, define FIRST(a) as the set of tokens that appear as the first symbol in some string that derives from a That is, x Î FIRST(a) iff a Þ * x g, for some g 32 Predictive Parsing Basic idea Given A a b, the parser should be able to choose between a & b FIRST sets For some rhs aîg, define FIRST(a) as the set of tokens that appear as the first symbol in some string that derives from a That is, x Î FIRST(a) iff a Þ * x g, for some g The LL(1) Property If A a and A b both appear in the grammar, we would like FIRST(a) Ç FIRST(b) = Æ This would allow the parser to make a correct choice with a lookahead of exactly one symbol! This is almost correct See the next slide 33 17

Predictive Parsing What about e-productions? Þ They complicate the definition of LL(1) If A a and A b and e Î FIRST(a), then we need to ensure that FIRST(b) is disjoint from FOLLOW(A), too, where FOLLOW(A) = the set of terminal symbols that can immediately follow A in a sentential form Define FIRST + (A a) as FIRST(a) È FOLLOW(A), if e Î FIRST(a) FIRST(a), otherwise Then, a grammar is LL(1) iff A a and A b implies FIRST + (A a) Ç FIRST + (A b) = Æ 34 Classic Expression Grammar 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 number 10 id 11 ( Expr ) Symbol FIRST FOLLOW num num Ø id id Ø + + Ø - - Ø * * Ø / / Ø ( ( Ø ) ) Ø eof eof Ø e e Ø Goal (,id,num eof Expr (,id,num ), eof FIRST + (A b ) is identical to FIRST(b ) Expr +, -, e ), eof except for productiond 4 and 8 Term (,id,num +, -, ), eof FIRST + (Expr e) is {e,), eof} Term *, /, e +,-, ), eof Comp FIRST 412, Fall (Term 2010 e) is {e,+,-, ), eof} Factor (,id,num +,-,*,/,),eof 35 18

Classic Expression Grammar 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 number 10 id 11 ( Expr ) Prod n FIRST+ 0 (,id,num 1 (,id,num 2 + 3-4 e,), eof 5 (,id,num 6 * 7 / 8 e,+,-, ), eof 9 number 10 id 11 ( 36 Example 0 Goal Expr 1 Expr Term Expr 2 Expr + Expr 3 - Expr 4 e 5 Term Factor Term 6 Term * Term 7 / Term 8 e 9 Factor number 10 id Clearly, FIRST + (2), FIRST + (3), & FIRST + (4) are disjoint, as are FIRST + (6), FIRST + (7), & FIRST + (8) The grammar now has the LL(1) property 37 19

Predictive Parsing Given a grammar that has the LL(1) property Can write a simple routine to recognize each lhs Code is both simple & fast Consider A b 1 b 2 b 3, with FIRST + (A b i ) Ç FIRST + (A b j ) = Æ if i j /* find an A */ if (current_word Î FIRST(A b 1 )) find a b 1 and return true else if (current_word Î FIRST(A b 2 )) find a b 2 and return true else if (current_word Î FIRST(A b 3 )) find a b 3 and return true else report an error and return false Grammars with the LL(1) property are called predictive grammars because the parser can predict the correct expansion at each point in the parse. Parsers that capitalize on the LL(1) property are called predictive parsers. One kind of predictive parser is the recursive descent parser. Of course, there is more detail to find a b i (p. 103 in EAC, 1 st Ed.) 38 Recursive Descent Parsing Recall the expression grammar, after transformation 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 This produces a parser with six mutually recursive routines: Goal Expr EPrime Term TPrime Factor Each recognizes one NT or T The term descent refers to the direction in which the parse tree is built. 39 20

Recursive Descent Parsing (Procedural) A couple of routines from the expression parser Goal( ) token next_token( ); if (Expr( ) = true & token = EOF) then report success; else report syntax error; return false; Expr( ) if (Term( ) = false) then return false; else return Eprime( ); looking for Number, Identifier, or (, found token instead, or failed to find Expr or ) after ( Factor( ) if (token = Number) then token next_token( ); return true; else if (token = Identifier) then token next_token( ); return true; else if (token = Lparen) token next_token( ); if (Expr( ) = true & token = Rparen) then token next_token( ); return true; // fall out of if statement report syntax error; return false; EPrime, Term, & TPrime follow the same basic lines (Figure 3.10, EaC2) 40 Bottom-up Parsing 21

Parsing Techniques Bottom-up parsers LR(1) 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 Bottom-up parsers handle a large class of grammars Bottom-up parsers build a rightmost derivation in reverse Parsers can be auto-generated from grammars We will skip this topic 42 Summary Top-down Recursive descent, LL(1) LR(1) Advantages Fast Good locality Simplicity Good error detection Fast Deterministic langs. Automatable Left associativity Disadvantages Hand-coded High maintenance Right associativity Large working sets Poor error messages Large table sizes 43 22