ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών. Lecture 7a Syntax Analysis Elias Athanasopoulos

Similar documents
Shift-Reduce parser E + (E + (E) E [a-z] In each stage, we shift a symbol from the input to the stack, or reduce according to one of the rules.

Bottom-Up Parsing. Ÿ rm E + F *idÿ rm E +id*idÿ rm T +id*id. Ÿ rm F +id*id Ÿ rm id + id * id

Syntax Analysis (Part 2)

Compiler Design Spring 2017

LR2: LR(0) Parsing. LR Parsing. CMPT 379: Compilers Instructor: Anoop Sarkar. anoopsarkar.github.io/compilers-class

Parsing -3. A View During TD Parsing

CS 406: Bottom-Up Parsing

n Top-down parsing vs. bottom-up parsing n Top-down parsing n Introduction n A top-down depth-first parser (with backtracking)

CA Compiler Construction

Syntax Analysis Part I

Syntax Analysis Part I

Announcements. H6 posted 2 days ago (due on Tue) Midterm went well: Very nice curve. Average 65% High score 74/75

I 1 : {S S } I 2 : {S X ay, Y X } I 3 : {S Y } I 4 : {X b Y, Y X, X by, X c} I 5 : {X c } I 6 : {S Xa Y, Y X, X by, X c} I 7 : {X by } I 8 : {Y X }

Computer Science 160 Translation of Programming Languages

Translator Design Lecture 16 Constructing SLR Parsing Tables

Bottom up parsing. General idea LR(0) SLR LR(1) LALR To best exploit JavaCUP, should understand the theoretical basis (LR parsing);

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

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

Context free languages

Compiler Design 1. LR Parsing. Goutam Biswas. Lect 7

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

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Construction Lent Term 2015 Lectures (of 16)

THEORY OF COMPILATION

EXAM. Please read all instructions, including these, carefully NAME : Problem Max points Points 1 10 TOTAL 100

Compiler Construction Lectures 13 16

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

Why augment the grammar?

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

CS Rewriting System - grammars, fa, and PDA

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

CS415 Compilers Syntax Analysis Bottom-up Parsing

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

Syntax Analysis, VI Examples from LR Parsing. Comp 412

Compilerconstructie. najaar Rudy van Vliet kamer 124 Snellius, tel rvvliet(at)liacs.

CSE302: Compiler Design

Lecture 11 Sections 4.5, 4.7. Wed, Feb 18, 2009

Chapter 4: Bottom-up Analysis 106 / 338

Bottom-Up Syntax Analysis

Parsing VI LR(1) Parsers

Top-Down Parsing and Intro to Bottom-Up Parsing

CS20a: summary (Oct 24, 2002)

Bottom-Up Syntax Analysis

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

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

LR(1) Parsers Part III Last Parsing Lecture. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

Curs 8. LR(k) parsing. S.Motogna - FL&CD

Bottom-Up Syntax Analysis

Creating a Recursive Descent Parse Table

CPS 220 Theory of Computation Pushdown Automata (PDA)

Talen en Compilers. Johan Jeuring , period 2. October 29, Department of Information and Computing Sciences Utrecht University

LR(1) Parsers Part II. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

Pushdown Automata (Pre Lecture)

Syntax Analysis - Part 1. Syntax Analysis

Syntax Analysis Part III

Everything You Always Wanted to Know About Parsing

Bottom-up Analysis. Theorem: Proof: Let a grammar G be reduced and left-recursive, then G is not LL(k) for any k.

Bottom-Up Syntax Analysis

Introduction to Bottom-Up Parsing

Compiler Construction

Introduction to Bottom-Up Parsing

Compiling Techniques

1. For the following sub-problems, consider the following context-free grammar: S AA$ (1) A xa (2) A B (3) B yb (4)

Introduction to Bottom-Up Parsing

Syntax Directed Transla1on

Introduction to Bottom-Up Parsing

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

Computing if a token can follow

TAFL 1 (ECS-403) Unit- III. 3.1 Definition of CFG (Context Free Grammar) and problems. 3.2 Derivation. 3.3 Ambiguity in Grammar

Compiler Design. Spring Syntactic Analysis. Sample Exercises and Solutions. Prof. Pedro C. Diniz

Syntactic Analysis. Top-Down Parsing

Parsing. Left-Corner Parsing. Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 17

Context-Free Languages (Pre Lecture)

1. Draw a parse tree for the following derivation: S C A C C A b b b b A b b b b B b b b b a A a a b b b b a b a a b b 2. Show on your parse tree u,

Type 3 languages. Type 2 languages. Regular grammars Finite automata. Regular expressions. Type 2 grammars. Deterministic Nondeterministic.

INF5110 Compiler Construction

CS415 Compilers Syntax Analysis Top-down Parsing

Syntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. source code. IR IR target.

PUSHDOWN AUTOMATA (PDA)

MA/CSSE 474 Theory of Computation

CS 314 Principles of Programming Languages

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

Context-Free Grammars and Languages. Reading: Chapter 5

1. For the following sub-problems, consider the following context-free grammar: S AB$ (1) A xax (2) A B (3) B yby (5) B A (6)

Accept or reject. Stack

Definition: A grammar G = (V, T, P,S) is a context free grammar (cfg) if all productions in P have the form A x where

Context-free Languages and Pushdown Automata

1. For the following sub-problems, consider the following context-free grammar: S A$ (1) A xbc (2) A CB (3) B yb (4) C x (6)

LR(1) Parsers Part II. Copyright 2010, Keith D. Cooper & Linda Torczon, all rights reserved.

Briefly on Bottom-up

Einführung in die Computerlinguistik

Bottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties

An Efficient Context-Free Parsing Algorithm. Speakers: Morad Ankri Yaniv Elia

Bottom-up syntactic parsing. LR(k) grammars. LR(0) grammars. Bottom-up parser. Definition Properties

Intro to Theory of Computation

Announcement: Midterm Prep

Handout 8: Computation & Hierarchical parsing II. Compute initial state set S 0 Compute initial state set S 0

SLR(1) and LALR(1) Parsing for Unrestricted Grammars. Lawrence A. Harris

CS153: Compilers Lecture 5: LL Parsing

CS Pushdown Automata

Transcription:

ΕΠΛ323 - Θεωρία και Πρακτική Μεταγλωττιστών Lecture 7a Syntax Analysis Elias Athanasopoulos eliasathan@cs.ucy.ac.cy

Operator-precedence Parsing A class of shiq-reduce parsers that can be writen by hand No ε-producvons, no two adjacent nonterminals on the right side E èeae (E) -E id E è+ - * / ^ x Operator Grammar E èe+e E-E E*E E/E E^E (E) -E id

OperaVon RelaVon Table RELATION α < β α = β α > β MEANING α yields precedence to β α has the same precedence β α takes precedence over β id + * $ id > > > + < > < > * < > > > $ < < < E èe+e E*E id

Example STACK INPUT ACTION $ id+id*id$ shift (push) $id +id*id$ reduce (pop) $ +id*id$ shift (push) $+ id*id$ shift (push) $+id *id$ reduce (pop) $+* id$ shift (push) $+*id $ shift (push) $+* $ reduce (pop) $+ $ reduce (pop) $ $ reduce (pop) stack < input: shift (push) stack > input: reduce (pop) id + * $ id > > > + < > < > * < > > > $ < < <

Le,-to-right scanning of the input. Number of symbols for taking a decision (lookahead). LR(k) parsers Construc8on of a rightmost deriva8on in reverse

LR parsers LR parsers can be constructed to recognize virtually all programming-language constructs for which contextfree grammars can be writen. The LR parsing method is the most general nonbacktracking shiq-reduce parsing method known, yet it can be implemented as efficiently as other shiqreduce methods. The class of grammars that can be parsed using LR methods is a proper superset of the class of grammars that can be parsed with predicvve parsers (e.g., LL(1)). An LR parser can detect a syntacvc error as soon as it is possible to do so on a leq-to-right scan of the input.

Recall LL(1) INPUT a + b $ STACK X Y PredicVve Parsing Program OUTPUT Z $ Parsing Table M $: end symbol X, Y, Z: non-terminals or terminals

LR parser INPUT a 1 a i $ a n STACK s m X m LR Parsing Program OUTPUT S m-1 X m-1... s 0 ac8ons goto parsing table Algorithm (for ac8on[s m, a i ]) 1. shiq s, where s is state 2. reduce by a grammar producvon 3. accept, and 4. error.

LR Parsers LR(0) SLR(1) (Simple LR) LALR(1) (Look Ahead LR) CLR(1) (Canonical LR) Power Simplicity

ConstrucEng an SLR parsing table

LR(0) item We hope to see a string derivable from XYZ next on the input. We have just seen on the input a string derivable from X and that we hope next to see a string derivable from YZ. We saw all input. A èxyz A è XYZ A èx YZ A èxy Z A èxyz A èε A è four items

Closure If I is a set of items for a grammar G, then closure(i) is the set of items constructed from I by the two rules: IniVally, every item in I is added to the closure(i). If Aèa Bb is in closure(i) and BèC is a producvon, then add the item Bè C to I, if it is not already there. We apply this rule unvl no more new items can be added to closure(i).

Example Grammar E àe E àe+t T T àt*f F F à(e) id I = {[E à E]} closure(i) E à E E à E+T E à T T à T*F T à F F à (E) F à id

Goto goto(i, X) is defined to be the closure of the set of all items [AàαX β] such that [Aàα Xβ] is in I. Grammar E àe E àe+t T T àt*f F F à(e) id I = {[E àe ], [EàE +T]} goto(i,+) E àe+ T T à T*F T à F F à (E) F à id How goto(i,+) is computed? We computed goto(i, +) by examining I for items with + immediately to the right of the dot. E àe is not such an item, but EàE +T is. We moved the dot over the + to get {EàE+ T} and then took the closure of this set.

Canonical collecvon of LR(0) items Augment the grammar with a new symbol that produces the starvng symbol of the grammar: S às Compute the closure of the new producvon, C := closure({[s às]}) For each set of items I in C, and each grammar symbol X, add goto(i, X) to C

Canonical collecvon of LR(0) items I 0 E à E E à E+T E à T T à T*F T à F F à (E) F à id I 4 F à( E) E à E+T E à T T à T*F T à F F à (E) F à id I 7 T àt* F F à (E) F à id I 8 F à(e ) E àe +T I 1 E àe E àe +T I 2 E àt T àt *F I 3 T àf I 5 F àid I 6 E àe+ T T à T*F T à F T à (E) T à id I 9 E àe+t T àt +F I 10 T àt*f I 11 F à(e)

TransiVon Diagram I 0 Τ E I 1 I 2 + * I 6 I 7 Τ F I 9 I 10 * F ( id to I 7 to I 5 to I 3 to I 4 F I 3 ( F ( id to I 4 to I 5 ( id I 4 I 5 id E T I 8 to I 2 ) + I 11 to I 6

SLR Parsing Table STATE action goto id + * ( ) $ E T F 0 s5 s4 1 2 3 1 s6 acc 2 r2 s7 r2 r2 3 r4 r4 r4 r4 4 s5 s4 8 2 3 5 r6 r6 r6 r6 6 s5 s4 9 3 7 s5 s4 10 8 s6 s11 9 r1 s7 r1 r1 10 r3 r3 r3 r3 11 r5 r5 r5 r5

Parsing Algorithm set ip to point the first symbol of w$; repeat forever begin end let s be the state on top of the stack and a the symbol pointed to by ip; if (action[s, a] = shift s then begin end push a then s on top of the stack; advance ip to the next input symbol else if action[s, a] = reduce Aàb then begin end pop 2x b symbols off the stack; let s be the state now on top of the stack; push A then goto[s, A] on top of the stack; output the production Aàb else if action[s, a] = accept then return else error()

id*id+id STACK INPUT ACTION (1) 0 id*id+id$ shiq (2) 0 id 5 *id+id$ reduce by Fàid (3) 0 F 3 *id+id$ reduce by TàF (4) 0 T 2 *id+id$ shiq (5) 0 T 2 * 7 id+id$ shiq (6) 0 T 2 * 7 id 5 +id$ reduce by Fàid (7) 0 T 2 * 7 F 10 +id$ reduce by TàT*F (8) 0 T 2 +id$ reduce by EàT (9) 0 E 1 +id$ shiq (10) 0 E 1 + 6 id$ shiq (11) 0 E 1 + 6 id 5 $ reduce by Fàid (12) 0 E 1 + 6 F 3 $ reduce by TàF (13) 0 E 1 + 6 T 9 $ EàE+T (14) 0 E 1 $ accept Productions (1) E àe+t (2) E àt (3) T àt*f (4) T àf (5) F à(e) (6) F àid