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

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

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Wednesday, September 9, 15. Parsers

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

3. Parsing. Oscar Nierstrasz

Top down vs. bottom up parsing

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

programming languages need to be precise a regular expression is one of the following: tokens are the building blocks of programs

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

Syntax. Syntax. We will study three levels of syntax Lexical Defines the rules for tokens: literals, identifiers, etc.

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

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

Lecture 4: Syntax Specification

CSE 3302 Programming Languages Lecture 2: Syntax

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

Lexical and Syntax Analysis. Top-Down Parsing

Syntactic Analysis. Top-Down Parsing

Lexical Analysis. COMP 524, Spring 2014 Bryan Ward

Languages and Compilers

Building Compilers with Phoenix

CS 315 Programming Languages Syntax. Parser. (Alternatively hand-built) (Alternatively hand-built)

Lexical and Syntax Analysis

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Monday, September 13, Parsers

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

A programming language requires two major definitions A simple one pass compiler

Wednesday, August 31, Parsers

CMSC 330: Organization of Programming Languages

Lexical and Syntax Analysis (2)

CS 406/534 Compiler Construction Parsing Part I

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

Theoretical Part. Chapter one:- - What are the Phases of compiler? Answer:

Abstract Syntax Trees & Top-Down Parsing

Parsing II Top-down parsing. Comp 412

CIT Lecture 5 Context-Free Grammars and Parsing 4/2/2003 1

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

Types of parsing. CMSC 430 Lecture 4, Page 1

Compilers. Predictive Parsing. Alex Aiken

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

EECS 6083 Intro to Parsing Context Free Grammars

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

A Simple Syntax-Directed Translator

Chapter 2 - Programming Language Syntax. September 20, 2017

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

Syntax Analysis Part I

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

CPS 506 Comparative Programming Languages. Syntax Specification

Lecturer: William W.Y. Hsu. Programming Languages

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Introduction to Parsing. Comp 412

Compilerconstructie. najaar Rudy van Vliet kamer 140 Snellius, tel rvvliet(at)liacs(dot)nl. college 3, vrijdag 22 september 2017

3. Context-free grammars & parsing

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

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

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.

Syntax Analysis, III Comp 412

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part2 3.3 Parse Trees and Abstract Syntax Trees

Note that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).

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

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

Dr. D.M. Akbar Hussain

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

COP 3402 Systems Software Syntax Analysis (Parser)

Part 3. Syntax analysis. Syntax analysis 96


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

Chapter 2 :: Programming Language Syntax

CA Compiler Construction

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

Architecture of Compilers, Interpreters. CMSC 330: Organization of Programming Languages. Front End Scanner and Parser. Implementing the Front End

Ambiguity. Grammar E E + E E * E ( E ) int. The string int * int + int has two parse trees. * int

CSCI312 Principles of Programming Languages

Syntax Analysis/Parsing. Context-free grammars (CFG s) Context-free grammars vs. Regular Expressions. BNF description of PL/0 syntax

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

Chapter 4. Lexical and Syntax Analysis

2.2 Syntax Definition

Formal Languages and Grammars. Chapter 2: Sections 2.1 and 2.2

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.

Syntax Analysis, III Comp 412

Introduction to Parsing

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Compiler Construction 2016/2017 Syntax Analysis

This book is licensed under a Creative Commons Attribution 3.0 License

VIVA QUESTIONS WITH ANSWERS

CMSC 330: Organization of Programming Languages

CMSC 330: Organization of Programming Languages

1 Introduction. 2 Recursive descent parsing. Predicative parsing. Computer Language Implementation Lecture Note 3 February 4, 2004

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

CMSC 330: Organization of Programming Languages. Context Free Grammars

Part 5 Program Analysis Principles and Techniques

LL parsing Nullable, FIRST, and FOLLOW

CMSC 330: Organization of Programming Languages. Context Free Grammars

Chapter 3. Describing Syntax and Semantics ISBN

EDA180: Compiler Construc6on. Top- down parsing. Görel Hedin Revised: a

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

Transcription:

Syntax Analysis Björn B. Brandenburg The University of North Carolina at Chapel Hill Based on slides and notes by S. Olivier, A. Block, N. Fisher, F. Hernandez-Campos, and D. Stotts.

The Big Picture Character Stream Scanner (lexical analysis) Token Stream Parser (syntax analysis) Parse Tree Semantic analysis & Symbol Table intermediate code gen. Abstract syntax tree Machine-independent optimization (optional) Modified intermediate form Target code generation. Machine language Machine-specific optimization (optional) Modified target language 2

