This preview shows pages 1–3. Sign up to view the full content.
COT
4210
Discrete Structures II
Fall 1998
BottomUp Parsing
In contrast with Topdown parsing algorithms and grammars, the family of Bottomup parsing
algorithms and grammars can be characterized by those algorithms that attempt to produce the
reverse of a rightmost derivation
with respect to some underlying contextfree grammar, G.
A
rightmost derivation in a contextfree grammar tends to expand the final terminal string in a
righttoleft fashion.
Whereas a Topdown parsing algorithms apply grammar rules in the
natural way – by expanding the leftside (nonterminal) of the rule with the rightside (handle) – a
Bottomup parser reverses this process by pushing input symbols into the stack (shift action)
until the rightside of some rule can be recognized in the stack.
The Bottomup parser then
“reduces the stack” by replacing the rightside of some rule (in the stack) with the corresponding
leftside (nonterminal) of the rule.
This process continues until one of two conditions occur: (1)
an erroneous symbol or token is recognized as not being consistent with the grammar (in some
way), or (2) the entire input has been successfully reduced to just the start symbol of the
grammar (S).
Figure 1 illustrates the conceptual model of a Bottomup parse.
Figure 1. Conceptual Model of a Bottomup Parser.
The family of deterministic Bottomup parsers and grammars is larger and more powerful than
the family of deterministic Topdown parsers.
The most commonly used family of grammars
for defining programming languages and building Bottomup parsers is the family of LR(k)
grammars.
UNIX tools such as YACC produce a parser as output when presented with an LR(1)
grammar as input.
In general, LR(k) parsers are very complicated and their parsers are quite
complex.
However, there is a subfamily of LR(1) grammars, the Weak Precedence Grammars,
that are easier to understand and much less difficult to parse.
We therefore continue with a
discussion of these grammars and their parsing algorithms.
4/29/2009
Page 1
© D. A. Workman
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document COT
4210
Discrete Structures II
Fall 1998
Weak Precedence Grammars
DEFINITION
(LEFT and RIGHT sets)
Let G = (N,
Σ
, P, S) be a reduce contextfree grammar.
We define the LEFT and RIGHT sets
for each nonterminal, X
∈
N, as follows.
LEFT(X) = { Y
∈
V
G

X
⇒+
Y
α
, for some
α
∈
V
G
* }
RIGHT(X) = { Y
∈
V
G

X
⇒+
α
Y, for some
α
∈
V
G
* }
Example
G = (N,
Σ
, P, S), where N = {S, X, Y, B},
Σ
= {a, b, c}
P = {
1:
S
→
XY
2:
X
→
a X B
3:
X
→
a b
4:
B
→
b
5:
Y
→
Y c
6:
Y
→
c
}
Symbol
LEFT
RIGHT
B
{ b }
{ b }
Y
{ Y, c }
{ c }
X
{ a }
{ b, B }
S
{ X, a }
{ Y, c }
DEFINITION
(WirthWeber Precedence Relations)
Let G = (N,
Σ
, P, S) be a reduce contextfree grammar.
For X,Y
∈
V
G
, the relations O
≈
are
defined on the vocabulary V
G
as follows:
(a) X
≈
Y if and only if
5
(A
→
uXYv
∈
P), where u,v are any strings.
(b) X OY
This is the end of the preview. Sign up
to
access the rest of the document.
This note was uploaded on 06/09/2011 for the course COT 4210 taught by Professor Staff during the Spring '08 term at University of Central Florida.
 Spring '08
 Staff
 Algorithms

Click to edit the document details