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

Syntax Analysis: Bottom-Up Parsing

Bottom-up parsing constructs a parse tree starting from the leaves and working upwards towards the root by performing reductions. It involves finding the right-most derivation of a given string in a context-free grammar. A bottom-up parser operates by repeatedly choosing reductions or shifts until it reaches a sentential form of S, accepting the input string.

Uploaded by

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

Syntax Analysis: Bottom-Up Parsing

Bottom-up parsing constructs a parse tree starting from the leaves and working upwards towards the root by performing reductions. It involves finding the right-most derivation of a given string in a context-free grammar. A bottom-up parser operates by repeatedly choosing reductions or shifts until it reaches a sentential form of S, accepting the input string.

Uploaded by

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

Bottom-Up Parsing

Shift-Reduce Parsing

Syntax Analysis: Bottom-Up Parsing

Lecture 5

Haythem
c O. Ismail Bottom-Up Parsing 1 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Objectives

By the end of this lecture you should be able to:


1 Identify bottom-up parsing.
2 Construct reductions of a given string and grammar.
3 Construct a shift-reduce PDA.
4 Identify types of shift-reduce conflicts.

Haythem
c O. Ismail Bottom-Up Parsing 2 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Outline

1 Bottom-Up Parsing

2 Shift-Reduce Parsing

Haythem
c O. Ismail Bottom-Up Parsing 3 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Outline

1 Bottom-Up Parsing

2 Shift-Reduce Parsing

Haythem
c O. Ismail Bottom-Up Parsing 4 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

What is Bottom-Up Parsing?

Definition
Bottom-up parsing consists in the construction of a parse tree starting
with the leaves up towards the root.

A particular style of bottom-up parsing may be equivalently defined


thus:
Definition
Bottom-up parsing consists in finding a right-most derivation of a
given string in a given CFG.

Haythem
c O. Ismail Bottom-Up Parsing 5 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

What is Bottom-Up Parsing?

Definition
Bottom-up parsing consists in the construction of a parse tree starting
with the leaves up towards the root.

A particular style of bottom-up parsing may be equivalently defined


thus:
Definition
Bottom-up parsing consists in finding a right-most derivation of a
given string in a given CFG.

Haythem
c O. Ismail Bottom-Up Parsing 5 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Example: G2

Example
E −→ E + T | T
T −→ T ∗ F | F
F −→ ( E ) | id | number
Input: id ∗ id.

Haythem
c O. Ismail Bottom-Up Parsing 6 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Example: Bottom-Up Parsing

Example


c Aho et al. (2007)

Haythem
c O. Ismail Bottom-Up Parsing 7 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Reductions

A reduction is the reverse of a derivation.


Definition

A sentential form α reduces to a sentential form β, denoted α 7→ β if

β ⇒ α.

Example

id∗id 7→ F∗id 7→ T∗id 7→ T∗F 7→ T 7→ E


At every step of a reduction, we replace a sentential form by a
variable.

Haythem
c O. Ismail Bottom-Up Parsing 8 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Reductions

A reduction is the reverse of a derivation.


Definition

A sentential form α reduces to a sentential form β, denoted α 7→ β if

β ⇒ α.

Example

id∗id 7→ F∗id 7→ T∗id 7→ T∗F 7→ T 7→ E


At every step of a reduction, we replace a sentential form by a
variable.

Haythem
c O. Ismail Bottom-Up Parsing 8 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Bottom-Up Parsing, Again

Definition
Given a CFG G = hV, Σ, R, Si and w ∈ Σ∗ , bottom-up parsing, with a
left-to-right scan of the input, is the problem of constructing a
reduction from w to S such that if

αγβ 7→ αAβ

is a step in the reduction, then no subsequent step may be of the form

δ1 δ2 δ3 λ 7→ δ1 Bδ3 λ

where Aβ 7→ λ and δ1 δ2 δ3 = α.

Thus constrained, such a reduction is the reverse of a right-most


derivation.
Haythem
c O. Ismail Bottom-Up Parsing 9 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Bottom-Up Parsing, Again

Definition
Given a CFG G = hV, Σ, R, Si and w ∈ Σ∗ , bottom-up parsing, with a
left-to-right scan of the input, is the problem of constructing a
reduction from w to S such that if

