Compilers. Predictive Parsing. Alex Aiken

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

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

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

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

Top down vs. bottom up parsing

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

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

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.

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).

Extra Credit Question

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

4 (c) parsing. Parsing. Top down vs. bo5om up parsing

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

Types of parsing. CMSC 430 Lecture 4, Page 1

CA Compiler Construction

Syntactic Analysis. Top-Down Parsing

LL Parsing: A piece of cake after LR

Monday, September 13, Parsers

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

More Bottom-Up Parsing

Table-Driven Parsing

Wednesday, September 9, 15. Parsers

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

Compilation Lecture 3: Syntax Analysis: Top-Down parsing. Noam Rinetzky

Compila(on (Semester A, 2013/14)

CSCI312 Principles of Programming Languages

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

Lexical and Syntax Analysis. Top-Down Parsing

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

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

Chapter 3. Parsing #1

Compiler Design 1. Top-Down Parsing. Goutam Biswas. Lect 5

The procedure attempts to "match" the right hand side of some production for a nonterminal.

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

CS502: Compilers & Programming Systems

Wednesday, August 31, Parsers

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

Compilers: CS31003 Computer Sc & Engg: IIT Kharagpur 1. Top-Down Parsing. Lect 5. Goutam Biswas

Lexical and Syntax Analysis

CS2210: Compiler Construction Syntax Analysis Syntax Analysis


3. Parsing. Oscar Nierstrasz

Fall Compiler Principles Lecture 3: Parsing part 2. Roman Manevich Ben-Gurion University

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

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

CS Parsing 1

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

LL parsing Nullable, FIRST, and FOLLOW

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


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

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

Compiler construction lecture 3

Syntax Analysis, III Comp 412

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

Outline. Top Down Parsing. SLL(1) Parsing. Where We Are 1/24/2013

CSC 4181 Compiler Construction. Parsing. Outline. Introduction

Context-free grammars

Syntax Analysis, III Comp 412

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

Compiler Construction: Parsing

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Part 3. Syntax analysis. Syntax analysis 96

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

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

PESIT Bangalore South Campus Hosur road, 1km before Electronic City, Bengaluru -100 Department of Computer Science and Engineering

Configuration Sets for CSX- Lite. Parser Action Table

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Introduction to parsers

CS 536 Midterm Exam Spring 2013

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

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

Compiler construction in4303 lecture 3

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

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

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

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

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

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

CSE431 Translation of Computer Languages

Context-free grammars (CFG s)

Chapter 4: LR Parsing

Syntax-Directed Translation. Lecture 14

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

Parsing Algorithms. Parsing: continued. Top Down Parsing. Predictive Parser. David Notkin Autumn 2008

Topdown parsing with backtracking

Sometimes an ambiguous grammar can be rewritten to eliminate the ambiguity.

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.

CS308 Compiler Principles Syntax Analyzer Li Jiang

CS 321 Programming Languages and Compilers. VI. Parsing

Lecture Notes on Top-Down Predictive LL Parsing

LR Parsing LALR Parser Generators

Parser. Larissa von Witte. 11. Januar Institut für Softwaretechnik und Programmiersprachen. L. v. Witte 11. Januar /23

Syntactic Analysis. Chapter 4. Compiler Construction Syntactic Analysis 1

Parsing. Lecture 11: Parsing. Recursive Descent Parser. Arithmetic grammar. - drops irrelevant details from parse tree

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

Transcription:

Compilers

Like recursive-descent but parser can predict which production to use By looking at the next fewtokens No backtracking Predictive parsers accept LL(k) grammars L means left-to-right scan of input L means leftmost derivation k means predict based on k tokens of lookahead In practice, LL(1) is used

In recursive descent, At each step, many choices of production touse Backtracking used to undo badchoices In LL(1), At each step, only one choice of production That is: When a non-terminal A is leftmost in a derivation The next input symbol is t There is either a unique production A α to use Or no production to use (an error state)

Recall the grammar Hard to predict because For T two productions start with int For E itis not clear how to predict We need to left-factor the grammar

Recall the grammar Factor out common prefixes of productions

Choose the alternative that correctlyleft factors if statements in the given grammar EXPR if true then {EXPR } if false then { EXPR} iftrue then { EXPR } else { EXPR } iffalse then { EXPR } else { EXPR } EXPR if BOOL then { EXPR} if BOOL then { EXPR } else { EXPR} BOOL EXPR EXPR BOOL true false EXPR EXPR else { EXPR} if BOOL then { EXPR} true false EXPR if BOOLEXPR EXPR then { EXPR} then { EXPR } else { EXPR} BOOL true false EXPR EXPR BOOL if BOOL then { EXPR } EXPR else { EXPR } true false

Choose the alternative that correctlyleft factors if statements in the given grammar EXPR if true then {EXPR } if false then { EXPR} iftrue then { EXPR } else { EXPR } iffalse then { EXPR } else { EXPR } EXPR if BOOL then { EXPR} if BOOL then { EXPR } else { EXPR} BOOL EXPR EXPR BOOL true false EXPR EXPR else { EXPR} if BOOL then { EXPR} true false EXPR if BOOLEXPR EXPR then { EXPR} then { EXPR } else { EXPR} BOOL true false EXPR EXPR BOOL if BOOL then { EXPR } EXPR else { EXPR } true false

Left-factored grammar E TX T (E) int Y The LL(1) parsing table: X + E Y * T next input token int * + ( ) $ E T X T X X + E T int Y ( E ) Y * T leftmost non-terminal rhs of production to use

Consider the [E, int] entry When current non-terminal is E and next input is int, use production E TX This can generate an int in the first position int * + ( ) $ E T X T X X + E T int Y ( E ) Y * T

Consider the [Y,+] entry When current non-terminal is Y and current token is +, get rid of Y Y can be followed by + only if Y int * + ( ) $ E T X T X X + E T int Y ( E ) Y * T

Blank entries indicate error situations Consider the [E,*] entry There is no way to derive a string starting with* from non-terminal E int * + ( ) $ E T X T X X + E T int Y ( E ) Y * T

Method similar to recursive descent,except For the leftmost non-terminal S We look at the next input tokena And choose the production shown at [S,a] A stack records frontier of parse tree Non-terminals that have yet to beexpanded Terminals that have yet to matched against theinput Top of stack = leftmost pending terminal or non-terminal Reject on reaching error state Accept on end of input &empty stack

initialize stack = <S $> and next repeat case stack of <X, rest> : if T[X,*next] = Y 1 Y n then stack <Y 1 Y n rest>; else error (); <t, rest> : if t == *next ++ then stack <rest>; else error (); until stack == < >

initialize stack = <S $> and next repeat case stack of <X, rest> : if T[X,*next] = Y 1 Y n then stack <Y 1 Y n rest>; else error (); <t, rest> : if t == *next ++ then stack <rest>; else error (); until stack == < > For terminal t on top of stack, check t matches next input token. $ marks bottom of stack For non-terminal X on top of stack, look up production Pop X, push production rhs on stack. Note leftmost symbol of rhs is on top of the stack.

Stack Input Action E $ int * int $ TX

Stack Input Action E $ int * int $ TX T X $ int * int $ int Y

Stack Input Action E $ int * int $ TX T X $ int * int $ int Y int Y X$ int * int $ terminal

Stack Input Action E $ int * int $ TX T X $ int * int $ int Y int Y X $ int * int $ terminal Y X $ * int $ * T

Stack Input Action E $ int * int $ TX T X $ int * int $ int Y int Y X $ int * int $ terminal Y X $ * int $ * T * T X $ * int $ terminal

Stack Input Action E $ int * int $ TX T X $ int * int $ int Y int YX $ int * int $ terminal Y X $ * int $ * T * T X $ * int $ terminal T X $ int $ int Y

Stack Input Action E $ int * int $ TX T X $ int * int $ int Y int Y X $ int * int $ terminal Y X $ * int $ * T * T X $ * int $ terminal T X $ int $ int Y int Y X $ int $ terminal

Stack Input Action E $ int * int $ TX T X $ int * int $ int Y int Y X $ int * int $ terminal YX $ * int $ * T * T X $ * int $ terminal T X $ int $ int Y int Y X $ int $ terminal YX $ $

Stack Input Action E $ int * int $ TX T X $ int * int $ int Y int YX $ int * int $ terminal YX $ * int $ * T * T X $ * int $ terminal T X $ int $ int Y int Y X $ int $ terminal YX $ $ X $ $

Stack Input Action E $ int * int $ TX T X $ int * int $ int Y int Y X $ int * int $ terminal Y X $ * int $ * T * T X $ * int $ terminal T X $ int $ int Y int Y X $ int $ terminal YX $ $ X $ $ $ $ ACCEPT

Choose the next parse state given the grammar, parse table,and current state below. The initial string is: if true then { true } else { if false then { false } } $ Current if then else { } true false $ E if B then { E}E B B E else { E} B true false Stack E $ $ Input else { if false then { false } } $ $ else {E } $ else { if false then { false } } $ E } $ iffalse then { false } } $ else {if B then {E } E } $ else { if false then { false } } $ E if B then { E} E B E else { E } B true false

