A compiler design is a universe where you often encounter terms like grammar, parsing, introduction, syntax directed translation, code optimisation, generation, phases of compiler and much more!
As a compiler converts a high level language into an understandable machine level language, it holds great importance for the programmers.
One such compiler design concept that demands a programmer’s attention is thefirst and following concept.
In a compile design, first and follow are described as two grammatical functions which are useful in entering table entries.
These two grammar associate functions are used to fill the entries and to serve other essential functions. The sets of first and follows are needed so a parser can easily apply them when there is a need to put the value at its correct position.
Do You Want to Know More About Them?
If yes, stay till the end of this blog and broaden your knowledge arena related to this concept.
Types of Grammar Symbol
There are basically two kinds of grammar symbols which are; terminal and non-terminal grammar symbols.
Terminal symbols will usually contain identifiers or lowercase elements or special characters on the right side of the production. Though, non-terminal will contain all the upper case alphabets which will be on the left side of the production.
First ()
First is defined as the function which specifies a set of certain terminals that will start from the string which has been derived from the production rule. It is touted as the first terminal which appears on the right side of the production.
Why FIRST?
Backtracking is essential in the case of syntax analysis which can be quite a complex process for implementation.
If a compiler has to come in advance to produce the first character in the string where the production rules are applied, it can be compared with the current characters or token of the input string to see or make a wise decision on what production rules need to be applied.
Computation of FIRST
The first [α] is called the collection of all elements or terminal symbols which are first letters of a string which is being derived from α.
Let’s take an example;
S – > cAd
A ->bc/ a
The input string in this case will be “cad”
In the above example after reading the given character “c” in an input string we can apply, S – > cAd.
The next character in a given input string would be “a” but here the production rule of A – >bc will be ignored. You can directly use the production A ->a [ As a is derived as the first character of a string is produced by the production rule, and it will be same as the current character of a particular string which is also called as “a”]
Hence, it will be validated with the compiler or parser to know about its first character of a string which can be applied with the help of production rules in order to get the given input string.
Follow ()
Follow is defined as the set of terminal analysis which can be displayed just at the right side of your non-terminal symbol in a sequence format. This will be the first non-terminal symbol that will appear after you are given the terminal symbol on the right side of any production.
Take an example of the input string;
E ->TE’ F-> [E]id
Here in this case we can see that on the right side of the production there will be the E statement which is occurring in a particular sequence you decided. We can only find this E in the production of the F->[E]/id with the help of which we can easily find the value of a given E.
Then the output that will follow E will be E = {)} in the non-terminal of a given string which will be on the right side of the production.
Follow () in syntax Analysis
In the concept offirst and follow, the Follow set is defined as the concept used in a syntax analysis specific to the context of parsing algorithms. It is defined as the set of terminals which can appear after a non-terminal in a grammar.
The Follow set non-termina A will be defined as the given set of terminals which will appear immediately after A in a grammar derivation. If A will appear on right side of the production rule, F
The follow set in a right terminal will be given by adding the production rules to the follow set of the given A in it.
The Following set is basically useful in the LR praise as it helps you to reduce the production rule. For instance; if the next symbol in a given input stream will use the Follow set of any non-terminal, a non-terminal can safely reduce using all the production rules while starting it with a non-terminal. (Xanax bars)
To compute your following set of the grammar, you can start with the given set of symbols by applying the rule of EOF or end of file symbol by continuing the given process on the right side of your production with the left side of the required production by applying the given set of rules.
This is denied as the fundamental concept in the case of a syntax analysis, it is useful in the LR parsing algorithms. Though, computation for this set of algorithms can be quite crucial and may involve the LR to parse the input in an efficient way.
The set of terminals in this kind of approach will appear immediately in the right side of its non-terminal which will be touted in the form of X in a sequential format so that you can get the desired output for all the input you are entering.
Wrapping Up
The concept of compiler and the various phases of compiler is quite wide and interesting!
With this blog, we have thrown light on the concept of first and follow!
Read this guide and widen your knowledge base!