Bottom-Up Parsing LR Parsing

Similar documents
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

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

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

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

Bottom-Up Parsing II. Lecture 8

Principles of Programming Languages

MODULE 14 SLR PARSER LR(0) ITEMS

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

SLR parsers. LR(0) items

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

CSE302: Compiler Design

More Bottom-Up Parsing

LR Parsing LALR Parser Generators

shift-reduce parsing

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

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

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

Bottom-Up Parsing. Lecture 11-12

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

LR Parsing Techniques

Bottom Up Parsing. Shift and Reduce. Sentential Form. Handle. Parse Tree. Bottom Up Parsing 9/26/2012. Also known as Shift-Reduce 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.

ECE 468/573 Midterm 1 October 1, 2014

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Bottom-Up Parsing. Lecture 11-12

UNIT-III BOTTOM-UP PARSING

Monday, September 13, Parsers

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

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

CS 4120 Introduction to Compilers

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

LR Parsing LALR Parser Generators

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


LALR Parsing. What Yacc and most compilers employ.

LR Parsers. Aditi Raste, CCOEW

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

LL(k) Parsing. Predictive Parsers. LL(k) Parser Structure. Sample Parse Table. LL(1) Parsing Algorithm. Push RHS in Reverse Order 10/17/2012

Wednesday, August 31, Parsers

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

Downloaded from Page 1. LR Parsing


UNIT III & IV. Bottom up parsing

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

Lecture Bottom-Up Parsing

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

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

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

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

Exercises II. Exercise: Lexical Analysis

Wednesday, September 9, 15. Parsers

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

Compiler Construction 2016/2017 Syntax Analysis

Lexical and Syntax Analysis. Bottom-Up Parsing

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

LR Parsing - The Items

CSC 4181 Compiler Construction. Parsing. Outline. Introduction

CS 164 Programming Languages and Compilers Handout 9. Midterm I Solution

Bottom-Up Parsing. Parser Generation. LR Parsing. Constructing LR Parser

Conflicts in LR Parsing and More LR Parsing Types

Context-free grammars

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

Compiler Construction: Parsing

A bottom-up parser traces a rightmost derivation in reverse. Bottom-Up Parsing. Bottom-up parsing is more general than topdown.

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

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

CS143 Midterm Sample Solution Fall 2010

LALR stands for look ahead left right. It is a technique for deciding when reductions have to be made in shift/reduce parsing. Often, it can make the

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

LR Parsing E T + E T 1 T

CS1622. Today. A Recursive Descent Parser. Preliminaries. Lecture 9 Parsing (4)

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

Note that for recursive descent to work, if A ::= B1 B2 is a grammar rule we need First k (B1) disjoint from First k (B2).

Concepts Introduced in Chapter 4

Compilers. Predictive Parsing. Alex Aiken

In One Slide. Outline. LR Parsing. Table Construction

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

CS606- compiler instruction Solved MCQS From Midterm Papers

Lecture 8: Deterministic Bottom-Up Parsing

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

Lecture 7: Deterministic Bottom-Up Parsing

Top down vs. bottom up parsing

Administrativia. WA1 due on Thu PA2 in a week. Building a Parser III. Slides on the web site. CS164 3:30-5:00 TT 10 Evans.

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

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

Acknowledgements. The slides for this lecture are a modified versions of the offering by Prof. Sanjeev K Aggarwal

8 Parsing. Parsing. Top Down Parsing Methods. Parsing complexity. Top down vs. bottom up parsing. Top down vs. bottom up parsing

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

LR Parsing Techniques

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

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

CS415 Compilers. LR Parsing & Error Recovery

LR Parsing - Conflicts

Algorithms for NLP. LR Parsing. Reading: Hopcroft and Ullman, Intro. to Automata Theory, Lang. and Comp. Section , pp.

Types of parsing. CMSC 430 Lecture 4, Page 1

Table-Driven Parsing

4. Lexical and Syntax Analysis

Syntax Analyzer --- Parser

CSCI312 Principles of Programming Languages

