14-03-2021 Lecture Notes Operator Precedence Parser
14-03-2021 Lecture Notes Operator Precedence Parser
Operator Grammar:
Example:
E EAE | id
A + | *
Example:
SSAS | a
A bSb |b
Implementation Example:
E E+E | E*E | id
Find all terminals from the operator grammar and build operator relational table.
Id + * $
$ Id + id * id $ Shift id
$ id + id * id $ Reduce E id
$E + id * id $ Shift +
$E+ Id * id $ Shift id
$ E + id *id $ Reduce E id
$E+E* Id $ Shift id
$ E + E * id $ Reduce E id
$E $ Accept
When top of stack is $ and Input pointer is also on $ then we can conclude the string is parsed
successfully.
Example no 2:
E E+T | T
T T*V | V
V a| b | c | d
+ * A B C D $
$ a+b*c*d$ Shift a
$V +b*c*d$ Shift +
$V+V *c * d $ Shift *
$V+V*V *d $ Reduce T V
$V+V*E *d $