Concepts of Programming Languages Recitation 1: Predictive Parsing

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

Table-Driven Parsing

CA Compiler Construction

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

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

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

CS 132 Compiler Construction, Fall 2012 Instructor: Jens Palsberg Multiple Choice Exam, Nov 6, 2012

Computing PREDICT and FOLLOW sets

Top down vs. bottom up parsing

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

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

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

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

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

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

CSE 401 Midterm Exam Sample Solution 2/11/15

CS 314 Principles of Programming Languages

Recursive Descent Parsers

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

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

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

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

Compilers. Predictive Parsing. Alex Aiken

Question Points Score

LECTURE 3. Compiler Phases

CS502: Compilers & Programming Systems

Language Processing note 12 CS

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

2068 (I) Attempt all questions.

RYERSON POLYTECHNIC UNIVERSITY DEPARTMENT OF MATH, PHYSICS, AND COMPUTER SCIENCE CPS 710 FINAL EXAM FALL 96 INSTRUCTIONS

Automatic generation of LL(1) parsers

BSCS Fall Mid Term Examination December 2012

LL(1) Grammars. Example. Recursive Descent Parsers. S A a {b,d,a} A B D {b, d, a} B b { b } B λ {d, a} D d { d } D λ { a }

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

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

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

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

Syntax Analysis, III Comp 412

Downloaded from Page 1. LR Parsing

Chapter 3. Parsing #1

Syntactic Analysis. Top-Down Parsing

Syntax Analysis, III Comp 412

Types of parsing. CMSC 430 Lecture 4, Page 1

Lecture 6: Top-Down Parsing. Beating Grammars into Programs. Expression Recognizer with Actions. Example: Lisp Expression Recognizer

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

Page No 1 (Please look at the next page )

Programming Languages (CS 550) Lecture 4 Summary Scanner and Parser Generators. Jeremy R. Johnson

Abstract Syntax Trees & Top-Down Parsing

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Lexical and Syntax Analysis. Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

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

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

Lexical and Syntax Analysis

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

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

1. Consider the following program in a PCAT-like language.

Chapter 2 Syntax Analysis

Compiler construction in4303 lecture 3

Defining syntax using CFGs

Outline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction

Syntax-Directed Translation. Lecture 14

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

CSCI312 Principles of Programming Languages

3. Parsing. Oscar Nierstrasz

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

CS 4120 Introduction to Compilers

Introduction to parsers

JavaCC Parser. The Compilation Task. Automated? JavaCC Parser

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

Review. Pat Morin COMP 3002

Question Marks 1 /20 2 /16 3 /7 4 /10 5 /16 6 /7 7 /24 Total /100

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

Lecture Notes on Bottom-Up LR Parsing

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

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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Chapter 4: Syntax Analyzer

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.

Introduction to Parsing. Lecture 8

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

Wednesday, August 31, Parsers

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

Syntax Analysis Check syntax and construct abstract syntax tree

CS 536 Midterm Exam Spring 2013

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

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

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

LL parsing Nullable, FIRST, and FOLLOW

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

Building A Recursive Descent Parser. Example: CSX-Lite. match terminals, and calling parsing procedures to match nonterminals.

LECTURE 7. Lex and Intro to Parsing

Syntax Analysis Part I

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

COP 3402 Systems Software Syntax Analysis (Parser)

Lecture Notes on Bottom-Up LR Parsing

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

A simple syntax-directed

First Midterm Exam CS164, Fall 2007 Oct 2, 2007

Transcription:

Concepts of Programming Languages Recitation 1: Predictive Parsing Oded Padon odedp@mail.tau.ac.il Reference: Modern Compiler Implementation in Java by Andrew W. Appel Ch. 3

Administrative Course website and forum (not using moodle): http://cs.tau.ac.il/~msagiv/courses/pl17.html https://groups.google.com/a/mail.tau.ac.il/d/forum/taupl17-group Recitation groups: Wednesday 13:10-14:00 Melamed Wednesday 14:10-15:00 Melamed Wednesday 15:10-16:00 Melamed Grade: 50% Exercises, 50% Exam Exercises: all mandatory, teams of 1 or 2 or 3 students First exercise is published My reception hours: Wednesdays, 17:00-18:00 email before you come!

Role of Parsing

Context Free Grammars Terminals (tokens) Non-terminals Start non-terminal Derivation rules (also called productions) <Non-Terminal> Symbol Symbol Symbol

Example Context Free Grammar Terminals (tokens) Non-terminals Start non-terminal 1 <S> <S> ; <S> 2 <S> id := <E> 3 <S> print (<L>) 4 <E> id 5 <E> num 6 <E> <E> + <E> 7 <E> (<S>, <E>) 8 <L> <E> 9 <L> <L>, <E>

