Computing PREDICT and FOLLOW sets

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

Types of parsing. CMSC 430 Lecture 4, Page 1

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

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.

Compilers. Predictive Parsing. Alex Aiken

Concepts of Programming Languages Recitation 1: Predictive Parsing

CS Parsing 1

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

Syntax Analysis, III Comp 412

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

Table-Driven Parsing

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

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

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

Syntax Analysis, III Comp 412

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

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Extra Credit Question

CSCI312 Principles of Programming Languages

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

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

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

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.

COP 3402 Systems Software Top Down Parsing (Recursive Descent)

Syntactic Analysis. Top-Down Parsing

CS502: Compilers & Programming Systems

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

a. Determine the EPS, FIRST and FOLLOWs set for this grammar.

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

Class 6: Efficiency in Scheme

Chapter 3. Parsing #1

A simple syntax-directed

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Top down vs. bottom up parsing

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

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

Abstract Syntax Trees & Top-Down Parsing

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

CSC 4181 Compiler Construction. Parsing. Outline. Introduction

3. Parsing. Oscar Nierstrasz

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

CA Compiler Construction

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

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

1 Parsing (25 pts, 5 each)

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 }

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

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

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

Recursive Descent Parsers

Abstract Syntax Trees & Top-Down Parsing

Abstract Syntax Trees & Top-Down Parsing

Chapter 3: CONTEXT-FREE GRAMMARS AND PARSING Part 1

Parsing II Top-down parsing. Comp 412

ECE 468/573 Midterm 1 October 1, 2014

LECTURE 7. Lex and Intro to Parsing

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

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

Principles of Programming Languages COMP251: Syntax and Grammars

CS143 Midterm Sample Solution Fall 2010

UNIVERSITY OF CALIFORNIA

Defining syntax using CFGs

Monday, September 13, Parsers

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

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

Chapter 2 - Programming Language Syntax. September 20, 2017

Introduction; Parsing LL Grammars

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

COP 3402 Systems Software Syntax Analysis (Parser)

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

CSE431 Translation of Computer Languages

Language Processing note 12 CS

LL Parsing: A piece of cake after LR

CSE 401 Midterm Exam Sample Solution 2/11/15

