0% found this document useful (0 votes)
2 views

Lecture 06

Bottom-up parsing is a general and efficient method that constructs parse trees from input strings by inverting grammar productions. It includes techniques like Shift-Reduce Parsing, Operator Precedence Parsing, and Table Driven LR Parsing, which utilize a stack and input buffer to manage parsing operations. The process alternates between shifting and reducing symbols until the input string is successfully parsed into the start symbol.

Uploaded by

nihafahima9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Lecture 06

Bottom-up parsing is a general and efficient method that constructs parse trees from input strings by inverting grammar productions. It includes techniques like Shift-Reduce Parsing, Operator Precedence Parsing, and Table Driven LR Parsing, which utilize a stack and input buffer to manage parsing operations. The process alternates between shifting and reducing symbols until the input string is successfully parsed into the start symbol.

Uploaded by

nihafahima9
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 35

Bottom-Up Parsing

1
Bottom-Up Parsing
■ Bottom-up parsing is more general than top-down
parsing
■ Just as efficient
■ Builds on ideas in top-down parsing

■ Bottom-up is the preferred method in practice

■ Reading: Section 4.5

2
Bottom-up parsing has been classified
into various parsing. These are as follows:
1.Shift-Reduce Parsing
2.Operator Precedence Parsing
3.Table Driven LR Parsing

3
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|T
T → int * T | int | (E)

■ Consider the string: int * int + int

4
Bottom-up parsing reduces a string to the start
symbol by inverting productions
int * int + T → int
int int * T T → int *
+ int T + T T → int
int E→T
T+T E→T+
T+ E
E E

5
Observation
■ Read productions from bottom-up
parse in reverse (i.e., from bottom
to top)
■ This is a rightmost derivation

int * int + T → int


int int * T T → int *
+ int T + T T→
int int
T + E→T
T T E→T+
+ E E
E

6
A Bottom-up Parse

int * int +
int int * T
+ int T +
int T E

T+T
T T
T+
E E Int * int int
+

A bottom-up parser traces a


rightmost derivation in reverse!

7
8
CFG
𝑆 → 𝑎𝐴𝐵𝑒
𝐴 → 𝐴𝑏𝑐 | 𝑏
𝐵→d

9
Shift-Reduce Parsing
Shift-reduce parsing is a bottom-up approach
used to analyze the structure of a string
based on a given grammar. The parser
attempts to reduce the input string to the
start symbol of the grammar by shifting and
reducing tokens.

•Bottom-up Parsing: It starts from the input and


tries to construct a parse tree up to the start
symbol.
•Shift-Reduce Parsing: It combines two
fundamental operations: shifting and reducing,
which work on a stack of grammar symbols and the
input buffer.
10
Key Components
•Stack: Stores the symbols (terminals and
non-terminals) that are currently being processed.
•Input Buffer: Holds the remaining part of the input
string yet to be parsed.
•Parsing Table: Guides the actions (shift or reduce)
based on the state of the stack and input buffer.

11
. Shift and Reduce Operations

•Shift Operation:
•The next symbol from the input buffer is pushed
onto the stack.
•The parser moves the input pointer one symbol
forward.
•Example: If the current input symbol is id, the parser
shifts id from the input buffer to the stack.

12
Reduce Operation:
•The parser pops symbols from the stack and replaces
them with the non-terminal defined by a grammar
production rule.
•A reduction occurs when the top of the stack matches
the right-hand side of a production rule.
•Example: If the top of the stack is id and the grammar
rule is E → id, the parser replaces id with E.

13
Shift-Reduce Parsing Process

The parser alternates between shifting and reducing as it


reads the input string. The steps are as follows:
1.Initialization: The stack is empty, and the input buffer holds
the entire input string.
2.Shifting: The parser shifts input symbols onto the stack
until a recognizable pattern (the right-hand side of a
production) is formed.
3.Reducing: When a match is found, the parser applies a
production rule, replaces the matched string with the
corresponding non-terminal, and continues processing.
4.Acceptance: The input string is successfully parsed if the
stack contains only the start symbol after the entire input
has been processed.
14
Types of Shift-Reduce Parsers

LR Parsers: Use more sophisticated lookahead


