LR Parsing - The Items

Similar documents
LR Parsing - Conflicts

Recursive Descent Parsers

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

CS 4120 Introduction to Compilers

Lecture Bottom-Up Parsing

MIT Parse Table Construction. Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology

SLR parsers. LR(0) items

LR Parsers. Aditi Raste, CCOEW

Example CFG. Lectures 16 & 17 Bottom-Up Parsing. LL(1) Predictor Table Review. Stacks in LR Parsing 1. Sʹ " S. 2. S " AyB. 3. A " ab. 4.

Principles of Programming Languages

S Y N T A X A N A L Y S I S LR

UNIT-III BOTTOM-UP PARSING

CSE302: Compiler Design

Lexical and Syntax Analysis. Bottom-Up Parsing

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce parsing

LR Parsing. Leftmost and Rightmost Derivations. Compiler Design CSE 504. Derivations for id + id: T id = id+id. 1 Shift-Reduce Parsing.

shift-reduce parsing

Bottom-up parsing. Bottom-Up Parsing. Recall. Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form

CUP. Lecture 18 CUP User s Manual (online) Robb T. Koether. Hampden-Sydney College. Fri, Feb 27, 2015

Syntax Analysis Part I

A left-sentential form is a sentential form that occurs in the leftmost derivation of some sentence.

Compiler Design 1. Bottom-UP Parsing. Goutam Biswas. Lect 6

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 27, 2013

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

In One Slide. Outline. LR Parsing. Table Construction

CS 2210 Sample Midterm. 1. Determine if each of the following claims is true (T) or false (F).

Abstract Syntax Trees Synthetic and Inherited Attributes

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

Compiler Construction 2016/2017 Syntax Analysis

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Context-free grammars

Lecture 8: Deterministic Bottom-Up Parsing

Outline CS412/413. Administrivia. Review. Grammars. Left vs. Right Recursion. More tips forll(1) grammars Bottom-up parsing LR(0) parser construction

Compiler Construction: Parsing

UNIT III & IV. Bottom up parsing

Solving Recursive Sequences by Iteration

Bottom-Up Parsing. Lecture 11-12

Recognition of Tokens

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

Recursive Sequences. Lecture 24 Section 5.6. Robb T. Koether. Hampden-Sydney College. Wed, Feb 26, 2014

Conflicts in LR Parsing and More LR Parsing Types

Regular Expressions. Lecture 10 Sections Robb T. Koether. Hampden-Sydney College. Wed, Sep 14, 2016

Bottom-Up Parsing. Lecture 11-12

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

Parsing. Handle, viable prefix, items, closures, goto s LR(k): SLR(1), LR(1), LALR(1)

Bottom up parsing. The sentential forms happen to be a right most derivation in the reverse order. S a A B e a A d e. a A d e a A B e S.

Introduction to Compiler Design

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

Operators. Lecture 12 Section Robb T. Koether. Hampden-Sydney College. Fri, Feb 9, 2018

The Critical-Path Algorithm

CS143 Handout 20 Summer 2012 July 18 th, 2012 Practice CS143 Midterm Exam. (signed)

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Midterm I (Solutions) CS164, Spring 2002

Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals.

Wednesday, August 31, Parsers

