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

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.

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

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

THEORY OF COMPILATION

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

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

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

Syntax Analysis (Part 2)

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

CA Compiler Construction

Pushdown Automata (Pre Lecture)

Parsing -3. A View During TD Parsing

Compiler Construction Lectures 13 16

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

Computer Science 160 Translation of Programming Languages

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

CS153: Compilers Lecture 5: LL Parsing

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

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

Compiler Construction Lent Term 2015 Lectures (of 16)

Compiler Construction Lent Term 2015 Lectures (of 16)

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

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)

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

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Bottom-Up Syntax Analysis

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

Creating a Recursive Descent Parse Table

Compiler Design Spring 2017

Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

CSE302: Compiler Design

Compiler Construction

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)

Top-Down Parsing and Intro to Bottom-Up Parsing

CS20a: summary (Oct 24, 2002)

Everything You Always Wanted to Know About Parsing

Everything You Always Wanted to Know About Parsing

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 }

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

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

Compiler Principles, PS4

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

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,

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

CPS 220 Theory of Computation Pushdown Automata (PDA)

Parsing VI LR(1) Parsers

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

MTH401A Theory of Computation. Lecture 17

INF5110 Compiler Construction

Briefly on Bottom-up

Context-Free Languages (Pre Lecture)

Theory of Computation

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

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

Direct Proof Floor and Ceiling

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

CMSC 330: Organization of Programming Languages. Pushdown Automata Parsing

Einführung in die Computerlinguistik

Computing if a token can follow

Syntactic Analysis. Top-Down Parsing

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

Direct Proof Universal Statements

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

CS 314 Principles of Programming Languages

5 Context-Free Languages

Exercises. Exercise: Grammar Rewriting

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

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

Chomsky Normal Form for Context-Free Gramars

Section 1 (closed-book) Total points 30

Closure Properties of Regular Languages

Syntax Analysis Part III

CS415 Compilers Syntax Analysis Top-down Parsing

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

Conflict Removal. Less Than, Equals ( <= ) Conflict

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

Pushdown Automata: Introduction (2)

Why augment the grammar?

Translator Design Lecture 16 Constructing SLR Parsing Tables

Syntax Analysis Part I

Syntax Analysis Part I

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

Instantaneous Rate of Change

Nondeterministic Finite Automata

Note: In any grammar here, the meaning and usage of P (productions) is equivalent to R (rules).

Pushdown automata. Twan van Laarhoven. Institute for Computing and Information Sciences Intelligent Systems Radboud University Nijmegen

Announcement: Midterm Prep

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

CFGs and PDAs are Equivalent. We provide algorithms to convert a CFG to a PDA and vice versa.

Pushdown Automata. We have seen examples of context-free languages that are not regular, and hence can not be recognized by finite automata.

St.MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

Syntax Analysis, VI Examples from LR Parsing. Comp 412

CS415 Compilers Syntax Analysis Bottom-up Parsing

Composition of Functions

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

SCHEME FOR INTERNAL ASSESSMENT TEST 3

Compiler Principles, PS7

Transcription:

The s s The s Lecture 11 Sections 4.5, 4.7 Hampden-Sydney College Wed, Feb 18, 2009

Outline The s s 1 s 2 3 4 5 6

The LR(0) Parsing s The s s There are two tables that we will construct. The action table contains shift reduce actions to be taken upon processing terminals. The goto table contains changes of state upon matching productions.

The s s The action table contains one row for each state in the PDA one column for each terminal EOF ($). The entries are Shift n. Push the current token move to state n. Reduce n. Pop the symbols of the right side of production n then push the nonterminal of the left side. Then change state according to the goto table.

Building the Action The s s The action table is built from items of the form [A α aβ] [A α ]. Items [A α aβ] produce shift operations. Items [A α ] produce reduce operations. The goto table is built from items of the form [A α Bβ].

Building the Action The s s If [A α aβ] is in state I i the PDA transition on a is from state I i to state I j, then set the (i, a) entry to shift j. In the table, we write sj, where j is the number of the destination state. Thus, every transition on a terminal becomes a shift entry in the action table. I i a I j

Building the Action The s s If [A α ] is in state I i A S, then set the (i, a) entry to reduce A α for all a in FOLLOW(A). In the table, we write rk, where k is the number of the production A α. I i a I k

Building the Action The s s If [S S ] is in state I i, then the (i, $) entry is accept. In the table, we write acc. All empty cells are labeled error.

The s s It is possible that a cell will contain both a shift operation a reduce operation. This is called a shift/reduce conflict. To choose between shift reduce, each case must be considered on its own merit. Consider the case of E E + E E E the inputs a+b*c a*b+c.