mechanisms to handle larger classes of
grammars. Examples include SLR, LALR, and
canonical LR parsers.
Operator Precedence Parsers: A specialized
type of shift-reduce parser used when
operators with different precedences need to
be parsed.

15
. Example of Shift-Reduce Parsing
Grammar:
E→E+E Input Str
E→E*E ing: id + id * id
E→(E)
E → id
Stack Input Action
[] id + id * id Shift
[id] + id * id Reduce E → id
[E] + id * id Shift
[E, +] id * id Shift
[E, +, id] * id Reduce E → id
[E, +, E] * id Shift
[E, +, E, *] id Shift
[E, +, E, *, id] Reduce E → id

[E, +, E, *, E] Reduce E → E * E

[E, +, E] Reduce E → E + E

[E] Accept 16
Shift-Reduce Parsing Working

• Initially, the stack contains the $ symbol, and the input buffer
contains the input string with the $ symbol at its end

• For performing the shift-reduce parsing, push the input


symbols at the top of the stack (shift operation) until a handle
ß appears on the top of a stack (reduce operation).
• Repeat the above step until
• An error is detected or
• The string is accepted, i.e., the stack contains only the
start symbol, and the input buffer becomes empty (has only
the $ symbol).

17
Shift-Reduce Parsing Working

18
•Rule 1: If the priority of the incoming operator is
higher than the operator's priority at the top of the
stack, then we perform the shift action.
•Rule 2: If the priority of the incoming operator is
equal to or less than the operator's priority at the
top of the stack, then we perform the reduce action.

19
Example

Consider the following grammar.


E→E+E
E→ExE
E → id
Perform shift-reduce parsing for the
input string “id + id x id”.

20
The priority order of operators is: id > x > + > E > $.
The top of the stack contains $ while the current
input symbol is the starting symbol, i.e., id. The
priority of id > $. Thus, we will follow rule 1 and will
perform the shift operation.

Stack Input Parsing


Buffer Action
$ id+idxid$

21
The top of the stack contains $ while the current input
symbol is the starting symbol, i.e., id. The priority of id
> $. Thus, we will follow rule 1 and will perform the shift
operation.

Stack Input Buffer Parsing Action


$ id+idxid$ Shift
$id +idxid$

22
Now, the symbol at the top of the stack is id, and the
current input symbol is +. Since the priority of + < id, we
will follow rule 2 and perform the reduce operation.
For performing the reduce operation, we will check the
grammar containing id on the right-hand side. Since E →
id, we can reduce id by E.

Stack Input Buffer Parsing Action


$ id+idxid$ Shift
$id +idxid$ Reduce by E →
id
$E +idxid$
23
Next, the priority of + > E, perform the shift operation.
Stack Input Buffer Parsing Action
$ id+idxid$ Shift
$id +idxid$ Reduce by E → id
$E +idxid$ Shift
$E+ idxid$

24
Next, the priority of id > +, perform the shift operation.

Stack Input Buffer Parsing Action


$ id+idxid$ Shift
$id +idxid$ Reduce by E →
id
$E +idxid$ Shift
$E+ idxid$ Shift
$E+id xid$

25
Next, the priority of x < id, perform the reduce
operation. Since in the grammar E → id, we can reduce id
by E.
Stack Input Buffer Parsing Action
$ id+idxid$ Shift
$id +idxid$ Reduce by E → id
$E +idxid$ Shift
$E+ idxid$ Shift
$E+id xid$ Reduce by E → id
$E+E xid$

26
Next, the priority of x > E, perform the shift operation.
Stack Input Buffer Parsing Action
$ id+idxid$ Shift
$id +idxid$ Reduce by E → id
$E +idxid$ Shift
$E+ idxid$ Shift
$E+id xid$ Reduce by E → id
$E+E xid$ Shift
$E+Ex id$

27
Next, the priority of id > x, perform the shift operation.

Stack Input Buffer Parsing Action


$ id+idxid$ Shift
$id +idxid$ Reduce by E → id
$E +idxid$ Shift
$E+ idxid$ Shift
$E+id xid$ Reduce by E → id
$E+E xid$ Shift
$E+Ex id$ Shift
$E+Exid $