Transcription:

Bottom-Up Parsing LR Parsing Maryam Siahbani 2/19/2016 1

What we need for LR parsing LR0) states: Describe all possible states in which parser can be Parsing table ransition between LR0) states Actions to take when transiting: shift, reduce, accept, error 2/19/2016 2

LR0) items For any production we can build a set of items by placing somewhere on the rhs he items for ): ) ) ) ) he only item for is 2/19/2016 3

Viable Prefixes γ is a viable prefix if there is some ω such that γ ω is a state of a shift-reduce parser γ ω stack rest of input Important fact: A viable prefix is a prefix of a handle An LR0) item [X ] says that is on top of the stack is a suffix of γ) he parser is looking for an X xpects to find input string derived from We can recognize viable prefixes via a NfA DFA) States of NFA are LR0) items States of DFA are sets of LR0) items LR0) states) 2/19/2016 4

Recognizing Viable Prefixes 1. Add a dummy production S S to G 2. States of NFA are LR0) items of G including dummy production 3. ransition between items: [ X ] X [ X ] X can be terminal or nonterminal) For item [ X ] and production X γ, add: [ X ] 4. very state is accepting state 5. Start from [S S] [X γ] X is non-terminal) Note: NFA reads states from bottom of the stack to the top 2/19/2016 5

) S S NFA for Recognizing VP ) ) S + * ) 2/19/2016 6 ) ) + + + + + * * * * *

S S NFA for Recognizing VP + S + * ) 2/19/2016 7

) S S NFA for Recognizing VP + * S + * ) 2/19/2016 8

) S S NFA for Recognizing VP + + * S + * ) 2/19/2016 9

) S S NFA for Recognizing VP ) + + * S + * ) 2/19/2016 10

) S S NFA for Recognizing VP ) + + * ) S + * ) 2/19/2016 11

) S S NFA for Recognizing VP ) + + * ) ) ) S + * ) 2/19/2016 12

) S S NFA for Recognizing VP ) + + * ) ) ) + S + + * ) 2/19/2016 13

) S S NFA for Recognizing VP ) + + * ) ) ) + S + + * ) + 2/19/2016 14

) S S NFA for Recognizing VP ) + + + * ) ) ) S + + * ) + 2/19/2016 15

) S S NFA for Recognizing VP ) + + + * ) ) ) * S + + * ) + 2/19/2016 16

) S S NFA for Recognizing VP ) + + + * ) ) S + * ) 2/19/2016 17 * * ) * + +

) S S NFA for Recognizing VP ) ) ) S + * ) 2/19/2016 18 ) + + + + + * * * * *

) S S NFA for Recognizing VP ) ) S + * ) 2/19/2016 19 ) ) + + + + + * * * * *

S + * ) S S + * ) DFA for recognizing VP + + * * * ) * + + * ) * + 2/19/2016 20 ) ) ) ) + * )

LR0) states he states of DFA are LR0) states which are also called: Canonical collection of items Canonical collection of LR0) items Dragon book gives a different way of constructing LR0) states Section 4.6, 4.7) 2/19/2016 21

Valid Items An item I is valid for a viable prefix if DFA recognizing viable prefixes terminates) terminates on input in a state s containing I he items in s describe what the top of the item stack might be after reading input 2/19/2016 22

Valid Items An item is often valid for many prefixes xample: the item ) is valid for prefixes:... 2/19/2016 23

S + * ) S S + * ) DFA for recognizing VP + + * * * ) * + + * ) * + 2/19/2016 24 ) ) ) ) + * )

Assume: Stack contains Next input is t LR0) Parsing DFA on input terminates in state s Reduce by X if S contains item X Shift if s contains item X tω quivalent to saying s has a transition labeled t 2/19/2016 25

LR0)-conditions LR0) has a reduce-reduce conflicts if: Any state has two reduce items: X and Y ω LR0) has shift-reduce conflicts if: Any state has a reduce item and a shift item: X and Y ω t γ If there is any conflict, grammar is not LR0) 2/19/2016 26