Reduce/Reduce The s s It is possible that a cell will contain two different reduce operations. This is called a reduce/reduce conflict. This occurs when a sequence of tokens matches the right-h sides of two different productions at the same time. For each such conflict in the table, we must choose which reduction to apply.

The s s (FIRST FOLLOW) To find the action table for our example, we need to know nullability, FIRST, FOLLOW. E E E E + T T T T * F F F (E) id num

The s s (FIRST FOLLOW) Nullable FIRST FOLLOW E No {(, id, num} {$} E No {(, id, num} {$, +, )} T No {(, id, num} {$, +, ), *} F No {(, id, num} {$, +, ), *}

The s s ( ) Then number the productions, starting with 0 for the special production: 0. E E 1. E E + T 2. E T 3. T T * F 4. T F 5. F (E) 6. F id 7. F num

The s s ( ) + * ( ) id num $ 0 s4 s5 s6 1 s7 acc 2 r2 s8 r2 r2 3 r4 r4 r4 r4 4 s4 s5 s6 5 r6 r6 r6 r6 6 r7 r7 r7 r7 7 s4 s5 s6 8 s4 s12 s5 s6 9 s7 r1 10 r1 s8 r3 r1 11 r3 r3 r5 r3 12 r5 r5 r5

The s s The goto table has one row for each state in the PDA one column for each nonterminal except S. The entries are states of the PDA.

Building the Goto The s s If [A α Bβ] is in state I i the PDA transition is then set the (i, B) entry to goto j. In the goto table, we write j. I i Thus, every transition on a nonterminal becomes an entry in the goto table. B I j

The s s ( ) E T F 0 1 2 3 1 2 3 4 9 2 3 5 6 7 10 3 8 11 9 10 11 12

The s s (Parse (id + num)*id) Stack Input 0 ( id + num ) * id $ 0 ( 4 id + num ) * id $ 0 ( 4 id 5 + num ) * id $ 0 ( 4 F 3 + num ) * id $ 0 ( 4 T 2 + num ) * id $ 0 ( 4 E 9 + num ) * id $ 0 ( 4 E 9 + 7 num ) * id $ 0 ( 4 E 9 + 7 num 6 ) * id $ 0 ( 4 E 9 + 7 F 3 ) * id $

The s s (LR Parsing) Stack Input 0 ( 4 E 9 + 7 T 10 ) * id $ 0 ( 4 E 9 ) * id $ 0 ( 4 E 9 ) 12 * id $ 0 F 3 * id $ 0 T 2 * id $ 0 T 2 * 8 id $ 0 T 2 * 8 id 5 $ 0 T 2 * 8 F 11 $ 0 T 2 $ 0 E 1 $ Accept $

The s s (A Simplified Grammar) We may simplify our grammar to E E + E E E * E E (E) E id E num In this form, the precedence rules for + * are not implicit. They must be incorporated into the tables.

The s s ( Goto s) Action Goto + * ( ) id num $ E 0 s2 s3 s4 1 1 s5 s6 acc 2 s2 s3 s4 7 3 r4 r4 r4 r4 4 r5 r5 r5 r5 5 s2 s3 s4 8 6 s2 s3 s4 9 7 s5 s6 s10 8 s5/r1 s6/r1 r1 r1 9 s5/r2 s6/r2 r2 r2 10 r3 r3 r3 r3

The s s ( ) The shift/reduce conflict in cell (8, +) is between shifting a + reducing by E E + E. If we choose shift, then we will make addition right associative. If we choose reduce, then we will make addition left associative. The case is similar in cell (9, *) regarding multiplication.

The s s ( ) The shift/reduce conflict in cell (9, +) is between shifting a + reducing by E E * E. If we choose shift, then we will give multiplication a higher precedence than addition. If we choose reduce, then we will give addition a higher precedence than multiplication. The case is similar in cell (8, *).

The s s ( Goto s) Action Goto + * ( ) id num $ E 0 s2 s3 s4 1 1 s5 s6 acc 2 s2 s3 s4 7 3 r4 r4 r4 r4 4 r5 r5 r5 r5 5 s2 s3 s4 8 6 s2 s3 s4 9 7 s5 s6 s10 8 r1 s6 r1 r1 9 r2 r2 r2 r2 10 r3 r3 r3 r3

The s s Homework The grammar R R R RR R* (R) a b generates all regular expressions on the alphabet Σ = {a, b}. Build the action goto tables. Resolve any conflicts in the table. Parse the expression ab*(a b).