# Bottom-Up Parsing. Lecture 11-12

Size: px
Start display at page:

Transcription

1 Bottom-Up Parsing Lecture (From slides by G. Necula & R. Bodik) 2/20/08 Prof. Hilfinger CS164 Lecture 11 1

2 Administrivia Test I during class on 10 March. 2/20/08 Prof. Hilfinger CS164 Lecture 11 2

3 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient Builds on ideas in top-down parsing Most common form is LR parsing L means that tokens are read left to right R means that it constructs a rightmost derivation 2/20/08 Prof. Hilfinger CS164 Lecture 11 3

4 An Introductory xample LR parsers don t need left-factored grammars and can also handle left-recursive grammars Consider the following grammar: + ( ) int Why is this not LL(1)? Consider the string: int + ( int ) + ( int ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 4

5 The Idea LR parsing reduces a string to the start symbol by inverting productions: sent input string of terminals while sent S: Identify first β in sent such that A β is a production and S * α A γ α β γ = sent Replace β by A in sent (so α A γ becomes new sent) Such α β s are called handles 2/20/08 Prof. Hilfinger CS164 Lecture 11 5

6 A Bottom-up Parse in Detail (1) int + (int) + (int) int + ( int ) + ( int ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 6

7 A Bottom-up Parse in Detail (2) int + (int) + (int) + (int) + (int) (handles in red) int + ( int ) + ( int ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 7

8 A Bottom-up Parse in Detail (3) int + (int) + (int) + (int) + (int) + () + (int) int + ( int ) + ( int ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 8

9 A Bottom-up Parse in Detail (4) int + (int) + (int) + (int) + (int) + () + (int) + (int) int + ( int ) + ( int ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 9

10 A Bottom-up Parse in Detail (5) int + (int) + (int) + (int) + (int) + () + (int) + (int) + () int + ( int ) + ( int ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 10

11 A Bottom-up Parse in Detail (6) int + (int) + (int) + (int) + (int) + () + (int) + (int) + () A reverse rightmost derivation int + ( int ) + ( int ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 11

12 Where Do Reductions Happen Because an LR parser produces a reverse rightmost derivation: If αβγ is step of a bottom-up parse with handle αβ And the next reduction is by A β Then γ is a string of terminals! Because αaγ αβγ is a step in a right-most derivation Intuition: We make decisions about what reduction to use after seeing all symbols in handle, rather than before (as for LL(1)) 2/20/08 Prof. Hilfinger CS164 Lecture 11 12

13 Notation Idea: Split the string into two substrings Right substring (a string of terminals) is as yet unexamined by parser Left substring has terminals and non-terminals The dividing point is marked by a I The I is not part of the string Marks end of next potential handle Initially, all input is unexamined: Ix 1 x 2... x n 2/20/08 Prof. Hilfinger CS164 Lecture 11 13

14 Shift-Reduce Parsing Bottom-up parsing uses only two kinds of actions: Shift: Move I one place to the right, shifting a terminal to the left string + (I int ) + (int I ) Reduce: Apply an inverse production at the handle. If + ( ) is a production, then + ( + ( ) I ) +( I ) 2/20/08 Prof. Hilfinger CS164 Lecture 11 14

15 Shift-Reduce xample I int + (int) + (int)\$ shift int + ( int ) + ( int )

16 Shift-Reduce xample I int + (int) + (int)\$ shift int I + (int) + (int)\$ red. int int + ( int ) + ( int )

17 Shift-Reduce xample I int + (int) + (int)\$ shift int I + (int) + (int)\$ red. int I + (int) + (int)\$ shift 3 times int + ( int ) + ( int )

18 Shift-Reduce xample I int + (int) + (int)\$ shift int I + (int) + (int)\$ red. int I + (int) + (int)\$ shift 3 times + (int I ) + (int)\$ red. int int + ( int ) + ( int )

19 Shift-Reduce xample I int + (int) + (int)\$ shift int I + (int) + (int)\$ red. int I + (int) + (int)\$ shift 3 times + (int I ) + (int)\$ red. int + ( I ) + (int)\$ shift int + ( int ) + ( int )

20 Shift-Reduce xample I int + (int) + (int)\$ shift int I + (int) + (int)\$ red. int I + (int) + (int)\$ shift 3 times + (int I ) + (int)\$ red. int + ( I ) + (int)\$ shift + () I + (int)\$ red. + () int + ( int ) + ( int )

21 Shift-Reduce xample I int + (int) + (int)\$ shift int I + (int) + (int)\$ red. int I + (int) + (int)\$ shift 3 times + (int I ) + (int)\$ red. int + ( I ) + (int)\$ shift + () I + (int)\$ red. + () I + (int)\$ shift 3 times int + ( int ) + ( int )

22 Shift-Reduce xample I int + (int) + (int)\$ shift int I + (int) + (int)\$ red. int I + (int) + (int)\$ shift 3 times + (int I ) + (int)\$ red. int + ( I ) + (int)\$ shift + () I + (int)\$ red. + () I + (int)\$ shift 3 times + (int I )\$ red. int int + ( int ) + ( int )

23 Shift-Reduce xample I int + (int) + (int)\$ shift int I + (int) + (int)\$ red. int I + (int) + (int)\$ shift 3 times + (int I ) + (int)\$ red. int + ( I ) + (int)\$ + () I + (int)\$ shift red. + () I + (int)\$ shift 3 times + (int I )\$ red. int + ( I )\$ shift int + ( int ) + ( int )

24 Shift-Reduce xample I int + (int) + (int)\$ shift int I + (int) + (int)\$ red. int I + (int) + (int)\$ shift 3 times + (int I ) + (int)\$ red. int + ( I ) + (int)\$ + () I + (int)\$ shift red. + () I + (int)\$ shift 3 times + (int I )\$ red. int + ( I )\$ + () I \$ shift red. + () int + ( int ) + ( int )

25 Shift-Reduce xample I int + (int) + (int)\$ shift int I + (int) + (int)\$ red. int I + (int) + (int)\$ shift 3 times + (int I ) + (int)\$ red. int + ( I ) + (int)\$ + () I + (int)\$ shift red. + () I + (int)\$ shift 3 times + (int I )\$ red. int + ( I )\$ + () I \$ shift red. + () I \$ accept int + ( int ) + ( int )

26 The Stack Left string can be implemented as a stack Top of the stack is the I Shift pushes a terminal on the stack Reduce pops 0 or more symbols from the stack (production rhs) and pushes a non-terminal on the stack (production lhs) 2/20/08 Prof. Hilfinger CS164 Lecture 11 26

27 Key Issue: When to Shift or Reduce? Decide based on the left string (the stack) Idea: use a finite automaton (DFA) to decide when to shift or reduce The DFA input is the stack up to potential handle DFA alphabet consists of terminals and nonterminals DFA recognizes complete handles We run the DFA on the stack and we examine the resulting state X and the token tok after I If X has a transition labeled tok then shift If X is labeled with A β on tok then reduce 2/20/08 Prof. Hilfinger CS164 Lecture 11 27

28 accept on \$ LR(1) Parsing. An xample + () on \$, + int ( ) ( ) 5 11 int on \$, + int 9 int on ), + int + () on ), + I int + (int) + (int)\$ shift int I + (int) + (int)\$ int I + (int) + (int)\$ + (int I ) + (int)\$ + ( I ) + (int)\$ + () I + (int)\$ I + (int)\$ + (int I )\$ + ( I )\$ + () I \$ I \$ shift(x3) int shift +() shift (x3) int shift +() accept

29 Representing the DFA Parsers represent the DFA as a 2D table As for table-driven lexical analysis Lines correspond to DFA states Columns correspond to terminals and nonterminals In classical treatments, columns are split into: Those for terminals: action table Those for non-terminals: goto table 2/20/08 Prof. Hilfinger CS164 Lecture 11 29

30 Representing the DFA. xample The table for a fragment of our DFA: ( ) 7 int 5 int on ), + + () on \$, int s5 s8 r int r +() r +() 2/20/08 Prof. Hilfinger CS164 Lecture ( s4 s7 ) r int \$ g6

31 The LR Parsing Algorithm After a shift or reduce action we rerun the DFA on the entire stack This is wasteful, since most of the work is repeated So record, for each stack element, state of the DFA after that state LR parser maintains a stack sym 1, state 1... sym n, state n state k is the final state of the DFA on sym 1 sym k 2/20/08 Prof. Hilfinger CS164 Lecture 11 31

32 The LR Parsing Algorithm Let I = w 1 w 2 w n \$ be initial input Let j = 1 Let DFA state 0 be the start state Let stack = dummy, 0 repeat case action[top_state(stack), I[j]] of shift k: push I[j], k ; j += 1 reduce X α: pop α pairs, push X, Goto[top_state(stack), X] accept: halt normally error: halt and report error 2/20/08 Prof. Hilfinger CS164 Lecture 11 32

33 Parsing Contexts Consider the state describing the situation at the I in the stack + ( I int ) + ( int ) Context: We are looking for an + ( ) Have have seen + ( from the right-hand side We are also looking for int or + ( ) Have seen nothing from the right-hand side One DFA state describes a set of such contexts (Traditionally, use to show where the I is.) 2/20/08 Prof. Hilfinger CS164 Lecture 11 33

34 LR(1) Items An LR(1) item is a pair: X α β, a X αβ is a production a is a terminal (the lookahead terminal) LR(1) means 1 lookahead terminal [X α β, a] describes a context of the parser We are trying to find an X followed by an a, and We have α already on top of the stack Thus we need to see next a prefix derived from βa 2/20/08 Prof. Hilfinger CS164 Lecture 11 34

35 Convention We add to our grammar a fresh new start symbol S and a production S Where is the old start symbol No need to do this if had only one production The initial parsing context contains: S, \$ Trying to find an S as a string derived from \$ The stack is empty 2/20/08 Prof. Hilfinger CS164 Lecture 11 35

36 Constructing the Parsing DFA. xample. 2 S, \$ 0 +(), \$/+ int int, \$/+ + S, \$ +(), \$/+ accept on \$ 6 +( ), \$/+ +(), )/+ and so on 1 int, \$/+ + (), \$/+ +( ), \$/+ +(), )/+ int, )/+ int on \$, + int, )/+ 2/20/08 Prof. Hilfinger CS164 Lecture ( int int on ), +

37 LR Parsing Tables. Notes Parsing tables (i.e. the DFA) can be constructed automatically for a CFG But we still need to understand the construction to work with parser generators.g., they report errors in terms of sets of items What kind of errors can we expect? 2/20/08 Prof. Hilfinger CS164 Lecture 11 37

38 Shift/Reduce Conflicts If a DFA state contains both [X α aβ, b] and [Y γ, a] Then on input a we could either Shift into state [X αa β, b], or Reduce with Y γ This is called a shift-reduce conflict 2/20/08 Prof. Hilfinger CS164 Lecture 11 38

39 Shift/Reduce Conflicts Typically due to ambiguities in the grammar Classic example: the dangling else S if then S if then S else S OTHR Will have DFA state containing [S if then S, else] [S if then S else S, \$] If else follows then we can shift or reduce 2/20/08 Prof. Hilfinger CS164 Lecture 11 39

40 More Shift/Reduce Conflicts Consider the ambiguous grammar + * int We will have the states containing [ *, +] [ *, +] [ +, +] [ +, +] Again we have a shift/reduce on input + We need to reduce (* binds more tightly than +) Solution: declare the precedence of * and + 2/20/08 Prof. Hilfinger CS164 Lecture 11 40

41 More Shift/Reduce Conflicts In bison declare precedence and associativity of terminal symbols: %left + %left * Precedence of a rule = that of its last terminal See bison manual for ways to override this default Resolve shift/reduce conflict with a shift if: input terminal has higher precedence than the rule the precedences are the same and right associative 2/20/08 Prof. Hilfinger CS164 Lecture 11 41

42 Using Precedence to Solve S/R Conflicts Back to our example: [ *, +] [ *, +] [ +, +] [ +, +] Will choose reduce because precedence of rule * is higher than of terminal + 2/20/08 Prof. Hilfinger CS164 Lecture 11 42

43 Using Precedence to Solve S/R Conflicts Same grammar as before + * int We will also have the states [ +, +] [ +, +] [ +, +] [ +, +] Now we also have a shift/reduce on input + We choose reduce because + and + have the same precedence and + is left-associative 2/20/08 Prof. Hilfinger CS164 Lecture 11 43

44 Using Precedence to Solve S/R Conflicts Back to our dangling else example [S if then S, else] [S if then S else S, x] Can eliminate conflict by declaring else with higher precedence than then However, best to avoid overuse of precedence declarations or you ll end with unexpected parse trees 2/20/08 Prof. Hilfinger CS164 Lecture 11 44

45 Reduce/Reduce Conflicts If a DFA state contains both [X α, a] and [Y β, a] Then on input a we don t know which production to reduce This is called a reduce/reduce conflict 2/20/08 Prof. Hilfinger CS164 Lecture 11 45

46 Reduce/Reduce Conflicts Usually due to gross ambiguity in the grammar xample: a sequence of identifiers S ε id id S There are two parse trees for the string id S id S id S id How does this confuse the parser? 2/20/08 Prof. Hilfinger CS164 Lecture 11 46

47 More on Reduce/Reduce Conflicts Consider the states [S id, \$] [S S, \$] [S id S, \$] [S, \$] id [S, \$] [S id, \$] [S id, \$] [S id S, \$] [S id S, \$] Reduce/reduce conflict on input \$ S S id S S id S id Better rewrite the grammar: S ε id S 2/20/08 Prof. Hilfinger CS164 Lecture 11 47

48 Relation to Bison Bison builds this kind of machine. However, for efficiency concerns, collapses many of the states together. Causes some additional conflicts, but not many. The machines discussed here are LR(1) engines. Bison s optimized versions are LALR(1) engines. 2/20/08 Prof. Hilfinger CS164 Lecture 11 48

49 A Hierarchy of Grammar Classes From Andrew Appel, Modern Compiler Implementation in Java 2/20/08 Prof. Hilfinger CS164 Lecture 11 49

50 Notes on Parsing Parsing A solid foundation: context-free grammars A simple parser: LL(1) A more powerful parser: LR(1) An efficiency hack: LALR(1) We use LALR(1) parser generators But let s also see a more powerful engine. 2/20/08 Prof. Hilfinger CS164 Lecture 11 50

### Bottom-Up Parsing. Lecture 11-12

Bottom-Up Parsing Lecture 11-12 (From slides by G. Necula & R. Bodik) 9/22/06 Prof. Hilfinger CS164 Lecture 11 1 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient

### Lecture 8: Deterministic Bottom-Up Parsing

Lecture 8: Deterministic Bottom-Up Parsing (From slides by G. Necula & R. Bodik) Last modified: Fri Feb 12 13:02:57 2010 CS164: Lecture #8 1 Avoiding nondeterministic choice: LR We ve been looking at general

### Lecture 7: Deterministic Bottom-Up Parsing

Lecture 7: Deterministic Bottom-Up Parsing (From slides by G. Necula & R. Bodik) Last modified: Tue Sep 20 12:50:42 2011 CS164: Lecture #7 1 Avoiding nondeterministic choice: LR We ve been looking at general

### LR Parsing LALR Parser Generators

Outline LR Parsing LALR Parser Generators Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the

### LR Parsing LALR Parser Generators

LR Parsing LALR Parser Generators Outline Review of bottom-up parsing Computing the parsing DFA Using parser generators 2 Bottom-up Parsing (Review) A bottom-up parser rewrites the input string to the

### In One Slide. Outline. LR Parsing. Table Construction

LR Parsing Table Construction #1 In One Slide An LR(1) parsing table can be constructed automatically from a CFG. An LR(1) item is a pair made up of a production and a lookahead token; it represents a

### LR Parsing. Table Construction

#1 LR Parsing Table Construction #2 Outline Review of bottom-up parsing Computing the parsing DFA Closures, LR(1) Items, States Transitions Using parser generators Handling Conflicts #3 In One Slide An

### Conflicts in LR Parsing and More LR Parsing Types

Conflicts in LR Parsing and More LR Parsing Types Lecture 10 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 2 Due Friday, Apr. 24, 11:55pm The usual lecture time is being replaced by a discussion

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

Compilers Bottom-up Parsing Yannis Smaragdakis U Athens Yannis Smaragdakis, U. Athens (original slides by Sam Guyer@Tufts) Bottom-Up Parsing More general than top-down parsing And just as efficient Builds

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

Bottom-Up Parsing II Different types of Shift-Reduce Conflicts) Lecture 10 Ganesh. Lecture 10) 1 Review: Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient Doesn

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