The Big Picture Character Stream Scanner (lexical analysis) Token Stream Parser (syntax analysis) Parse Tree Semantic analysis & Symbol Table intermediate code gen. Abstract syntax tree Machine-independent Syntax Analysis: Discovery optimization of Program (optional) Structure Modified intermediate form Target code generation. Turn the stream of individual input tokens into a Machine language complete, hierarchical representation Modified target language Machine-specific of the program (or compilation unit). optimization (optional) 3

Syntax Specification and Parsing Syntax Specification How can we succinctly describe the structure of legal programs? Syntax Recognition How can a compiler discover if a program conforms to the specification? Context-free Grammars LL and LR Parsers 4

Context-Free Grammars Review: grammar. Collection of productions. regular grammar + recursion A production defines a non-terminal (on the left, the head ) in terms of a string terminal and non-terminal symbols. Terminal symbols are elements of the alphabet of the grammar. A non-terminal can be the head of multiple productions. Example: Natural Numbers digit 0 1 2 3 4 5 6 7 8 9 non_zero_digit 1 2 3 4 5 6 7 8 9 natural_number non_zero_digit digit* 5

Context-Free Grammars Review: grammar. Collection Regular grammars. of productions. regular grammar + recursion Restriction: no unrestricted recursion. A production defines a non-terminal (on the left, the head ) in A non-terminal symbol cannot be defined in terms of itself. terms of a string terminal and non-terminal symbols. (except for special cases that equivalent to a Kleene Closure) Terminal symbols are elements of the alphabet of the grammar. Serious limitation: e.g., cannot express matching parenthesis. A non-terminal can be the head of multiple productions. Example: Natural Numbers digit 0 1 2 3 4 5 6 7 8 9 non_zero_digit 1 2 3 4 5 6 7 8 9 natural_number non_zero_digit digit* 6

Context-Free Grammars regular grammar + recursion Context-free Grammars (CFGs) allow recursion. Arithmetic expression with parentheses expr id number - expr ( expr ) expr op expr op + - * / 7

Context-Free Grammars regular grammar + recursion Recursion Context-free Grammars (CFGs) allow recursion. An expression is a a minus sign followed by an expression. Arithmetic expression with parentheses expr id number - expr ( expr ) expr op expr op + - * / 8

Context-Free Grammars regular grammar + recursion Can express matching parenthesis requirement. Context-free Grammars (CFGs) allow recursion. Arithmetic expression with parentheses expr id number - expr ( expr ) expr op expr op + - * / 9

Context-Free Grammars regular grammar + recursion Key difference to lexical grammar: terminal symbols are tokens, Context-free Grammars (CFGs) allow recursion. not individual characters. Arithmetic expression with parentheses expr id number - expr ( expr ) expr op expr op + - * / 10

Context-Free Grammars regular grammar + recursion One of the non-terminals, usually the first one, is called the start symbol, and it defines the Context-free Grammars (CFGs) allow recursion. construct defined by the grammar. Arithmetic expression with parentheses expr id number - expr ( expr ) expr op expr op + - * / 11

BNF vs. EBNF Backus-Naur-Form (BNF) Originally developed for ALGOL 58/60 reports. Textual notation for context-free grammars. expr id number - expr ( expr ) expr op expr is written as <expr> ::= id number - <expr> ( <expr> ) <expr> <op> <expr> 12

BNF vs. EBNF Backus-Naur-Form (BNF) Originally developed for ALGOL 58/60 reports. Textual notation for context-free grammars. expr id number - expr ( expr ) expr op expr is written as <expr> ::= id number - <expr> ( <expr> ) <expr> <op> <expr> Strictly speaking, it does not include the Kleene Star and similar notational sugar. 13

BNF vs. EBNF Extended Backus-Naur-Form (EBNF) Many authors extend BNF to simplify grammars. One of the first to do so was Niklaus Wirth. There exists an ISO standard for EBNF (ISO/IEC 14977). But many dialects exist. 14

BNF vs. EBNF Extended Backus-Naur-Form (EBNF) Many authors extend BNF to simplify grammars. One of the first to do so was Niklaus Wirth. There exists an ISO standard for EBNF (ISO/IEC 14977). But many dialects exist. Features Terminal symbols are quoted. Use of ʻ=ʻ instead of ʻ::=ʼ to denote. Use of ʻ,ʼ for concatenation. [A] means A can occur optionally (zero or one time). {A} means A can occur repeatedly (Kleene Star). Parenthesis are allowed for grouping. And then some 15