αγβ 7→ αAβ

is a step in the reduction, then no subsequent step may be of the form

δ1 δ2 δ3 λ 7→ δ1 Bδ3 λ

where Aβ 7→ λ and δ1 δ2 δ3 = α.

Thus constrained, such a reduction is the reverse of a right-most


derivation.
Haythem
c O. Ismail Bottom-Up Parsing 9 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

General Structure of a Bottom-Up Parser

Given G = hV, Σ, R, Si and w.


1 SF = w.
2 α = ε.
3 For SF = αβ, choose one the following.
1a. Choose γ(6= ε), δ, α1 , and α2 such that β = γδ and α = α1 α2 .
1b. Choose (A → α2 γ) ∈ R.
1c. SF = α1 Aδ.
1d. α = α1 .
2a. Choose γ, δ and, λ such that β = γδλ.
2b. Choose (A → δ) ∈ R.
2c. SF = αγAλ.
2d. α = αγ.
4 If no choices are available, then fail.
5 If SF = S then return.
6 Goto 3.

α2 γ in 1b and δ in 2b are referred to as handles of SF.

Haythem
c O. Ismail Bottom-Up Parsing 10 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Outline

1 Bottom-Up Parsing

2 Shift-Reduce Parsing

Haythem
c O. Ismail Bottom-Up Parsing 11 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

PDA Perspective

You may think of a shift-reduce parser as a PDA P.


P has three main states: qs , qloop , and qa .
In qs , it pushes $ and enters qloop .
While in qloop
nondeterministically, do one of the following:
1 Shift, by reading an input symbol and pushing it onto the stack.
2 Reduce, by popping zero or more stack symbols s1 · · · sn , with sn
on top of the stack, and pushing a variable A, where A → s1 · · · sn
is a nondeterministically chosen rule of the grammar.
3 Pop the start symbol and then $ and enter qa .

Haythem
c O. Ismail Bottom-Up Parsing 12 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Exercise

Example
Draw the state diagram of the shift-reduce PDA which is equivalent to
the CFG G2 .
Do it yourself.

Haythem
c O. Ismail Bottom-Up Parsing 13 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Exercise (Cont’d)

Example


c Aho et al. (2007)

Haythem
c O. Ismail Bottom-Up Parsing 14 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Conflicts

Given the nondeterminism of a shift-reduce parser, two types of


conflicts may be encountered during parsing:
1 Shift/Reduce conflict, which occurs when it is possible to shift
and to reduce. This can only happen when there is at least one
remaining input symbol.
2 Reduce/Reduce conflict, which occurs when there is more than
one way to reduce. This depends on the grammar rules.
In particular, it will occur whenever there are two rules A −→ α
and B −→ β with β a suffix of α.

Haythem
c O. Ismail Bottom-Up Parsing 15 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Dangling Else (I)

Example
stmnt −→ if bexpr then stmnt
stmnt −→ if bexpr then stmnt else stmnt
stmnt −→ . . .
.. ..
. .
bexpr −→ . . .
.. ..
. .
What should the PDA do in the following configuration?
S TACK I NPUT
. . . if bexpr then stmnt else . . .

Haythem
c O. Ismail Bottom-Up Parsing 16 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Dangling Else (II)

Example
This is a shift/reduce conflict.
We may impose the common disambiguation strategy by
preferring shifting over reducing.

Haythem
c O. Ismail Bottom-Up Parsing 17 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Arrays and Procedures (I)

Example

stmnt −→ id ( parameter_list ) | expr := expr


parameter_list −→ parameter_list , parameter | parameter
parameter −→ id
expr −→ id ( expr_list ) | id
expr_list −→ expr_list , expr | expr

What should the PDA do in the following configuration?


S TACK I NPUT
. . . id ( id , id ) . . .

Haythem
c O. Ismail Bottom-Up Parsing 18 / 19
Bottom-Up Parsing
Shift-Reduce Parsing

Arrays and Procedures (II)

Example
Assume the PDA can cheat and detect that shifting is no good.
Which rule should be used to reduce id?
This is a reduce/reduce conflict.

Haythem
c O. Ismail Bottom-Up Parsing 19 / 19

You might also like