Download presentation
Presentation is loading. Please wait.
1
Compiler Construction
Sohail Aslam Lecture 21 compiler: Bottom-up Parsing
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
Shift-Reduce: The Stack
Left string can implemented as a stack Top of the stack is the ► Shift pushes a terminal on the stack end of lec 20 compiler: Bottom-up Parsing
5
►int + (int) + (int) $ shift int ► + (int) + (int) $ reduce E → int
E ► + (int) + (int) $ shift 3 times E + (int ►) + (int) $ int ( + ► E stack
6
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)
7
►int + (int) + (int) $ shift int ► + (int) + (int) $ reduce E → int
E ► + (int) + (int) $ shift 3 times E + (int ►) + (int) $ E ► int ( + E stack
8
Discovering Handles A bottom-up parser builds the parse tree starting with its leaves and working toward its root
9
Discovering Handles The upper edge of this partially contructed parse tree is called its upper frontier.
10
int + (int) + (int) E + (int) + (int) E + (E) + (int) E + (int)
11
Discovering Handles At each step, the parser looks for a section of the upper frontier that matches right-hand side of some production.
12
Discovering Handles When it finds a match, the parser builds a new tree node with the production’s left-hand non-terminal thus extending the frontier upwards towards the root.
13
Discovering Handles The critical step is developing an efficient mechanism that finds matches along the tree’s current frontier.
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 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
17
Discovering Handles We can represent each potential match as a pair A→b,k, where k is the position on the tree’s current frontier of the right-end of b.
18
Discovering Handles The pair A→b,k, is called the handle of the bottom-up parse.
19
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.
20
Handles The bottom-up parser uses a stack to hold the frontier.
The stack simplifies the parsing algorithm in two ways.
21
Handles The bottom-up parser uses a stack to hold the frontier.
The stack simplifies the parsing algorithm in two ways.
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 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
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 handle’s position
25
Handles Second, the stack ensures that all handles occur with their right end at the top of the stack eliminates the need to represent handle’s position
26
E ► + (int) $ shift 3 times E + (int ►) $ reduce E → int E + (E ►) $
red E → E+(E) E ► $ accept 1 2 3 4 handle: E→int,4 handle: E→E+(E),5
27
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
28
Shift-Reduce Parsing Algorithm
else if (sym $ ) push sym onto stack sym ← nextToken() else /* no handle, no input */ report error and halt end of lec 21 compiler: Bottom-up Parsing
29
Building a parser with YACC and Lex
expr.y YACC y.tab.c expr.exe y.tab.h CC This is a note expr.l lex lex.yy.c compiler: Bottom-up Parsing
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.