BNF vs. EBNF Extended Backus-Naur-Form (EBNF) Many authors extend BNF to simplify grammars. One of the first to so was Niklaus Wirth. There exists an ISO standard for EBNF (ISO/IEC 14977). We will use mostly BNF-like grammars with the But many dialects exist. addition of the Kleene Star, ε, and parenthesis. Features Terminal symbols are quoted. Use of ʻ=ʻ instead of ʻ::=ʼ to denote. Use of ʻ,ʼ for concatenation. [A] means A can occur optionally (zero or one time). {A} means A can occur repeatedly (Kleene Star). Parenthesis are allowed for grouping. And then some 16

Example: EBNF to BNF Conversion id_list id (, id )* is equivalent to id_list id id_list id_list, id (Remember that non-terminals can be the head of multiple productions.) 17

Derivation A grammar allows programs to be derived. Productions are rewriting rules. A program is syntactically correct if and only if it can be derived from the start symbol. Derivation Process Begin with string consisting only of start symbol. while string contains a non-terminal symbol: Choose one non-terminal symbol X. Choose production where X is the head. Replace X with right-hand side of production. 18

Derivation A If we grammar always allows choose programs the left-most to be derived. Productions are rewriting rules. non-terminal symbol, then it is called Program is syntactically correct if and only if it can be a left-most derivation. derived from the start symbol. Derivation Process Begin with string consisting only of start symbol. while string contains a non-terminal symbol: Choose one non-terminal symbol X. Choose production where X is the head. Replace X with right-hand side of production. 19

Derivation A grammar allows programs to be derived. Productions are rewriting rules. A program is syntactically correct if and only if it can be derived from the start symbol. If we always choose the right-most non-terminal symbol, then it is called Derivation Process Begin with string consisting only of start symbol. a right-most or canonical derivation. while string contains a non-terminal symbol: Choose one non-terminal symbol X. Choose production where X is the head. Replace X with right-hand side of production. 20

Example Derivation Arithmetic grammar: expr id number - expr ( expr ) expr op expr op + - * / Program slope * x + intercept 21

Example Derivation Arithmetic grammar: expr id number - expr ( expr ) expr op expr op + - * / Program slope * x + intercept expr expr op expr 22

Example Derivation Arithmetic grammar: expr id number - expr ( expr ) expr op expr op + - * / Program slope * x + intercept denotes derived from expr expr op expr 23

Example Derivation Arithmetic grammar: expr id number - expr ( expr ) expr op expr op + - * / Program slope * x + intercept expr expr op expr expr op id 24

Example Derivation Arithmetic grammar: expr id number - expr ( expr ) expr op expr op + - * / Program slope * x + intercept expr expr op expr expr op id expr + id 25

Example Derivation Arithmetic grammar: expr id number - expr ( expr ) expr op expr op + - * / Program expr op expr + id slope * x + intercept expr expr op expr expr op id expr + id 26

Example Derivation Arithmetic grammar: expr id number - expr ( expr ) expr op expr op + - * / Program expr op expr + id slope * x + intercept expr op id + id expr expr op expr expr op id expr + id 27

Example Derivation Arithmetic grammar: expr id number - expr ( expr ) expr op expr op + - * / Program expr op expr + id slope * x + intercept expr op id + id expr expr op expr expr * id + id expr op id expr + id 28

Example Derivation Arithmetic grammar: expr id number - expr ( expr ) expr op expr op + - * / Program expr op expr + id slope * x + intercept expr op id + id expr expr op expr expr * id + id expr op id id * id + id expr + id 29

Example Derivation Arithmetic grammar: expr id number - expr ( expr ) expr op expr op + - * / Substitute values of identifier tokens. Program expr op expr + id slope * x + intercept expr op id + id expr expr op expr expr * id + id expr op id id * id + id expr + id slope * x + intercept 30

Example Derivation Arithmetic grammar: expr id number - expr ( expr ) expr op expr op + - * / This is a right-most derivation. Program expr op expr + id slope * x + intercept expr op id + id expr expr op expr expr * id + id expr op id id * id + id expr + id slope * x + intercept 31

Parse Tree A parse tree is a hierarchical representation of the derivation that does not show the derivation order. expr expr op expr expr op expr + id (intercept) id (slope) * id (x) 32

Parse Tree A parse tree is a hierarchical representation of the derivation that does not show the derivation order. expr Properties Each interior node is a non-terminal expr op expr symbol. Its children are the right-hand side of the production that it was replaced with. expr op expr + Leaf nodes are terminal id (intercept) symbols (tokens). Many-to-one: many derivations can id (slope) * id (x) yield identical parse trees. The parse tree defines the structure of the program. 33

