Download as pdf or txt
Download as pdf or txt
You are on page 1of 9

Operator-Precedence Parser

Introduction:
Operator precedence grammar is kinds of shift reduce parsing method. It is
applied to a small class of operator grammars.

A grammar is said to be operator precedence grammar if it has two

properties:

1. Production rule should not have Ɛ on its RHS (body of the production).
2. There is no two adjacent non-terminals on RHS of production.
Precedence Relations:

• In operator-precedence parsing, we define three disjoint precedence


relations between certain pairs of terminals.
a <. b b has higher precedence than a
a =· b b has same precedence as a
a .> b b has lower precedence than a
• The determination of correct precedence relations between terminals are
based on the traditional notions of associativity and precedence of
operators. (Unary minus causes a problem).

Operator Precedence Grammar:


An operator precedence grammar is an perator grammar having disjoint

precedence relation <. , =. , .> between any pair of terminals.

PROBLEM : 1

Construct Operator precedence table and function graph for the given
Grammar:

STEP TO SOLVE:

(1) Compute LEADNING AND TRAILING

(2) Computation of Precedence relation

(3) Construct operator precedence table


(4) Parse the Input String

(5) Draw Precedence Functional graph

STEP 1:
Computation of LEADING ( ) and TRAILING ( )
(1) Computation of LEADING

The LEADING tells about the first encounter terminals

(i) ‘a’ is a in LEADING (A) if


LEADING(A)={a}
• A→aY, here Y is a grammar symbol (or)
• A→XaY, here X is a single non-terminal
(ii) If there is a production
• A→BX and ‘a’ is in LEADING(B) then ‘a’ will be in
LEADING (A)
(2 ) Computaion of TRAILING

The TRAILING tells about the last encounter terminals

TRAILING(A)={a}

(i) ‘a’ is in TRAILING (A) if


• A→Xa, here X is a grammar symbol (or)
• A→XaY, here X is a single non-terminal
(ii) If there is a production
• A→XB
TRAILING(B) will be in TRAILING(A) where X is a
grammar symbol
STEP 2 : Computation of Precedence Relations:

5 Rules:

I. S is a start symbol, then


$<. LEADNIG (S)
TRAILING (S) .> $
II. If A→XY

If X, Y are terminals then set

X=.Y

III. If A→XBY
X,Y - Terminals
B-Non Terminals
Set X=.Y

IV if A→XB
X – Terminal
X<. LEADING (B)

V if A -> BX
X→ Terminal
Trailing (B).> X

NOTE:

<. Terminal followed by non terminal

.> Non terminal followed by terminal

=. Terminal followed by NT and followed by terminal


EXAMPLE 1
Construct Operator precedence table for the given Grammar.
And parse the input string id+id*id
E→E+T
E→T
T→T*F
T→F
F→ ( E )

F →id

Step 1: computation of LEADING & TRAILING

LEADING (E ) ={+, L(T)}={+, *,(,id}

LEADING(T)={*, L(F)}={*,(,id}

LEADING(F)={(,id}

TRAILING(E) ={+,T(T}}={+,*,),id}

TRAILING(T)={*,T(F)}={*,),id}

TRAILING(F)={ ), id}
Step 2: COMPUTE Precedence relations:

For Starting symbol NT then $ E $

$ <.LEADING (E) TRAILING(E) .>$


$<.+ +.>$
$<.* *.>$
$<.( ).>$
$<.id Id.>$
According to rule iii A-> XBY

F→( E)
(=.)
According to rule iv A→XB; X<.LEADING (B)

E→E+T T→T*F F→ (E)


+<.LEADING (T) *<.LEADING(F) (<. LEADING ( E )
+<.* *<.( (<.+
+<.( *<.id (<.*
+<.id (<.(
(<.id
According to rule v A→BX TRAILING (B).> X

E→E+T T→T*F F→ (E)


TRAILING (E) .>+ TRAILING (T).>* TRAILING (E) .>)
+.>+ *.>* +.>)
*.>+ ).>* *.>)
).>+ Id.>* ).>)
Id.>+ Id.>)
Step: 3 Operator Precedence Relation Table

Step 4: Operator-Precedence Parsing the input string -Algorithm


STEP 5: Precedence Function

Step 6: Precedence function table:

+ * id $
f 2 4 4 0
g 1 3 5 0

You might also like