Docsity

Bottom up Parsing - Compiler Construction - Lecture Notes, Study notes of Compiler Construction

Toggle sidebar
bg1
Sohail Aslam Compiler Construction Notes Set:3-63
63
L
Le
ec
ct
tu
ur
re
e
2
20
0
Bottom-up Parsing
Bottom-up parsing is more general than top-down parsing. Bottom-up parsers handle a
large class of grammars. It is the preferred method in practice. It is also called LR
parsing; L means that tokens are read left to right and R means that the parser constructs a
rightmost derivation. LR parsers do not need left-factored grammars. LR parsers can
handle left-recursive grammars.
LR parsing reduces a string to the start symbol by inverting productions.A derivation
consists of a series of rewrite steps
S ⇒ γ0 ⇒ γ1 ⇒ ... ⇒ γn-1
⇒ γn ⇒ sentence
Each γi is a sentential form. If γ contains only terminals, γ is a sentence in L(G). If γ
contains ≥ 1 nonterminals, γ is a sentential form. A bottom-up parser builds a derivation
by working from input sentence back towards the start symbol S.
Consider the grammar
S → aABe
A → Abc | b
B → d
The sentence abbcde can be reduced to S:
abbcde
aAbcde
aAde
aABe
S
These reductions, in fact, trace out the following right-most derivation in reverse:
S ⇒ aABe
⇒ aAde
⇒ aAbcde
⇒ abbcde
S ⇒ aBy ⇒ aγy ⇒ xy
pf3

Partial preview of the text

Download Bottom up Parsing - Compiler Construction - Lecture Notes and more Study notes Compiler Construction in PDF only on Docsity!

Le Leccttuurree 2 200

Bottom-up Parsing

Bottom-up parsing is more general than top-down parsing. Bottom-up parsers handle a large class of grammars. It is the preferred method in practice. It is also called LR parsing; L means that tokens are read left to right and R means that the parser constructs a rightmost derivation. LR parsers do not need left-factored grammars. LR parsers can handle left-recursive grammars.

LR parsing reduces a string to the start symbol by inverting productions.A derivation consists of a series of rewrite steps

S ⇒ γ 0 ⇒ γ 1 ⇒ ... ⇒ γn- ⇒ γn ⇒ sentence

Each γi is a sentential form. If γ contains only terminals, γ is a sentence in L(G). If γ contains ≥ 1 nonterminals, γ is a sentential form. A bottom-up parser builds a derivation by working from input sentence back towards the start symbol S.

Consider the grammar

S → aABe A → Abc | b B → d

The sentence abbcde can be reduced to S:

abbcde aAbcde aAde aABe S

These reductions, in fact, trace out the following right-most derivation in reverse:

S ⇒ aABe ⇒ aAde ⇒ aAbcde ⇒ abbcde

S ⇒ aBy ⇒ aγy ⇒ xy

Consider the grammar

  1. E → E + (E)
  2. | int

The bottom-up parse of the string int + (int) + (int) would be

int + (int) + (int) E + (int) + (int) E + (E) + (int) E + (int) E + (E) E

The consequence of an LR parser tracing a rightmost derivation in reverse is that given αβγ be a step of a bottom-up parse, assuming that next reduction is A → β τhen γ is a string of terminals. The reason is that αAγ → αβγ is a step in a rightmost derivation. This observation provides a strategy for building bottom up parsers: split the input string into two substrings. Right substring (a string of terminals) is as yet unexamined by parser and left substring has terminals and non-terminals. The dividing point is marked by a ► (the ► is not part of the string). Initially, all input is unexamined: ►x 1 x1... xn.

Shift-Reduce Parsing

Bottom-up parsing uses only two kinds of actions:

  1. Shift
  2. Reduce

α

x y

B

S

γ

rule: B → γ

Terminals only