Docsity

Bottom-Up Parsing - Programming Language Translation - Lecture Slides, Slides of Programming Languages

Partial preview of the text

Download Bottom-Up Parsing - Programming Language Translation - Lecture Slides and more Slides Programming Languages in PDF only on Docsity!

Chapter 4 - Part 3: Bottom-Up Parsing

Basic Intuition

 Recall that

 LL(k) works

TOP-DOWN

With a LEFTMOST Derivation Predicts the right production to select based on lookahead

 Our new motto

 LR(k) works

BOTTOM-UP

With a RIGHTMOST Derivation Commits to the production choice after seeing the whole body (left hand side), working in “reverse”

Type of Derviation

 Grammar:

S → a A B e

A → Abc | b

B → d

 Key Issues:

 How do we Determine which Substring to

“Reduce”?

 How do we Know which Production Rule to Use?

 What is the General Processing for BUP?

 How are Conflicts Resolved?

 What Types of BUP are Considered?

TDP: S ⇒ aABe ⇒ aAbcBe ⇒ abbcBe ⇒ abbcde BUP: S ⇒ aABe ⇒ aAde ⇒ aAbcde ⇒ abbcde Is a rightmost derivation that happens in reverse!

What is a Handle?

 Defn: A Right-Sentential Form is Sentential Form that

has Been Derived in a Righmost Derivation

 S ⇒ aABe ⇒ aAde⇒ aAbcde ⇒ abbcde

 Underline all Right Sentential Forms

 Handle is a Substring of a Right Sentential Form that:

 Appears on Right Hand Side of Production Rule

 Can be Used to Reduce the Right Sentential Form

via a Substitution in a Step of a RM Derivation

 Formally is a rule A → β and position in Right

Sentential Form γ s.t.

S ⇒ RM*^ αAw ⇒ RM αβw and A occurs at γ in αAw

 Example: Handles are Underlined in:

 S ⇒ aABe ⇒ aAde⇒ aAbcde ⇒ abbcde

 Abc is Right hand Side of Rule A → Abc at

Position 2 in Right Sentential Form γ = aAbcdeDocsity.com

Handle Pruning

 What bottom-up really means...

abbcde aAbcde

Handle Pruning

aAbcde aAde

Handle Pruning

aABe S

What’s Going on in Parse Tree?

 Consider Right Sentential Form: αβw and Rule A → β

S

α β w

A

What Does α Signify?

What Does β Represent?

What Does Input Processed w Contain? Still on Parsing Stack

Candidate Handle to be Reduced

Input yet to be Consumed

General Processing of BUP

 Basic mechanisms

 “Shift”

 “Reduce”

 Basic data-structure

 A stack of grammar symbols (Terminals and Non-

Terminals)

 Basic idea

 Shift input symbols on the stack until ... the entire

handle of the last rightmost reduction

 When the body of the last RM reduction is on

Stack, reduce it by replacing the body by the right-

hand-side of the Production Rule

 When only start symbol is left

 We are done.

Example

$ abbcde$ Shift $a bbcde$ Shift $ab bcde$ Reduce $aA bcde$ Shift $aAb cde$ Shift $aAbc de$ Reduce $aA de$ Shift $aAd e$ Reduce $aAB e$ Shift $aABe $ Reduce $S $ Accept

Handle

Rule to Reduce with

Docsity.com

Example

$ abbcde$ Shift $a bbcde$ Shift $ab bcde$ Reduce $aA bcde$ Shift $aAb cde$ Shift $aAbc de$ Reduce $aA de$ Shift $aAd e$ Reduce $aAB e$ Shift $aABe $ Reduce $S $ Accept

Handle (^) Rule to

Reduce with

Docsity.com

Example

$ abbcde$ Shift $a bbcde$ Shift $ab bcde$ Reduce $aA bcde$ Shift $aAb cde$ Shift $aAbc de$ Reduce $aA de$ Shift $aAd e$ Reduce $aAB e$ Shift $aABe $ Reduce $S $ AcceptDocsity.com

Key Observation

 At any point in time

 Content of the stack is a prefix of a

right-sentencial form

 This prefix is called a viable prefix

 Check again!

 Below = all the right-sentencial form

of a rightmost derivation

 S ⇒ aABe

⇒ aAde

⇒ aAbcde

⇒ abbcde

$a $ab $aA $aAb $aAbc $aA $aAd $aAB $aABe $S

What is General Processing for BUP?

 Utilize a Stack Implementation:

 Contains Symbols, Non-Terminals, and Input

 Input is Examined w.r.t. Stack/Current State

 General Operation: Options to Process Stack Include:

 Shift Symbols from Input onto Stack

 When Handle β on Top of Stack

 Reduce by using Rule: A → β  Pop all Symbols of Handle β  Push Non-Terminal A onto Stack

 When Configuration ($S, $) of Stack, ACCEPT

 Error Occurs when Handle Can’t be Found or S is

on Stack with Non-Empty Input