+ All Categories
Home > Documents > Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing...

Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing...

Date post: 29-Mar-2018
Category:
Upload: phunghuong
View: 218 times
Download: 0 times
Share this document with a friend
41
Bottom-Up Parsing 1 Aditi Raste, CCOEW
Transcript
Page 1: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Bottom-Up Parsing

1 Aditi Raste, CCOEW

Page 2: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Bottom-Up Parsing

Constructs parse tree for an input string beginning at the

leaves (bottom) and working up towards the root (top).

Tries to find the rightmost derivation of a string in reverse.

Aditi Raste, CCOEW 2

Expression Grammar

E -> E + T | T

T -> T * F | F

F -> (E) | id

1. id * id 2. F * id

id

3. T * id

F

id

4. T * F

F id

id

5. T

T * F

F id

id

6. E

T

T * F

F id

id

Page 3: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Bottom-Up Parsing: Definitions

Reductions

Bottom up parsing is the process of reducing a string w to the start symbol of the grammar.

At each reduction step specific substring matching the body of a production is replaced by the non terminal at the head of the production.

Key decisions

As the parse proceeds,

when to reduce

what production to apply

Reduction is the reverse of a step in a derivation.

Aditi Raste, CCOEW 3

Page 4: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Handle

Bottom up parsing constructs a rightmost derivation of

input string in reverse.

A handle is a substring that matches the body of a

production and whose reduction represents one step along

the reverse of a rightmost derivation.

But not every substring matches the right side of a

production rule is handle

Aditi Raste, CCOEW 4

Bottom-Up Parsing: Definitions

E -> E + T | T

T -> T * F | F

F -> (E) | id

Input String: id * id

Right Sentential form Handle Production

id * id id F -> id

F * id F T -> F

T * id id F -> id

T * F T * F T -> T * F

T T E-> T

Handles during a parse of id * id

T cannot be a

handle here

Page 5: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Bottom-Up Parsing: Definitions

Handle Pruning :-

Process of identifying the correct handle out of all

possible handles to replace by the corresponding

non terminal on LHS.

Example: Consider the derivation step

T * id

Here, T and id both can be handles. But id is chosen

as the choice of T is incorrect.

Aditi Raste, CCOEW 5

Page 6: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Handles in Bottom-Up parsing

A handle is a string that can be reduced and also allows further reductions back to the start symbol.

Reduction occurs only at handles.

There are no known efficient algorithms to recognize handles.

Heuristics are used to guess handles.

If G is unambiguous then every right sentential form will have a unique handle.

Aditi Raste, CCOEW 6

Page 7: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Types of Bottom-Up Parsers

Aditi Raste, CCOEW 7

Page 8: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Types of Bottom-Up Parsers

Aditi Raste, CCOEW 8

Bottom Up

Parsers

Shift Reduce

Parsers LR parser

SLR LALR LR Operator

Precedence parser

Page 9: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Shift-Reduce Parsing

Efficient table driven bottom up parser.

Simple to understand.

Data structures used:- 1. Stack:- holds grammar symbols

2. Input buffer:- holds the rest of the input string to be parsed

Handle always appears at the top of the stack never inside the stack.

Handles are never to the left of the rightmost non terminal.

Aditi Raste, CCOEW 9

Page 10: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Working of Shift-Reduce Parsing

Initial configuration

Stack Input

$ w$

Parser shifts zero or more symbols on the stack while scanning

the input

Parser may then reduce a string to the LHS of appropriate

production

This continues till an error occurs or below configuration is

reached

Stack Input

$ S $

Parser now halts and announces successful parsing of input string

Aditi Raste, CCOEW 10

Page 11: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Possible actions of Shift-Reduce parser

1. Shift:- Shift the next input symbol onto the top of stack.

2. Reduce:-

Top of stack indicates the right end of the string to be reduced.

Locate its left end within the stack and decide with what non terminal to replace the string.

3. Accept:- Announce successful completion of parsing.

4. Error:- Discover a syntax error and call an error recovery routine.

Aditi Raste, CCOEW 11

Page 12: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Main challenges in Shift-Reduce Parser

Identify correct handle in each reduction step.

Identify which production to use for reducing

in each reduction step.

Aditi Raste, CCOEW 12

Page 13: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Shift-Reduce parser Examples

Aditi Raste, CCOEW 13

Page 14: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Shift-Reduce Parser

1. Consider the below grammar

D -> type tlist ;

type -> int | float

tlist -> tlist, id | id

Use shift reduce parser to check acceptance of

string: int id, id;

Aditi Raste, CCOEW 14

Page 15: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Aditi Raste, CCOEW 15

Shift-Reduce Parser Grammar

D -> type tlist ;

type -> int | float

tlist -> tlist, id | id