Compilers. Bottom-up Parsing. (original slides by Sam

LR Parsing. Table Construction

Minimal Spanning Trees

More Bottom-Up Parsing

CS606- compiler instruction Solved MCQS From Midterm Papers

4. Lexical and Syntax Analysis

CS 314 Principles of Programming Languages

Lecture Notes on Bottom-Up LR Parsing

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

LR Parsing, Part 2. Constructing Parse Tables. An NFA Recognizing Viable Prefixes. Computing the Closure. GOTO Function and DFA States

Magnification and Minification

LR Parsing LALR Parser Generators

Stacks and their Applications

List Iterator Implementation

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Lecture 7: Deterministic Bottom-Up Parsing

The Traveling Salesman Problem Brute Force Method

Chapter 4: LR Parsing

MODULE 14 SLR PARSER LR(0) ITEMS

Recursive Linked Lists

4. Lexical and Syntax Analysis

CSE 130 Programming Language Principles & Paradigms Lecture # 5. Chapter 4 Lexical and Syntax Analysis

LR Parsing E T + E T 1 T

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery. Last modified: Mon Feb 23 10:05: CS164: Lecture #14 1

Lecture Notes on Bottom-Up LR Parsing

LR(0) Parsing Summary. LR(0) Parsing Table. LR(0) Limitations. A Non-LR(0) Grammar. LR(0) Parsing Table CS412/CS413

Scope and Parameter Passing

Boolean Expressions. Lecture 31 Sections 6.6, 6.7. Robb T. Koether. Hampden-Sydney College. Wed, Apr 8, 2015

LR Parsing LALR Parser Generators

Friends and Unary Operators

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

PART 3 - SYNTAX ANALYSIS. F. Wotawa TU Graz) Compiler Construction Summer term / 309

2068 (I) Attempt all questions.

Lecture 14 Sections Mon, Mar 2, 2009

Monday, September 13, Parsers

Downloaded from Page 1. LR Parsing

Top down vs. bottom up parsing

How do LL(1) Parsers Build Syntax Trees?

Bottom-Up Parsing LR Parsing

Fall Compiler Principles Lecture 5: Parsing part 4. Roman Manevich Ben-Gurion University

Parsers. Xiaokang Qiu Purdue University. August 31, 2018 ECE 468

Fall Compiler Principles Lecture 4: Parsing part 3. Roman Manevich Ben-Gurion University of the Negev

Bottom-Up Parsing II. Lecture 8

Review of CFGs and Parsing II Bottom-up Parsers. Lecture 5. Review slides 1

Transcription:

LR Parsing - The Items Lecture 10 Sections 4.5, 4.7 Robb T. Koether Hampden-Sydney College Fri, Feb 13, 2015 Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 1 / 31

1 LR Parsers 2 LR(0) Items 3 Building the PDA 4 Assignment Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 2 / 31

Outline 1 LR Parsers 2 LR(0) Items 3 Building the PDA 4 Assignment Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 3 / 31

LR Parsers A bottom-up parser follows a rightmost derivation from the bottom up. Such parsers typically use an LR algorithm and are called LR parsers. L means process tokens from Left to right. R means follow a Rightmost derivation, albeit, in reverse order. Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 4 / 31

LR Parsers Furthermore, in LR parsing, the production is applied only after the pattern has been matched. In LL (predictive) parsing, the production was selected, and then the tokens were matched to it. Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 5 / 31

Example Example (Rightmost Derivations) Let the grammar be E E + T T T T * F F F ( E ) id Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 6 / 31

Example Example (Rightmost Derivation of (id + id) id) E T T *F T *id F*id (E)*id (E+T )*id (E+F)*id (E+id)*id (T +id)*id (F+id)*id (id+id)*id Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 7 / 31

LR Parsers An LR parser uses A parse table (transition function) An input buffer A stack of LR(0) states. Whereas a lexer is a DFA (deterministic finite automaton), an LR parser is a PDA (push-down automaton). A PDA is essentially a DFA (or NFA) with a stack. Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 8 / 31

LR Parsers The parser performs three types of operation. Shift a token from the input buffer to the stack. Reduce the contents of the stack by applying a production. That is, pop a sequence of tokens that match the body of a production and replace them with the head of the production. Go to a new state. Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 9 / 31

LR Parsers An LR(0) parser (0 lookahead) reduces as soon as the body of a production is matched, regardless of the next token. An SLR parser (Simple LR) may or may not reduce, depending on the next token. An LR(1) parser (1 lookahead) is more complicated. It incorporates the next token as part of the current state of the process. An LALR parser (Look Ahead LR) is an LR(1) parser with its states much consolidated. Our parser generator CUP will generate an LALR parser. Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 10 / 31

