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

Similar documents
Syntax Analysis Check syntax and construct abstract syntax tree

Introduction to Bottom-Up Parsing


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

Bottom-Up Parsing. Lecture 11-12

Syntax Analysis. Prof. James L. Frankel Harvard University. Version of 6:43 PM 6-Feb-2018 Copyright 2018, 2015 James L. Frankel. All rights reserved.

Context-free grammars

Bottom-Up Parsing. Lecture 11-12

Compiler Construction: Parsing

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.

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

Chapter 4: LR Parsing

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

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

Parsing III. CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones

Outline. The strategy: shift-reduce parsing. Introduction to Bottom-Up Parsing. A key concept: handles

Chapter 4. Lexical and Syntax Analysis

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

Lecture 8: Deterministic Bottom-Up Parsing

University of Technology Department of Computer Sciences. Final Examination st Term. Subject:Compilers Design

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

Lecture 7: Deterministic Bottom-Up Parsing

MODULE 12 LEADING, TRAILING and Operator Precedence Table

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

SLR parsers. LR(0) items

UNIT-III BOTTOM-UP PARSING

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

Chapter 4. Lexical and Syntax Analysis. Topics. Compilation. Language Implementation. Issues in Lexical and Syntax Analysis.

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

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

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

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

COP4020 Programming Languages. Syntax Prof. Robert van Engelen

Intro to Bottom-up Parsing. Lecture 9

Compiler Design Concepts. Syntax Analysis

CS 314 Principles of Programming Languages

VIVA QUESTIONS WITH ANSWERS

4. Lexical and Syntax Analysis

CS502: Compilers & Programming Systems

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

Compiler Construction 2016/2017 Syntax Analysis

4. Lexical and Syntax Analysis

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

LR Parsing Techniques

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

More Bottom-Up Parsing

Bottom-Up Parsing II. Lecture 8

Introduction to Syntax Analysis

컴파일러입문 제 6 장 구문분석

Parsing Techniques. CS152. Chris Pollett. Sep. 24, 2008.

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

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

CSE302: Compiler Design

3. Parsing. Oscar Nierstrasz

In One Slide. Outline. LR Parsing. Table Construction

Syntax Analysis Part I

Lecture Bottom-Up Parsing

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

Lexical and Syntax Analysis. Bottom-Up Parsing

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

Introduction to Syntax Analysis. The Second Phase of Front-End

Question Points Score

CSE302: Compiler Design

Bottom-up Parser. Jungsik Choi

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

Concepts Introduced in Chapter 4

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

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

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

Types of parsing. CMSC 430 Lecture 4, Page 1

CS 4120 Introduction to Compilers

Principles of Programming Languages

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. Context-free grammars & parsing

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

Specifying Syntax. An English Grammar. Components of a Grammar. Language Specification. Types of Grammars. 1. Terminal symbols or terminals, Σ

LR Parsing LALR Parser Generators

Introduction to Lexing and Parsing

Formal Languages and Compilers Lecture V: Parse Trees and Ambiguous Gr

Defining syntax using CFGs

CS 321 Programming Languages and Compilers. VI. Parsing

Syntactic Analysis. Top-Down Parsing

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

MODULE 14 SLR PARSER LR(0) ITEMS

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

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

CS415 Compilers. Syntax Analysis. These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University

Part 5 Program Analysis Principles and Techniques

Parsing Part II. (Ambiguity, Top-down parsing, Left-recursion Removal)

LANGUAGE PROCESSORS. Presented By: Prof. S.J. Soni, SPCE Visnagar.

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

LR Parsing LALR Parser Generators

CS 406/534 Compiler Construction Parsing Part I

UNIT III & IV. Bottom up parsing

Chapter 3. Describing Syntax and Semantics ISBN

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

MIT Specifying Languages with Regular Expressions and Context-Free Grammars

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

CSCI312 Principles of Programming Languages

Transcription:

Parsing - 1 What is parsing? Shift-reduce parsing Shift-reduce conflict Reduce-reduce conflict Operator precedence parsing Parsing-1 BGRyder Spring 99 1

Parsing Parsing is the reverse of doing a derivation By looking at the terminal string, effectively try to build the parse tree from the bottom up Finding which sequences of terminals and nonterminals form the right hand side of production and reducing them to the left hand side nonterminal Parsing-1 BGRyder Spring 99 2

Shift-reduce Parsing Handle- substring which is right hand side of some production; corresponds to the last expansion in a rightmost derivation Replacement of handle by its corresponding nonterminal left hand side, results in reduction to the distinguished nonterminal by a reverse rightmost derivation Parse works by shifting symbols onto the stack until have handle on top; then reduce; then continue Parsing-1 BGRyder Spring 99 3

S Ε (1) Ε Ε + Τ (2) Ε T (3) T id (4) Rightmost derivation of a+b+c, handles in red S E Ε + Τ Ε + id Ε + Τ + id Ε + id + id T + id + id id + id + id Example E + T T id S E E + T id id Parsing-1 BGRyder Spring 99 4

Example Actions: shift, reduce, accept, error Stack Input Action $ id1 + id2 + id3 $ shift $ id1 + id2 + id3 $ reduce (4) $ T + id2 + id3 $ reduce (3) $ E + id2 + id3 $ shift $ E + id2 + id3 $ shift $ E + id2 + id3 $ reduce(4) $ E + T + id3 $ reduce (2) $ E + id3 $ shift $ E + id3 $ shift $ E + id3 $ reduce (4) $ E + T $ reduce(2) $ E $ reduce (1) $ S $ accept S Ε (1) Ε Ε + Τ (2) Ε T (3) T id (4) Parsing-1 BGRyder Spring 99 5

