You are on page 1of 23

BOTTOM UP PARSER ( SR –PARSER)

LR(0), SLR, CLR, LALR


Unambiguous
Parser ( Syntax Analyzer )
grammar

Top-Down Bottom-UP (SR)

Non left recursive


ambiguous
Non deterministic
Without Operator
With Backtrck LR Precedence
Backtrack
Recursive Non Recursive
Descent Descent –Predictive SLR LALR CLR
Parsing (LL)
What When to Reduce
Alternative to
use
BOTTOM UP PARSER
• Shift-Reduce Parser (SR – Parser)
• Construct a parse tree from leaves to the root
• Implements rightmost derivations in reverse
order
OPERATOR GRAMMAR
• No Ɛ-transition.
• No two adjacent non-terminals.
Eg.
E  E op E | id
op  + | *
The above grammar is not an operator
grammar but:
E  E + E | E* E | id
• Simple to implement and can take ambiguous
grammar
• Only small class of grammars can be parsed.
• The operators like ‘-’ (minus) or ‘+’ (plus) are
difficult to handle.
• Mainly suitable for the grammar for which the
precedence of the terminals can be clearly
specified.
• Three disjoint relations <, > , = between pair
of terminals
Relation Meaning
a<b a yields precedence to b
a=b a has the same precedence as b
a>b a takes the precedence over b
OPERATOR PRECEDENCE

• If a has higher precedence over b; a .> b


• If a has lower precedence over b; a <. b
• If a and b have equal precedence; a =. b
Note:
– id has higher precedence than any other symbol
– $ has lowest precedence.
– if two operators have equal precedence, then we
check the Associativity of that particular operator.
Operator precedence relation
id + * s
id > > >
+ < > < >
* < > > >
$ < < <

a+b+c
a+b*c
a*b+c
a*b*c
Example: w= $id + id * id$
$<.id.>+<.id.>*<.id.>$

Handle: is a substring that matches the


right side of some productions and whose
reduction to the nonterminal on the left
side of the production represents one step
along the reverse of a rightmost
derivations
BASIC PRINCIPLE
• Scan input string left to right, try to detect .>
and put a pointer on its location.
• Now scan backwards till reaching <.
• String between <. And .> is our handle.
• Replace handle by the head of the respective
production.
• REPEAT until reaching start symbol.
ALGORITHM
w  input
a  input symbol
b  stack top
Repeat
{
if(a is $ and b is $)
return
if(a .> b)
push a into stack
move input pointer
else if(a <. b)
c  pop stack
until(c .> b)
else
error()
}
EXAMPLE
STACK INPUT ACTION/REMARK
$ id + id * id$ $ <. Id
$ id + id * id$ id >. +
$ + id * id$ $ <. +
$+ id * id$ + <. Id
$ + id * id$ id .> *
$+ * id$ + <. *
$+* id$ * <. Id
$ + * id $ id .> $
$+* $ * .> $
$+ $ + .> $
$ $ accept
EXAMPLE
STACK INPUT ACTION/REMARK
$ id + id * id$ $ <. Id
$ id + id * id$ id >. +
$E + id * id$ $ <. +
$E + id * id$ + <. Id
$ E+ id * id$ id .> *
$ E+ E * id$ + <. *
$ E+E * id$ * <. Id
$ E+ E* id $ id .> $
$ E+E *E $ * .> $
$E +E $ + .> $
$ $ accept
Operator precedence relation from
Associativity and Precedence
+ - * / id ( ) $
+
-
*
/

id
(
)
$
PRECEDENCE FUNCTIONS
• Operator precedence parsers use precedence functions that map terminal
symbols to integers.
Algorithm for Constructing Precedence Functions
1. Create functions fa and ga for each grammar terminal a and for the end
of string symbol.
2. Partition the symbols in groups so that fa and gb are in the same group if
a =· b (there can be symbols in the same group even if they are not
connected by this relation e.g if a=.b and a=.c then fa, gb, gc are in the
same group even though no relation exists between b and c ).
3. Create a directed graph whose nodes are in the groups, next for each
symbols a and b do: place an edge from the group of gb to the group of fa
if a <· b, otherwise if a ·> b place an edge from the group of fa to that of
gb.
4. If the constructed graph has a cycle then no precedence functions exist.
When there are no cycles collect the length of the longest paths from
the groups of fa and gb respectively.
• Terminals be = { a,b, c,$ }
• Groups to be created
– {fa, fb, fc, f$ , ga, gb, gc, g$ }
• If a=.b  then fa and gb are in the same
group. That is we can represent it as

fa gb
Consider the following table:
g

f

Resulting graph: gid fid

f* g*

g+ f+

f$ g$
• From the previous graph we extract the
following precedence functions:
id + * $
f
g
• From the previous graph we extract the
following precedence functions:
id + * $
f 4 2 4 0
g 5 1 3 0

You might also like