CS453 Intro and PA1 1

Similar documents
Predictive parsing as a specific subclass of recursive descent parsing complexity comparisons with general parsing

Homework #7. True False. d. Given a CFG, G, and a string w, it is decidable whether w ε L(G) True False

Parsing Algorithms. CS 4447/CS Stephen Watt University of Western Ontario

Turing Machines. Human-aware Robotics. 2017/10/17 & 19 Chapter 3.2 & 3.3 in Sipser Ø Announcement:

READING STATECHART DIAGRAMS

CHAPTER 2 Algebraic Expressions and Fundamental Operations

Differentiation Applications 1: Related Rates

CS1150 Principles of Computer Science Loops

Compiling Techniques

Follow sets. LL(1) Parsing Table

Administrivia. Test I during class on 10 March. Bottom-Up Parsing. Lecture An Introductory Example

Compiler Construction Lent Term 2015 Lectures (of 16)

Tutorial 3: Building a spectral library in Skyline

Medium Scale Integrated (MSI) devices [Sections 2.9 and 2.10]

Compiler Construction Lent Term 2015 Lectures (of 16)

Context Free Grammars

Syntactic Analysis. Top-Down Parsing

Five Whys How To Do It Better

Compiling Techniques

Computing if a token can follow

6.3: Volumes by Cylindrical Shells

NUMBERS, MATHEMATICS AND EQUATIONS

CONSTRUCTING STATECHART DIAGRAMS

CHM112 Lab Graphing with Excel Grading Rubric

Compiling Techniques

Experiment #3. Graphing with Excel

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

Eng. Maha Talaat Page 1

This section is primarily focused on tools to aid us in finding roots/zeros/ -intercepts of polynomials. Essentially, our focus turns to solving.

CS153: Compilers Lecture 5: LL Parsing

CKY & Earley Parsing. Ling 571 Deep Processing Techniques for NLP January 13, 2016

[COLLEGE ALGEBRA EXAM I REVIEW TOPICS] ( u s e t h i s t o m a k e s u r e y o u a r e r e a d y )

Computer Science 160 Translation of Programming Languages

5 th grade Common Core Standards

Syntax Analysis Part I. Position of a Parser in the Compiler Model. The Parser. Chapter 4

, which yields. where z1. and z2

Top-Down Parsing and Intro to Bottom-Up Parsing

Finite Automata. Human-aware Robo.cs. 2017/08/22 Chapter 1.1 in Sipser

CAUSAL INFERENCE. Technical Track Session I. Phillippe Leite. The World Bank

Activity Guide Loops and Random Numbers

1 The limitations of Hartree Fock approximation

37 Maxwell s Equations

Announcement: Midterm Prep

Compiler Construction Lectures 13 16

TP1 - Introduction to ArcGIS

Name: Period: Date: ATOMIC STRUCTURE NOTES ADVANCED CHEMISTRY

Syntax Analysis Part I

Syntax Analysis Part III

Chapter Summary. Mathematical Induction Strong Induction Recursive Definitions Structural Induction Recursive Algorithms

EXAM. CS331 Compiler Design Spring Please read all instructions, including these, carefully

AP Physics Kinematic Wrap Up

Computer Science 160 Translation of Programming Languages

Graduate AI Lecture 16: Planning 2. Teachers: Martial Hebert Ariel Procaccia (this time)

Name: Period: Date: PERIODIC TABLE NOTES ADVANCED CHEMISTRY

Section 6-2: Simplex Method: Maximization with Problem Constraints of the Form ~

Name: Period: Date: PERIODIC TABLE NOTES HONORS CHEMISTRY

Compiling Techniques

Computational modeling techniques

CHAPTER 3 INEQUALITIES. Copyright -The Institute of Chartered Accountants of India

THE LIFE OF AN OBJECT IT SYSTEMS

Weathering. Title: Chemical and Mechanical Weathering. Grade Level: Subject/Content: Earth and Space Science

Parsing VI LR(1) Parsers

20 Faraday s Law and Maxwell s Extension to Ampere s Law

SUPPLEMENTARY MATERIAL GaGa: a simple and flexible hierarchical model for microarray data analysis

Syntax Analysis Part I

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

Hypothesis Tests for One Population Mean

CS 477/677 Analysis of Algorithms Fall 2007 Dr. George Bebis Course Project Due Date: 11/29/2007

x 1 Outline IAML: Logistic Regression Decision Boundaries Example Data

Floating Point Method for Solving Transportation. Problems with Additional Constraints

Purpose: Use this reference guide to effectively communicate the new process customers will use for creating a TWC ID. Mobile Manager Call History

Context-Free Grammars (and Languages) Lecture 7

ALE 21. Gibbs Free Energy. At what temperature does the spontaneity of a reaction change?

Introduction to Spacetime Geometry

Lesson Plan. Recode: They will do a graphic organizer to sequence the steps of scientific method.

CSC 4181Compiler Construction. Context-Free Grammars Using grammars in parsers. Parsing Process. Context-Free Grammar

Thermodynamics and Equilibrium

