2. Operator
Precedence
Grammar
HARISON FEKADU
Brief
In this presentation I will discuss the third and
fourth steps of Operator Precedence Parsing
which are:
• Parsing the input string based on the
relation table constructed in the previous
step.
• Constructing a parse tree from the parse
table.
Fig. 1 Operator
Precedence Parsing
Steps
3. Operator
Precedence
Grammar
HARISON FEKADU
Parsing the Input String
In this step we’re going to make use of a buffer
to hold the input string, a stack to store tokens
from the input string as we parse them and a
handle as a temporary storage.
The parsing process comprises of the
following steps:
1. Inclose the input string with $.
2. Shift the first item in the buffer to the stack.
3. Compare and identify the OP relation of the
current token in the buffer with the top-
most terminal in the stack. If the relation
doesn’t exist in the precedence table, report
error and end.
4. Perform an action based on the OP relation.
5. Finish if the action performed is accept, else
go to step 3.
4. Operator
Precedence
Grammar
HARISON FEKADU
Operator Precendence Relations
There are three operator precedence relations.
• a<b: a has a lower priority than b.
• a>b: a has a higher priority than b.
• a=b: a and b have the same precedence.
It is important to note that we don’t compare
non-terminals. If a non-terminal exists then we
compare with the closest terminal if it exists.
id + * $
id > > >
+ < > < >
* < > > >
$ < < < Accept
Table 1 A Simple Precedence Table
5. Operator
Precedence
Grammar
HARISON FEKADU
Possible Actions
Assuming the current token in the buffer is a
and the top-most terminal in the stack is b we
can perform the following four actions:
• Shift: If a>b or a=b we push a into the
stack.
• Reduce: If a<b we pop the stack and pre-
pend the value to the handle until a>b. Then
we replace the handle with the appropriate
non-terminal and push this non-terminal to
the stack.
• Accpet: If the value of both a and b is $ we
announce the successful completion of
parsing.
• Error: If there is no relation between a and
b in the precedence table or the parser can’t
reach accept.
6. Operator
Precedence
Grammar
HARISON FEKADU
Example 1
Given the following grammar determine
whether the input string id*id-id is valid or
not using operator precedence parsing:
S → A-A
A → A*A | id
id - * $
id > > >
- < > < >
* < > > >
$ < < < Accept
Table 2 Precedence table for the input string
8. Operator
Precedence
Grammar
HARISON FEKADU
Constructing a Parse Tree
• We will construct the Parse Tree from the
bottom up, meaning that we will start from
the input string and try to arrive at the start
symbol.
• We will do this by focusing on the reduce
actions as shown in the table on the left,
which is a simplified version of the parse
table from Example 1.
Input Action
id*id-id Reduce: A→id
A*id-id Reduce: A→id
A*A-id Reduce: A→A*A
A-id Reduce: A→id
A-A Reduce: S→A-A
S Accept
Table 4 Isolated reduce actions from Table 3
9. Operator
Precedence
Grammar
HARISON FEKADU
Parse Tree for Example 1
id * id - id
A A
A A
S
Fig. 2 Parse tree for Example 1
Input Action
id*id-id Reduce: A→id
A*id-id Reduce: A→id
A*A-id Reduce: A→A*A
A-id Reduce: A→id
A-A Reduce: S→A-A
S Accept
Table 4 Isolated reduce actions from Table 3
10. Operator
Precedence
Grammar
HARISON FEKADU
Example 2
Given the following grammar determine
whether the input string (id+id)/id is valid
or not using operator precedence parsing:
S → SBA|(A)
A → (A+A)|id
B → /
id + / ( ) $
id x > > x > >
+ < > < < > >
/ < > > < > >
( < < < < = x
) x > > x > >
$ < < < < x Accept
Table 5 Precedence table for the input string
Operator Precedence Grammar
S → S/A|(A)
A → A+A|id
12. Operator
Precedence
Grammar
HARISON FEKADU
Parse Tree for Example 2
id + id ) /
A A
A
A
S
(
Fig. 3 Parse tree for Example 2
Input Action
(id+id)/id Reduce: A→id
(A+id)/id Reduce: A→id
(A+A)/id Reduce: A→A+A
(A)/id Reduce: S→(A)
S/id Reduce: A→id
S/A Reduce: S→S/A
S Accept
Table 7 Isolated reduce actions from Table 6
id
S