CSE302: Compiler Design

Similar documents
CSE302: Compiler Design

LR Parsing - The Items

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

SLR parsers. LR(0) items

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

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

Formal Languages and Compilers Lecture VII Part 3: Syntactic A


CSE302: Compiler Design

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

Context-free grammars

Compiler Construction: Parsing

Bottom-Up Parsing LR Parsing

Compiler Construction 2016/2017 Syntax Analysis

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

Principles of Programming Languages

ECE 468/573 Midterm 1 October 1, 2014

Lecture Bottom-Up Parsing

CSE302: Compiler Design

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

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

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

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

Bottom-Up Parsing II. Lecture 8

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

4. Lexical and Syntax Analysis

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

4. Lexical and Syntax Analysis

MODULE 14 SLR PARSER LR(0) ITEMS

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

CS 314 Principles of Programming Languages

Bottom-Up Parsing. Lecture 11-12

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

Lexical and Syntax Analysis. Bottom-Up Parsing

UNIT III & IV. Bottom up parsing

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Conflicts in LR Parsing and More LR Parsing Types

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

Bottom-Up Parsing. Lecture 11-12

Lecture 8: Deterministic Bottom-Up Parsing

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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.

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

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

In One Slide. Outline. LR Parsing. Table Construction

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

Lecture 7: Deterministic Bottom-Up Parsing

CSE302: Compiler Design

Parsing. Roadmap. > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing

Concepts Introduced in Chapter 4

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

LALR Parsing. What Yacc and most compilers employ.

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

Compiler Construction Class Notes

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

Monday, September 13, Parsers

Types of parsing. CMSC 430 Lecture 4, Page 1

How do LL(1) Parsers Build Syntax Trees?

CSE302: Compiler Design

CS 4120 Introduction to Compilers

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

Downloaded from Page 1. LR Parsing

Bottom-up Parser. Jungsik Choi