Parse Tree This parse tree represents the formula slope * x + intercept. expr expr op expr expr op expr + id (intercept) id (slope) * id (x) 34

Parse Tree Letʼs do a left-most derivation of slope * x + intercept. Arithmetic grammar: expr id number - expr ( expr ) expr op expr op + - * / 35

Parse Tree Letʼs do a left-most derivation of slope * x + intercept. expr expr op expr id (slope) * expr op expr id (x) + id (intercept) 36

Parse Tree (Ambiguous) This parse tree represents the formula slope * (x + intercept), which is not equal to slope * x + intercept. expr This grammar, is ambiguous and can construct expr op expr the following parse tree. id (slope) * expr op expr id (x) + id (intercept) 37

Parse Tree (Ambiguous) This parse tree represents the formula slope * (x + intercept), which is not equal to slope * x + intercept. expr Ambiguity The parse tree defines the structure This grammar, is ambiguous and can construct of the program. A program the expr following should have parse only optree. one valid expr interpretation! Two solutions: Make grammar unambiguous, id (slope) * i.e., ensure that all derivations yield expr op expr identical parse trees. Provide disambiguating rules. id (x) + id (intercept) 38

Disambiguating the Grammar The problem with our original grammar is that it does not fully express the grammatical structure (i.e., associativity and precedence). To create an unambiguous grammar, we need to fully specify the grammar and differentiate between terms and factors. 39

Disambiguating the Grammar The problem with our original grammar is that it does not fully express the grammatical structure (i.e., associativity and precedence). To create an unambiguous grammar, we need to fully specify the grammar and differentiate between terms and factors. expr term expr add_op term term factor term mult_op factor factor id number - factor ( expr ) add_op + - mult_op * / 40

Disambiguating the Grammar The problem with our original grammar is that it does not fully express the grammatical structure (i.e., associativity and precedence). This gives precedence to multiply. To create an unambiguous grammar, we need to fully specify the grammar and differentiate between terms and factors. expr term expr add_op term term factor term mult_op factor factor id number - factor ( expr ) add_op + - mult_op * / 41

Example Parse Tree 3+4*5 expr expr add_op term term + term mul_op factor factor factor * number(5) number (3) number (4) 42

Multiplication precedes addition. Example Parse Tree 3+4*5 expr expr add_op term term + term mul_op factor factor factor * number(5) number (3) number (4) 43

Another Example Lets try deriving 3*4+5*6+7. expr term expr add_op term term factor term mult_op factor factor id number - factor ( expr ) add_op + - mult_op * / 44

3*4+5*6+7 expr expr Parse Tree add_op term expr add_op term + factor term + term mul_op factor 7 term mul_op factor factor * 6 factor * 4 5 3 45

Parser The purpose of the parser is to construct the parse tree that corresponds to the input token stream. (If such a tree exists, i.e., for correct input.) 46

Parser The purpose of the parser is to construct the parse tree that corresponds to the input token stream. (If such a tree exists, i.e., for correct input.) This is a non-trivial problem: for example, consider 3*4 and 3+4. 47

3+4 3*4 expr expr expr add_op term term term + factor term mul_op factor factor 4 factor * 4 3 3 48

3+4 3*4 expr expr expr add_op term term term + factor term mul_op factor factor 4 factor * 4 3 3 How can a computer derive these trees by examining one token at a time? 49

3+4 3*4 expr expr expr add_op term term term + factor term mul_op factor factor 4 factor * 4 3 3 In order to derive these trees, the first character that we need to examine is the math operator in the middle. 50

3+4 3*4 expr expr expr add_op term term term + factor term mul_op factor factor 4 factor * 4 3 3 Writing ad-hoc parsers is difficult, tedious, and error-prone. 51

Complexity of Parsing Arbitrary CFGs can be parsed in O(n 3 ) time. n is length of the program (in tokens). Earleyʼs algorithm. Cocke-Younger-Kasami (CYK) algorithm. This is too inefficient for most purposes. Efficient parsing is possible. There are (restricted) types of grammars that can be parsed in linear time, i.e., O(n). Two important classes: LL: Left-to-right, Left-most derivation LR: Left-to-right, Right-most derivation These are sufficient to express most programming languages. 52

