Coders Packet

First And Follow Using C++

By Jahanvi Solanki

First And Follow in C++ is used so that the parser can apply the production rule easily at the correct position.

First and Follow is a concept that is used in compiler designing. It is used so that the parser can apply the production rule easily at the correct position. It is the concept used in syntax analysis.

The following is the algorithm for the same.

For computing the first:
1. If X is a terminal then FIRST(X) = {X}
Example: F -> (E) | id
We can write it as FIRST(F) -> { ( , id }
2. If X is a non-terminal like E -> T then to get FIRST(E) substitute T with other productions until you get a terminal as the first symbol
3. If X -> ε then add ε to FIRST(X).

For computing the Follow:
1. Always check the right side of the productions for a non-terminal, whose FOLLOW set is being found. ( never see the left side ).
2. (a) If that non-terminal (S, A, B...) is followed by any terminal (a,b...,*,+,(,)...), then add that “terminal” into the FOLLOW sets.
    (b) If that non-terminal is followed by any other non-terminal then add “FIRST of another nonterminal” into the FOLLOW set

The code can be directly used by running it on the C++ compiler.

Download project

Reviews Report

Submitted by Jahanvi Solanki (Jahanvi)

Download packets of source code on Coders Packet