You need to be able to define the following terms and answer basic questions about them:

Pipetting 101 Developed by BSU CityLab

Checking the resolved resonance region in EXFOR database

Lecture VII Part 2: Syntactic Analysis Bottom-up Parsing: LR Parsing. Prof. Bodik CS Berkley University 1

Prof. Mohamed Hamada Software Engineering Lab. The University of Aizu Japan

Revisiting the Socrates Example

CA Compiler Construction

Review. Earley Algorithm Chapter Left Recursion. Left-Recursion. Rule Ordering. Rule Ordering

Lecture 11 Context-Free Languages

Administrativia. Assignment 1 due thursday 9/23/2004 BEFORE midnight. Midterm exam 10/07/2003 in class. CS 460, Sessions 8-9 1

PHYS 314 HOMEWORK #3

COMP 551 Applied Machine Learning Lecture 5: Generative models for linear classification

Admin. MDP Search Trees. Optimal Quantities. Reinforcement Learning

When a substance heats up (absorbs heat) it is an endothermic reaction with a (+)q

ANSWER KEY FOR MATH 10 SAMPLE EXAMINATION. Instructions: If asked to label the axes please use real world (contextual) labels

N 2 (g) + 3H 2 (g) 2NH 3 (g) o Three mole ratios can be derived from the balanced equation above: Example: Li(s) + O 2 (g) Li 2 O(s)

OTHER USES OF THE ICRH COUPL ING CO IL. November 1975

Dispersion Ref Feynman Vol-I, Ch-31

Last Updated: Oct 14, 2017

Fall 2013 Physics 172 Recitation 3 Momentum and Springs

IAML: Support Vector Machines

Purchase Order Workflow Processing

Dataflow Analysis and Abstract Interpretation

Transcription:

Plan fr day Ambiguus Grammars Disambiguating ambiguus grammars Predictive parsing IR and OLLOW sets Predictive Parsing table C453 Lecture p-dwn Predictive Parsers 1 Ambiguus Grammars Ambiguus grammar: >1 parse tree fr 1 sentence xpressin grammar parse tree 1 à à à - à ( ) à ID à NUM tring parse tree 2 42 7 6 what abut 42-7-6? C453 Lecture p-dwn Predictive Parsers 2 (42) (42) Gal: disambiguate the grammar Unambiguus grammar fr simple expressins Cause: the grammar did nt specify the precedence nr the assciativity f the peratrs,-, w ptins: keep the ambiguus grammar, but add extra directives t the parser, s that nly ne tree is frmed (ee PA0.cup fr imple xpressin Language) Rewrite the grammar, making the precedence and assciativity explicit in the grammar. Grammar à - à à ( ) ID NUM tring 4276 parse tree Hw is the precedence encded? (42) Hw is the assciativity encded? C453 Lecture p-dwn Predictive Parsers 3 C453 Lecture p-dwn Predictive Parsers 4 C453 Intr and PA1 1

Augmenting the grammar with nd f ile Grammar defines the syntactically valid strings Parser recgnizes them (same as reg.exp. and scanner) deal with end-f-file we augment the grammar with an end-f-file symbl ($), and create a new start symbl: à $ C453 Lecture p-dwn Predictive Parsers 5 Predictive Parsing Predictive parsing, such as recursive descent parsing, creates the parse tree OP DOWN, starting at the start symbl. r each nn-terminal N there is a methd recgnizing the strings that can be prduced by N, with ne (case) clause fr each prductin. his wrked great fr a slightly changed versin f ur example frm last lecture: start -> stmts O stmts -> ε stmt stmts stmt -> iftmt whiletmt ID = NUM iftmt -> I id { stmts } whiletmt -> WHIL id { stmts } because each clause culd be uniquely identified by lking ahead ne tken. Let s predictively build the parse tree fr if t { while b { x = 6 }} $ C453 Lecture p-dwn Predictive Parsers 6 When Predictive Parsing wrks, when it des nt What abut ur expressin grammar: à - à à ( ) ID NUM he methd cannt decide lking ne tken ahead whether t predict, -, r. ame prblem fr. irst Given a phrase γ f terminals and nn-terminals (a rhs f a prductin), IR(γ) is the set f all terminals that can begin a string derived frm γ. IR() =? IR()=? IR(XYZ) = IR(X)? Predictive parsing wrks fr grammars where the first terminal symbl f each sub expressin prvides enugh infrmatin t decide which prductin t use. NO! X culd prduce ε and then IR(Y) cmes int play we must keep track f which nn terminals are NULLABL C453 Lecture p-dwn Predictive Parsers 7 C453 Lecture p-dwn Predictive Parsers 8 C453 Intr and PA1 2