Example Parse Tree <S> <S> ; <S> <S> <S> ; id := E <S> ; <S> id := <E> ; id := <E> id := num ; id := <E> id := <E> id := <E> id := num ; id := <E> + <E> id := num ; id := <E> + num num <E> + <E> id := num ; id :=num + num num num

Ambiguous Grammars Two leftmost derivations Two rightmost derivations Two parse trees 1 <E> <E> + <E> 2 <E> <E> * <E> 3 <E> id 4 <E> (<E>) <E> a + b + c <E> id+id+id <E> + <E> <E> + <E> id:a id:b + id:c id:a + id:b id:c

Non Ambiguous Grammars for Arithmetic Expressions Ambiguous grammar: 1 <E> <E> + <E> 2 <E> <E> * <E> 3 <E> id 4 <E> (<E>) Non-Ambiguous grammar: 1 <E> <E> + <T> 2 <E> <T> 3 T <T> * <F> 4 T <F> 5 F id 6 F (<E>)

Non-Ambiguous grammar: 1 <E> <E> + <T> 2 <E> <T> 3 T <T> * <F> 4 T <F> 5 F id 6 F (<E>) Non Ambiguous Grammars for Arithmetic Expressions <E> <T> <F> id:a <E> <E> + <T> + <T> <F> <F> id:b id:c a + b + c id+id+id

Predictive Parsing LL(1) Left to right, Left most derivation, 1 token look ahead We must uniquely predict the next rule by the current nonterminal and 1 next token Grammar must be non ambiguous Grammar must not contain left recursion (right recursion is ok) Grammar must be left factored These conditions are necessary but not sufficient

<S> id := <E> <S> if (<E>) <S> else <S> <E> <T> <EP> <T> id (<E>) <EP> + <E> Example Predictive Parser def parse_s(): if next(id): match(id) match(assign) parse_e() elif next(if_tok): match(if_tok) match(lp) parse_e() match(rp) parse_s() match(else_tok) parse_s() else: syntax_error() def parse_e(): parse_t() parse_ep() def parse_t(): if next(id): match(id) elif next(lp): match(lp) parse_e() match(rp) else: syntax_error() def parse_ep(): if next(plus): match(plus) parse_e() elif (next(rp) or next(else_tok) or next(eof)): return # else: syntax_error()

<S> id := <E> <S> if (<E>) <S> else <S> <E> <T> <EP> <T> id (<E>) <EP> + <E> Example Predictive Parser <S> <S> ; <S> def parse_s(): if next(id): match(id) match(assign) parse_e() elif next(if_tok): match(if_tok) match(lp) parse_e() match(rp) parse_s() match(else_tok) parse_s() else: syntax_error() def parse_e(): parse_t() parse_ep() def parse_t(): if next(id): match(id) elif next(lp): match(lp) parse_e() match(rp) else: syntax_error() def parse_ep(): if next(plus): match(plus) parse_e() elif (next(rp) or next(else_tok) or next(eof)): return # else: syntax_error()

Nullable Non-Terminals Non-terminal A is nullable iff A * Example: <S> id := <E> <S> if (<E>) <S> else <S> <E> <T> <EP> <T> id (<E>) <EP> + <E> Is S nullable? Is E nullable? Is T nullable? Is EP nullable?

Computing Nullable Non-terminal A nullable iff A * Nulllable = for each rule A : add A to Nullable while changes occur do: for each rule A A 1 A 2 A n : if {A 1, A 2,, A n } Nullable: add A to Nullable

Example Computing Nullable <S> id := <E> <S> if (<E>) <S> else <S> <E> <T> <EP> <T> id (<E>) <EP> + <E> Nulllable = for each rule A : add A to Nullable while changes occur do: for each rule A A 1 A 2 A n : if {A 1, A 2,, A n } Nullable: add A to Nullable Nulllable = Nullable = {EP} Fix point reached

First Sets First( ) set of all tokens that can be the start of First( ) = { t : * t } Example: <S> id := <E> <S> if (<E>) <S> else <S> <E> <T> <EP> <T> id (<E>) <EP> + <E> First(S) = First(E) = First(T) = First(EP) =

Computing First First( ) set of all tokens that can be the start of First( ) = { t : * t } For each token t: First(t) = {t} For each non-terminal A: First(A) = while changes occur do: for each rule A A 1 A 2 A n : for each 1 i n: if {A 1, A 2,, A i-1 } Nullable: add First(A i ) to First(A) For convenience, we defined First(t) = {t} for tokens

