Find First and Follow of a Grammar C Program

By | October 9, 2016

Let us learn how to find first and follow of a grammar in C programming. There are two different C programs to find first and follow of non-terminals in a given grammar.

The first of a grammar can be written using structures as well. Here, we have used array data structure to calculate first and follow in C programming.

Must Read: Difference Between Compiler and Interpreter

What is First and Follow?

The first and follow sets are used in compiler designing process. The first set is used in LL parsers which are top – down parsers that read or interpret from left to right.

The follow set is used in LR parsers which are bottom-up parsers that read or interpret from right to left.

The LL parsers use leftmost derivations whereas the LR parsers use rightmost derivations. A predictive parser is constructed using first and follow sets that are associated with Grammar G.

Must Read: C Program For Recursive Descent Parsing

Note: This C program to find First and Follow sets of a Grammar using Array is compiled with GNU GCC compiler and developed using gEdit Editor in Linux Ubuntu operating system.

C Program To Find First of a Given Grammar using Array



C Program To Find First and Follow of a Grammar using Array

Must Read: Lexical Analyser in C Programming


C Program To Find Follow of a Grammar using Array


C Program To Find Follow of a Grammar using Array

If you have any compilation error or doubts in this first and follow of a grammar program in C language, let us know about it in the comment section below.

17 thoughts on “Find First and Follow of a Grammar C Program

  1. Harsh Singh

    Bottom-Up and To-Down Parsers uses follow and first functions to make a parsing tree from a given grammar G.

  2. Mahesh kunal

    This code finally works for computation of follow and first for designing my compiler.

  3. Ayan Sharma

    A switch case or a while loop would be much easier to understand. Do while confuses a lot.

    1. syed zahid hasan

      plz help me……………, i could not understand any code,plz forgive me,plz help me…………plz

  4. Vikas Kumar

    You can also use Extended Backus Naur Form to automatically calculate Follow and First of a Grammar.

  5. Taurn Talreja

    This program for follow is not working . It gives segmentation fault . Please Help. My code is similar to you . Input is

    First I give E as input to find follow and then A it gives correct output for E but gives segementaton fault for A

  6. Ravindra Pal

    This is one of the best explanations of parsing with first and follow set program in c programming language. Thank you so much CodingAlpha team.

  7. Vishal Sethi

    If we use the following for loop for(count = 1; count <= limit; count++) in this first of grammar c program, will it make any difference?

  8. Mayank Rathor

    Is the first and follow concept used in compiler designing?

  9. Pawan Chauhan

    For learning compiler designing, you must read Compiler Design – K. Muneeswaran. Its a very good book available on Amazon.

  10. Renuga

    i got a problem while running the follow program, the program was stopped when the find_follow function was calling.i can’t find that what issue is there.


Let's Discuss