Syntax Analysis Part IV

Σχετικά έγγραφα
Syntax Analysis Part V

Chap. 6 Pushdown Automata

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 19/5/2007

C.S. 430 Assignment 6, Sample Solutions

derivation of the Laplacian from rectangular to spherical coordinates

CHAPTER 25 SOLVING EQUATIONS BY ITERATIVE METHODS

Homework 3 Solutions

Ordinal Arithmetic: Addition, Multiplication, Exponentiation and Limit

Chapter 4 (c) parsing

Overview. Transition Semantics. Configurations and the transition relation. Executions and computation

The Simply Typed Lambda Calculus

Lecture 2: Dirac notation and a review of linear algebra Read Sakurai chapter 1, Baym chatper 3

Formal Semantics. 1 Type Logic

Lecture 2. Soundness and completeness of propositional logic

Top down vs. bottom up parsing. Top down vs. bottom up parsing

Math 6 SL Probability Distributions Practice Test Mark Scheme

Section 7.6 Double and Half Angle Formulas

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 6/5/2006

LECTURE 2 CONTEXT FREE GRAMMARS CONTENTS

2 Composition. Invertible Mappings

k A = [k, k]( )[a 1, a 2 ] = [ka 1,ka 2 ] 4For the division of two intervals of confidence in R +

Section 8.3 Trigonometric Equations

HOMEWORK 4 = G. In order to plot the stress versus the stretch we define a normalized stretch:

EE512: Error Control Coding

Fractional Colorings and Zykov Products of graphs

3.4 SUM AND DIFFERENCE FORMULAS. NOTE: cos(α+β) cos α + cos β cos(α-β) cos α -cos β

Phys460.nb Solution for the t-dependent Schrodinger s equation How did we find the solution? (not required)

ΚΥΠΡΙΑΚΟΣ ΣΥΝΔΕΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY 21 ος ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ Δεύτερος Γύρος - 30 Μαρτίου 2011

Syntax Analysis Part I

Απόκριση σε Μοναδιαία Ωστική Δύναμη (Unit Impulse) Απόκριση σε Δυνάμεις Αυθαίρετα Μεταβαλλόμενες με το Χρόνο. Απόστολος Σ.

Concrete Mathematics Exercises from 30 September 2016

TMA4115 Matematikk 3

Fourier Series. MATH 211, Calculus II. J. Robert Buchanan. Spring Department of Mathematics

Potential Dividers. 46 minutes. 46 marks. Page 1 of 11

Example Sheet 3 Solutions

LTL to Buchi. Overview. Buchi Model Checking LTL Translating LTL into Buchi. Ralf Huuck. Buchi Automata. Example

Finite Field Problems: Solutions

Physical DB Design. B-Trees Index files can become quite large for large main files Indices on index files are possible.

Nowhere-zero flows Let be a digraph, Abelian group. A Γ-circulation in is a mapping : such that, where, and : tail in X, head in

How to register an account with the Hellenic Community of Sheffield.

Second Order RLC Filters

Instruction Execution Times

Other Test Constructions: Likelihood Ratio & Bayes Tests

Advanced Subsidiary Unit 1: Understanding and Written Response

Math221: HW# 1 solutions

PARTIAL NOTES for 6.1 Trigonometric Identities

Solution Series 9. i=1 x i and i=1 x i.

Section 9.2 Polar Equations and Graphs

The challenges of non-stable predicates

ΤΕΧΝΟΛΟΓΙΚΟ ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΥΠΡΟΥ ΣΧΟΛΗ ΓΕΩΤΕΧΝΙΚΩΝ ΕΠΙΣΤΗΜΩΝ ΚΑΙ ΔΙΑΧΕΙΡΙΣΗΣ ΠΕΡΙΒΑΛΛΟΝΤΟΣ. Πτυχιακή εργασία

Approximation of distance between locations on earth given by latitude and longitude

FSM Toolkit Exercises

