First and Follow Predictive Parser: Compiler Design
First and Follow Predictive Parser: Compiler Design
LAB Report
For
Version 1.0
Prepared by
Md. Saidur Rahman Kohinoor
ID: 142-15-3669
Table of Contents
Table of Contents .......................................................................................................................... ii
Revision History ............................................................................................................................ ii
1. Problem Title .............................................................................. Error! Bookmark not defined.
1.1 Problem Definition ....................................................................... Error! Bookmark not defined.
1.2 Problem Description ..................................................................... Error! Bookmark not defined.
2. Problem Solution ......................................................................................................................2
2.1 General solution procedure ............................................................................................................. 2
2.2 Specific solution procedure ............................................................................................................ 2
3. Functionality .............................................................................................................................5
3.1 Algorithm ........................................................................................................................................ 5
3.2 Editor .............................................................................................................................................. 5
3.3 Code ................................................................................................................................................ 6
3.4 Sample Input/Output ..................................................................................................................... 10
4. Limitations ..............................................................................................................................11
5. Conclusion .............................................................................................................................11
Revision History
Name Date Reason For Changes Version
1. Problem Title
Write a C Program to find FIRST and FOLLOW of a given context free grammar.
Sample Input:
E TE'
E' +TE'
E'
T FT'
T' *FT'
T'
F (E)
F id
Sample Output:
FIRST(E) = { (, id }
FIRST(E) = { +, }
FIRST(T) = { (, id }
FIRST(T) = { *, }
FIRST(F) = { (, id }
FOLLOW(E) = { $, ) }
FOLLOW(E) = { $, ) }
FOLLOW(T) = { +, $, ) }
FOLLOW(T) = { +, $, ) }
FOLLOW(F) = { *,+,$, ) }
2. Problem Solution
2.1 General Solution Procedure
FIRST(S), where S is any string of grammar symbols is the set of terminals that begin strings
derived from a. If S a, then a will be added among FIRST (S). First sets are the set of all what
can begin a production rule. For example a number must begin with a digit, a identifier must begin
with a letter, and so on.
On the other hand, Follow set are all what can follow a given symbol. For example after the symbol
"+" you can have a number (thus a digit), an identifier (thus a letter) or a parenthesis (thus "(").
FOLLOW(S) for nonterminal S, to be the set of terminals a that can appear immediately to the
right of A in some sentential form; that is, the set of terminals a such that there exists a derivation
of the form S aAa. There may have been symbols between S and a, at some time during the
derivation, but if so, they derive and disappeared. A can also be the rightmost symbol in some
sentential form, then $ is in FOLLOW(A), where $ is a special "endmarker" symbol that is not to be
a symbol of any grammar.
In T' and E' , we apply the rule that, if there is a Production X then add to first(X).
FIRST(E) = { }
FIRST(E') = { }
FIRST(T) = { }
FIRST(T') = { }
FIRST(F) = { }
Now construct another rule, If there is a Production X Y1 Y2..Yk then add first(Y1Y2..Yk) to
first(X). We apply this in T' *FT' which tells us that we can add everything in First(*FT') into
First(T'), Since First(*) using previous rule is * we can add * to First(T')
FIRST(E) = { }
FIRST(E') = { +, }
FIRST(T) = { }
FIRST(T') = { *, }
FIRST(F) = { }
Two more productions begin with terminals F (E) and F id if we apply the previous rule, i.e.
FIRST(E) = { }
FIRST(E') = { +, }
FIRST(T) = { }
FIRST(T') = { *, }
FIRST(F) = { (, id }
Next we apply that rule to T FT' once again this tells us that we can add First(FT') to First(T)
Since First(F) doesn't contain that means that First(FT') is just First(F)
FIRST(E) = { }
FIRST(E') = { +, }
FIRST(T) = { (, id }
FIRST(T') = { *, }
FIRST(F) = { (, id }
Lastly we apply the previous rule to E TE' once again this tells us that we can add First(TE') to
First(E). Since First(T) doesn't contain that means that First(TE') is just First(T)
FIRST(E) = { (, id }
FIRST(E') = { +, }
FIRST(T) = { (, id }
FIRST(T') = { *, }
FIRST(F) = { (, id }
Doing anything else doesn't change the sets so we are done the FIRST!!!
Follow():
Now, We want to make Follow sets so first we list the sets we need
FOLLOW(E) = { }
FOLLOW(E') = { }
FOLLOW(T) ={ }
FOLLOW(T') = { }
FOLLOW(F) = { }
Next we apply a rule to E' +TE' which says that, If there is a production A aBb, then
everything in FIRST(b) except for is placed in FOLLOW(B). That means, everything in First(E')
except for should be in Follow(T)-
FOLLOW(E) = { $ }
FOLLOW(E') = { }
FOLLOW(T) ={ + }
FOLLOW(T') = { }
FOLLOW(F) = { }
Next we apply another rule to E TE' which says that, if there is a production A aB, then
everything in FOLLOW(A) is in FOLLOW(B). That means add everything in Follow(E) into
Follow(E') -
FOLLOW(E) = { $ }
FOLLOW(E') = { $ }
FOLLOW(T) ={ + }
FOLLOW(T') = { }
FOLLOW(F) = { }
Next we apply the previous rule to T FT' This says that we should add everything in Follow(T)
into Follow(T')
FOLLOW(E) = { $ }
FOLLOW(E') = { $ }
FOLLOW(T) ={ + }
FOLLOW(T') = { + }
FOLLOW(F) = { }
Now we apply the rule to T' *FT' which says that everything in First(T') except for should be in
Follow(F)
FOLLOW(E) = { $ }
FOLLOW(E') = { $ }
FOLLOW(T) ={ + }
FOLLOW(T') = { + }
FOLLOW(F) = { * }
Now we apply the rule to F (E) which says that everything in First(')') should be in Follow(E)
FOLLOW(E) = { $, ) }
FOLLOW(E') = { $ }
FOLLOW(T) ={ + }
FOLLOW(T') = { + }
FOLLOW(F) = { * }
Next we apply the rule to E TE' which says that we should add everything in Follow(E) into
Follow(E')
FOLLOW(E) = { $, ) }
FOLLOW(E') = { $, ) }
FOLLOW(T) = { + }
FOLLOW(T') = { + }
FOLLOW(F) = { * }
Next we apply last rule to E' +TE', which says that, If there is a production A aBb, where
FIRST(b) contains , then everything in FOLLOW(A) is in FOLLOW(B). That means, we should
add everything in Follow(E') into Follow(T) (because First(E') contains )
FOLLOW(E) = { $, ) }
FOLLOW(E') = { $, ) }
FOLLOW(T) = { +, $, ) }
FOLLOW(T') = { + }
FOLLOW(F) = { * }
Next we apply the rule to T FT', which says that we should add everything in Follow(T) into
Follow(T')
FOLLOW(E) = { $, ) }
FOLLOW(E') = { $, ) }
FOLLOW(T) = { +, $, ) }
FOLLOW(T') = { +, $, ) }
FOLLOW(F) = { * }
Finally we apply the previous rule to T' *FT' This says that we should add everything in
Follow(T') into Follow(F)
FOLLOW(E) = { $, ) }
FOLLOW(E') = { $, ) }
FOLLOW(T) = { +, $, ) }
FOLLOW(T') = { +, $, ) }
FOLLOW(F) = { *, +, $, ) }
And the job is done and we get the desire FOLLOW set.
3. Functionality
3.1 Algorithm
Algorithm of FIRST set:
If X is a terminal then First(X) is just X!
if there is a Production X then add to first(X).
If there is a Production X Y1 Y2...Yk then add first(Y1Y2...Yk) to first(X).
First(Y1 Y2...Yk) is either
1. First(Y1 (if First(Y1) doesn't contain ).
2. OR (if First(Y1) does contain ) then First (Y1 Y2..Yk) is everything in First(Y1)
<except for > as well as everything in First(Y2..Yk)
3. If First(Y1) First(Y2)..First(Yk) all contain then add to First(Y1 Y2..Yk) as well.
3.2 IDE/Editor
Code Blocks
Notepad
For input, open the notepad and type the grmmar as follows (here E, T and replace by
R, S and # respectively) and save the as name as First_Follow.txt.
4. Limitations
If user need to input grammar, at first should open the txt file and manually input it and save,
then run the program for execution. That means this program is not very user friendly.
This program can not able to count E as one input. Thats why use R instead of E and S
instead of T.
In right hand side, my program cannot ignore the space during First and Follow count.
5. Conclusion
In top- down parsing, FIRST and FOLLOW is used to choose production to apply based on next
input symbol in the given string. In panic-mode error recovery tokens produced by FOLLOW are
used for synchronizing. Actually, the implementation of that kind of stuff by C programming is little
bit complex and lengthy. But finally it comes to the desire goal through deeply analyzing on that
context and helps to improve the confidence level.
~ The END