Compila(on (Semester A, 2013/14)

COP4020 Programming Assignment 2 - Fall 2016

Part 3. Syntax analysis. Syntax analysis 96

CS 321 Programming Languages and Compilers. VI. Parsing

ECE 468/573 Midterm 1 September 30, 2015

Lecture 10 Parsing 10.1

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Lecture Bottom-Up Parsing

Lecturer: William W.Y. Hsu. Programming Languages

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

Lexical and Syntax Analysis. Top-Down Parsing

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

Downloaded from Page 1. LR Parsing

CS 314 Principles of Programming Languages

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

Homework & Announcements

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

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

Context-Free Languages. Wen-Guey Tzeng Department of Computer Science National Chiao Tung University

CS 406/534 Compiler Construction Parsing Part I

Building Compilers with Phoenix

It parses an input string of tokens by tracing out the steps in a leftmost derivation.

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

2068 (I) Attempt all questions.

Transcription:

Computing PREDICT and FOLLOW sets SI 413 - Programming Languages and Implementation Dr. Daniel S. Roche United States Naval Academy Fall 2011 Roche (USNA) SI413 PREDICT & FOLLOW Fall 2011 1 / 9

Set Definitions Given any CFG in BNF (no alternation or Kleene,+), we can construct the following sets. Each set contains only tokens, and FIRST(A), FOLLOW(A), and PREDICT(A) are defined for every non-terminal A in the language. EPS: All non-terminals that could expand to ɛ, the empty string FIRST(A): The set of tokens that could appear as the first token in an expansion of A FOLLOW(A): The set of tokens that could appear immediately after A in an expansion of S, the start symbol. PREDICT(A): The set of tokens that could appear next in a valid parse of a string in the language, when the next non-terminal in the parse tree is A. Each set is defined using mutual recursion. Roche (USNA) SI413 PREDICT & FOLLOW Fall 2011 2 / 9

Do we need FIRST? For top-down parsing, the only sets we really need are the PREDICT sets. It turns out the FIRST sets aren t necessary to compute PREDICT. So (for now) we will forget about FIRST and just worry about the other three (EPS, PREDICT, FOLLOW). Roche (USNA) SI413 PREDICT & FOLLOW Fall 2011 3 / 9

EPS Definition: EPS contains all non-terminals that could expand to ɛ, the empty string EPS contains: 1 Any non-terminal that has an epsilon production 2 Any non-terminal that has a production containing only non-terminals that are all in EPS Roche (USNA) SI413 PREDICT & FOLLOW Fall 2011 4 / 9

PREDICT Definition: PREDICT(A) contains all tokens that could appear next on the token stream when we are expecting an A. PREDICT(A) contains: 1 Any token that appears as the leftmost symbol in a production rule for A. 2 For every non-terminal B that appears as the leftmost symbol in a production rule for A, every token in PREDICT(B). 3 If A EPS, every token in FOLLOW(A). Roche (USNA) SI413 PREDICT & FOLLOW Fall 2011 5 / 9

FOLLOW Definition: FOLLOW(A) contains all tokens that could come right after A in a valid parse. FOLLOW(A) contains: 1 Any token that immediately follows A on any right-hand side of a production 2 For any non-terminal B that immediately follows A on any right-hand side of a production, every token in PREDICT(B) 3 For any non-terminal B such that A appears right-most in a right-hand side of a production of B, every token in FOLLOW(B). There is also the special rule for the start symbol that FOLLOW(S) always contains $, the end-of-file token. Roche (USNA) SI413 PREDICT & FOLLOW Fall 2011 6 / 9

Example: Calculator Language Here s the LL(1) grammar for the calculator language: S exp STOP exp term exptail exptail ɛ OPA term exptail term sfactor termtail termtail ɛ OPM factor termtail sfactor OPA factor factor factor NUM LP exp RP Roche (USNA) SI413 PREDICT & FOLLOW Fall 2011 7 / 9

EPS = {} S $ exp exptail term termtail sfactor factor

S $ exp exptail term termtail sfactor factor First construct EPS; we just have to use Rule 1.

S $ exp exptail OPA term termtail OPM sfactor OPA factor NUM, LP Apply Rule 1 for PREDICT in four places

S $ exp STOP,RP exptail OPA term termtail OPM sfactor OPA factor NUM, LP Apply Rule 1 for FOLLOW in two places

S $ exp STOP,RP exptail OPA STOP,RP term termtail OPM sfactor OPA factor NUM, LP Apply Rule 3 for FOLLOW to exptail

S $ exp STOP,RP exptail OPA,STOP,RP STOP,RP term termtail OPM sfactor OPA factor NUM, LP Apply Rule 3 for PREDICT to exptail

S $ exp STOP,RP exptail OPA,STOP,RP STOP,RP term OPA,STOP,RP termtail OPM sfactor OPA factor NUM, LP Apply Rule 2 for FOLLOW to term

S $ exp STOP,RP exptail OPA,STOP,RP STOP,RP term OPA,STOP,RP termtail OPM OPA,STOP,RP sfactor OPA factor NUM, LP Apply Rule 3 for FOLLOW to termtail

S $ exp STOP,RP exptail OPA,STOP,RP STOP,RP term OPA,STOP,RP termtail OPM,OPA,STOP,RP OPA,STOP,RP sfactor OPA factor NUM, LP Apply Rule 3 for PREDICT to termtail

S $ exp STOP,RP exptail OPA,STOP,RP STOP,RP term OPA,STOP,RP termtail OPM,OPA,STOP,RP OPA,STOP,RP sfactor OPA OPM,OPA,STOP,RP factor NUM, LP OPM,OPA,STOP,RP Apply Rule 2 for FOLLOW in two places

S OPA,NUM,LP $ exp OPA,NUM,LP STOP,RP exptail OPA,STOP,RP STOP,RP term OPA,NUM,LP OPA,STOP,RP termtail OPM,OPA,STOP,RP OPA,STOP,RP sfactor OPA,NUM,LP OPM,OPA,STOP,RP factor NUM, LP OPM,OPA,STOP,RP Apply Rule 2 for PREDICT from the bottom up

S OPA,NUM,LP $ exp OPA,NUM,LP STOP,RP exptail OPA,STOP,RP STOP,RP term OPA,NUM,LP OPA,STOP,RP termtail OPM,OPA,STOP,RP OPA,STOP,RP sfactor OPA,NUM,LP OPM,OPA,STOP,RP factor NUM, LP OPM,OPA,STOP,RP Check that none of the rules add anything to any set.

Tagging set elements For use in top-down parsing, every symbol in EPS and in any PREDICT set is tagged with a production rule, as follows: Every non-terminal in EPS is tagged with the rule that gives an ɛ-production for that non-terminal. Every token in PREDICT(A) that was added from Rule 1 or Rule 2 on Slide 5 is tagged with the production of A that brought in that token. Every token in PREDICT(A) that was added from Rule 3 on Slide 5 is tagged with the tag on A in EPS; that is, the rule that gives the ɛ-production of A. These tags indicate what the top-down parser should do when it expects A and sees a token in PREDICT(A). Roche (USNA) SI413 PREDICT & FOLLOW Fall 2011 9 / 9