+ All Categories
Home > Documents > Introduction to Bottom-Up Parsing

Introduction to Bottom-Up Parsing

Date post: 11-Feb-2016
Category:
Upload: kera
View: 51 times
Download: 0 times
Share this document with a friend
Description:
Introduction to Bottom-Up Parsing. Lecture 11. Outline. The strategy: shift-reduce parsing Ambiguity and precedence declarations Next lecture: bottom-up parsing algorithms. Predictive Parsing Summary. First and Follow sets are used to construct predictive tables - PowerPoint PPT Presentation
47
CS 536 Spring 2001 1 Introduction to Bottom-Up Parsing Lecture 11
Transcript
Page 1: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 1

Introduction to Bottom-Up

Parsing

Lecture 11

Page 2: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 2

Outline

• The strategy: shift-reduce parsing

• Ambiguity and precedence declarations

• Next lecture: bottom-up parsing algorithms

Page 3: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 3

Predictive Parsing Summary

• First and Follow sets are used to construct predictive tables– For non-terminal A and input t,

use a production A where t First()

– For non-terminal A and input t, if First(A) and t Follow(), then use a production A where First()

• We’ll see First and Follow sets again . . .

Page 4: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 4

Bottom-Up Parsing

• Bottom-up parsing is more general than top-down parsing– And just as efficient– Builds on ideas in top-down parsing

• Bottom-up is the preferred method in practice

• Concepts today, algorithms next time

Page 5: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 5

An Introductory Example

• Bottom-up parsers don’t need left-factored grammars

• Hence we can revert to the “natural” grammar for our example:

E T + E | TT int * T | int | (E)

• Consider the string: int * int + int

Page 6: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 6

The Idea

Bottom-up parsing reduces a string to the start symbol by inverting productions:

int * int + int T intint * T + int T int * TT + int T intT + T E TT + E E T + EE

Page 7: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 7

TEST YOURSELF #1

• Question: find the rightmost derivation of the string int * int + int

Page 8: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 8

Observation

• Read the productions found by bottom-up parse in reverse (i.e., from bottom to top)

• This is a rightmost derivation!int * int + int T intint * T + int T int * TT + int T intT + T E TT + E E T + EE

Page 9: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 9

Important Fact #1

Important Fact #1 about bottom-up parsing:

A bottom-up parser traces a rightmost derivation in reverse

Page 10: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 10

A Bottom-up Parse

int * int + intint * T + intT + intT + TT + EE

E

T E

+ int

*int

T

int

T

Page 11: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 11

A Bottom-up Parse in Detail (1)

+ int

*int int

int * int + int

Page 12: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 12

A Bottom-up Parse in Detail (2)

int * int + intint * T + int

+ int

*int int

T

Page 13: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 13

A Bottom-up Parse in Detail (3)

int * int + intint * T + intT + int T

+ int

*int int

T

Page 14: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 14

A Bottom-up Parse in Detail (4)

int * int + intint * T + intT + intT + T

T

+ int

*int

T

int

T

Page 15: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 15

A Bottom-up Parse in Detail (5)

int * int + intint * T + intT + intT + TT + E

T E

+ int

*int

T

int

T

Page 16: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 16

A Bottom-up Parse in Detail (6)

int * int + intint * T + intT + intT + TT + EE

E

T E

+ int

*int

T

int

T

Page 17: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 17

A Trivial Bottom-Up Parsing Algorithm

Let I = input stringrepeat

pick a non-empty substring of Iwhere X is a production

if no such , backtrackreplace one by X in I

until I = “S” (the start symbol) or all possibilities are exhausted

Page 18: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 18

Questions

• Does this algorithm terminate?

• How fast is the algorithm?

• Does the algorithm handle all cases?

• How do we choose the substring to reduce at each step?

Page 19: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 19

How to build the house of cards?

Page 20: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 20

Where Do Reductions Happen

Important Fact #1 has an interesting consequence:– Let be a step of a bottom-up parse– Assume the next reduction is by X – Then is a string of terminals

Why? Because X is a step in a right-most derivation

Page 21: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 21

Notation

• Idea: Split string into two substrings– Right substring is as yet unexamined by

parsing (a string of terminals)– Left substring has terminals and non-terminals

• The dividing point is marked by a |– The | is not part of the string

• Initially, all input is unexamined |x1x2 . . . xn

Page 22: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 22

Shift-Reduce Parsing

Bottom-up parsing uses only two kinds of actions:

Shift

Reduce

Page 23: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 23

Shift

• Shift: Move | one place to the right– Shifts a terminal to the left string

ABC|xyz ABCx|yz

Page 24: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 24

Reduce

• Apply an inverse production at the right end of the left string– If A xy is a production, then

Cbxy|ijk CbA|ijk

Page 25: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 25

The Example with Reductions Only