Lecture 14: Parser Conflicts, Using Ambiguity, Error Recovery Last modified: Mon Feb 23 10:05:56 2015 CS164: Lecture #14 1 Shift/Reduce Conflicts If a DFA state contains both [X: α aβ, b] and [Y: γ, a],

### More Bottom-Up Parsing

More Bottom-Up Parsing Lecture 7 Dr. Sean Peisert ECS 142 Spring 2009 1 Status Project 1 Back By Wednesday (ish) savior lexer in ~cs142/s09/bin Project 2 Due Friday, Apr. 24, 11:55pm My office hours 3pm

### 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

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

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

Outline Introduction to Bottom-Up Parsing Lecture Notes by Profs. Alex Aiken and George Necula (UCB) he strategy: -reduce parsing A key concept: handles Ambiguity and precedence declarations CS780(Prasad)

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

MIT 6.035 Parse Table Construction Martin Rinard Laboratory for Computer Science Massachusetts Institute of Technology Parse Tables (Review) ACTION Goto State ( ) \$ X s0 shift to s2 error error goto s1

### Lexical and Syntax Analysis. Bottom-Up Parsing

Lexical and Syntax Analysis Bottom-Up Parsing Parsing There are two ways to construct derivation of a grammar. Top-Down: begin with start symbol; repeatedly replace an instance of a production s LHS with

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

