SLR parsers. LR(0) items

Similar documents
Formal Languages and Compilers Lecture VII Part 3: Syntactic A

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

LR Parsing - The Items

LR Parsers. Aditi Raste, CCOEW

Context-free grammars

Compiler Construction: Parsing

Lecture Bottom-Up Parsing

Principles of Programming Languages

Bottom-Up Parsing II. Lecture 8

Review: Shift-Reduce Parsing. Bottom-up parsing uses two actions: Bottom-Up Parsing II. Shift ABC xyz ABCx yz. Lecture 8. Reduce Cbxy ijk CbA ijk

Lexical and Syntax Analysis. Bottom-Up Parsing

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

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

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

MODULE 14 SLR PARSER LR(0) ITEMS

Bottom-Up Parsing LR Parsing

CSE302: Compiler Design

Chapter 4: LR Parsing

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

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

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

Lecture 7: Deterministic Bottom-Up Parsing

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

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

Compiler Construction 2016/2017 Syntax Analysis

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

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

CS 4120 Introduction to Compilers

Bottom-Up Parsing II (Different types of Shift-Reduce Conflicts) Lecture 10. Prof. Aiken (Modified by Professor Vijay Ganesh.

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

Lecture 8: Deterministic Bottom-Up Parsing

UNIT-III BOTTOM-UP PARSING

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

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

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

UNIT III & IV. Bottom up parsing

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Concepts Introduced in Chapter 4

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

Panic-mode error recovery. Top-down parsing with a parsing table (once more)

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

Bottom-Up Parsing. Lecture 11-12

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.

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

Bottom-Up Parsing. Lecture 11-12

LR Parsing Techniques

shift-reduce parsing

LR Parsing E T + E T 1 T

Parser Generation. Bottom-Up Parsing. Constructing LR Parser. LR Parsing. Construct parse tree bottom-up --- from leaves to the root

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

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.

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

In One Slide. Outline. LR Parsing. Table Construction

Monday, September 13, Parsers

Parsing III. (Top-down parsing: recursive descent & LL(1) )

How do LL(1) Parsers Build Syntax Trees?


Bottom Up Parsing Handout. 1 Introduction. 2 Example illustrating bottom-up parsing

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

Wednesday, August 31, Parsers

Parsing - 1. What is parsing? Shift-reduce parsing. Operator precedence parsing. Shift-reduce conflict Reduce-reduce conflict

4. Lexical and Syntax Analysis

4. Lexical and Syntax Analysis

Syntax Analyzer --- Parser

LR(0) Parsers. CSCI 3130 Formal Languages and Automata Theory. Siu On CHAN. Fall Chinese University of Hong Kong 1/31

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

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

Wednesday, September 9, 15. Parsers

Parsers. What is a parser. Languages. Agenda. Terminology. Languages. A parser has two jobs:

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

Conflicts in LR Parsing and More LR Parsing Types

The role of the parser

Chapter 4. Lexical and Syntax Analysis

Types of parsing. CMSC 430 Lecture 4, Page 1

Top-Down Parsing and Intro to Bottom-Up Parsing. Lecture 7

CA Compiler Construction

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

CS 406/534 Compiler Construction Parsing Part I

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

LR Parsing LALR Parser Generators

LL(1) predictive parsing

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

CSCI312 Principles of Programming Languages

Syntax Analysis. Martin Sulzmann. Martin Sulzmann Syntax Analysis 1 / 38

Parsing. Earley Parsing. Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 39

LR Parsing Techniques

Midterm I (Solutions) CS164, Spring 2002

Syntactic Analysis. Top-Down Parsing

Ambiguous Grammars and Compactification


LR Parsing LALR Parser Generators

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

Parsing. Cocke Younger Kasami (CYK) Laura Kallmeyer. Winter 2017/18. Heinrich-Heine-Universität Düsseldorf 1 / 35

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

Computer Science 160 Translation of Programming Languages

Parsing Part II (Top-down parsing, left-recursion removal)

Roll No. :... Invigilator's Signature :. CS/B.Tech(CSE)/SEM-7/CS-701/ LANGUAGE PROCESSOR. Time Allotted : 3 Hours Full Marks : 70

LR Parsing. Table Construction

Transcription:

SLR parsers LR(0) items As we have seen, in order to make shift-reduce parsing practical, we need a reasonable way to identify viable prefixes (and so, possible handles). Up to now, it has not been clear that this can be done without considering the whole stack contents (to decide whether the string on the stack is a prefix of at least one right-sentential form whose rightmost handle extends to at least the end of the stack contents). Remarkably, in LR parsers this is nicely done, using a DFA to recognize viable prefixes! We ll consider approaches to LR parsing, of varying complexity and generality, beginning with the simplest SLR parsing. (SLR S simple L left-to-right scanning R rightmost derivation ) Definition An LR(0) item, or simply an item, of a grammar G is a production of G with a single dot inserted at some position in the rhs. For example, the production S ABC yields four items S ABC A BC AB C ABC The production A ǫ yields a single item A Intuitively, an item indicates how much of the rhs of a production we have accounted for at a given point in a parse. For instance, the item S A BC indicates that we have seen a string derivable from A and hope to see a string derivable from BC. 1 2

When an item is valid for a viable prefix Definition An item A β 1 β 2 is valid for a viable prefix αβ 1 if there is a γ s.t. αaγ is a right-sentential form from which there is a rightmost derivation of αβ 1 β 2 γ. Observation It follows that A β 1 β 2 is valid for a viable prefix αβ 1 iff there is a γ s.t. A β 1 β 2 is handle for αβ 1 β 2 γ in the position following α. Intuitively, this means that the production corresponding to the item may turn out to be a handle. An item A β 1 β 2 is valid for a viable prefix αβ 1 if there is a γ s.t. αaγ is a right-sentential form from which there is a rightmost derivation of αβ 1 β 2 γ. A a b is valid for ca, since cad cabd. A a is valid for ca, since cad cad. Is S ca d valid for ca? A ab is valid for cab, since cad cabd. Example S cad A ab a S ca d is valid for ca, since S cad. S cad is valid for cad, since S cad. S cad is valid for ǫ, since S cad. S c Ad is valid for c, since S cad. A ab is valid for c, since cad cabd. A a is valid for c, since cad cad. 3 Roughly, if an item whose rhs is dotted at the far right is valid for the stack contents, we can reduce. (Why? There is something we could add to the stack contents to obtain a right-sentential form for which the valid item s production is a handle at the top of the stack. Note that, in this formulation, we do not take into account any lookahead.) So we want a nice way to identify valid items during the course of a parse... 4

STACK INPUT ACTION $ cad$ shift $c ad$ shift $ca d$ A a $ca d$ shift $cad $ S cad $S $ accept S cad is valid for ǫ, since S cad. Initial stack contents are viable; valid item corresponds to lone S-production. (At start, valid items include left-dotted versions of the S-productions. And, if any of those items have a variable X after the dot, then the valid items include the left-dotted versions of the X-productions, and so on.) S c Ad is valid for c, since S cad. A ab is valid for c, since cad cabd. A a is valid for c, since cad cad. c is a viable prefix, with 3 valid items, the first of which corresponds to the prior valid item. The other 2 valid items represent, roughly speaking, the available ways to get an A onto the stack after the c, which a successful parse in the grammar must do. That is, we ll have to reduce using an A-production in order to move right (over the A) in the 1st item. 5 STACK INPUT ACTION $ cad$ shift $c ad$ shift $ca d$ A a $ca d$ shift $cad $ S cad $S $ accept A a b is valid for ca, since cad cabd. A a is valid for ca, since cad cad. ca is a viable prefix, with 2 valid items, corresponding to 2 of the prior valid items. Notice that we ve gotten to the right end of the 2nd valid item it is now possible to reduce with it, and produce that A on the stack that we need. S ca d is valid for ca, since S cad. ca is viable, with valid item corresponding to one of the valid items for c. S cad is valid for cad, since S cad. cad is viable, with one valid item corresponding to the unique valid item for ca. S is viable, but with no valid item. (The start symbol is always viable.) 6

An NFA that accepts the viable prefixes and identifies the valid items for each viable prefix Given a grammar with start symbol S, consider the augmented grammar with new start symbol S and additional production S S. We construct an NFA as follows: The states are the items of the augmented grammar. The initial state is S S. All states are accepting. For any pair of items of the form A α Xβ A αx β there is a transition from the first to the second labeled X. (Here X can be either a variable or a terminal.) For any pair of items of the form A α Bβ B γ there is an ǫ-transition from the first to the second. (Notice that B is a variable.) 7 The states are the items of the augmented grammar. For any two items of the forms A α Xβ and A αx β there is a transition from the first to the second labeled X. For any two items of the forms A α Bβ and B γ there is an ǫ-transition from the first to the second. All states are accepting. The initial state is S S. Claim 1 This NFA accepts the language of all viable prefixes (of the original grammar)! Claim 2 The states of the NFA that you can reach by reading a string α are exactly the items (of the augmented grammar) that are valid for α. Example S S S What states can the NFA reach by reading ǫ? (What is ǫ-closure({s S})?) {S S, S cad} What states can the NFA reach by reading c? {S c Ad, A ab, A a} What states can the NFA reach by reading ca? { A a b, A a } 8

The states are the items of the augmented grammar. For any two items of the forms A α Xβ and A αx β there is a transition from the first to the second labeled X. For any two items of the forms A α Bβ and B γ there is an ǫ-transition from the first to the second. All states are accepting. The initial state is S S. So we can construct an NFA that accepts only viable prefixes identifies valid items for each viable prefix In the standard way, we can reduce this NFA to a DFA. S S S Example S S S What states can the NFA reach by reading cab? Recall that ca takes the NFA to { A a b, A a }. So cab goes to {A ab }. What states can the NFA reach by reading ca? Recall that c takes the NFA to {S c Ad, A ab, A a} What states can the NFA reach by reading cad? What states can the NFA reach by reading a? Recall: ǫ-closure({s S}) = {S S, S cad}. Neither of those states has an outgoing a-transition. In the DFA, the states are: 0. {S S, S cad} 1. {S c Ad, A ab, A a} 2. { A a b, A a } 3. {A ab } 4. {S ca d} 5. {S cad } 6. {S S } We will use this DFA to guide the actions of a shift-reduce parser. 9 10

For next time Continue reading 4.7. 11