S + * ) S S + * ) Shift-reduce conflict LR0) Parsing + + * * * ) * + + * ) * + 2/19/2016 27 ) ) ) ) + * )

Assume: Stack contains Next input is t SLR Parsing DFA on input terminates in state s Reduce by X if s contains item X t FollowX) Shift if If there is still conflicts under hese rules, grammar is not SLR s contains item X tω 2/19/2016 28

S + * ) S S + * ) Follow)={$,)} Follow)={$,),+} + SLR Parsing + * * * ) * + + * ) * + 2/19/2016 29 ) ) ) ) + * )

SLR Parsing Let M be DFA for viable prefixes of G Let x 1 x n $ be initial configuration Repeat until configuration is S $ Let ω be current configuration Run M on current stack If M rejects, report parsing error Stack is not a viable prefix If M accepts with items I, let a be the next input Shift [X a γ] I Reduce if [X ] I and a FollowX) If there is any conflict in the last step more than two valid action), grammar is not SLRk) in practice k=1 Report parsing error if neither applies 2/19/2016 30

race * configuration Stack input) * $ DFA halt state Action 2/19/2016 31

S + * ) 2 S S 1 + * ) 11 5 + 3 * * * ) * * $ + 6 4 + + * ) * 2/19/2016 32 ) 10 ) ) 9 + 8 ) + * ) 7

race * configuration Stack input) * $ * $ DFA halt state Action 1 Shift 2/19/2016 33

S + * ) 2 S S 1 + * ) Follow)={$,),+} * $ 11 5 + + 3 * * * ) * 6 4 + + * ) * 2/19/2016 34 ) 10 ) ) 9 + 8 ) + * ) 7

race * configuration Stack input) * $ * $ * $ DFA halt state 1 3 * Follow) Action Shift Shift 2/19/2016 35

S + * ) 2 S S 1 + * ) 11 5 + + 3 * * * ) * 6 4 * $ + + * ) * 2/19/2016 36 ) 10 ) ) 9 + 8 ) + * ) 7

race * configuration Stack input) * $ * $ * $ * $ DFA halt state 1 3 * Follow) 11 Action Shift Shift Shift 2/19/2016 37

S + * ) 2 S S 1 + * ) Follow)={$,),+} * $ 11 5 + + 3 * * * ) * 6 4 + + * ) * 2/19/2016 38 ) 10 ) ) 9 + 8 ) + * ) 7

race * configuration Stack input) * $ * $ * $ * $ * $ DFA halt state 1 3 * Follow) 11 3 $ Follow) Action Shift Shift Shift Reduce 2/19/2016 39

S + * ) 2 S S 1 + * ) Follow)={$,),+} * $ 11 5 + + 3 * * * ) * 6 4 + + * ) * 2/19/2016 40 ) 10 ) ) 9 + 8 ) + * ) 7

race * configuration Stack input) * $ * $ * $ * $ * $ $ DFA halt state 1 3 * Follow) 11 3 $ Follow) 4 $ Follow) Action Shift Shift Shift Reduce Reduce * 2/19/2016 41

S + * ) 2 S S 1 + * ) Follow)={$,)} 11 5 + + 3 * * * ) * 6 4 $ + + * ) * 2/19/2016 42 ) 10 ) ) 9 + 8 ) + * ) 7

race * configuration Stack input) * $ * $ * $ * $ * $ $ $ DFA halt state 1 3 * Follow) 11 3 $ Follow) 4 $ Follow) 5 $ Follow) Action Shift Shift Shift Reduce Reduce * Reduce 2/19/2016 43

race * configuration Stack input) * $ * $ * $ * $ * $ $ $ DFA halt state 1 3 * Follow) 11 3 $ Follow) 4 $ Follow) 5 $ Follow) Action Shift Shift Shift Reduce Reduce * Reduce Accept 2/19/2016 44