Outline 1 LR Parsers 2 LR(0) Items 3 Building the PDA 4 Assignment Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 11 / 31

LR(0) Items Definition (LR(0) item) An LR(0) item is a production with a special marker ( ) that marks a position within the body of the production. To build an LR parse table, we must first find the LR(0) items. Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 12 / 31

Example Example (LR(0) Items) If the production is E E + T, then the possible LR(0) items are [E E + T ] [E E + T ] [E E + T ] [E E + T ] Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 13 / 31

LR(0) Items The interpretation of [A α β] is We have processed α and we might process β next. Whether we do actually process β will be borne out by the subsequent tokens. Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 14 / 31

Outline 1 LR Parsers 2 LR(0) Items 3 Building the PDA 4 Assignment Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 15 / 31

LR Parsing We will build a PDA whose states are sets of LR(0) items. First we augment the grammar with a new start symbol S. S S This guarantees that the start symbol will not recurse. Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 16 / 31

States of the PDA The initial state is called I 0 (item 0). State I 0 is the closure of the set {[S S]}. To form the closure of a set of items For each item [A α Bβ] in the set and for each production B γ in the grammar, add the item [B γ] to the set. Continue in this manner until there is no further change. We will call [B γ] an initial B-item. Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 17 / 31

Example Example (LR Parsing) Continuing with our example, the augmented grammar is E E E E + T T T T * F F F ( E ) id Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 18 / 31

Example Example (The PDA States) The state I 0 consists of the items in the closure of item [E E]. I 0 E E E E + T E T T T * F T F F ( E ) F id Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 19 / 31

Transitions There will be a transition from one state to another state for each grammar symbol that immediately follows the marker in an item in that state. If the item [A α Xβ] is in the state, then A transition from that state occurs when the symbol X is processed. The transition is to the state that is the closure of the item [A αx β]. Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 20 / 31

Example Example (Building the PDA) Thus, from the state I 0, there will be transitions for the symbols E, T, F, (, id, and num. For example, on processing E, the items [E E] and [E E + T ] become [E E ] and [E E + T ]. Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 21 / 31

Example Example (Building the PDA) Let state I 1 be the closure of these items. I 1 S E Then the PDA has the transition E E + T E I 0 I 1 Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 22 / 31

Example Example (Building the PDA) Similarly we determine the other transitions from I 0. On T On F I 2 I 3 E T T F T T * F Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 23 / 31

Example Example (Building the PDA) On ( On id I 4 I 5 F ( E ) E E + T E T T T * F T F F ( E ) F id F id Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 24 / 31

Example Example (Building the PDA) Now find the transitions from states I 1 through I 5 to other states, and so on, until no new states appear. Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 25 / 31

Example Example (Building the PDA) On + On * On E I 6 I 7 I 8 E E + T T T * F T F F ( E ) F id T T * F F ( E ) F id F ( E ) E E + T Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 26 / 31

Example Example (Building the PDA) On T On F On ) I 9 I 10 I 11 E E + T T T * F T T * F F ( E ) Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 27 / 31

Example Example (Building the PDA) From I 0 I 1 I 2 I 3 I 4 I 5 I 6 I 7 I 8 I 9 I 10 I 11 I 0 E T F ( id I 1 + I 2 * I 3 I 4 T F ( id E I 5 I 6 F ( id T I 7 ( id F I 8 + ) I 9 * I 10 I 11 To The transition table Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 28 / 31

Outline 1 LR Parsers 2 LR(0) Items 3 Building the PDA 4 Assignment Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 29 / 31

Assignment Homework Let the grammar be E E + E E * E ( E ) id Write the LR(0) items for this grammar. Write the LR(0) states. Draw the transition diagram for the PDA of an SLR parser. (continued... ) Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 30 / 31

Assignment Homework Let the grammar be S ( L ) id L L, S S Write the LR(0) items for this grammar. Write the LR(0) states. Draw the transition diagram for the PDA of an SLR parser. Robb T. Koether (Hampden-Sydney College) LR Parsing - The Items Fri, Feb 13, 2015 31 / 31