6.7. Transforming A CFG Into A Precedence Grammar: E, E' T, T '
6.7. Transforming A CFG Into A Precedence Grammar: E, E' T, T '
( { , +, *, (, ), }
, {A, E, T, P} , , A )
A E
E T
E E+T
T P
T T*P
P
P (E)
First (x)
, (
T, P, , (
E, T, P, , (
Last (x)
, )
P, , )
T, P, , )
E
T
P
)
*
+
= ,<
( =, <
<
<
=, <
E,E
=
<
<
<
<
<
<
<
=
>
>
>
= =
> >
> >
> >
> >
=
>
>
>
>
<
<
<
<
T,T '
More than one precedence relation holds between some pairs of symbols.
Therefore, the grammar G is not precedence grammar.
56
a.
From
E E+T
, N {T} , {T T} , S )
we get + = T
+ =T
and + < T
57
58
59
a > xi+1
Example: G = ( {+,*, ( , ) ,, } , { E, E, T, P} , , E )
Where consists of:
N
P
T
E
E
E
T
P
LEADING (N)
, (
L(P), *, , (
+, L(T), *, , (
= :
< :
E
T
P
E E+T
T T* P
P (E)
TRAILING (N)
, )
T(P), *, , )
+, T(T), *, , )
( = )
=
+
*
(
<
<
<
<
> : T(E)
T(E)
T(T)
T(E)
<
>
>
>
<
<
<
<
<
<
<
<
<
<
L(E)
L(T)
L(P)
L(E)
>
>
>
>
+
*
)
+
*
)
(
>
>
>
>
<
<
>
>
>
>
>
>
>
>
S0 S1 Si tj tj+1 tm
At any step, it is necessary to be able to tell solely from the symbols Si-1, Si,
and tj whether:
1) Si is the tail symbol of the leftmost prime phrase in the stack
2) Si is not the tail and tj must be pushed into the stack
In an operator precedence language ,the operator precedence relations may be
used for detecting the sub-string which may be reduced ( the prime phrase)
60
Start
initialize
Action
Accept
stop
Shift
Fail
Reduce
stop
Sentential Form
Stack
Initially
Partially reduced sentence
Finally
S1..Si
U
62
Input Buffer
a1 a2 .
sentence
ak ak+1.an
start
a1
ak
i 0
Si
Input
Tape
Si
k0
Push-down
Automaton
S0
Stack
k k+1
R ak
Output
Tape
ji
Accept
position of terminal
on top of stack
i if top is terminal
i1 if nonterminal
Sj = &
R=
Yes
No
entry Si & R
is empty
Yes
Fail
No
Yes
(Reduce)
Sj > R
No
(Shift)
lj
i i+1
Si R
Q Sl
l l1
Prime phrase
is
Sl+1 Si
Sl is
terminal
No
k k+1
R ak
Get new
token to
LA
Yes
No
l l1
Si < Q
Yes
push
Si U
j i1
63
7.5. Example:
Parsing of the sentence, v + v * (v + v) , using the grammar shown in 7.2.
Stack
Sj , R
>
Input
v + v * (v + v)
+ v * (v + v)
<
<
+ v * (v + v)
v * (v + v)
<
<
U
U +
<
U + v
<
* (v + v)
<
* (v + v)
<
(v + v)
<
v + v)
U + U *
<
<
U + U * (
<
>
<
U + U
<
<
<
U + U * ( v
<
<
U + U * ( U
<
<
<
<
<
<
U + U * ( U + v
<
<
<
<
<
<
<
<
<
<
<
U + U * ( U )
<
<
<
U + U * U
<
<
+ v)
<
v)
<
>
>
>
>
>
<
U + U
<
U + U * ( U
+ v)
U + U * ( U + U
>
U + U * ( U +
<
< <
64
Action
Shift
Reduce
Shift
Shift
Reduce
Shift
Shift
Shift
Reduce
Shift
Shift
Reduce
Reduce
Shift
Reduce
Reduce
Reduce
Accept
In most cases, the table of precedence relations can be encoded by the two
precedence functions.
7.6.1. Computation of the Linear Precedence Functions
Input: An n n matrix M whose entries are < , = , > and blank
Output: two vectors of integers f = ( f1, .. ,fn ) and g = ( g1, ,gn ), such that :
fi < gj
if Mij = <
fi = gj if Mij = =
fi > gj if Mij =
>
2)
3)
if Mij = >
if Mij = <
4)
5)
65
Example:
1
1
2
3
4
5
<
<
=
>
>
>
<
<
5
<
<
G1
G2
>
>
1
0
3
2
1
2
3
2
0
F3
G4
F4
Precedence Matrix
i
fi
gi
F5
3
4
1
2
F1
G3
5
3
1
F2
G5
1
Linearization Graph
Precedence Functions
7.6.2. Alternative Method:
1) for all x in
F (x)
G (x)
66
Operator Precedence
Languages
67
68