Operator Precedence Parsing in Compiler Design
Operator Precedence Parsing in Compiler Design
RELATION MEANING
1. If operator θ1 has higher precedence than operator θ2, then make θ1. > θ2 and θ2
< . θ1
2. If operators θ1 and θ2, are of equal precedence, then make θ1. > θ2 and θ2. > θ1
if operators are left associative θ1 < . θ2 and θ2 < . θ1 if right associative
3. Make the following for all operators θ:
θ < . id , id . > θ
θ<.(,(<.θ
).>θ,θ.>)
θ.>$,$<.θ
4.Also make
(=) , (<.( ,).>) , ( < . id , id . > ) , $ < . id , id . > $ ,
$<.( , ).>$
These rules ensure that both id and (E) will be reduced to E. Also, $ serves as both
the left and right end marker, causing handles to be found between $’s wherever
possible
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.
Example:
Operator-precedence relations for the grammar
E → E+E | E-E | E*E | E/E | E↑E | (E) | -E | id , is given in the following table
assuming
1. ^ is of highest precedence and right-associative
2. * and / are of next higher precedence and left-associative, and
3. + and - are of lowest precedence and left-associative
Note that the X in the table denote error entries
+ - * / ^ ( ) id $
+ > > <<<<> <>
- > > <<<<> <>
* > > > > <<> <>
/ > > > > <<> <>
^ > > > > <<> <>
( <<<<<<= <
) > > > > > > >
id > > > > > > >
$ <<<<<<<
Operator-precedence parsing algorithm:
Input: an input string w & table of precedence relations (holds precedence relations
between certain terminals).
Output: if w is well formed, a skeletal parse tree, with a placeholder non-terminal
E labeling all interior nodes; otherwise, an error indication.
Method: initially the stack contains $ and the input buffer the string w$.to parse,
we execute the following program:
Algorithm:
set p to point to the first symbol of w$ ;
repeat forever
if ( $ is on top of the stack and p points to $ ) then return
else {
let a be the topmost terminal symbol on the stack and let b be the symbol
pointed to by p;
.
if ( a < b or a =· b ) then { /* SHIFT */
push b onto the stack;
advance p to the next input symbol;
}
.
else if ( a > b ) then /* REDUCE */
repeat pop stack
.
until ( the top of stack terminal is related by < to the terminal most
recently popped );
else error();
}
Operator precedence parsing uses a stack and precedence relation table for its
implementation of above algorithm. It is a shift-reduce parsing containing all four
actions shift, reduce, accept and error (like shift-reduce technique but in the other
manner).
The initial configuration of an operator precedence parsing is
Stack Input
$ W$
Where W is the input string to be parsed
The precedence and associativity of the rule on the top of stack, and the current
tokenare used to determine whether to shift or reduce. This is done as follow:
When the relation between the top of stack and the leftmost of input word is .> this
is mean perform reduce action, otherwise (when the relation < or =) the action is
Shift .example 1 explain how use the Operator precedence for parse the an
expression
Ex: - 1
Use Stack implementation of operator precedence parser to check this sentence
id + id by this grammar: E→ E+E | E*E | id
Sol:
Ex2: Consider the following grammar
O - | +| *|/|
Sol:
E E+E | E-E | E*E | E/E | E^E | (E) | -E | id