ΕΛΛΗΝΙΚΗ ΔΗΜΟΚΡΑΤΙΑ ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΡΗΤΗΣ. Ψηφιακή Οικονομία. Διάλεξη 10η: Basics of Game Theory part 2 Mαρίνα Μπιτσάκη Τμήμα Επιστήμης Υπολογιστών

Δημιουργία Λογαριασμού Διαχείρισης Business Telephony Create a Management Account for Business Telephony

Partial Differential Equations in Biology The boundary element method. March 26, 2013

Exercises 10. Find a fundamental matrix of the given system of equations. Also find the fundamental matrix Φ(t) satisfying Φ(0) = I. 1.

CHAPTER 48 APPLICATIONS OF MATRICES AND DETERMINANTS

Αλγόριθμοι Ταξινόμησης Μέρος 3

ΕΠΙΧΕΙΡΗΣΙΑΚΗ ΑΛΛΗΛΟΓΡΑΦΙΑ ΚΑΙ ΕΠΙΚΟΙΝΩΝΙΑ ΣΤΗΝ ΑΓΓΛΙΚΗ ΓΛΩΣΣΑ

ΠΤΥΧΙΑΚΗ ΕΡΓΑΣΙΑ ΒΑΛΕΝΤΙΝΑ ΠΑΠΑΔΟΠΟΥΛΟΥ Α.Μ.: 09/061. Υπεύθυνος Καθηγητής: Σάββας Μακρίδης

ST5224: Advanced Statistical Theory II

Οδηγίες Αγοράς Ηλεκτρονικού Βιβλίου Instructions for Buying an ebook

department listing department name αχχουντσ ϕανε βαλικτ δδσϕηασδδη σδηφγ ασκϕηλκ τεχηνιχαλ αλαν ϕουν διξ τεχηνιχαλ ϕοην µαριανι

About these lecture notes. Simply Typed λ-calculus. Types

SCHOOL OF MATHEMATICAL SCIENCES G11LMA Linear Mathematics Examination Solutions

Modbus basic setup notes for IO-Link AL1xxx Master Block

Matrices and Determinants

ΕΘΝΙΚΟ ΜΕΤΣΟΒΙΟ ΠΟΛΥΤΕΧΝΕΙΟ ΣΧΟΛΗ ΗΛΕΚΤΡΟΛΟΓΩΝ ΜΗΧΑΝΙΚΩΝ ΚΑΙ ΜΗΧΑΝΙΚΩΝ ΥΠΟΛΟΓΙΣΤΩΝ ΤΟΜΕΑΣ ΗΛΕΚΤΡΙΚΗΣ ΙΣΧΥΟΣ

Econ 2110: Fall 2008 Suggested Solutions to Problem Set 8 questions or comments to Dan Fetter 1

Dynamic types, Lambda calculus machines Section and Practice Problems Apr 21 22, 2016

Models for Probabilistic Programs with an Adversary

Homework 8 Model Solution Section

ΕΛΛΗΝΙΚΗ ΔΗΜΟΚΡΑΤΙΑ ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΡΗΤΗΣ. Ψηφιακή Οικονομία. Διάλεξη 7η: Consumer Behavior Mαρίνα Μπιτσάκη Τμήμα Επιστήμης Υπολογιστών

Every set of first-order formulas is equivalent to an independent set

Lecture 34 Bootstrap confidence intervals

Εγκατάσταση λογισμικού και αναβάθμιση συσκευής Device software installation and software upgrade

ΑΛΓΟΡΙΘΜΟΙ Άνοιξη I. ΜΗΛΗΣ

Capacitors - Capacitance, Charge and Potential Difference

From the finite to the transfinite: Λµ-terms and streams

4.6 Autoregressive Moving Average Model ARMA(1,1)

Areas and Lengths in Polar Coordinates

the total number of electrons passing through the lamp.