Complexity of Parsing Arbitrary CFGs can be parsed in O(n 3 ) time. The class of all grammars for which a left-most n is length of the program (in tokens). derivation always yields a parse tree. Earleyʼs algorithm. Cocke-Younger-Kasami (CYK) algorithm. This is too inefficient for most purposes. Efficient parsing is possible. There are (restricted) types of grammars that can be parsed in linear time, i.e., O(n). Two important classes: LL: Left-to-right, Left-most derivation LR: Left-to-right, Right-most derivation These are sufficient to express most programming languages. 53

Complexity of Parsing Arbitrary CFGs can be parsed in O(n 3 ) time. n is length of the program (in tokens). Earleyʼs algorithm. The class of all grammars for which a right-most Cocke-Younger-Kasami (CYK) algorithm. derivation always yields a parse tree. This is too inefficient for most purposes. Efficient parsing is possible. There are (restricted) types of grammars that can be parsed in linear time, i.e., O(n). Two important classes: LL: Left-to-right, Left-most derivation LR: Left-to-right, Right-most derivation These are sufficient to express most programming languages. 54

LL-Parsers vs. LR-Parsers LL-Parsers Find left-most derivation. Create parse-tree in top-down order, beginning at the root. Can be either constructed manually or automatically generated with tools. Easy to understand. LL grammars sometimes appear unnatural. Also called predictive parsers. LR-Parsers Find right-most derivation. Create parse-tree in bottom-up order, beginning at leaves. Are usually generated by tools. Operating is less intuitive. LR grammars are often natural. Also called shift-reduce parsers. Strictly more expressive: every LL grammar is also an LR grammar, but the converse is not true. Both are used in practice. We focus on LL. 55

LL vs. LR Example A simple grammar for a list of identifiers. id_list id id_list_tail id_list_tail,id id_list_tail id_list_tail ; Input A, B, C; 56

LL Example id_list id id_list_tail id_list_tail,id id_list_tail id_list_tail ; A, B, C ; the (as of yet empty) parse tree current token 57

LL Example id_list id id_list_tail id_list_tail,id id_list_tail id_list id_list_tail ; A, B, C ; current token Step 1 Begin with root (start symbol). 58

LL Example id_list id id_list_tail id_list_tail,id id_list_tail id_list id_list_tail ; id (A) id_list_tail A, B, C ; current token Step 2 Apply id_list production. This matches the first identifier to the expected id token. 59

LL Example id_list id id_list_tail id_list_tail,id id_list_tail id_list id_list_tail ; id (A) id_list_tail A, B, C ;, id (B) id_list_tail current token Step 3 Apply a production for id_list_tail. There are two to choose from. Predict that the first one applies. This matches two more tokens. 60

LL Example id_list id id_list_tail id_list_tail,id id_list_tail id_list id_list_tail ; id (A) id_list_tail A, B, C ;, id (B) id_list_tail current token, id (C) id_list_tail Step 4 Substitute the id_list_tail, predicting the first production again. This matches a comma and C. 61

LL Example id_list id id_list_tail id_list_tail,id id_list_tail id_list id_list_tail ; id (A) id_list_tail A, B, C ;, id (B) id_list_tail current token, id (C) id_list_tail Step 5 ; Substitute the final id_list_tail. This time predict the other production, which matches the ʻ;ʼ. 62

LL Parse Tree id_list id id_list_tail id_list_tail,id id_list_tail id_list Top-Down Construction id_list_tail ; id (A) id_list_tail A, B, C ;, id (B) id_list_tail, id (C) id_list_tail ; 63

LL Parse Tree id_list id id_list_tail id_list_tail,id id_list_tail id_list Top-Down Construction id_list_tail ; id (A) id_list_tail A, B, C ;, id (B) id_list_tail, id (C) id_list_tail ; Notice that the input tokens are placed in the tree from the left to right. 64

LR Example id_list id id_list_tail id_list_tail,id id_list_tail id_list_tail ; A, B, C ; forest (a stack) 65

LR Example id_list id id_list_tail id_list_tail,id id_list_tail Forest = set of (partial) trees. id_list_tail ; A, B, C ; forest (a stack) 66

LR Example id_list id id_list_tail id_list_tail,id id_list_tail Step 1 id_list_tail ; Shift encountered token into forest. A, B, C ; current token id (A) forest (a stack) 67

LR Example id_list id id_list_tail Step 2 Determine that no right-hand id_list_tail,id id_list_tail id_list_tail ; side of any production matches the top of the forest. Shift next token into forest. A, B, C ; current token id (A), forest (a stack) 68

