I am completely aware of how to calculate first and follow, my professor has taught me that well , but he had not explained what is the need of First() and Follow(), so any explanation why we need them will help me very much.

  • 2
    $\begingroup$ FYI, there's a really good answer to this over on Stack Overflow. $\endgroup$
    – ggorlen
    Apr 25 '20 at 23:40

$FIRST$ and $FOLLOW$ sets are used by the algorithm which produces an $LL(k)$ parser from a grammar. They're also used in a number of other algorithms which analyze grammars, but most students will run into them when learning about top-down parsers.

Parsing is a tiny part of the work of producing a compiler, and in practice you can generate a parser using code-generation tools which avoid you having to know much about implementation details. So I'd say that th4e direct answer to your question is that $FIRST$ and $FOLLOW$ sets have very little to do with compiler design, but they are useful in the understanding of how particular parsing algorithms work.


$FIRST$ of a non-terminal would refer to the very first character of the strings that can be derived starting from that non-terminal. The $FIRST$ of a terminal would be the terminal itself.

The $FOLLOW$ of a non-terminal $A$ refers to the $FIRST$ of the non-terminal or terminal that immediately comes after $A$ in the dervivation rules.

They are used by parsing algorithms to determine which production to use for parsing a string. If we have a choice of multiple productions, and we wish to generate a string $x$ we would like to use the productions that can generate strings that start with $x$. This is where $FIRST$ can be used to identify such productions.

  • $\begingroup$ why are they important? what are there importance in compiling? $\endgroup$
    – Turing101
    Jun 9 '19 at 18:11
  • $\begingroup$ I have edited the answer $\endgroup$
    – kauray
    Jun 10 '19 at 5:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.