ΚΥΠΡΙΑΚΗ ΕΤΑΙΡΕΙΑ ΠΛΗΡΟΦΟΡΙΚΗΣ CYPRUS COMPUTER SOCIETY ΠΑΓΚΥΠΡΙΟΣ ΜΑΘΗΤΙΚΟΣ ΔΙΑΓΩΝΙΣΜΟΣ ΠΛΗΡΟΦΟΡΙΚΗΣ 11/3/2006

Srednicki Chapter 55

Development of a CFPSG. Coverage of linguistic phenomena such as agreement and word order

ΤΕΧΝΟΛΟΓΙΚΟ ΠΑΝΕΠΙΣΤΗΜΙΟ ΚΥΠΡΟΥ ΤΜΗΜΑ ΝΟΣΗΛΕΥΤΙΚΗΣ

ΠΑΝΕΠΙΣΤΗΜΙΟ ΠΕΙΡΑΙΑ ΤΜΗΜΑ ΝΑΥΤΙΛΙΑΚΩΝ ΣΠΟΥΔΩΝ ΠΡΟΓΡΑΜΜΑ ΜΕΤΑΠΤΥΧΙΑΚΩΝ ΣΠΟΥΔΩΝ ΣΤΗΝ ΝΑΥΤΙΛΙΑ

Durbin-Levinson recursive method

9.09. # 1. Area inside the oval limaçon r = cos θ. To graph, start with θ = 0 so r = 6. Compute dr

Inverse trigonometric functions & General Solution of Trigonometric Equations

Right Rear Door. Let's now finish the door hinge saga with the right rear door

Mean bond enthalpy Standard enthalpy of formation Bond N H N N N N H O O O

Τεχνικές Συµπίεσης Βίντεο. Δρ. Μαρία Κοζύρη Τµήµα Πληροφορικής Πανεπιστήµιο Θεσσαλίας

Κεφάλαιο 2: Τυπικές γλώσσες

Block Ciphers Modes. Ramki Thurimella

Numerical Analysis FMN011

forms This gives Remark 1. How to remember the above formulas: Substituting these into the equation we obtain with

( )( ) ( ) ( )( ) ( )( ) β = Chapter 5 Exercise Problems EX α So 49 β 199 EX EX EX5.4 EX5.5. (a)

FSM Toolkit Exercises Part II

CHAPTER 101 FOURIER SERIES FOR PERIODIC FUNCTIONS OF PERIOD

Transcript:

Syntax Analysis Part IV Chapter 4: Bottom-Up Parsing Sles adapted from : Robert van Engelen, Flora State University

Bottom-Up Parsing LR methods (Left-to-right, Rightmost derivation) SLR, Canonical LR, LALR Other cases: Shift-reduce parsing Operator-precedence parsing

Bottom-Up Parsing The process of reducing an input string w to the start symbol of the grammar At each reduction step, a specific substring matching the right-hand se of a production is replaced by the nonterminal at the lefthand se of that production

Bottom-Up Parsing Grammar E E + T T T T * F F F ( E ) Rightmost derivation E rm T rm T * F rm T * rm F * rm *

Bottom-Up Parsing * F * T * T * F T E F F T * F T F T * F F E rm T rm T * F rm T * rm F * rm *

Handle Pruning A rightmost derivation in reverse can be obtained by handle-pruning A handle is a substring of grammar symbols that matches a right-hand se of a production in a rightmost sentential form

Handle Pruning Locate the handle and replace by the lefthand se of the production S A Step in a rightmost derivation α β x

Handle Pruning Grammar: S a A B e A A b c b B d Underlined substrings are handles Reducing a sentence: a b b c d e a A b c d e a A d e a A B e S Shift-reduce corresponds to a rightmost derivation: S rm a A B e rm a A d e rm a A b c d e rm a b b c d e S A A A A A A B A B a b b c d e a b b c d e a b b c d e a b b c d e

Handle Pruning Grammar: S a A B e A A b c b B d a b b c d e a A b c d e a A d e a A B e S a b b c d e a A b c d e a A A e? Handles NOT a handle, because further reductions will fail (result is not a sentential form)

