0% found this document useful (0 votes)
34 views

6.7. Transforming A CFG Into A Precedence Grammar: E, E' T, T '

The document describes operator-precedence parsing. It defines operator grammars and operator-precedence grammars. For an operator-precedence grammar, there exist at most one of the operator precedence relations <, =, or > between each pair of terminal symbols. These relations guide the parsing process by determining which part of the sentential form should be reduced at each step. The document outlines the calculation of operator-precedence relations from a context-free grammar and describes how these relations are used during parsing to select the leftmost prime phrase for reduction at each step. An example of parsing a sentence using an operator-precedence grammar is provided.

Uploaded by

Ahmed Salem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
34 views

6.7. Transforming A CFG Into A Precedence Grammar: E, E' T, T '

The document describes operator-precedence parsing. It defines operator grammars and operator-precedence grammars. For an operator-precedence grammar, there exist at most one of the operator precedence relations <, =, or > between each pair of terminal symbols. These relations guide the parsing process by determining which part of the sentential form should be reduced at each step. The document outlines the calculation of operator-precedence relations from a context-free grammar and describes how these relations are used during parsing to select the leftmost prime phrase for reduction at each step. An example of parsing a sentence using an operator-precedence grammar is provided.

Uploaded by

Ahmed Salem
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

6.7.

Transforming a CFG into a Precedence Grammar

Consider the grammar G shown below


G=

( { , +, *, (, ), }

, {A, E, T, P} , , A )

Where is defined by:


1:
2:
3:
4:
5:
6:
7:

A E
E T
E E+T
T P
T T*P
P
P (E)

The sets First and Last are:


x
P
T
E

First (x)

, (
T, P, , (
E, T, P, , (

Last (x)
, )
P, , )
T, P, , )

The precedence matrix is:


E

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

The grammar G can be transformed into a precedence grammar through the


insertion of non-terminal symbols:

a.

From the rule 3 : E E + T ,

From

the two rules 3 and 5 :

E E+T

and T T * P , we get + < T

To resolve this conflict , we add to the set of non-terminals N a new symbol


T N, replace T in 5 by T and then add another production T T to
the set of productions. That is, G becomes G where,
G =

, N {T} , {T T} , S )

in this equivalent grammar, we have


b.

we get + = T

+ =T

and + < T

Similarly, the conflict, ( = E and ( < E can be resolved by


introducing a new non-terminal E N and replacing 7 by P ( E )
, and then adding to the rule E E.

Also, the conflict, = E and < E can be resolved by replacing


1 : A E by the rule A E ( No need to add new nonterminal )

The grammar G is then transformed to the equivalent grammar shown in section


6.5. which gives the same language generated by G, but is simple precedence
grammar.

57

7. Operator Precedence Parsing


7.1. Definition:
7.1.1. Operator Grammars:
An operator grammar is a CFG with the property that:
No production right side is empty
No production right side with two adjacent non-terminals
7.1.2. Operator Precedence Grammar:
An operator -precedence grammar is an operator grammar in which there
exist at most one of the operator precedence relations < , > and =
Those relations guide the selection of part of a sentential form to be reduced
at each parse step.
7.1.3. Operator-precedence Relations:
Let G = ( , N, , S ) be an operator grammar. For each terminal symbols
t1, t2 in we say that:
1) t1 = t2 , t1 has same precedence as t2 , iff there is a production
A t1 t2
or
A t1 B t2 , for some B in N
2) t1 < t2, t1 yields precedence to t2 , iff, there is a production
A t1 B
and a deviation
+
B t2 or B + C t2
for some B, C in N and in ( N )*
3) t1 > t2 , t1 takes precedence over t2 ,iff, there is a production
A B t2
and a deviation
+
B t1
or
B + t1 C
for some B, C in N and in ( N )*
The operator grammar G is an operator precedence grammar, iff for any pair
of terminals t1 and t2 never more than one of the relations: t1 < t2 , t1 = t2
and t1 > t2 is true ( i.e. the relations are disjoint )
7.1.4. Prime phrase:
A prime phrase is a simple phrase that contains at least one terminal symbol

58

Equivalently, is a prime phrase of at least one sentential form , iff,


contains at least one terminal and either there exists a production U , or
a production U where * and the only productions applied in
the deviation are of the form UI Uj , i.e., there is no branching
7.2. Calculation of Operator-Precedence Relations
To construct the matrix of all precedence relations:
Step1:
Construct two sets LEADING (A) and TRAILING (A) for each non-terminal A,
defined by:
LEADING (A) = { a | A + a , where is or a single non-terminal }
TRAILING (A) = { a | A + a , where is or a single non-terminal }
Step2:
Execute program shown below, examining each position of the right side of
each production
Step3: {augmented grammar }
Set < a , a in LEADING (S)
Set a > , a in TRAILING (S) where S is the start symbol of G
Set =
for each production A x1 x2 xn do
for i := 1 to n 1 do
begin
If xi and xi+1 are both terminals
then set xi = xi+1 ;
If i n - 2 and xi , xi+2 are terminals
and xi+1 is non-terminal then set xI = xi+2;
If xi is terminal and xi+1 is a non-terminal
then for all a in LEADING (xi+1) do set xi < a;
If xi is non-terminal and xi+1 is terminal
then for all a in TRAILING (xi) do set
end.
Calculation of operator-precedence relations

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)

>
>
>
>

+
*
)

