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

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

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Top-Down Parsing and Intro to Bottom-Up Parsing

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

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.

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

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

CS 406: Bottom-Up Parsing

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Construction Lent Term 2015 Lectures (of 16)

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

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

Compiling Techniques

This lecture covers Chapter 5 of HMU: Context-free Grammars

CA Compiler Construction

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

Creating a Recursive Descent Parse Table

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

Bottom-Up Syntax Analysis

Pushdown Automata (Pre Lecture)

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

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

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

CS415 Compilers Syntax Analysis Bottom-up Parsing

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

Compiler Design Spring 2017

Computability Theory

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

Compiler Construction Lectures 13 16

Parsing -3. A View During TD Parsing

CS153: Compilers Lecture 5: LL Parsing

Chapter 4: Bottom-up Analysis 106 / 338

Computer Science 160 Translation of Programming Languages

Context-free Grammars and Languages

CSE 105 THEORY OF COMPUTATION

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

Syntax Analysis Part I

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)

CS20a: summary (Oct 24, 2002)

Syntax Analysis, VI Examples from LR Parsing. Comp 412

Harvard CS 121 and CSCI E-207 Lecture 10: Ambiguity, Pushdown Automata

Compiler Principles, PS4

Introduction to Theory of Computing

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

CSE302: Compiler Design

Why augment the grammar?

Pushdown Automata: Introduction (2)

Syntax Analysis Part I

Syntactical analysis. Syntactical analysis. Syntactical analysis. Syntactical analysis

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

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

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

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

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)

Syntax Analysis (Part 2)

CS Pushdown Automata

THEORY OF COMPILATION

Syntax Analysis - Part 1. Syntax Analysis

CISC4090: Theory of Computation

CS 314 Principles of Programming Languages

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 }

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

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

Computer Science 160 Translation of Programming Languages

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

Context Free Grammars

