You are on page 1of 29

Compiler Construction

Sohail Aslam Lecture 21

Shift-Reduce: The Stack

Left string can implemented as a stack Top of the stack is the Shift pushes a terminal on the stack
2

Shift-Reduce: The Stack

Left string can implemented as a stack Top of the stack is the Shift pushes a terminal on the stack
3

Shift-Reduce: The Stack

Left string can implemented as a stack Top of the stack is the Shift pushes a terminal on the stack
4

int + (int) + (int) $ int + (int) + (int) $ E + (int) + (int) $ E + (int ) + (int) $

shift reduce E int shift 3 times reduce E int

int ( + E
stack
5

Shift-Reduce: The Stack

Reduce pops zero or more symbols from the stack (production rhs) and pushes a non-terminal on the stack (production lhs)
6

int + (int) + (int) $ int + (int) + (int) $ E + (int) + (int) $ E + (int ) + (int) $

shift reduce E int shift 3 times reduce E int

int

( + E
stack
7

Discovering Handles
A bottom-up parser builds the parse tree starting with its leaves and working toward its root

Discovering Handles
The upper edge of this partially contructed parse tree is called its upper frontier.

int + (int) + (int) E + (int) + (int) E + (E) + (int) E + (int) E + (E) E

int + ( int ) + ( int )


10

Discovering Handles
At each step, the parser looks for a section of the upper frontier that matches right-hand side of some production.
11

Discovering Handles
When it finds a match, the parser builds a new tree node with the productions left-hand non-terminal thus extending the frontier upwards towards the root.
12

Discovering Handles
The critical step is developing an efficient mechanism that finds matches along the trees current frontier.
13

Discovering Handles
Formally, the parser must find some substring b, of the upper frontier where
1. b is the right-hand side of some production A b, and 2. A b is one step in right-most derivation of input stream
14

Discovering Handles
Formally, the parser must find some substring b, of the upper frontier where
1. b is the right-hand side of some production A b, and 2. A b is one step in right-most derivation of input stream
15

Discovering Handles
Formally, the parser must find some substring b, of the upper frontier where
1. b is the right-hand side of some production A b, and 2. A b is one step in right-most derivation of input stream
16

Discovering Handles
We can represent each potential match as a pair Ab,k, where k is the position on the trees current frontier of the rightend of b.
17

Discovering Handles
The pair Ab,k, is called the handle of the bottomup parse.

18

Handle Pruning
A bottom-up parser operates by repeatedly locating handles on the frontier of the partial parse tree and performing reductions that they specify.
19

Handles
The bottom-up parser uses a stack to hold the frontier. The stack simplifies the parsing algorithm in two ways.
20

Handles
The bottom-up parser uses a stack to hold the frontier. The stack simplifies the parsing algorithm in two ways.
21

Handles
First, the stack trivializes the problem of managing space for the frontier
To extend the frontier, the parser simply pushes the current input token onto the top of the stack
22

Handles
First, the stack trivializes the problem of managing space for the frontier
To extend the frontier, the parser simply pushes the current input token onto the top of the stack
23

Handles
Second, the stack ensures that all handles occur with their right end at the top of the stack
eliminates the need to represent handles position
24

Handles
Second, the stack ensures that all handles occur with their right end at the top of the stack
eliminates the need to represent handles position
25

E + (int) $ 1 2 3 4 E + (int ) $ E + (E ) $
E + (E) $ E$

shift 3 times reduce E int shift


red E E+(E) accept

handle: Eint,4 handle: EE+(E),5


26

Shift-Reduce Parsing Algorithm


push $ onto stack sym nextToken() repeat until (sym == $ and the stack contains exactly Goal on top of $) if a handle for A b on top of stack pop |b| symbols off the stack push A onto the stack
27

Shift-Reduce Parsing Algorithm


else if (sym $ )

push sym onto stack sym nextToken() else /* no handle, no input */ report error and halt

28

Building a parser with YACC and Lex


expr.y YACC y.tab.c

y.tab.h
expr.l lex lex.yy.c

CC

expr.exe

29

You might also like