Review of CFGs and Parsing II Bottom-up Parsers Lecture 5 1 Outline Parser Overview op-down Parsers (Covered largely through labs) Bottom-up Parsers 2 he Functionality of the Parser Input: sequence of

### Bottom-Up Parsing II. Lecture 8

Bottom-Up Parsing II Lecture 8 1 Review: Shift-Reduce Parsing Bottom-up parsing uses two actions: Shift ABC xyz ABCx yz Reduce Cbxy ijk CbA ijk 2 Recall: he Stack Left string can be implemented by a stack

### Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing Lecture 11 CS 536 Spring 2001 1 Outline he strategy: shift-reduce parsing Ambiguity and precedence declarations Next lecture: bottom-up parsing algorithms CS 536 Spring

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

LR parsing There are three commonly used algorithms to build tables for an LR parser: 1. SLR(1) = LR(0) plus use of FOLLOW set to select between actions smallest class of grammars smallest tables (number

### Intro to Bottom-up Parsing. Lecture 9

Intro to Bottom-up Parsing Lecture 9 Bottom-Up Parsing Bottom-up parsing is more general than topdown parsing And just as efficient Builds on ideas in top-down parsing Bottom-up is the preferred method

### CS 4120 Introduction to Compilers

CS 4120 Introduction to Compilers Andrew Myers Cornell University Lecture 6: Bottom-Up Parsing 9/9/09 Bottom-up parsing A more powerful parsing technology LR grammars -- more expressive than LL can handle

### Syntax-Directed Translation. Lecture 14

Syntax-Directed Translation Lecture 14 (adapted from slides by R. Bodik) 9/27/2006 Prof. Hilfinger, Lecture 14 1 Motivation: parser as a translator syntax-directed translation stream of tokens parser ASTs,

### shift-reduce parsing

Parsing #2 Bottom-up Parsing Rightmost derivations; use of rules from right to left Uses a stack to push symbols the concatenation of the stack symbols with the rest of the input forms a valid bottom-up

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

CS 2210 Sample Midterm 1. Determine if each of the following claims is true (T) or false (F). F A language consists of a set of strings, its grammar structure, and a set of operations. (Note: a language

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

EDAN65: Compilers, Lecture 06 A LR parsing Görel Hedin Revised: 2017-09-11 This lecture Regular expressions Context-free grammar Attribute grammar Lexical analyzer (scanner) Syntactic analyzer (parser)

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

LR(0) Parsing ummary C412/C41 Introduction to Compilers Tim Teitelbaum Lecture 10: LR Parsing February 12, 2007 LR(0) item = a production with a dot in RH LR(0) state = set of LR(0) items valid for viable

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

Top-Down Parsing and Intro to Bottom-Up Parsing Lecture 7 1 Predictive Parsers Like recursive-descent but parser can predict which production to use Predictive parsers are never wrong Always able to guess

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

Bottom-up Parsing: Table-driven using an explicit stack (no recursion!). Stack can be viewed as containing both terminals and non-terminals. Basic operation is to shift terminals from the input to the

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

CSE P 501 Compilers LR Parsing Hal Perkins Spring 2018 UW CSE P 501 Spring 2018 D-1 Agenda LR Parsing Table-driven Parsers Parser States Shift-Reduce and Reduce-Reduce conflicts UW CSE P 501 Spring 2018

### Lecture Bottom-Up Parsing

Lecture 14+15 Bottom-Up Parsing CS 241: Foundations of Sequential Programs Winter 2018 Troy Vasiga et al University of Waterloo 1 Example CFG 1. S S 2. S AyB 3. A ab 4. A cd 5. B z 6. B wz 2 Stacks in

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

Top-Down Parsing and Intro to Bottom-Up Parsing Lecture 7 1 Predictive Parsers Like recursive-descent but parser can predict which production to use Predictive parsers are never wrong Always able to guess

### Principles of Programming Languages

Principles of Programming Languages h"p://www.di.unipi.it/~andrea/dida2ca/plp- 14/ Prof. Andrea Corradini Department of Computer Science, Pisa Lesson 8! Bo;om- Up Parsing Shi?- Reduce LR(0) automata and

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

LR Parsing Compiler Design CSE 504 1 Shift-Reduce Parsing 2 LR Parsers 3 SLR and LR(1) Parsers Last modifled: Fri Mar 06 2015 at 13:50:06 EST Version: 1.7 16:58:46 2016/01/29 Compiled at 12:57 on 2016/02/26

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

Parsers Xiaokang Qiu Purdue University ECE 468 August 31, 2018 What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure

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

3. Syntax Analysis Andrea Polini Formal Languages and Compilers Master in Computer Science University of Camerino (Formal Languages and Compilers) 3. Syntax Analysis CS@UNICAM 1 / 54 Syntax Analysis: the

### Monday, September 13, Parsers

Parsers Agenda Terminology LL(1) Parsers Overview of LR Parsing Terminology Grammar G = (Vt, Vn, S, P) Vt is the set of terminals Vn is the set of non-terminals S is the start symbol P is the set of productions

### Introduction to Parsing. Lecture 5

Introduction to Parsing Lecture 5 1 Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity 2 Languages and Automata Formal languages are very important

### Wednesday, August 31, Parsers

Parsers How do we combine tokens? Combine tokens ( words in a language) to form programs ( sentences in a language) Not all combinations of tokens are correct programs (not all sentences are grammatically

### Context-free grammars

Context-free grammars Section 4.2 Formal way of specifying rules about the structure/syntax of a program terminals - tokens non-terminals - represent higher-level structures of a program start symbol,

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

Syntax Analysis: Context-free Grammars, Pushdown Automata and Part - 4 Department of Computer Science and Automation Indian Institute of Science Bangalore 560 012 NPTEL Course on Principles of Compiler

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

MODULE 18 LALR parsing After understanding the most powerful CALR parser, in this module we will learn to construct the LALR parser. The CALR parser has a large set of items and hence the LALR parser is

### Wednesday, September 9, 15. Parsers

Parsers What is a parser A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

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

What is a parser Parsers A parser has two jobs: 1) Determine whether a string (program) is valid (think: grammatically correct) 2) Determine the structure of a program (think: diagramming a sentence) Agenda

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

Bottom-up parsing Bottom-up parsing Recall Goal: For a grammar G, withstartsymbols, any string α such that S α is called a sentential form If α V t,thenα is called a sentence in L(G) Otherwise it is just

### Introduction to Parsing. Lecture 8

Introduction to Parsing Lecture 8 Adapted from slides by G. Necula Outline Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Languages and Automata Formal languages

### 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

LALR parsing 1 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 decisions without using a look ahead.

### Compiler Construction: Parsing

Compiler Construction: Parsing Mandar Mitra Indian Statistical Institute M. Mitra (ISI) Parsing 1 / 33 Context-free grammars. Reference: Section 4.2 Formal way of specifying rules about the structure/syntax

### Lecture Notes on Bottom-Up LR Parsing

Lecture Notes on Bottom-Up LR Parsing 15-411: Compiler Design Frank Pfenning Lecture 9 September 23, 2009 1 Introduction In this lecture we discuss a second parsing algorithm that traverses the input string

### 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.

Bottom up parsing Construct a parse tree for an input string beginning at leaves and going towards root OR Reduce a string w of input to start symbol of grammar Consider a grammar S aabe A Abc b B d And

### Lecture Notes on Bottom-Up LR Parsing

Lecture Notes on Bottom-Up LR Parsing 15-411: Compiler Design Frank Pfenning Lecture 9 1 Introduction In this lecture we discuss a second parsing algorithm that traverses the input string from left to

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

Compiler Design 1 Bottom-UP Parsing Compiler Design 2 The Process The parse tree is built starting from the leaf nodes labeled by the terminals (tokens). The parser tries to discover appropriate reductions,

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

Parsing Wrapup Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing LR(1) items Computing closure Computing goto LR(1) canonical collection This lecture LR(1) parsing Building ACTION

### Syntax Analysis. Amitabha Sanyal. (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

Syntax Analysis (www.cse.iitb.ac.in/ as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay September 2007 College of Engineering, Pune Syntax Analysis: 2/124 Syntax

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

Also known as Shift-Reduce parsing More powerful than top down Don t need left factored grammars Can handle left recursion Attempt to construct parse tree from an input string eginning at leaves and working

### Outline. Limitations of regular languages. Introduction to Parsing. Parser overview. Context-free grammars (CFG s)

Outline Limitations of regular languages Introduction to Parsing Parser overview Lecture 8 Adapted from slides by G. Necula Context-free grammars (CFG s) Derivations Languages and Automata Formal languages

### CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution

CS143 Handout 20 Summer 2011 July 15 th, 2011 CS143 Practice Midterm and Solution Exam Facts Format Wednesday, July 20 th from 11:00 a.m. 1:00 p.m. in Gates B01 The exam is designed to take roughly 90

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

PART 3 - SYNTAX ANALYSIS F. Wotawa (IST @ TU Graz) Compiler Construction Summer term 2016 64 / 309 Goals Definition of the syntax of a programming language using context free grammars Methods for parsing

### Introduction to Parsing. Lecture 5

Introduction to Parsing Lecture 5 1 Outline Regular languages revisited Parser overview Context-free grammars (CFG s) Derivations Ambiguity 2 Languages and Automata Formal languages are very important

### Formal Languages and Compilers Lecture VII Part 3: Syntactic A

Formal Languages and Compilers Lecture VII Part 3: Syntactic Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

### UNIT III & IV. Bottom up parsing

UNIT III & IV Bottom up parsing 5.0 Introduction Given a grammar and a sentence belonging to that grammar, if we have to show that the given sentence belongs to the given grammar, there are two methods.

### 4. Lexical and Syntax Analysis

4. Lexical and Syntax Analysis 4.1 Introduction Language implementation systems must analyze source code, regardless of the specific implementation approach Nearly all syntax analysis is based on a formal

### 3. Parsing. Oscar Nierstrasz

3. Parsing Oscar Nierstrasz Thanks to Jens Palsberg and Tony Hosking for their kind permission to reuse and adapt the CS132 and CS502 lecture notes. http://www.cs.ucla.edu/~palsberg/ http://www.cs.purdue.edu/homes/hosking/

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

Roadmap > Context-free grammars > Derivations and precedence > Top-down parsing > Left-recursion > Look-ahead > Table-driven parsing The role of the parser > performs context-free syntax analysis > guides

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

Section A 1. What do you meant by parser and its types? A parser for grammar G is a program that takes as input a string w and produces as output either a parse tree for w, if w is a sentence of G, or

### 4. Lexical and Syntax Analysis

4. Lexical and Syntax Analysis 4.1 Introduction Language implementation systems must analyze source code, regardless of the specific implementation approach Nearly all syntax analysis is based on a formal

### 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.

Example CFG Lectures 16 & 17 Bottom-Up Parsing CS 241: Foundations of Sequential Programs Fall 2016 1. Sʹ " S 2. S " AyB 3. A " ab 4. A " cd Matt Crane University of Waterloo 5. B " z 6. B " wz 2 LL(1)

### G53CMP: Lecture 4. Syntactic Analysis: Parser Generators. Henrik Nilsson. University of Nottingham, UK. G53CMP: Lecture 4 p.1/32

G53CMP: Lecture 4 Syntactic Analysis: Parser Generators Henrik Nilsson University of Nottingham, UK G53CMP: Lecture 4 p.1/32 This Lecture Parser generators ( compiler compilers ) The parser generator Happy

### Concepts Introduced in Chapter 4

Concepts Introduced in Chapter 4 Grammars Context-Free Grammars Derivations and Parse Trees Ambiguity, Precedence, and Associativity Top Down Parsing Recursive Descent, LL Bottom Up Parsing SLR, LR, LALR

### How do LL(1) Parsers Build Syntax Trees?

How do LL(1) Parsers Build Syntax Trees? So far our LL(1) parser has acted like a recognizer. It verifies that input token are syntactically correct, but it produces no output. Building complete (concrete)

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

Ambiguity, Precedence, Associativity & Top-Down Parsing Lecture 9-10 (From slides by G. Necula & R. Bodik) 9/18/06 Prof. Hilfinger CS164 Lecture 9 1 Administrivia Please let me know if there are continued

### Simple LR (SLR) LR(0) Drawbacks LR(1) SLR Parse. LR(1) Start State and Reduce. LR(1) Items 10/3/2012

LR(0) Drawbacks Consider the unambiguous augmented grammar: 0.) S E \$ 1.) E T + E 2.) E T 3.) T x If we build the LR(0) DFA table, we find that there is a shift-reduce conflict. This arises because the

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

