# What are LEADING and TRAILING operation of an operator precedence grammar?

Compiler DesignProgramming LanguagesComputer Programming

#### Web Design for Beginners: Build Websites in HTML & CSS 2022

68 Lectures 8 hours

#### HTML5 & CSS3 Site Design

61 Lectures 6 hours

If production is of form A → aα or A → Ba α where B is Non-terminal, and α can be any string, then the first terminal symbol on R.H.S is

If production is of form A → Bα, if a is in LEADING (B), then a will also be in LEADING (A).

TRAILING

If production is of form  A→ αa or A → αaB where B is Non-terminal, and α can be any string then,

TRAILING (A) = {a}

If production is of form  A → αB. If a is in TRAILING (B), then a will be in TRAILING (A).

Algorithm to compute LEADING

Input − Context Free Grammar G

Output − LEADING (A) = {a} iff Boolean Array L [A, a] = true

Method − Procedure Install (A, a) will make L (A, a) to true if it was not true earlier.

• begin

• For each non-terminal A and terminal a

L [A, a] = false ;

• For each production of form A ⟶ aα or A → B a α

Install (A, a) ;

• While the stack not empty

Pop top pair (B, a) form Stack ;

For each production of form A → B α

Install (A, a);

• end

Procedure Install (A, a)

• begin
• If not L [A, a] then

L [A, a] = true

push (A, a) onto stack.

• end

Algorithm to compute TRAILING

Input − Context Free Grammar G

Output − TRAILING (A) = {a} iff Boolean Array T [A, a] = true

Method

• begin
• For each non-terminal A and terminal a

T [A, a] = false ;

• For each production of form A ⟶ αa or A → α a B

Install (A, a) ;

• While the stack not empty

Pop top pair (B, a) form Stack ;

For each production of form A → αB

Install (A, a);

• end

Procedure Install (A, a)

• begin
• If not T [A, a] then

T [A, a] = true

push (A, a) onto stack.

• end

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+1

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