Input string

int id, id;

STACK INPUT PARSING ACTION

$ int id, id;$ shift

$ int id, id; $ Reduce type-> int

$ type id, id; $ shift

$ type id , id; $ reduce tlist -> id

$ type tlist , id; $ shift

$ type tlist , id; $ shift

$ type tlist , id ; $ reduce tlist -> tlist, id

$ type tlist ; $ shift

$ type tlist ; $ reduce D-> type tlist;

$ D $ accept

Page 16: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Shift-Reduce Parser

2. Consider the below grammar

S -> ( L ) | a

L -> L, S | S

Use shift reduce parser to check acceptance of

string: (a, a)

Aditi Raste, CCOEW 16

Page 17: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Shift-Reduce Parser

3. Consider the below grammar

E -> E + T | T

T -> T * F | F

F -> (E) | id

Use shift reduce parser to check acceptance of

string: id1 * id2

Aditi Raste, CCOEW 17

Page 18: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Bottom Up Parsers

More powerful than Top Down parsers

Do ’t e ui e left fa to ed g a a

Can handle left recursion

Aditi Raste, CCOEW 18

Page 19: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Conflicts during Shift-Reduce

Parsing

Aditi Raste, CCOEW 19

Page 20: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Conflicts during Shift-Reduce parsing

Conflicts:-

Shift/Reduce conflict

Reduce/Reduce conflict

Aditi Raste, CCOEW 20

Page 21: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Conflicts during Shift-Reduce parsing

Shift/Reduce conflict

Parser reaches a configuration in which knowing

the entire stack contents and next input symbol

cannot decide whether to shift or to reduce.

Yacc parser resolves this conflict in favour of shift.

Aditi Raste, CCOEW 21

Page 22: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Conflicts during Shift-Reduce parsing

Shift/Reduce conflict

Aditi Raste, CCOEW 22

Dangling else grammar

stmt -> if expr then stmt

| if expr then stmt else stmt

| other

STACK INPUT

...if expr then stmt else ...$

Is if expr the st t a ha dle?

Page 23: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Conflicts during Shift-Reduce parsing

Reduce/Reduce conflict

Parser reaches a configuration in which it knows it

has a handle but the stack contents and the next

input symbol are insufficient to determine which

production should be used in a reduction.

Yacc parser resolves this conflict by choosing the

production listed first in the Yacc specification file.

Aditi Raste, CCOEW 23

Page 24: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Operator Precedence Parser

Aditi Raste, CCOEW 24

Page 25: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Operator Precedence Parsing

Shift – Reduce parsing method applied to a

small class of grammars called operator

grammar.

Uses a precedence table to make shift/reduce

decisions.

Aditi Raste, CCOEW 25

Page 26: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Operator Grammar

Contain no ε productions

No production would have two adjacent non

terminals.

Example: Operator grammar recognizing

expressions,

Aditi Raste, CCOEW 26

E --> E + E

| E – E

| E * E

| E / E

| E ^ E

| – E

| (E)

| id

Page 27: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Operator Grammar

Where can operator grammar be used?

(i) Desktop calculator

(ii) Programming languages use operator grammar

for evaluating expressions.

Aditi Raste, CCOEW 27

Page 28: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Aditi Raste, CCOEW 28

a + b $

Operator

precedence Parsing

Program

Precedence

Table O

X

Y

Z

$

Input

Output Stack

Components of Operator Precedence parser

Page 29: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Operator Precedence parser

components

Input:- input string to be parsed followed by $

Stack:-

o tai s g a a sy ols oth T a d NT’s. $ marks the bottom of stack.

when the stack becomes empty, parsing is complete.

Operator precedence parsing program:-

Takes the input string and determines whether it can be derived from the grammar.

Uses operator precedence relations table and stack to arrive at this decision.

Aditi Raste, CCOEW 29

Page 30: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Operator Precedence relations table O:-

two dimensional array

contains precedence relationship between a pair of

terminals

O[a][b] entry determines the precedence relationship

between terminal a and another terminal b.

Three precedence relations:-

(i) Relation a <. b a has lower precedence than b

(ii) Relation a =. b a and b have same precedence

(iii) Relation a .> b a has higher precedence than b

Aditi Raste, CCOEW 30

Operator Precedence parser

components

Page 31: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Operator Precedence parser

components

The determination of correct precedence

relations between terminals are based on the

traditional notions of associativity and

precedence of operators.

Aditi Raste, CCOEW 31

Page 32: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Parsing Actions

The action depends upon

a – topmost terminal symbol on stack

b – current input symbol

Below are three combinations of a and b

Aditi Raste, CCOEW 32

Condition Action

a = b = $ Success

a <. b or a =. b Shift current input symbol on

stack and advance the input

pointer