LR Example id_list id id_list_tail Steps 3-6 No right hand side id_list_tail,id id_list_tail id_list_tail ; matches top of forest. Repeatedly shift next token into forest. A, B, C ; current token id (A), id (B), id (C) ; forest (a stack) 69

LR Example id_list id id_list_tail Step 7 Detect that last production matches id_list_tail,id id_list_tail id_list_tail ; the top of the forest. Reduce top token to partial tree. A, B, C ; current token id (A), id (B), id (C) id_list_tail ; forest (a stack) 70

LR Example id_list id id_list_tail Step 8 id_list_tail,id id_list_tail Detect that second production matches. Reduce top of forest. id_list_tail ; A, B, C ; id (A), id (B) id_list_tail current token id_list_tail, id (C) ; forest (a stack) 71

LR Example Step 9 id_list id id_list_tail id_list_tail,id id_list_tail Detect that second production matches. Reduce top of forest. id_list_tail ; A, B, C ; id (A) id_list_tail id_list_tail current token, id (B) id_list_tail, id (C) ; forest (a stack) 72

LR Example id_list id id_list_tail id_list_tail,id id_list_tail id_list_tail ; id_list A, B, C ; id (A) id_list_tail id_list_tail current token, id (B) id_list_tail, id (C) ; Step 10 forest (a stack) Detect that first production matches. Reduce top of forest. 73

LR Parse Tree id_list id id_list_tail id_list_tail,id id_list_tail Bottom-Up Construction id_list_tail ; id_list A, B, C ; id (A) id_list_tail id_list_tail, id (B) id_list_tail, id (C) ; The problem with this grammar is that it can require an arbitrarily large number of terminals to be shifted before reduction takes place. 74

An Equivalent Grammar better suited to LR parsing id_list id_list_prefix ; id_list_prefix id_list_prefix, id id_list_prefix id This grammar limits the number of suspended non-terminals. 75

An Equivalent Grammar better suited to LR parsing id_list id_list_prefix ; id_list_prefix id_list_prefix, id id_list_prefix id However, this creates a problem for the LL parser. When the parser discovers an id it cannot predict the number of id_list_prefix productions that it needs to match. 76

Two Approaches to LL Parser Construction Recursive Descent. A mutually recursive set of subroutines. One subroutine per non-terminal. Case statements based on current token to predict subsequent productions. Table-Driven. Not recursive; instead has an explicit stack of expected symbols. A loop that processes the top of the stack. Terminal symbols on stack are simply matched. Non-terminal symbols are replaced with productions. Choice of production is driven by table. 77

Recursive Descent Example recursive descent = climb from root to leaves, calling a subroutine for every level Identifier List Grammar. Recall our LL-compatible original version. id_list id id_list_tail id_list_tail,id id_list_tail id_list_tail ; Recursive Descent Approach. We need one subroutine for each non-terminal. Each subroutine adds tokens into the growing parse tree and/or calls further subroutines to resolve non-terminals. 78

Recursive Descent Example recursive descent = climb from root to leaves, calling a subroutine for every level Identifier List Grammar. Recall our LL-compatible original version. id_list id id_list_tail Possibly itself. Recursive descent: id_list_tail,id either id_list_tail directly or indirectly. id_list_tail ; Recursive Descent Approach. We need one subroutine for each non-terminal. Each subroutine adds tokens into the growing parse tree and/or calls further subroutines to resolve non-terminals. 79

Recursive Descent Example Helper routine match. Used to consume expected terminals/ tokens. id_list id id_list_tail id_list_tail,id id_list_tail Given an expected token type (e.g., id, ʻ;ʼ, or ʻ,ʼ), checks if next token is of correct type. Raises error otherwise. id_list_tail ; subroutine match(expected_type): token = get_next_token() if (token.type == expected_type): make token a child of left-most non-terminal in tree else: throw ParseException( expected + expected_type) 80

Recursive Descent Example Helper routine match. Used to consume expected terminals/ Example of a match failure: id_list id id_list_tail tokens. id_list_tail,id id_list_tail Given an expected token type (e.g., id, ʻ;ʼ, or ʻ,ʼ), checks if next token is of correct type. Raises error otherwise. id_list_tail ; subroutine match(expected_type): Eclipse: class token expected, but got id token = get_next_token() if (token.type == expected_type): make token a child of left-most non-terminal in tree else: throw ParseException( expected + expected_type) 81

Recursive Descent Example Parsing id_list. id_list id id_list_tail Trivial, there is only one production. Simply match an id, and then delegate id_list_tail,id id_list_tail parsing of the tail to the subroutine for id_list_tail. id_list_tail ; subroutine parse_id_list(): match(id_token) parse_id_list_tail() 82

