Operator Precedence Grammar
Operator Precedence Grammar
Problem-01:
Consider the following grammar and construct the operator precedence parser-
E → EAE | id
A→+|x
Then parse the following string: id + id x id
Solution-
Step-01: Convert the given grammar to operator precedence grammar-
The equivalent operator precedence grammar is-
E → E + E | E x E | id
Step-02: Construct the operator precedence table-
The terminal symbols are-
{ id, + , x , $ }
Operator Precedence Table-
id + x $
a ( ) , $
Parse the given string-
a > > > >
Step-01:
( < > > > >
Given string to be parsed is:
) < > > > > (a,(a,a))
, < < > > > Now, Inserting $ at both the ends of the string, we
$ < < < < get-
$(a,(a,a))$
Now, inserting precedence operators in between the string, we get-
$<(<a>,<(<a>,<a>)>)>$
Step-02: Scanning and Parsing-
$<(<a>,<(<a>,<a>)>)>$
$<(S,<(<a>,<a>)>)>$
$<(S,<(S,<a>)>)>$
$<(S,<(S,S)>)>$
$<(S,<(L,S)>)>$
$<(S,<(L)>)>$
$<(S,S)>$
$ < ( L, S ) > $
$ < ( L) > $
$<S>$
$$
Problem-03:
Consider the following grammar-
E → E + E | E * E | id
1. Construct Operator Precedence Parser
2. Find the Operator Precedence Functions
Solution-
The terminal symbols in the given grammar are-
{ + , * , id , $ }
g→
f↓ id + * $
+ * id $
f 2 4 4 0
g 1 3 5 0