The Pumping Lemma. for all n 0, u 1 v n u 2 L (i.e. u 1 u 2 L, u 1 vu 2 L [but we knew that anyway], u 1 vvu 2 L, u 1 vvvu 2 L, etc.

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

Top-Down Parsing, Part II

CPS 220 Theory of Computation

Syntactic Analysis. Top-Down Parsing

Intro to Theory of Computation

Foundations of Informatics: a Bridging Course

Syntax Analysis, VII The Canonical LR(1) Table Construction. Comp 412

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Parsing VI LR(1) Parsers

Syntax Directed Transla1on

Syntax Analysis: Context-free Grammars, Pushdown Automata and Parsing Part - 3. Y.N. Srikant

Deterministic Finite Automaton (DFA)

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

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,

5 Context-Free Languages

CS:4330 Theory of Computation Spring Regular Languages. Finite Automata and Regular Expressions. Haniel Barbosa

Outline. CS21 Decidability and Tractability. Machine view of FA. Machine view of FA. Machine view of FA. Machine view of FA.

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

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

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

CPS 220 Theory of Computation Pushdown Automata (PDA)

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

Announcement: Midterm Prep

Turing machines and linear bounded automata

Compiling Techniques

Fundamentele Informatica II

Turing machines and linear bounded automata

Context-Free Languages (Pre Lecture)

Everything You Always Wanted to Know About Parsing

Computational Models - Lecture 4

Transcription:

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

Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient Builds on ideas in top-down parsing Preferred method in practice Also called LR parsing L means that tokens are read left to right R means that it constructs a rightmost derivation! Prof. Bodik CS 164 Lecture 7-8 2

An Introductory xample LR parsers don t need left-factored grammars and can also handle left-recursive grammars Consider the following grammar: ( ) Why is this not LL(1)? Consider the string: ( ) ( ) Prof. Bodik CS 164 Lecture 7-8 3

The Idea LR parsing reduces a string to the start symbol by inverting productions: str: input string of terminals repeat Identify β in str such that A β is a production (i.e., str = α β γ) Replace β by A in str (i.e., str becomes α A γ) until str = S Prof. Bodik CS 164 Lecture 7-8 4

A Bottom-up Parse in Detail (1) () () ( ) ( ) Prof. Bodik CS 164 Lecture 7-8 5

A Bottom-up Parse in Detail (2) () () () () ( ) ( ) Prof. Bodik CS 164 Lecture 7-8 6

A Bottom-up Parse in Detail (3) () () () () () () ( ) ( ) Prof. Bodik CS 164 Lecture 7-8 7

A Bottom-up Parse in Detail (4) () () () () () () () ( ) ( ) Prof. Bodik CS 164 Lecture 7-8 8

A Bottom-up Parse in Detail (5) () () () () () () () () ( ) ( ) Prof. Bodik CS 164 Lecture 7-8 9

A Bottom-up Parse in Detail (6) () () () () () () () () A rightmost derivation in reverse ( ) ( ) Prof. Bodik CS 164 Lecture 7-8 10

Important Fact #1 Important Fact #1 about bottom-up parsing: An LR parser traces a rightmost derivation in reverse Prof. Bodik CS 164 Lecture 7-8 11

Where Do Reductions Happen Important Fact #1 has an eresting consequence: Let αβγ be a step of a bottom-up parse Assume the next reduction is by A β Then γ is a string of terminals! Why? Because αaγ αβγ is a step in a rightmost derivation. Prof. Bodik CS 164 Lecture 7-8 12

Notation Idea: Split string o two substrings Right substring (a string of terminals) is as yet unexamined by parser Left substring has terminals and non-terminals The dividing po is marked by a I The I is not part of the string Initially, all input is unexamined: Ix 1 x 2... x n Prof. Bodik CS 164 Lecture 7-8 13

Shift-Reduce Parsing Bottom-up parsing uses only two kinds of actions: Shift Reduce Prof. Bodik CS 164 Lecture 7-8 14

Shift Shift: Move I one place to the right Shifts a terminal to the left string (I ) ( I ) Prof. Bodik CS 164 Lecture 7-8 15

Reduce Reduce: Apply an inverse production at the right end of the left string If ( ) is a production, then ( ( ) I ) ( I ) Prof. Bodik CS 164 Lecture 7-8 16

Shift-Reduce xample I () ()$ shift ( ) ( )

Shift-Reduce xample I () ()$ shift I () ()$ red. ( ) ( )

Shift-Reduce xample I () ()$ shift I () ()$ red. I () ()$ shift 3 times ( ) ( )

Shift-Reduce xample I () ()$ shift I () ()$ red. I () ()$ shift 3 times ( I ) ()$ red. ( ) ( )

Shift-Reduce xample I () ()$ shift I () ()$ red. I () ()$ shift 3 times ( I ) ()$ ( I ) ()$ red. shift ( ) ( )

Shift-Reduce xample I () ()$ shift I () ()$ red. I () ()$ shift 3 times ( I ) ()$ ( I ) ()$ () I ()$ red. shift red. () ( ) ( )

Shift-Reduce xample I () ()$ shift I () ()$ red. I () ()$ shift 3 times ( I ) ()$ ( I ) ()$ () I ()$ I ()$ red. shift red. () shift 3 times ( ) ( )

Shift-Reduce xample I () ()$ shift I () ()$ red. I () ()$ shift 3 times ( I ) ()$ ( I ) ()$ () I ()$ I ()$ red. shift red. () shift 3 times ( I )$ red. ( ) ( )

Shift-Reduce xample I () ()$ shift I () ()$ red. I () ()$ shift 3 times ( I ) ()$ red. ( I ) ()$ shift () I ()$ red. () I ()$ shift 3 times ( I )$ red. ( I )$ shift ( ) ( )

Shift-Reduce xample I () ()$ shift I () ()$ red. I () ()$ shift 3 times ( I ) ()$ ( I ) ()$ () I ()$ I ()$ red. shift red. () shift 3 times ( I )$ ( I )$ red. shift () I $ red. () ( ) ( )

Shift-Reduce xample I () ()$ shift I () ()$ red. I () ()$ shift 3 times ( I ) ()$ ( I ) ()$ () I ()$ I ()$ red. shift red. () shift 3 times ( I )$ ( I )$ red. shift () I $ I $ red. () accept ( ) ( )

The Stack Left string can be implemented by a stack Top of the stack is the I Shift pushes a terminal on the stack Reduce pops 0 or more symbols off of the stack (production rhs) and pushes a nonterminal on the stack (production lhs) Prof. Bodik CS 164 Lecture 7-8 28

Key Issue: When to Shift or Reduce? Decide based on the stack s content and the lookahead Idea: use a finite automaton (DFA) to decide when to shift or reduce The DFA input is the stack s content The alphabet consists of terminals and non-terminals We run the DFA on the stack and we examine the resulting state X and the token tok after I If X has a transition labeled tok then shift If X is labeled with A β on tok then reduce Prof. Bodik CS 164 Lecture 7-8 29

accept on $ LR(1) Parsing. An xample () on $, 0 1 ( 2 3 4 7 8 ) 10 6 ( ) 5 11 on $, 9 on ), () on ), I () ()$ shift I () ()$ I () ()$ shift(x3) ( I ) ()$ ( I ) ()$ shift () I ()$ () I ()$ shift (x3) ( I )$ ( I )$ shift () I $ () I $ accept

Representing the DFA Parsers represent the DFA as a 2D table Lines correspond to DFA states Columns correspond to terminals and nonterminals Typically columns are split o: Those for terminals: action table Those for non-terminals: goto table Prof. Bodik CS 164 Lecture 7-8 31

Representing the DFA. xample The table for a fragment of our DFA: ( 3 4 6 ) 7 () on $, 5 on ), ( ) $ 3 s4 4 s5 g6 5 r -> r -> 6 s8 s7 7 r -> () r -> () Prof. Bodik CS 164 Lecture 7-8 32

A Hierarchy of Grammar Classes From Andrew Appel, Modern Compiler Implementation in Java Prof. Bodik CS 164 Lecture 7-8 33