Example Computing First 1 <S> id := <E> 2 <S> if (<E>) <S> else <S> 3 <E> <T> <EP> 4,5 <T> id (<E>) 6,7 <EP> + <E> Nullable = {EP} For each token t: First(t) = {t} For each non-terminal A: First(A) = while changes occur do: for each rule A A 1 A 2 A n : for each 1 i n: if {A 1, A 2,, A i-1 } Nullable: add First(A i ) to First(A) First(t) = {t} for t in {id, ass, if, lp, rp, else, plus} add id to First(S) # 1 add if to First(S) # 2 # 3 add id to First(T) # 4 add lp to First(T) # 5 # 6 add plus to First(EP) # 7 add id,lp to first(e) # 3 Fix point reached A S E T EP First(A) id, if id, lp id, lp plus

Follow Sets Follow(A) set of all tokens that can appear after A Follow(A) = { t, : <S> * <A> t } Example: <S> id := <E> <S> if (<E>) <S> else <S> <E> <T> <EP> <T> id (<E>) <EP> + <E> Follow(S) = Follow(E) = Follow(T) = Follow(EP) =

Computing Follow Follow(A) set of all tokens that can appear after A Follow(A) = { t, : <S> * <A> t } For each non-terminal A: Follow(A) = For the start non-terminal S: Follow(S) = {eof} while changes occur do: for each rule <A> A 1 A 2 A n : for each 1 i n: if {A i+1,, A n } Nullable: add Follow(A) to Follow(A i ) for each 1 i < j n: if {A i+1,, A j-1 } Nullable: add First(A j ) to Follow(A i )

1 <S> id := <E> 2 <S> if (<E>) <S> else <S> 3 <E> <T> <EP> 4,5 <T> id (<E>) 6,7 <EP> + <E> Example Computing Follow Nullable = {EP} First(S) = {id, if} First(E) = First(T) = {id, lp} First(EP) = {plus} Follow(A) = for A in {E, T, EP} Follow(S) = {eof} For each non-terminal A S: Follow(A) = For the start non-terminal: Follow(S) = {eof} while changes occur do: for each rule <A> A 1 A 2 A n : for each 1 i n: if {A i+1,, A n } Nullable: add Follow(A) to Follow(A i ) for each 1 i < j n: if {A i+1,, A j-1 } Nullable: add First(A j ) to Follow(A i ) add eof to Follow(E) # 1 add rp to Follow(E) # 2 add else to Follow(S) # 2 add eof, rp to Follow(T) # 3 add eof, rp to Follow(EP) # 3 add plus to Follow(T) # 3 add else to Follow(E) # 1 add else to Follow(T) # 3 add else to Follow(EP) # 3 Fix point reached A S E T EP Follow(A) eof, else eof, rp, else eof, rp, plus, else eof, rp, else

Select Sets For each rule A : If is not nullable: Select(A ) = First( ) If is nullable: Select(A ) = First( ) Follow(A) If we need to parse A, we can decide which rule to apply according to the next token and the select sets The grammar is LL(1) iff: for every two grammar rules <A> and <A> : Select(A ) Select(A ) =

<S> id := <E> <S> if (<E>) <S> else <S> <E> <T> <EP> <T> id (<E>) <EP> + <E> Example Computing Select A Nullable First(A) Follow(A) S no id, if eof, else For each rule A : If is not nullable: Select(A ) = First( ) If is nullable: Select(A ) = First( ) Follow(A) E no id, lp eof, rp, else T no id, lp eof, rp, plus, else EP yes plus eof, rp, else A Select(A ) <S> id := <E> id <S> if (<E>) <S> else <S> if <E> <T> <EP> id, lp <T> id <T> (<E>) <EP> <EP> + <E> id lp eof, rp, else plus

Generating a Predictive Parser Compute: Nullable, First, Follow, Select If the grammar is not LL(1) report an error Create a procedure for every non-terminal A: For every rule A : If the next token is in Select(A ), apply the rule <A> : this contains recursive calls to procedures of other non-terminals If the next token is not in any Select(A ), report syntax error

Putting It All Together def parse_s(): if next(id): match(id) match(assign) parse_e() elif next(if_tok): match(if_tok) match(lp) parse_e() match(rp) parse_s() match(else_tok) parse_s() else: syntax_error() def parse_e(): parse_t() parse_ep() <S> id := <E> def parse_t(): if next(id): match(id) elif next(lp): match(lp) parse_e() match(rp) else: syntax_error() A Select(A ) <S> if (<E>) <S> else <S> <E> <T> <EP> <T> id <T> (<E>) <EP> <EP> + <E> id if id, lp id lp eof, rp, else plus def parse_ep(): if next(plus): match(plus) parse_e() elif (next(rp) or next(else_tok) or next(eof)): return # else: syntax_error()