

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!


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
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:
α
x y
γ
rule: B → γ
Terminals only