S + * ) 2 S S 1 + * ) 11 5 + + 3 * * * ) * 6 4 + + * ) * 2/19/2016 45 ) 10 ) ) 9 + 8 ) + * ) 7

Constructing SLR states Begin with item S S, calculate related items closure) Determine following states: what states can be reached on a single input token or non-terminal GOO) Construct closure of each resulting states 2/19/2016 46

Closure ClosureI, G): C=I repeat /* I : set of items */ for each [X Y ] C for each Y γ G /* for each production Y*/ if [Y γ] C add [Y γ] to C until no change in C return C 2/19/2016 47

Closure Initial item: [S ] C= {[S ]} First pass: C= {[S ], [ ], [ +]} Second pass: S + C= {[S ], [ ], [ +], [ ], [ *], [ )]} * ) 2/19/2016 48

Closure example Closure results in a new state in DFA S 1 + * ) S + * ) 2/19/2016 49

New states GOO function o determine possible states reachable from existing states use goto function GOOI, X, G): /* X is an input token or a nonterminal*/ C={} for each [Y X ] I /* can be empty*/ add [Y X ] to C return ClosureC, G) 2/19/2016 50

S 1 + * ) GOO example 3 * ) 8 + * ) S + * ) 2/19/2016 51

Sets-of-Items Construction LR0) states function itemsg ) /*G augmented grammar*/ C = { closure{s S}, G) } repeat foreach I C do foreach X N U ) do /*N: non-terminals*/ /*: input tokens*/ C = C U { GOOI, X,G) } until no change to C return C /*set of all LR0) states*/ 2/19/2016 52

S + * ) 2 S S 1 + * ) 11 5 + + 3 * * * ) * 6 + + * ) 4 * 2/19/2016 53 ) 10 ) ) 9 + 7 8 ) + * )

Constructing SLR Parsing able Rows are LR0) states Columns are input tokens and non-terminals and $) 1. Construct LR0) states the initial state is the one containing S S) 2. For each transition A X B in the set of states, add an action shift B in column X for row A 3. For each item [Y α] A, add an action reduce Y α in columns X FollowY) for row A 2/19/2016 54

1) + 2) 3) 4) * 5) ) R5: reduce to rule 5 S8: - if on top of the stack: shift and go to state 8 - else: go to state 8 1. Grammar is not SLR if there is more than one action in any table entry 2. mpty entries are errors SLR Parsing able Action/Goto able * + ) $ 1 S8 S3 5 2 2 ACC! 3 S11 R3 R3 R3 4 R4 R4 R4 5 S6 R2 R2 6 S8 S3 5 7 7 R1 R1 8 S8 S3 5 9 9 S10 10 R5 R5 R5 11 S8 S3 4 2/19/2016 55

SLR1) Conditions A grammar is SLR1) if for each LR0) state item sets): For any item [A x ], x there is no item [B ], x FollowB) For any two items [A ] and [B ] FollowA) FollowB) = LR0) Grammars SLR1) Grammars 2/19/2016 56

S S S AxB S B A yb A z B A S S 1 S AxB S B A yb A z B A S S S 2 Is this grammar SLR1)? S A xb B A B FollowB)={$,x} A z S B y 4 3 S y B B A A yb A z z A z S Ax B B A A yb A z 2/19/2016 57 y 6 x 5 y B A z A 8 B S AxB B A A yb 9 7 10

1) S AxB 2) S B 3) A yb 4) A z 5) B A Grammar is not SLR Reduce is a bad choice SLR Parsing able x y z $ S A B 1 S5 S6 2 3 4 2 ACC! 3 S8,R5 R5 4 R2 5 S5 S6 7 9 6 R4 R4 7 R5 R5 8 S5 S6 7 10 9 R3 R3 10 R1 2/19/2016 58

LR1) Parsing Limit roduced by SLR parsing in using Follow set to decide reductions Idea: augment LR items with 1 character lookahead [B A, $] making an LR1) item Reduce to B only if lookahead token is $ More accurate than just Follow set Similar to SLR parsing just use LR1) items rather than LR0) items 2/19/2016 59