llw It als turns ut t be useful t determine which terminals can directly fllw a nn terminal X (t decide parsing X is finished). terminal t is in OLLOW(X) if there is any derivatin cntaining Xt. his can ccur if the derivatin cntains XYZt and Y and Z are nullable IR and OLLOW sets NULLABL X is a nnterminal nullable(x) is true if X can derive the empty string IR IR(z) = {z}, where z is a terminal IR(X) = unin f all IR( rhs i ), where X is a nnterminal and X -> rhs i IR(rhs i ) = unin all f IR(sym) n rhs up t and including first nnnullable OLLOW(Y), nly relevant when Y is a nnterminal lk fr Y in rhs f rules (lhs -> rhs) and unin all IR sets fr symbls after Y up t and including first nnnullable if all symbls after Y are nullable then als unin in OLLOW(lhs) C453 Lecture p-dwn Predictive Parsers 9 C453 Lecture p-dwn Predictive Parsers 10 Cnstructive Definitin f nullable, first and fllw fr each terminal t IR(t)={t} Anther ransitive Clsure algrithm: keep ding P until nthing changes P: fr each prductin X à Y 1 Y 2 Y k if Y 1 t Y k nullable (r k = 0) nullable(x) = true fr each i frm 1 t k, each j frm i1 t k 1: if Y 1 Y i-1 nullable (r i=1) IR(X) = IR(Y i ) //: unin 2: if Y i1 Y k nullable (r i=k) OLLOW(Y i ) = OLLOW(X) 3: if Y i1 Y j-1 nullable (r i1=j) OLLOW(Y i ) = IR(Y j ) Class xercise Cmpute nullable, IR and OLLOW fr Z à d X Y Z X à a Y Y à c ε We can cmpute nullable, then IR, and then OLLOW C453 Lecture p-dwn Predictive Parsers 11 C453 Lecture p-dwn Predictive Parsers 12 C453 Intr and PA1 3

Cnstructing the Predictive Parser able A predictive parse table has a rw fr each nn-terminal X, and a clumn fr each input tken t. ntries table[x,t] cntain prductins: fr each X -> gamma fr each t in IR(gamma) table[x,t] = X->gamma if gamma is nullable fr each t in OLLOW(X) Cmpute the predictive parse table fr Z à d X Y Z X à a Y Y à c ε table[x,t] = X->gamma a c d X Xà a Xà Y Xà Y Xà Y Y Yà ε Yà ε Yà ε Yà c Z Zà XYZ Zà XYZ Zà XYZ Zà d Multiple entries in the Predictive parse table: Ambiguity An ambiguus grammar will lead t multiple entries in the parse table. Our grammar I ambiguus, e.g. but als Z à d Zà XYZà YZà d r grammars with n multiple entries in the table, we can use the table t prduce ne parse tree fr each valid sentence. We call these grammars LL(1): Left t right parse, Left-mst derivatin, 1 symbl lkahead. A recursive descent parser examines input left t right. he rder it expands nn-terminals is leftmst first, and it lks ahead 1 tken. C453 Lecture p-dwn Predictive Parsers 13 C453 Lecture p-dwn Predictive Parsers 14 Left recursin and Predictive parsing What happens t the recursive descent parser if we have a left recursive prductin rule, e.g. à calls calls frever eliminate left recursin we rewrite the grammar: frm: t: à - à à à - ε à ( ) ID NUM à à ε à ( ) ID NUM replacing left recursin Xà Xγ α (where α des nt start with X) by right recursin, as X prduces α γ that can be prduced right recursively. Nw we can augment the grammar (à $), cmpute nullable, IR and OLLOW, and prduce an LL(1) predictive parse table, see iger ectin 3.2. C453 Lecture p-dwn Predictive Parsers 15 Left actring Left recursin des nt wrk fr predictive parsing. Neither des a grammar that has a nn-terminal with tw prductins that start with a cmmn phrase, s we left factr the grammar: αβ 1 αβ 2 Left refactr.g.: if statement: à I t HN L I t HN becmes à I t HN X Xà L ε α' ' β 1 β 2 When building the predictive parse table, there will be a multiple entries. WHY? C453 Lecture p-dwn Predictive Parsers 16 C453 Intr and PA1 4

Dangling else prblem: ambiguity Dangling else disambiguatin Given à I t HN X Xà L ε cnstruct tw parse trees fr I t HN I t HN L he crrect parse tree is: I t HN X I t HN X ε L I t HN X I t HN X L ε Which is the crrect parse tree? (C, Java rules) I t HN I t HN We can get this parse tree by remving the Xà ε rule in the multiple entry slt in the parse tree. ee written hmewrk 2. X L X ε C453 Lecture p-dwn Predictive Parsers 17 C453 Lecture p-dwn Predictive Parsers 18 One mre time Balanced parentheses grammar 1: à ( ) ε 1. Augment the grammar 2. Cnstruct Nullable, irst and llw 3. Build the predictive parse table, what happens? One mre time, but this time with feeling Balanced parentheses grammar 1: à ( ) ε 1. Augment the grammar 2. Cnstruct Nullable, irst and llw 3. Build the predictive parse table 4. Using the predictive parse table, cnstruct the parse tree fr ( ) ( ( ) ) $ and ( ) ( ) ( ) $ C453 Lecture p-dwn Predictive Parsers 19 C453 Lecture p-dwn Predictive Parsers 20 C453 Intr and PA1 5