Exercise Conser the grammar: E E E + E E - E ( E ) What is the correct series of reductions for the string -( + ) +?

Exercise (cont d) -( + ) + -( + E ) + -( + E) + -(E + E) + -(E) + -E + E + E + E E + E E E E E + E E - E ( E )

Exercise (cont d) -( + ) + -(E + ) + -(E + E ) + -(E + E ) + E -(E + E) + E -(E) + E -E + E E + E E + E E E E E + E E - E ( E )

Exercise (cont d) -( + ) + -(E + ) + -(E + E ) + -(E + E) + -(E) + -E + E + E + E E + E E E E E + E E - E ( E )

Shift-Reduce Parsing Shift-reduce is a family of bottom-up parsers based on handle pruning Use a stack data structure and four operations Shift: move symbol from input to stack Reduce: replace handle in stack with lhs Accept Reject

Shift-Reduce Parsing Grammar: E E + E E E * E E ( E ) E Find handles to reduce Stack $ $ $E $E+ $E+ $E+E $E+E* $E+E* $E+E*E $E+E $E Input +*$ +*$ +*$ *$ *$ *$ $ $ $ $ $ Next Action shift reduce E shift shift reduce E shift (or reduce?) shift reduce E reduce E E * E reduce E E + E accept How to resolve conflicts?

Exercise Conser the grammar: E E E + E E - E ( E ) What is the correct shift-reduce parse for the string + -?

Exercise (cont d) Stack $ $ $E + $E +- $E +- $E +-E $E +-E $E +E $E +E $E Input +-$ +-$ -$ $ $ $ $ $ $ $ E E E + E E - E ( E )

Exercise (cont d) Stack $ $ $+ $+- $+- $+-E $+E $+E $E +E $E Input +-$ +-$ -$ $ $ $ $ $ $ $ E E E + E E - E ( E )

Exercise (cont d) Stack $ $ $E $E + $E +- $E +- $E +-E $E +E $E +E $E Input +-$ +-$ +-$ -$ $ $ $ $ $ $ E E E + E E - E ( E )

Exercise Conser the grammar: E E E + E E - E ( E ) Identify the handle for the shift-reduce parse state $E + -, + -( + )$ E + - - E + -E

Handle and Stack The handle will always eventually appear at the top of the stack (not at a deeper position) Case 1: B inse A S A B α β γ y z * S α A z rm α β B y z rm α β γ y z rm

Handle and Stack The handle will always eventually appear at the top of the stack Case 2: B and A on different branches B S A α γ x y z * S α B x A z rm α B x y z rm α γ x y z rm

Conflicts Shift-reduce and reduce-reduce conflicts are caused by Ambiguity of the grammar The limitations of the parsing method (even when the grammar is unambiguous)

Shift-Reduce Conflicts Ambiguous grammar: S if E then S if E then S else S other Stack $ $ if E then S Input $ else $ Next Action shift or reduce? Resolve in favor of shift, so else matches closest if

Reduce-Reduce Conflicts Grammar: C A B A a B a Stack $ $a Input aa$ a$ Action shift reduce A a or B a? Resolve in favor of reduce A a, otherwise we are stuck!

Rightmost Derivations Handle recognition is related to the recognition of rightmost sentential forms We need to develop a formal model for the above language

Rightmost Derivations Grammar : E E + T T T T * F F F ( E ) E E E + T + T T * F Derivation : E rm E + T T F F F

Rightmost Derivations Grammar : E E + T T T T * F F F ( E ) E E E + T + T T * F Derivation : E rm E + T rm E + T * F T F F F

Rightmost Derivations Grammar : E E + T T T T * F F F ( E ) E E E + T + T T * F Derivation : E rm E + T rm E + T * F rm E + T * T F F F

Rightmost Derivations Grammar : E E + T T T T * F F F ( E ) E E E + T + T T * F Derivation : E rm E + T rm E + T * F * rm E + * T F F F