28
Next, the priority of $ < id, perform the reduce operation. Since in the
grammar E → id, we can reduce id by E.

Stack Input Buffer Parsing Action


$ id+idxid$ Shift
$id +idxid$ Reduce by E → id
$E +idxid$ Shift
$E+ idxid$ Shift
$E+id xid$ Reduce by E → id
$E+E xid$ Shift
$E+Ex id$ Shift
$E+Exid $ Reduce by E → id
$E+ExE $
29
the priority of $ < E, we will perform the reduce operation. Since there is no
grammar containing E, we will check the grammar containing xE. However, there
is also no grammar containing xE, check the grammar containing ExE. We find
that E → E x E. So, we will reduce ExE by E.

Stack Input Buffer Parsing Action


$ id+idxid$ Shift
$id +idxid$ Reduce by E → id
$E +idxid$ Shift
$E+ idxid$ Shift
$E+id xid$ Reduce by E → id
$E+E xid$ Shift
$E+Ex id$ Shift
$E+Exid $ Reduce by E → id
$E+ExE $ Reduce by E → E x E

$E+E $ 30
Next, the priority of $ < E, we will perform the reduce operation. Since there
is no grammar containing E, we will check the grammar containing +E. However,
there is also no grammar containing +E, check the grammar containing E+E. We
find that E → E + E. So, we will reduce E+E by E.
Stack Input Buffer Parsing Action
$ id+idxid$ Shift
$id +idxid$ Reduce by E → id
$E +idxid$ Shift
$E+ idxid$ Shift
$E+id xid$ Reduce by E → id
$E+E xid$ Shift
$E+Ex id$ Shift
$E+Exid $ Reduce by E → id
$E+ExE $ Reduce by E → E x E

$E+E $ Reduce by E → E + E

$E $ 31
Now, the stack only contains the starting symbol of the input string E and the
input buffer is empty, i.e., includes the $ symbol. Hence, the parser will accept
the string and will be completed.

Stack Input Buffer Parsing Action


$ id+idxid$ Shift
$id +idxid$ Reduce by E → id
$E +idxid$ Shift
$E+ idxid$ Shift
$E+id xid$ Reduce by E → id
$E+E xid$ Shift
$E+Ex id$ Shift
$E+Exid $ Reduce by E → id
$E+ExE $ Reduce by E → E x E

$E+E $ Reduce by E → E + E

$E $ Accept 32
Example 2:
Consider the following grammar.
S→S+S
S→S-S
S → (S)
S→a
Perform shift-reduce parsing for the input string
“a1-(a2+a3)”.
Solution: The priority order of operators is: a1/a2/a3 >
() > +/- > S > $.

33
Stack Input Buffer Parsing
Action
$ a1-(a2+a3)$ Shift
$a1 -(a2+a3)$ Reduce by S → a
$S -(a2+a3)$ Shift
$S- (a2+a3)$ Shift
$S-( a2+a3)$ Shift
$S-(a2 +a3)$ Reduce by S → a
$S-(S +a3)$ Shift
$S-(S+ a3)$ Shift
$S-(S+a3 )$ Reduce by S → a
$S-(S+S )$ Shift
$S-(S+S) $ Reduce by S → S + S
$S-(S) $ Reduce by S → (S)
$S-S $ Reduce by S → S - S
$S $ Accept
Grammar:
1.S → S+S Input
2.S → S-S string: a1-(a2+a3)
3.S → (S)
4.S → a
Stack contents Input String Actions
$ a1-(a2+a3)$ Shift a1
$a1 -(a2+a3)$ Reduce by s->a
$S -(a2+a3)$ Shift -
$S- (a2+a3)$ Shift (
$S-( a2+a3)$ Shift a2
$S-(a2 +a3)$ Reduce by S->a
$S-(S +a3)$ Shift +
$S-(S+ a3)$ Shift a3
$S-(S+a3 )$ Reduce by S->a
$S-(S+S )$ shift)
$S-(S+S) $ Reduce by S->S+S
$S-(S) $ Reduce by S->(S)
$S-S $ Reduce by S->S-S 35
$S $ Accept

You might also like