What are Precedence Relations in Operator Grammar?

Compiler DesignProgramming LanguagesComputer Programming

For terminals a and b in an Operator Grammar we can have the following precedence Relations −

  • a =. b(Equal Precedence) − If R.H.S of production is of form α a β b γ, where β can be ε or single non-terminal then a =. b.

Here, α and γ can be any strings.

Example − In grammar, S → m A c B e d

On Comparing mAcBed with αaβbγ

α = mA, a = c, β = B, b = e, γ = d

ΑAβbγ
mACBed

So, comparing a with c and b with e we get c =.e.

We can also make a different combination for a and b.

In Grammar S → m A c Bed

α = ε, a = m, β = A, b = c, γ = Bed

ΑAβbγ
ΕMAcBed

So, comparing a with m and b with c

∴ m =. c

  • a<.b (Less then)

If R.H.S of production is of form α a A β and A ⟹+ γb$ where γ is ε or single nonterminal then a <.b.

Example − In Grammar  S → m A c D

                                        A → i

On comparing m A c D with α a A β and A → i with A → γb$

ΑAAβ
ΕMAcD
A →Γb$
A →Eiε

∴   α = ε, a = m, A = A, β = cD

∴        γ = ε,

and    b = i

∴  Applying the rule,

a <. b means m <. i

  • a .> b (Greater Than)

If R.H.S of production is of form αAbβ and A ⟹+ γa$ where $ is ε or single nonterminal then a .> b.

Example − In Grammar   S → m A c D

                                          A → i

On comparing m A cD with α a bβ and A → i with A → γa$

ΑAbβ
MAcD


Α →Γa$
Α →Eiε

∴ α = m, A = A, b = c, β = D

On comparing i with γa $

∴ γ = ε, a = i, $ = ε

∴ On applying the rule,

a .> b means i .> c.

The Precedence relations between terminals symbols can also be shown by a Parse Tree −

Algorithm for Computing Operator Precedence Relations

Input − An Operator Grammar

Output − A Precedence Relations between terminals and symbols.

Method

  • begin
  • For each production A → B1, B2, … … … . Bn

                          for i = 1 to n – 1

                  If Bi and Bi+1 are both terminals then

                         set Bi = Bi+1

                 If i ≤ n − 2 and Bi and Bi+2are both terminals and Bi+1 is non-terminal then

                         set Bi = Bi+2

                 If Biis terminal & Bi+1is non-terminal then for all a in LEADING (Bi+1)

                         set Bi <. a

                If Biis non-terminal & Bi+1 is terminal then for all a in TRAILING (Bi)

                          set a . > Bi+1

  • end
raja
Published on 29-Oct-2021 11:26:55
Advertisements