+
*

)
(

>
>
>
>
<
<

>
>
>
>

>
>
>
>

7.3. Using operator-precedence relations in parsing :


The parser reduces at each step the leftmost prime phrase.
During the parse of a sentence t1 .. tm , a stack will contain symbols
S0 S1 .. , Si

of the partially reduced string

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

Suppose t0 t is a sub-string of a sentential form = t0 t , and


that the terminal symbols in the sub-string are in the order t1, t2, tn
(with n 1 ). Now suppose the following relations hold between the terminals
t0, t1, , tn and t are :
t0 < t1 = t2 = = tn > t
(Note that non-terminals of play no role here)
Then is a prime phrase. Furthermore, the reduction of to some U may
be executed to yield the sentential form t0 U t
Parsing of a sentence is as follows:
a. Symbols are pushed into the stack until the relation tn > t holds between
the top terminal stack symbol tn and the next incoming symbol t. If the
string is indeed a sentence of the language, the top stack elements then
form a string t0 as described above.
b. One searches back in the stack using the relations to find t0 , at the
beginning of .
c. The sub-string ( i.e., everything to the right of t0 and to the left of t
including any intervening or surrounding non-terminals in the sentential
form t0 t ) is then a prime phrase and can be reduced to some U
yielding t0 U in the stack. Since the non-terminals do not influence the
parse, we need not worry about distinguishing among them.
d. the process is then repeated by comparing t0 with t
7.4. Operator-Precedence Parsing Technique
In this technique, the leftmost prime phrase is reduced at each parse step
The passer is a push down automaton working with one stack
The actions to be carried by the parser is determined by examining the top
terminal stack symbol ( Si, or Si-1 if Si is non terminal ) and the incoming
symbol ( R )
There are four types of actions carried out by the parser:
1)
2)
3)
4)

Shift: push next input symbol R onto stack


Reduce: pop prime phrase from stack. push a dummy non terminal
onto stack
Accept: successful parse
Fail: unsuccessful parse
61

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

Replace prime phrase by U


i l +1

push

Si U

Call semantic routine


ji

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

7.6. Operator-precedence functions:


The relations > , = , and < can be kept in an n n matrix, where n is the
number of terminal symbols of the grammar.
The space needed for the relations may be reduced to two vectors of length n, if
two integer precedence functions f (t) and g (t) can be found such that
t1 < t2
t1 = t2
t1 > t2

implies f (t1) < g (t2)


implies f (t1) = g (t2)
implies f (t1) > g (t2)

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 =

>

OR the output No if no such vectors exist


Method:
1)

Construct a directed graph with 2 n nodes labeled


F1, F2, , Fn , G1, G2, , Gn

2)

if Mij = = , create a new node N by merging Fi and Gj. Node N now


represents all those nodes previously represented by Fi and Gj

3)

if Mij = >
if Mij = <

4)

if the resulting graph is cyclic, answer No

5)

if the linearization graph is acyclic, let fi be length of the longest path


beginning at Fi and let gi be the length of the longest path beginning at Gi

, draw an edge from Fi to Gj


, draw an edge from Gj to Fi

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

set f (x) = 1 , g (x) = 1

2) for all x > y


if f (x) g (y) , set f (x) = g (y) + 1
3) for all x < y
if f (x) g (y) , set g (y) = f (x) + 1
4) for all x = y ,
if f (x) g (y) , set the smaller one from f (x) and g (y) to the value of the
greater one
5) iterate steps 2, 3, 4 till the process converts , or till f (x) or g (x) exceeds 2n,
in which case the algorithm fails ( No solution exists )
for the grammar in 7.2., the linear functions F and G are:
x

F (x)

G (x)

66

7.6.3. Precedence-Relations vs Precedence functions


1) Storage of the precedence relations requires a matrix of size n n, where n is
the number of terminals in the grammar ,while storage of the precedence
functions requires two vectors of size n
2) When using the precedence functions, error entries in the precedence matrix
are obscured since one of the three relations < , = , > holds no matter what
the pair terminals a and b are. Thus, another method should be devised to
detect illegal input strings
3) Not every table of precedence relations has precedence functions to encode it
( but in practical cases the functions usually exist )
7.7. Operator-Precedence vs Simple Precedence
1. Speed
Operator precedence parsing technique is more efficient than the simple
precedence technique :
a. less parsing steps: operator precedence parser builds unlabelled tree
showing branching only, all productions of the form U1 U2 used in the
derivation of the sentence will not appear
b. no need to scan the whole set of values at each step, since the prime phrase
is reduced to unlabelled non-terminal. (non terminals play no role in
operator-precedence parsing technique)
2. memory requirement:
Operator precedence technique requires less space to store the precedence relation
(the size of precedence relation matrix is n n, where n is cardinality of in
operator precedence technique, and cardinality of N in simple precedence
technique
3. Class of language processed:
Simple precedence technique is a suitable for processing of much wider class of
languages than operator precedence technique
Context-free Language ( CFL )
Deterministic CFL ( LR )
Simple Precedence Language
LL

Operator Precedence
Languages

67

4. Ease of Embedding Semantic Processing


Embedding semantic processing into parsing process is easier and much clear in
simple precedence than in operator precedence technique :
In simple precedence parsing there is a separate semantic routine to process
each different handle (i.e., each rule of syntax defining a non-terminal). Thus,
the semantic routine defines the action specified by the rule of semantics
associated with the used rule of syntax in each parse step.
In operator precedence grammar, the structure of the tree is unambiguous, but
the nodes of a tree may not be unambiguous; a prime phrase may be reduced to
more than one nonterminal, since there is no restriction that the right part of
productions be unique. The semantic routine for processing the prime phrase
(A separate semantic routine is written to process each different prime phrase)
must resolve such ambiguity
5. Necessity to manipulate the grammar before processing
One must manipulate a grammar for an average programming language
considerably before it is simple precedence grammar ( it may be necessary to
insert intermediate productions or even to use a different symbols for a terminal
character depending on its context). The final grammar could not be presented to
a programmer as a reference to the language.

68

You might also like