Possible Problems Can get into conflicts where one rule implies shift while another implies reduce S if E then S if E then S else S On stack: if E then S Input: else Should shift trying for 2nd rule or reduce by first rule? Parsing-1 BGRyder Spring 99 6

Possible Problems Can have two grammar rules with same right hand side which leads to reduce-reduce conflicts A α and B α both in grammar When α on top of the stack, how know which production choose? That is, whether to reduce to A or B? In both kinds of conflicts, problem is with the grammar, not necessarily the language Recall, there can be many context-free grammars corresponding to the same language! Parsing-1 BGRyder Spring 99 7

Shift-Reduce Parsing Actions Shift - push token onto stack Reduce - remove handle from stack and push on corresponding nonterminal Accept - recognize sentence when stack contains only the distinguished symbol and input is empty Error - happens when none of the above is possible; means original input was not a sentence! Parsing-1 BGRyder Spring 99 8

Handles Any string of terminals and nonterminals derived from the distinguished nonterminal is called a sentential form If grammar is unambiguous, then each right sentential form has a unique handle * Z α A w α β w, rm rm where α is a mixture of terminals and nonterminals; β is the handle; and w is a string of terminals Parsing-1 BGRyder Spring 99 9

A Handle in the Parse Tree Z Action: reduce β to A A α β w Parsing-1 BGRyder Spring 99 10

Ambiguity Example Ζ E E E or E a Two rightmost derivations (handles in red): Z E E or E E or a E or E or a E or a or a a or a or a Z E E or E E or E or E Ε or E or a E or a or a a or a or a Shift a, reduce to E, shift or, shift a, reduce to E (now have E or E on stack). In deriv1, reduce E or E to E. In deriv2 shift or and a onto stack. SHIFT-REDUCE conflict. Parsing-1 BGRyder Spring 99 11

Justification of Handle Use How can we be sure that the handle will always be at the top of the stack? Conventions: Greek letters for strings of terminals and nonterminals. Arabic letters for strings of terminals only. Capital letters are nonterminals. The following is a rightmost derivation: Case 1: A s production contains a rightmost nonterminal B. Z * α A q α β B y q α β γ y q, where rm rm rm B γ Parsing-1 BGRyder Spring 99 12

Justification, cont. Stack will contain $αβγ with yq in the input. This will be reduced to $αβb with yq still in the input. Handle can t be below B in the stack or else the derivation would have to have been: X B δ B with δ in the αβ on the stack. But this isn t a rightmost derivation, because B is to the right of X and X is being expanded first! #CONTRADICTION Parsing-1 BGRyder Spring 99 13

Justification, cont. Therefore handle must contain B and it is not buried in the stack. Assume the handle is βby (β or y may be empty) Case 2: A s production does not contain a nonterminal * Z α C x A r α C x y r α γ x y r rm rm where A y and C γ rm Parsing-1 BGRyder Spring 99 14

Justification, cont. Stack will contain $αγ with input xyr. This will be reduced to $αc, and then x and y will be shifted onto stack. Then $αcxy will be reduced to $αcxa on the stack with r remaining in the input. So the handle is not buried in the stack. Parsing-1 BGRyder Spring 99 15

Operator Precedence Parsing A simplified bottom up parsing technique used for expression grammars Requires No right hand side of rule is empty No right hand side has 2 adjacent nonterminals Drawbacks Small class of grammars qualify Overloaded operators are hard (unary minus) Parser correctness hard to prove ASU, Ch 4.6 Parsing-1 BGRyder Spring 99 16

Operator Precedence Define three precedence relations a < b, a yields in precedence to b a > b, a takes precedence over b a = b, a has same precedence as b Find handle as <====> pattern at top of stack; Check relation between top of stack and next input symbol Basically, ignore nonterminals Parsing-1 BGRyder Spring 99 17

Example Z E E Ε Ε Ε + Ε id Define precedence relations between + and *. + < *, * > +, + > +, * > * (last 2 ensure left associativity) Form table of precedences. Now parse using the table, and keep track of the operand nonterminals, too. Sometimes can embed error handling in matrix. id + * $ id > > > + < > < > * < > > > $ < < < Parsing-1 BGRyder Spring 99 18

Example Compare top of stack token to next input token. Stack Compares Input $ < id1 + id2 * id3 $ $ < id1 > + id2 * id3 $ $ E < + id2 * id3 $ $ E + < id2 * id3 $ $ E + < id2 > * id3 $ $ E + E < * id3 $ $ E + E * < id3 $ $ E + E * < id3 > $ $ E + < E * E > $ $ < E + E > $ $ < E > $ accept Parsing-1 BGRyder Spring 99 19

Making OP parsing practical How to store these precedences compactly? Precedence functions Find functions f(), g() such that f(token1) > g(token2) means token1 > token2 f(token1)=g(token2) means token1 = token2 f(token1) < g(token2) means token1 < token2 Graph partitioning algorithm to find f(),g() if possible. Parsing-1 BGRyder Spring 99 20

Precedence Functions Form graph from table of precedences Nodes formed by f(token1),f(token2),,g(token1) etc. Form equivalence classes of nodes based on the = relation (equal precedence, e.g., * / ) Edges show required relations between function values If token1 > token2, then f(token1)-->g(token2) If token1 < token2, then f(token1)<--g(token2) If the graph is acyclic, then can find integer value assignments for the range values of f,g. Let value of f(token1) be the length of the longest path from the node representing f(token1) Parsing-1 BGRyder Spring 99 21

Example id + * $ id > > > + < > < > * < > > > $ < < < f(id) g(id) g(+) g(*) g($) f(*) f(+) Acyclic graph yields id + * $ f: 4 2 4 0 g: 5 1 3 0 f($) Parsing-1 BGRyder Spring 99 22