Choose the next parse state given the grammar, parse table,and current state below. The initial string is: if true then { true } else { if false then { false } } $ Current if then else { } true false $ E if B then { E}E B B E else { E} B true false Stack E $ $ Input else { if false then { false } } $ $ else {E } $ else { if false then { false } } $ E } $ iffalse then { false } } $ else {if B then {E } E } $ else { if false then { false } } $ E if B then { E } E B E else { E } B true false

Compilers First Sets AlexAiken

First Sets Consider non-terminal A, productiona, & token t T[A,t] = in twocases: If * t can derive a t inthe first position We say that t First( ) If A and * and S * A t Useful if stack has A, input is t, and A cannot derive t In this case only option is to get rid of A (by deriving ) Can work only if t can follow A in at least one derivation We say t Follow(A) AlexAiken

First Sets Definition First(X) = { t X * t } { X * } Algorithm sketch: 1. First(t) = {t} 2. First(X) if X if X A 1 A n and First(A i ) for 1 i n 3. First( ) First(X) if X A 1 A n and First(A i ) for 1 i n AlexAiken

First Sets Recall the grammar AlexAiken

Compilers Follow Sets AlexAiken

Follow Sets Definition: Follow(X) = { t S * X t } Intuition If X A B then First(B) Follow(A) and Follow(X) Follow(B) if B * then Follow(X) Follow(A) If S is the start symbol then $ Follow(S) AlexAiken