Recursive Descent Example Parsing id_list. id_list id id_list_tail Trivial, there is only one production. Simply match an id, and then delegate parsing of the tail to the subroutine for id_list_tail. This delegation is the descent part in recursive descent parsing. id_list_tail,id id_list_tail id_list_tail ; subroutine parse_id_list(): match(id_token) parse_id_list_tail() 83

Recursive Descent Example Parsing id_list_tail. id_list id id_list_tail There are two productions to choose from. id_list_tail,id id_list_tail This require predicting which one is the correct one. id_list_tail ; This requires looking ahead and examining the next token (without consuming it). subroutine parse_id_list_tail(): type = peek_at_next_token_type() case type of COMMA_TOKEN: match(comma_token); match(id_token); parse_id_list_tail() SEMICOLON_TOKEN: match(semicolon_token); 84

Recursive Descent Example Parsing id_list_tail. id_list id id_list_tail There are two productions to choose from. id_list_tail,id id_list_tail This require predicting which one is the correct one. This delegation id_list_tail is the recursive ; This requires looking ahead and examining the next token (without consuming it). part in recursive descent parsing. subroutine parse_id_list_tail(): type = peek_at_next_token_type() case type of COMMA_TOKEN: match(comma_token); match(id_token); parse_id_list_tail() SEMICOLON_TOKEN: match(semicolon_token); 85

Recursive Descent Example We need one token lookahead. Parsing id_list_tail. id_list id id_list_tail There are two productions to choose from. This require predicting which one is Parsers that require k tokens lookahead id_list_tail,id id_list_tail are called LL(k) (or LR(k)) parsers. the correct one. id_list_tail ; This requires looking ahead and examining the next token (without consuming it). Thus, this is a LL(1) parser. subroutine parse_id_list_tail(): type = peek_at_next_token_type() case type of COMMA_TOKEN: match(comma_token); match(id_token); parse_id_list_tail() SEMICOLON_TOKEN: match(semicolon_token); 86

LL(k) Parsers Recall our non-ll compatible grammar. Better for LR-parsing, but problematic for predictive parsing. id_list id_list_prefix ; id_list_prefix id_list_prefix, id id_list_prefix id Cannot be parsed by LL(1) parser. Cannot predict which id_list_production to choose if next token is of type id. However, a LL(2) parser can parse this grammar. Just look at the second token ahead and disambiguate based on ʻ,ʼ vs. ʻ;ʼ. 87

LL(k) Parsers Recall our non-ll compatible grammar. Better for LR-parsing, but problematic for predictive parsing. Bottom-line: id_list id_list_prefix ; can enlarge class of supported grammars by using id_list_prefix k > 1 lookahead, id_list_prefix, but at idthe expense of reduced performance / backtracking. id_list_prefix id Most production LL parsers use k = 1. Cannot be parsed by LL(1) parser. Cannot predict which id_list_production to choose if next token is of type id. However, a LL(2) parser can parse this grammar. Just look at the second token ahead and disambiguate based on ʻ,ʼ vs. ʻ;ʼ. 88

Predict Sets subroutine parse_id_list_tail(): type = peek_at_next_token_type() case type of COMMA_TOKEN: match(comma_token); match(id_token); parse_id_list_tail() SEMICOLON_TOKEN: match(semicolon_token); The question is how do we label the case statements in general, i.e., for arbitrary LL grammars? 89

First, Follow, and Predict sets of terminal symbols FIRST(A): The terminals that can be the first token of a valid derivation starting with symbol A. Trivially, for each terminal T, FIRST(T) = {T}. FOLLOW(A): The terminals that can follow the symbol A in any valid derivation. (A is usually a non-terminal.) PREDICT(A α): The terminals that can be the first tokens as a result of the production A α. (α is a string of symbols) The terminals in this set form the label in the case statements to predict A α. 90

First, Follow, and Predict sets of terminal symbols FIRST(A): The terminals (including ε) that can be the first token of a valid Note: For a non-terminal A, the set FIRST(A) is the union of the predict sets of all productions with A as the head: derivation starting with symbol A. Trivially, for each terminal T, FIRST(T) = {T}. if there exist three productions A α, A β, and A λ, then FOLLOW(A): FIRST(A) = The terminals that can follow the symbol A in any valid PREDICT(A α) PREDICT(A β) PREDICT(A λ) derivation. PREDICT(A α): The terminals that can be the first tokens as a result of the production A α. (α is a string of symbols) The terminals in this set form the label in the case statements to predict A α. 91