Compilers. Yannis Smaragdakis, U. Athens (original slides by Sam

Some Basic Definitions. Some Basic Definitions. Some Basic Definitions. Language Processing Systems. Syntax Analysis (Parsing) Prof.

LR Parsers. Aditi Raste, CCOEW

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

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.

ECE 468/573 Midterm 1 September 30, 2015

CS415 Compilers. LR Parsing & Error Recovery

LR Parsing Techniques

UNIT-III BOTTOM-UP PARSING

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

CSE 105 THEORY OF COMPUTATION

CS502: Compilers & Programming Systems

Syntax Analysis Part I

CSCI312 Principles of Programming Languages

3. Parsing. Oscar Nierstrasz

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

CSE302: Compiler Design

LL(1) predictive parsing

Table-Driven Parsing

VIVA QUESTIONS WITH ANSWERS

Parser. Larissa von Witte. 11. Januar Institut für Softwaretechnik und Programmiersprachen. L. v. Witte 11. Januar /23

CS 164 Handout 11. Midterm Examination. There are seven questions on the exam, each worth between 10 and 20 points.

LR Parsing LALR Parser Generators

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

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

Wednesday, August 31, Parsers

More Bottom-Up Parsing

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

LR Parsing. Table Construction

LR Parsing LALR Parser Generators

CSC 4181 Compiler Construction. Parsing. Outline. Introduction

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


Chapter 4. Lexical and Syntax Analysis

The Parsing Problem (cont d) Recursive-Descent Parsing. Recursive-Descent Parsing (cont d) ICOM 4036 Programming Languages. The Complexity of Parsing

Transcription:

CSE302: Compiler Design Instructor: Dr. Liang Cheng Department of Computer Science and Engineering P.C. Rossin College of Engineering & Applied Science Lehigh University March 20, 2007

Outline Recap LR(0) parsing and SLR(1) parsing General/Canonical LR(1) parsing Lookahead LR(1) / LALR(1) parsing Summary and homework

LR Parsing: A Schematic View Bottom-up parsing Rightmost derivations and right-sentential forms Parsing stack Input buffer Actions $ InputString$ lookahead zero or one token, decide S/R...... $StartSymbol $ accept

Finite Automata for Both LR(0) and SLR(1) Parsing Finite automata of parsing states LR(0) items are used to identify the parsing states A α A.α is an item (initial item) We may about to recognize A by A α A α. is also an item (complete item) α may be a handle for reduction A βγ A β.γ, A.βγ, and A βγ. are LR(0) items NFA construction Subset construction for NFA to DFA

LR(0) Parser NFA Construction X A α.xβ A αx.β Shift action if X is a terminal ε A α.xβ X.γ Reduction action if X is a non-terminal

The LR(0) and SLR(1) Parsing Algorithms LR(0) parsing If state s contains A α.xβ where X is a terminal, then shift and the state changes to s containing A αx.β If state s contains A γ., then reduce by A γ(stack ops) and the state changes to s containing B λa.η SLR(1) parsing If state s contains A α.xβ where X is a terminal, and the lookahead token is X, then shift and the state changes to s containing A αx.β If state s contains A γ., and the lookahead token is in FOLLOW(A), then reduce by A γ(stack ops) and the state changes to s containing B λa.η Examples S (S)S ε Input: () A (A) a Input: ((a))

Limits of LR(0) and SLR(1) Parsing LR(0) parsing cannot handle a grammar that in its DFA there is a state s s contains a shift item A α.xβ and a complete item B δ. s contains two complete items A γ. and B δ. SLR(1) parsing cannot handle a grammar that in its DFA there is a state s s contains a shift item A α.xβ with X a terminal and a complete item B δ. with X in Follow(B) s contains two complete items A γ. and B δ. with a nonempty Follow(A) Follow(B)

Observations SLR(1) parsing is more powerful than LR(0) parsing due to its consideration of lookaheads in the parsing process

Another Example Another example stmt call-stmt assign-stmt call-stmt identifier assign-stmt var=expr var identifier expr var number Is this an SLR(1) grammar? An equivalent grammar S id V=E V id E V n

Observations SLR(1) parsing is more powerful than LR(0) parsing due to its consideration of lookaheads in the parsing process However, the lookaheads are not used in the finite automata construction The limit of SLR(1) parsing can be improved if its NFA/DFA construction does not ignore lookaheads

Outline Recap LR(0) parsing and SLR(1) parsing General/Canonical LR(1) parsing Lookahead LR(1) / LALR(1) parsing Summary and homework

Finite Automata of Parsing States Finite automata for LR(1) parsers LR(1) items are used to identify the parsing states An LR(1) item is a pair consisting of an LR(0) item and a lookahead token [A α.β, a] NFA construction: transitions between LR(1) items Non-ε transitions Given an LR(1) item [A α.xβ, a], where X is any symbol, there is a transition on X to the item [A αx.β, a] ε-transitions Given an LR(1) item [A α.bγ, a], where B is a nonterminal, there are ε-transitions to items [B.β, b] for every production B βand for every token b in First(γa)

LR(1) NFA/DFA and Parsing Table Construction Examples Grammar S S S id V=E V id E V n NFA construction: transitions between LR(1) items Non-ε transitions Given an LR(1) item [A α.xβ, a], where X is any symbol, there is a transition on X to the item [A αx.β, a] ε-transitions Given an LR(1) item [A α.bγ, a], where B is a nonterminal, there are ε-transitions to items [B.β, b] for every production B βand for every token b in First(γa) Input: id=n

LR(1) Parsing Algorithm LR(1) parsing If state s contains [A α.xβ,a] where X is a terminal, and the lookahead token is X, then shift and the state changes to s containing [A αx.β,a] If state s contains [A γ.,a], and the lookahead token is a, then reduce by A γ(stack ops) and the state changes to s containing [B λa.η,b] LR(0) parsing If state s contains A α.xβ where X is a terminal, then shift and the state changes to s containing A αx.β If state s contains A γ., then reduce by A γ(stack ops) and the state changes to s containing B λa.η SLR(1) parsing If state s contains A α.xβ where X is a terminal, and the lookahead token is X, then shift and the state changes to s containing A αx.β If state s contains A γ., and the lookahead token is in FOLLOW(A), then reduce by A γ(stack ops) and the state changes to s containing B λa.η

Another LR(1) DFA and Parsing Table Construction Examples Grammar A A A (A) A a NFA construction: transitions between LR(1) items Non-ε transitions Given an LR(1) item [A α.xβ, a], where X is any symbol, there is a transition on X to the item [A αx.β, a] ε-transitions Given an LR(1) item [A α.bγ, a], where B is a nonterminal, there are ε-transitions to items [B.β, b] for every production B βand for every token b in First(γa) Compare the LR(1) DFA with the LR(0) DFA

Outline Recap LR(0) parsing and SLR(1) parsing General/Canonical LR(1) parsing Lookahead LR(1) / LALR(1) parsing Summary and homework

Two Principles of LALR(1) Parsing The core of a state in LR(1) DFA is a state in the LR(0) DFA. Given two states s1 and s2 in the LR(1) DFA that have the same core. Suppose there is a transition on X from s1 to a state t1. Then there is also a transition on X from s2 to a state t2, and t1 and t2 have the same core. Therefore based on LR(1) DFA, we can transform it to a DFA that is identical to the LR(0) DFA, except that each state consists of items with sets of lookaheads.

Constructing LALR(1) DFA Identifying all states that have the same core and forming the union of the lookaheads for each LR(0) item Linking the new states based on the links in the LR(1) DFA An example A A A (A) A a

Outline Recap Bottom-up parsing (Section 4.5) Summary and homework