Find First and Follow of a Grammar C Program

C Program To Find First and Follow of a Grammar

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

Output

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

C Program To Find Follow of a Grammar using Array

   

Output

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.

10 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.

    Reply
  2. Mahesh kunal

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

    Reply
  3. Ayan Sharma

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

    Reply
  4. Vikas Kumar

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

    Reply
  5. Taurn Talreja

    This program for follow is not working . It gives segmentation fault . Please Help. My code is similar to you . Input is
    8
    E=TA
    A=+TA
    A=$
    T=FB
    B=*FB
    B=$
    F=a
    F=(E)

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

    Reply
  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.

    Reply
  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?

    Reply
  8. Mayank Rathor

    Is the first and follow concept used in compiler designing?

    Reply
  9. Pawan Chauhan

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

    Reply

Let's Discuss