Bottom-up parsing Recall For a grammar G, with start symbol S, any string α such that S α is a sentential form If α V t, then α is a sentence in L(G) A left-sentential form is a sentential form that occurs

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

Parser Generation Main Problem: given a grammar G, how to build a top-down parser or a bottom-up parser for it? parser : a program that, given a sentence, reconstructs a derivation for that sentence ----

### Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Formal Languages and Compilers Lecture VII Part 4: Syntactic Analysis Free University of Bozen-Bolzano Faculty of Computer Science POS Building, Room: 2.03 artale@inf.unibz.it http://www.inf.unibz.it/

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

TDDD16 Compilers and Interpreters TDDB44 Compiler Construction LR Parsing, Part 2 Constructing Parse Tables Parse table construction Grammar conflict handling Categories of LR Grammars and Parsers An NFA

### Configuration Sets for CSX- Lite. Parser Action Table

Configuration Sets for CSX- Lite State s 6 s 7 Cofiguration Set Prog { Stmts } Eof Stmts Stmt Stmts State s s Cofiguration Set Prog { Stmts } Eof Prog { Stmts } Eof Stmts Stmt Stmts Stmts λ Stmt if ( Expr

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

Chapter 4 Lexical and Syntax Analysis Introduction - Language implementation systems must analyze source code, regardless of the specific implementation approach - Nearly all syntax analysis is based on

### Compiler Construction 2016/2017 Syntax Analysis

Compiler Construction 2016/2017 Syntax Analysis Peter Thiemann November 2, 2016 Outline 1 Syntax Analysis Recursive top-down parsing Nonrecursive top-down parsing Bottom-up parsing Syntax Analysis tokens

### Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University

Compilation 2012 Parsers and Scanners Jan Midtgaard Michael I. Schwartzbach Aarhus University Context-Free Grammars Example: sentence subject verb object subject person person John Joe Zacharias verb asked

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

C12/1 Introduction to Compilers and Translators pring 00 Outline More tips forll1) grammars Bottom-up parsing LR0) parser construction Lecture 5: Bottom-up parsing Lecture 5 C 12/1 pring '00 Andrew Myers

### Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5. Derivations.

Outline Regular languages revisited Introduction to Parsing Lecture 5 Parser overview Context-free grammars (CFG s) Derivations Prof. Aiken CS 143 Lecture 5 1 Ambiguity Prof. Aiken CS 143 Lecture 5 2 Languages

SYNTAX ANALYSIS 1. Define parser. Hierarchical analysis is one in which the tokens are grouped hierarchically into nested collections with collective meaning. Also termed as Parsing. 2. Mention the basic

### CS415 Compilers. LR Parsing & Error Recovery

CS415 Compilers LR Parsing & Error Recovery These slides are based on slides copyrighted by Keith Cooper, Ken Kennedy & Linda Torczon at Rice University Review: LR(k) items The LR(1) table construction

### Outline. Limitations of regular languages Parser overview Context-free grammars (CFG s) Derivations Syntax-Directed Translation

Outline Introduction to Parsing Lecture 8 Adapted from slides by G. Necula and R. Bodik Limitations of regular languages Parser overview Context-free grammars (CG s) Derivations Syntax-Directed ranslation

### Bottom-Up Parsing LR Parsing

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

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

Topics Chapter 4 Lexical and Syntax Analysis Introduction Lexical Analysis Syntax Analysis Recursive -Descent Parsing Bottom-Up parsing 2 Language Implementation Compilation There are three possible approaches

