Sunteți pe pagina 1din 4

Operator Precedence Parsing

One big difference between simple precedence and operator precedence is that in simple precedence
parsing, the non-terminal symbols matter. In operator precedence parsing all non-terminal symbols are
treated as one generic non-terminal, N. Thus, a grammar such as
1.
2.
3.
4.
5.
6.

E
T
P

E + T
| T
T * P
| P
( E )
| V

will look like, in operator precedence terms, the following:


1.
2.
3.
4.
5.
6.

N
N
N

N + N
| N
N * N
| N
( N )
| V

Note: We no longer have specific non-terminal symbols, but one generic non-terminal, N.

Operator Precedence Parsing Algorithm


Input: an operator precedence matrix or precedence functions
modified grammar
sentence with appended to the end
Assumptions:

< Vi >

where Vi

Vt,

Algorithm:
1. Push

onto stack

2. Read first input symbol and push it onto stack


3. Do
3.1 Obtain OP relation between the top terminal symbol on the stack and the next input symbol
3.2 If the OP relation is <o or =o
Then
3.2.1 Stack input symbol.
Else { relation is >o }

3.2.2 Pop top of the stack into handle, include non-terminal symbol if appropriate
3.2.3 Obtain the relation between the top terminal symbol on the stack and the leftmost
terminal symbol in the handle
3.2.4 While the OP relation between terminal symbols is =o Do
3.2.4.1 Pop top terminal symbol and associated non-terminal symbol on stack into
handle
3.2.4.2 Obtain the OP relation between the top terminal symbol on the stack and
the leftmost terminal symbol in the handle
3.2.5 Match the handle against the RHS of all productions
3.2.6 Push N onto the stack
4. Until end-of-file and only

and N are on the stack

Note: The algorithm above does not detect any syntax errors which we will talk about later.

Def: A operator precedence parse configuration triple is (S, , ) where S is the stack,
and is the action sequence.

is the input string,

Initially, we have ( , , ) where represents the empty stack and is the input string. We terminate
when we have (N, , ) where N is the generic non-terminal symbol, and is the action sequence. Using
the second grammar above whose operator precedence matrix is given below, we construct the operator
precedence parse configuration sequence for the input string
V*(V+V)

V*(V+V)

)
1.
2.
3.
4.
5.
6.

N
N
N

+
*
(
)
V

N + N
| N
N * N
| N
( N )
| V
+
*
(
)
V
--------------------| G | L | L | G | L |
--------------------| G | G | L | G | L |
--------------------| L | L | L | E | L |
--------------------| G | G |
| G |
|
--------------------| G | G |
| G |
|
---------------------

Click here for the simple precedence parse sequence.

Simple Precedence Parsing Errors


In all of our examples above we have only had sentences in the language of the operator precedence
grammar to parse. This situation will not always be the case. There are two types of operator precedence
parsing errors, namely character pair errors and reducibility errors. We will examine each of these errors
next.

Def: A character pair error occurs when there is no operator precedence relation between pairs of symbols
in the grammar.

A character pair error is identified when you go the operator precedence matrix, and there is no operator
precedence relation between the two symbols.

Def: A reducibility error occurs when you cannot reduce the handle to the left hand side of some
production.

A reducibility error is identified when you take the potential handle and cannot find a matching right hand
side of a production in the grammar.

Modify the Operator Precedence Parsing Algorithm above to include the detection of the three types of
operator precedence parsing errors.

ANSWERS

The operator precedence parse configuration for the above sentence is


Parse Sequence
(
(
(
(
(
(
(
(
(
(
(
(
(

V*(V+V)
V, * ( V + V )
N, * ( V + V )
N * (, V + V )
N * ( V, + V )
N * ( N, + V )
N * ( N +, V )
N * ( N + V, )
N * ( N, + V )
N * ( N + N, )
N * ( N ),
,
N * N,
, 6
N,
, 4 )

,
,
,
,
,
,
,
,
,
,
- )
)

- )
- )
6 )
- )
- )
6 )
- )
- )
6 )
1 )

Relations

V
(
V
(
+
V
+
(
)

< V
> *
< *
< V
> +
< +
< V
> )
> )
= )
>
=

Action

S
R
S
S
R
S
S
R
R
S
R
R

6
1
5
3

The reduction sequence (reverse rightmost derivation sequence) for the above parse is:
6, 6, 6, 1, 5, 3

Return

S-ar putea să vă placă și