Rightmost Derivations Grammar : E E + T T T T * F F F ( E ) E E E + T + T T * F Derivation : E rm E + T * rm E + * * rm E + + * T F F F

Rightmost Derivations In order to cut a derivation tree into a rightmost sentential form, iterate the following steps Vertically descend one step Move right horizontally zero or more steps within the current rule Stop when the right corner of the tree is reached

Viable Prefixes A viable prefix is the prefix of a rightmost sentential form, up to the part of terminal symbols already derived We can show that a viable prefix is the tree cut obtained through the vertical-horizontal procedure, up the the point in which the yield of the tree is reached (proof omitted)

Viable Prefixes viable prefix E + T * E E E + T + T T * F already derived terminals T F F F

Viable Prefixes A viable prefix can always be decomposed into the concatenation of prefixes of righthand ses of productions

Viable Prefixes Viable prefix : E + T * Concatenation of prefixes of righthand ses in the cut E T F E E + T + T T * F F F

Viable Prefixes A viable prefix can be decomposed into prefixes of right-hand ses in more than one way

Viable Prefixes S S A B T A B C R C D D Viable prefix : A B C D Viable prefix : A B C D

Viable Prefixes For any CFG G, the set of all possible viable prefixes of G is a regular language (proof omitted) We prove the construction of a NFA for the viable prefixes of G

LR(0) Items An LR(0) item is any production of G with a dot at some position in the right-hand se Thus, a production A X Y Z has four items: [A X Y Z] [A X Y Z] [A X Y Z] [A X Y Z ] Production A ε has a single item [A ]

LR(0) Items The bullet in an item [A α β] marks the portion α of the right-hand se that has already been horizontally processed Convention: We add to the grammar a new starting symbol S, and a production of the form S S

NFA for Viable Prefixes Input alphabet is the set of nonterminal symbols of G union the set of terminal symbols of G State set is the set of LR(0) items Initial state: [S S] Every state is also a final state NFA is partial

NFA for Viable Prefixes For each item [A α X β] add the following transitions, represented by relation For each nonterminal/terminal symbol X [A α X β] X [A α X β] For each production X γ in G [A α X β] ε [X γ]

NFA for Viable Prefixes Transitions on nonterminal/terminal symbol X are used to simulate horizontal part of the tree cut Transitions on symbol ε are used to simulate vertical part of the tree cut

NFA : Example Augmented grammar : S E E T + E T T int * T int ( E )

NFA : Example [S E] Augmented grammar : S E E T + E T T int * T int ( E )

NFA : Example [S E ] E [S E] ε [E T + E ] ε [E T] Augmented grammar : S E E T + E T T int * T int ( E )

NFA : Example ε [E T] T ε [E T ] ε [T ( E )] [T int * T ] [T int] Augmented grammar : S E E T + E T T int * T int ( E )

NFA : Example [T ( E )] [T ( E )] ( Augmented grammar : S E E T + E T T int * T int ( E )

NFA : Example [E T + E ] ε E [T ( E )] [T ( E )] [E T] ε Augmented grammar : S E E T + E T T int * T int ( E )

NFA : Example [S E ] E [S E] ε [E T + E ] ε E [T ( E )] [T ( E )] ε ε [E T] T ε [E T ] ε ε [T ( E )] ( [T int * T ] [T int] Augmented grammar : S E E T + E T T int * T int ( E )

DFA for Viable Prefixes The NFA for viable prefixes can be made deterministic Each state of the resulting DFA is a set of LR(0) items

DFA for Viable Prefixes Each path through the DFA represents several ways of cutting a viable prefix into prefixes of rule right-hand ses If item [X γ ] belongs to a state of the DFA, we have found a handle

LR Parser The LR parser uses the DFA for viable prefixes to detect handles The DFA is run on the stack of the LR parser To avo reading the full stack at each step, we save DFA states for each symbol in the stack