Lecture 06
Lecture 06
1
Bottom-Up Parsing
■ Bottom-up parsing is more general than top-down
parsing
■ Just as efficient
■ Builds on ideas in top-down parsing
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
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
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
+
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.
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
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
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
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.
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.
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.
24
Next, the priority of id > +, perform the shift operation.
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.
28
Next, the priority of $ < id, perform the reduce operation. Since in the
grammar E → id, we can reduce id by 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.
$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