int * int | + int reduce T intint * T | + int reduce T int * T

T + int | reduce T intT + T | reduce E TT + E | reduce E T + EE |

Page 26: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 26

The Example with Shift-Reduce Parsing|int * int + int shiftint | * int + int shiftint * | int + int shiftint * int | + int reduce T intint * T | + int reduce T int * TT | + int shiftT + | int shiftT + int | reduce T intT + T | reduce E TT + E | reduce E T + EE |

Page 27: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 27

A Shift-Reduce Parse in Detail (1)

+ int

*int int

|int * int + int

Page 28: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 28

A Shift-Reduce Parse in Detail (2)

+ int

*int int

|int * int + intint | * int + int

Page 29: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 29

A Shift-Reduce Parse in Detail (3)

+ int

*int int

|int * int + intint | * int + intint * | int + int

Page 30: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 30

A Shift-Reduce Parse in Detail (4)

+ int

*int int

|int * int + intint | * int + intint * | int + intint * int | + int

Page 31: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 31

A Shift-Reduce Parse in Detail (5)

+ int

*int int

T

|int * int + intint | * int + intint * | int + intint * int | + intint * T | + int

Page 32: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 32

A Shift-Reduce Parse in Detail (6)

T

+ int

*int int

T

|int * int + intint | * int + intint * | int + intint * int | + intint * T | + intT | + int

Page 33: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 33

A Shift-Reduce Parse in Detail (7)

T

+ int

*int int

T

|int * int + intint | * int + intint * | int + intint * int | + intint * T | + intT | + intT + | int

Page 34: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 34

A Shift-Reduce Parse in Detail (8)

T

+ int

*int int

T

|int * int + intint | * int + intint * | int + intint * int | + intint * T | + intT | + intT + | intT + int |

Page 35: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 35

A Shift-Reduce Parse in Detail (9)

T

+ int

*int

T

int

T

|int * int + intint | * int + intint * | int + intint * int | + intint * T | + intT | + intT + | intT + int | T + T |

Page 36: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 36

A Shift-Reduce Parse in Detail (10)

T E

+ int

*int

T

int

T

|int * int + intint | * int + intint * | int + intint * int | + intint * T | + intT | + intT + | intT + int | T + T |T + E |

Page 37: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 37

A Shift-Reduce Parse in Detail (11)

E

T E

+ int

*int

T

int

T

|int * int + intint | * int + intint * | int + intint * int | + intint * T | + intT | + intT + | intT + int | T + T |T + E |E |

Page 38: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 38

The Stack

• Left string can be implemented by a stack– Top of the stack is the |

• Shift pushes a terminal on the stack

• Reduce pops 0 or more symbols off of the stack (production rhs) and pushes a non-terminal on the stack (production lhs)

Page 39: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 39

Key Issue (will be resolved by algorithms)

• How do we decide when to shift or reduce?– Consider step int | * int + int– We could reduce by T int giving T | * int + int– A fatal mistake: No way to reduce to the start

symbol E

Page 40: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 40

Conflicts

• Generic shift-reduce strategy:– If there is a handle on top of the stack, reduce– Otherwise, shift

• But what if there is a choice?– If it is legal to shift or reduce, there is a

shift-reduce conflict– If it is legal to reduce by two different

productions, there is a reduce-reduce conflict

Page 41: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 41

Source of Conflicts

• Ambiguous grammars always cause conflicts

• But beware, so do many non-ambiguous grammars

Page 42: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 42

Conflict Example

Consider our favorite ambiguous grammar:

E E + E

| E * E| (E)| int

Page 43: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 43

One Shift-Reduce Parse

|int * int + int shift. . . . . .E * E | + int reduce E E * EE | + int shiftE + | int shiftE + int| reduce E intE + E | reduce E E + EE |

Page 44: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 44

Another Shift-Reduce Parse

|int * int + int shift. . . . . .E * E | + int shiftE * E + | int shiftE * E + int | reduce E intE * E + E| reduce E E + EE * E | reduce E E * EE |

Page 45: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 45

Example Notes

• In the second step E * E | + int we can either shift or reduce by E E * E

• Choice determines associativity of + and *

• As noted previously, grammar can be rewritten to enforce precedence

• Precedence declarations are an alternative

Page 46: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 46

Precedence Declarations Revisited

• Precedence declarations cause shift-reduce parsers to resolve conflicts in certain ways

• Declaring “* has greater precedence than +” causes parser to reduce at E * E | + int

• More precisely, precedence declaration is used to resolve conflict between reducing a * and shifting a +

Page 47: Introduction to Bottom-Up Parsing

CS 536 Spring 2001 47

Precedence Declarations Revisited (Cont.)

• The term “precedence declaration” is misleading

• These declarations do not define precedence; they define conflict resolutions– Not quite the same thing!


Recommended