Anda di halaman 1dari 5

# Operator precedence grammar

## Any grammar G is called an Operator precedence grammar if it meets the following

two conditions-
o There exists no production rule which contains ε (epsilon) on its right hand
side.
o There exists no production rule which contains two non-terminals adjacent to
each other on its right hand side.
o It represents a class of grammar that is small but important because of its
Operator precedence parser-
A parser that reads and understands an operator precedence grammar is called as
Operator precedence parser.
Example of a grammar which is not an Operator precedence grammar-
E → EAE | (E) | -E | id
A→+|–|x|/|^
Example of a grammar which is an Operator precedence grammar-
E → E + E | E – E | E x E | E / E | E ^ E | (E) | -E | id
Designing an Operator precedence parser-
In operator precedence parsing, firstly precedence relations are defined between every pair of
terminal symbols and then operator precedence table is constructed.

##  If precedence of b is higher than precedence of a, then we define a < b

 If precedence of b is same as precedence of a, then we define a = b
 If precedence of b is lower than precedence of a, then we define a > b

## Rules to determine precedence relations-

The precedence relation between terminal symbols is determined by making use of
the traditional ideas of associative and operators’ precedence.
Remember these rules-
 Any identifier id will always be given higher precedence than the precedence of any
other symbol.
 Symbol \$ will always be given the lowest precedence.
 If there exists two operators having the same precedence, then in that case, we go by
checking the associativity rule for that operator.
Steps for parsing any given string-
Step-01:
Insert-
 \$ symbol at the beginning and at the ending of the input string.
 Precedence operator in the middle of each symbol of the string by referring the
designed precedence table.
Step-02:
 Start scanning the string from the left hand side until you find the > and keep a pointer
on its location.
 Now, start scanning the string in the backward direction until you find the <.
 Everything that lies in the middle of the two relations < and > forms the handle.
Replace handle with the head of the respective production.
 Keep repeating this step until you reach the start symbol.
 The implementation of this type of parsing is extremely easy and simple.
 This parser is quite powerful for expressions in programming languages.
The handling of tokens like minus sign which is known to have two different
precedence becomes difficult as it depends on if it is a unary minus operator or binary
minus operator.
 Only small class of grammars can be parsed using operator precedence parser.
Operator precedence functions-
 In practice, precedence table with the relations is not stored by the operator
precedence parsers because of the large space that it occupies.
 Instead, operator precedence parsers are implemented in a very unique style. They are
implemented using operator precedence functions.
 Precedence functions perform the mapping of terminal symbols to the integers. Then,
numerical comparison is performed for deciding the precedence relations between the
symbols. This reduces the space complexity to a large extent.

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 \$

## Parse the given string-

Step-01:
Given string to be parsed is:
id + id * id
Now, inserting \$ at both the ends of the string, we get-
\$ id + id * id \$
Now, inserting precedence operators in between the string symbols, we get-
\$ < id > + < id > * < id > \$
Step-02: Scanning and Parsing-
\$ < id > + < id > * < id > \$
\$ E + < id > *< id > \$
\$ E + E * < id > \$
\$ E + E *E \$
\$+*\$
\$<+<*>\$
\$<+>\$
\$\$
Problem-02:
Consider the following grammar and construct the operator precedence parser-
S→(L)|a
L→L,S|S
Then parse the following string: (a , ( a , a ) )
Solution
The terminal symbols in the given grammar are-
{(,),a,,}
Now, we build the operator precedence table for these operators-
Operator Precedence Table

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 + * \$

## * < > > >

\$ < < <
Operator precedence table

## Here, the longest paths are-

 fid → gx → f+ → g+ → f\$
 gid → fx → gx → f+ → g+ → f\$

+ * id \$

f 2 4 4 0

g 1 3 5 0