Professional Documents
Culture Documents
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
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.
< Vi >
where Vi
Vt,
Algorithm:
1. Push
onto stack
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
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.
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 |
|
---------------------
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
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