### LR Parsing Techniques

LR Parsing Techniques Introduction Bottom-Up Parsing LR Parsing as Handle Pruning Shift-Reduce Parser LR(k) Parsing Model Parsing Table Construction: SLR, LR, LALR 1 Bottom-UP Parsing A bottom-up parser

### ( ) i 0. Outline. Regular languages revisited. Introduction to Parsing. Parser overview. Context-free grammars (CFG s) Lecture 5.

Outline Regular languages revisited Introduction to Parsing Lecture 5 Parser overview Context-free grammars (CFG s) Derivations Prof. Aiken CS 143 Lecture 5 1 Ambiguity Prof. Aiken CS 143 Lecture 5 2 Languages

### LR Parsing - The Items

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

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

Parsing III (Top-down parsing: recursive descent & LL(1) ) (Bottom-up parsing) CS434 Lecture 8 Spring 2005 Department of Computer Science University of Alabama Joel Jones Copyright 2003, Keith D. Cooper,

### CS2210: Compiler Construction Syntax Analysis Syntax Analysis

Comparison with Lexical Analysis The second phase of compilation Phase Input Output Lexer string of characters string of tokens Parser string of tokens Parse tree/ast What Parse Tree? CS2210: Compiler

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

Predictive Parsers LL(k) Parsing Can we avoid backtracking? es, if for a given input symbol and given nonterminal, we can choose the alternative appropriately. his is possible if the first terminal of

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

Free University of Bolzano Principles of Compilers Lecture IV Part 4, 2003/2004 AArtale (1) Principle of Compilers Lecture IV Part 4: Syntactic Analysis Alessandro Artale Faculty of Computer Science Free

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

ICOM 4036 Programming Languages Lexical and Syntax Analysis Lexical Analysis The Parsing Problem Recursive-Descent Parsing Bottom-Up Parsing This lecture covers review questions 14-27 This lecture covers

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

CS453 : JavaCUP and error recovery CS453 Shift-reduce Parsing 1 Shift-reduce parsing in an LR parser LR(k) parser Left-to-right parse Right-most derivation K-token look ahead LR parsing algorithm using

### Syntax Analysis Part I

Syntax Analysis Part I Chapter 4: Context-Free Grammars Slides adapted from : Robert van Engelen, Florida State University Position of a Parser in the Compiler Model Source Program Lexical Analyzer Token,