a .> b This triggers reduce

operation

Page 33: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Reduce operation of operation

precedence parser

Parser pops elements one by one from the stack.

This continues till the TOS element has lower precedence than the most recently popped out element.

The popped out elements form the handle.

If the handle is a valid RHS of some production rule than the non terminal on the LHS is pushed onto the stack and parsing continues.

Else an error message is printed. The parser can recover and continue parsing or stop parsing.

Aditi Raste, CCOEW 33

Page 34: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Operator precedence parsing algorithm The input string is w$, the initial stack is $ and a table holds precedence relations between

certain terminals.

Algorithm:

set p to point to the first symbol of w$ ;

repeat forever

if ( $ is on top of the stack and p points to $ ) then return

else {

let a be the topmost terminal symbol on the stack and let b be the symbol pointed to

by p;

if ( a <. b or a =· b ) then { /* SHIFT */

push b onto the stack;

advance p to the next input symbol;

}

else if ( a .> b ) then /* REDUCE */

repeat pop stack

until ( the top of stack terminal is related by <. to the terminal most recently

popped );

else error();

}

Aditi Raste, CCOEW 34

Page 35: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Using Operator Precedence Relations

Aditi Raste, CCOEW 35

E E+E | E-E | E*E | E/E | E^E | (E) | -E | id

The partial operator-precedence

table for this grammar

Lets parse the input string id + id * id$ with the

operator precedence parser

id + * $

id .> .> .>

+ <. .> <. .>

* <. .> .> .>

$ <. <. <.

Page 36: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Parsing the input string id + id * id$ using

operator precedence parser

STACK INPUT ACTION

$ id + id * id $ O[$][id] = <. Shift

$ id + id * id $ O[id][+] = .> Reduce

$ E + id * id $ O[$][+] = <. Shift

$ E + id * id $ O[+][id] = <. Shift

$ E + id * id $ O[id][*] = .> Reduce

$ E + E * id $ O[+][*] = <. Shift

$ E + E * id $ O[*][id]= <. Shift

$ E + E * id $ O[id][$] = .> Reduce

$ E + E * E $ O[*][$] = .> Reduce

$ E + E $ O[+][$] = .> Reduce

$ E $ Accept

Aditi Raste, CCOEW 36

Page 37: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

How to Create Operator-Precedence Relations

Associativity and precedence relation is used among operators.

1. If operator O1 has higher precedence than operator O2, O1 .> O2 and O2 <. O1

2. If operator O1 and operator O2 have equal precedence,

they are left-associative O1 .> O2 and O2 .> O1

they are right-associative O1 <. O2 and O2 <. O1

3. For all operators O,

O <. id, id .> O, O <. (, (<. O, O .> ), ) .> O, O .> $, and $ <. O

4. Also, let (=·) $ <. ( id .> ) ) .> $ ( <. ( $ <. id id .> $ ) .> ) ( <. id

Aditi Raste, CCOEW 37

Page 38: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Operator precedence relations table

for all operators

Aditi Raste, CCOEW 38

+ - * / ^ id ( ) $

+ .> .> <. <. <. <. <. .> .>

- .> .> <. <. <. <. <. .> .>

* .> .> .> .> <. <. <. .> .>

/ .> .> .> .> <. <. <. .> .>

^ .> .> .> .> <. <. <. .> .>

id .> .> .> .> .> Err Err .> .>

( <. <. <. <. <. <. <. =· Err

) .> .> .> .> .> Err Err .> .>

$ <. <. <. <. <. <. <. Err acc

Page 39: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Precedence Functions

Compilers using operator precedence parsers do not need to store the table of precedence relations.

The table can be encoded by two precedence functions f and g that map terminal symbols to integers.

For symbols a and b.

f(a) < g(b) whenever a <. b

f(a) = g(b) whenever a =· b

f(a) > g(b) whenever a .> b

Precedence functions help in reducing the memory consumption by eliminating the table and replacing with the functions.

Aditi Raste, CCOEW 39

Page 40: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Advantages of Operator Precedence

Parser

Simple and easy to implement

Simple to debug

Powerful enough for describing expressions

used in programming languages

Aditi Raste, CCOEW 40

Page 41: Bottom-Up Parsingthirdyearengineering.weebly.com/uploads/3/8/2/8/38286065/unit_2...Bottom-Up Parsing Aditi Raste, CCOEW 1 . Bottom-Up Parsing Constructs parse tree for an input string

Disadvantages of Operator Precedence

Parser

Cannot handle unary minus. The lexical

analyzer should handle unary minus

Scanner will return two different operators for the

unary minus and binary minus

Scanner will need to lookahead to distinguish

between binary minus and unary minus.

Small class of grammar

Aditi Raste, CCOEW 41


Recommended