PREDICT(A α) If α is ε, i.e., if A is derived to nothing : PREDICT(A ε) = FOLLOW(A) Otherwise, if α is a string of symbols that starts with X: PREDICT(A X ) = FIRST(X) 92

Inductive Definition of FIRST(A) If A is a terminal symbol, then: FIRST(A) = {A} If A is a non-terminal symbol and there exists a production A X, then FIRST(X) FIRST(A) (X can be terminal or non-terminal) 93

Inductive Definition of FIRST(A) Notation: X is the first symbol of the production body. If A is a terminal symbol, then: FIRST(A) = {A} If A is a non-terminal symbol and there exists a production A X, then FIRST(X) FIRST(A) (X can be terminal or non-terminal) 94

Inductive Definition of FOLLOW(A) If the substring AX exists anywhere in the grammar, then FIRST(X) FOLLOW(A) If there exists a production X A, then FOLLOW(X) FOLLOW(A) 95

Inductive Definition of FOLLOW(A) Notation: A is the last symbol of the production body. If the substring AX exists anywhere in the grammar, then FIRST(X) FOLLOW(A) If there exists a production X A, then FOLLOW(X) FOLLOW(A) 96

Computing First, Follow, and Predict Inductive Definition. FIRST, FOLLOW, and PREDICT are defined in terms of each other. Exception: FIRST for terminals. This the base case for the induction. Iterative Computation. Start with FIRST for terminals and set all other sets to be empty. Repeatedly apply all definitions (i.e., include known subsets). Terminate when sets do not change anymore. 97

Predict Set Example id_list id_list_prefix; [1] id_list_prefix id_list_prefix, id [2] id_list_prefix id 98

Predict Set Example id_list id_list_prefix; [1] id_list_prefix id_list_prefix, id [2] id_list_prefix id Base case: FIRST(id) = {id} Induction for [2]: FIRST(id) FIRST(id_list_prefix) = {id} Induction for [1]: FIRST(id_list_prefix) FIRST(id_list_prefix) Predict sets for (2) and (1) are identical: not LL(1)! 99

Left Recursion Leftmost symbol is a recursive non-terminal symbol. This causes a grammar not to be LL(1). Recursive descent would enter infinite recursion. It is desirable for LR grammars. id_list id_list_prefix; id_list_prefix id_list_prefix, id id_list_prefix id 100

Left Recursion To parse an id_list_prefix, call the Leftmost symbol is a recursive non-terminal symbol. parser for id_list_prefix, which calls the This causes a grammar not to be LL(1). parser for id_list_prefix, which Recursive descent would enter infinite recursion. It is desirable for LR grammars. id_list id_list_prefix; id_list_prefix id_list_prefix, id id_list_prefix id 101

Left-Factoring Introducing tail symbols to avoid left recursion. Split a recursive production in an unambiguous prefix and an optional tail. expr term expr add_op term is equivalent to prefix expr term expr_tail tail expr_tail ε tail expr_tail add_op expr 102

Another Predict Set Example [1] cond if expr then statement [2] cond if expr then statement else statement 103

Another Predict Set Example [1] cond if expr then statement [2] cond if expr then statement else statement PREDICT([1]) = {if} PREDICT([2]) = {if} If the next token is an if, which production is the right one? 104

Common Prefix Problem Non-disjoint predict sets. In order to predict which production will be applied, all predict sets for a given non-terminal need to be disjoint! If there exist two productions A α, A β such that there exists a terminal x for which x PREDICT(A α) PREDICT(A β), then an LL(1) parser cannot properly predict which production must be chosen. Can also be addressed with left-factoring 105

Dangling else Even if left recursion and common prefixes have been removed, a language may not be LL(1). In many languages an else statement in if-then-else statements is optional. Ambiguous grammar: which if to match else to? if AAA then if BBB then CCC else DDD 106

Dangling else if AAA then if BBB then CCC else DDD Can be handled with a tricky LR grammar. There exists no LL(1) parser that can parse such statements. Even though a proper LR(1) parser can handle this, it may not handle it in a method the programmer desires. Good language design avoids such constructs. 107

Dangling else To write this code correctly (based on indention) begin and end statements must be added. This is LL compatible. if AAA then if BBB then CCC else DDD if AAA then begin if BBB then CCC end else DDD 108

Dangling else statement cond cond if expr then block_statement cond if expr then block_statement else block_statement block_statement begin statement* end A grammar that avoids the dangling else problem. 109