Follow Sets Algorithm sketch: 1. $ Follow(S) 2. First( ) - { } Follow(X) For each production A X 3. Follow(A) Follow(X) For each production A X where First( ) AlexAiken

Follow Sets Recall the grammar AlexAiken

Compilers LL(1) Parsing Tables

LL(1) Parsing Tables Construct a parsing table T for CFG G For each production A in G do: For each terminal t First( ) do T[A, t] = If First( ), for each t Follow(A) do T[A, t] = If First( ) and $ Follow(A) do T[A, $] =

E TX T (E ) int Y X + E Y * T LL(1) Parsing Tables int * + ( ) $ E T X T X X + E T int Y ( E ) Y * T

S S a b First( S ) = { b } Follow( S ) = { $, a } LL(1) Parsing Tables S a b $ b,sa

LL(1) Parsing Tables If any entry is multiply defined then G is not LL(1) If G is ambiguous If G is left recursive If G is not left-factored And in other cases as well Most programming language CFGs are not LL(1)

LL(1) Grammars LL(1) grammars == they have no multiply-defined entries in the parsing table. Properties of LL(1) grammars: Grammar can t be ambiguous or left recursive Grammar is LL(1) when for all A 1. First( ) First( ) = ; besides, only one of or can derive 2. if derives, then Follow(A) First( ) = Note: It may not be possible for a grammar to be manipulated into an LL(1) grammar

Compilers Transition Diagrams

Transition Diagrams Unlike lexical equivalents, each edge represents a token Transition implies: if token, match input else call proc

Transition Diagrams

Transition Diagrams

Transition Diagrams

Transition Diagrams

Transition Diagrams

Similar steps for T and T Transition Diagrams

Simplified Transition diagrams Transition Diagrams