Follow Symbols in Compiler

In this class, We discuss Follow Symbols in Compiler.

For Complete YouTube Video: Click Here

The reader should have prior knowledge of finding the first symbols for grammar. Click Here.

Follow Symbols: The terminal symbols encounter after completing a given non-terminal.

The reader should be more focused on understanding the ‘follow’ symbols.

We should understand the below conditions to find follow symbols.

1) We understand the first condition with an example.

S – aAB

A – b

B – c

Follow(a) means terminal symbols encounter after completion of production A.

The below diagram shows the expansion of CFG.

After completion of A, we had the symbol B. So we need to find the First(B)

Follow(A) = First(B)

Before finding follow symbols, one should find the first symbols.

2) Add $ as follow symbol for the first non-terminal.

Why do we need to add $ as follow symbol?

In our first class on syntax analysis, we discussed a pascal example.

We identified the syntax analysis for a complete program.

The below context-free grammar is used to identify syntax analysis.

Compundstmnt – begin STMT end

The first non-terminal in the CFG is compoundstmt.

The compiler will add a dollar symbol at the end of the program To identify the completion of the first non-terminal.

Once the dollar is identified, the program is finished.

So we need to add the dollar as follow symbol to the first non-terminal.

3) if the production is of the form A – αB

Follow(B) = Follow(A)

Example:

S – ABC

A – a

B – b

C – c | ε

The below diagram shows the expansion of S.

Follow(C) means The terminals that encounter after completion of C.

Here C completed means S has been completed.

So we find Follow(C) symbols the same as Follow(S).

4) If the production is of the form A – αBβ

Follow(B) = {First(β) – ε} U {Follow(A)}

We take an example and understand the equation.

S – ABC

A – a

B – b

C – c | ε

Follow(B) = First(C)

First(c) has epsilon. We can substitute epsilon in C.

After substituting epsilon in C, we can encounter the symbols in Follow(S) from the third condition.