You are on page 1of 13

Compiler Design (CSE332)

LAB Report

For

Predictive Parsing using


First and Follow Function

Version 1.0

Prepared by
Md. Saidur Rahman Kohinoor
ID: 142-15-3669

Daffodil International University


Department of CSE
38th Batch, Section: F

April 22, 2017


LAB Report on Predictive Parsing using First and Follow Function Page ii

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

Md. Saidur Rahman Kohinoor | mdsaidur.r.kohinoor@ieee.org


LAB Report on Predictive Parsing using First and Follow Function Page 1

1. Problem Title
Write a C Program to find FIRST and FOLLOW of a given context free grammar.

1.1 Problem Definition


For checking the ambiguity of a grammar or error recovering, we need to the first and follow. In top-
down parsing, first and follow is used to choose which production to apply based on next input symbol
in the given string. Here given problem is writing a C program to find out the first and follow and
mention the concept, procedure, how first and follow work as well as the developing algorithm.

1.2 Problem Description


In this report, my job is to write down about first and follow grammar with coding implementation
that can take input from a user directly on the console or the input can be read from a text file.
After compilation it will produce the First and Follow result, where can use # symbol instead of
symbol and = symbol instead of symbol.

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) = { *,+,$, ) }

Md. Saidur Rahman Kohinoor | mdsaidur.r.kohinoor@ieee.org


LAB Report on Predictive Parsing using First and Follow Function Page 2

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.

2.2 Specific Solution Procedure


FIRST(): For finding first set, at first we should list the sets we need
FIRST(E) = { }
FIRST(E') = { }
FIRST(T) = { }
FIRST(T') = { }
FIRST(F) = { }

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 }

Md. Saidur Rahman Kohinoor | mdsaidur.r.kohinoor@ieee.org


LAB Report on Predictive Parsing using First and Follow Function Page 3

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) = { }

The First thing we do is Add $ to the start Symbol 'E'


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) = { }

Md. Saidur Rahman Kohinoor | mdsaidur.r.kohinoor@ieee.org


LAB Report on Predictive Parsing using First and Follow Function Page 4

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)

Md. Saidur Rahman Kohinoor | mdsaidur.r.kohinoor@ieee.org


LAB Report on Predictive Parsing using First and Follow Function Page 5

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.

Algorithm of FOLLOW set:


First put $ in Follow(S) When S is the start symbol
If there is a production A aBb, (where a can be a whole string) then everything in
FIRST(b) except for is placed in FOLLOW(B).
If there is a production A aB, then everything in FOLLOW(A) is in FOLLOW(B)
If there is a production A aBb, where FIRST(b) contains , then everything in
FOLLOW(A) is in FOLLOW(B)

3.2 IDE/Editor

Code Blocks
Notepad

Md. Saidur Rahman Kohinoor | mdsaidur.r.kohinoor@ieee.org


LAB Report on Predictive Parsing using First and Follow Function Page 6

3.3 Implemented Code: (Language C)

Md. Saidur Rahman Kohinoor | mdsaidur.r.kohinoor@ieee.org


LAB Report on Predictive Parsing using First and Follow Function Page 7

Md. Saidur Rahman Kohinoor | mdsaidur.r.kohinoor@ieee.org


LAB Report on Predictive Parsing using First and Follow Function Page 8

Md. Saidur Rahman Kohinoor | mdsaidur.r.kohinoor@ieee.org


LAB Report on Predictive Parsing using First and Follow Function Page 9

Md. Saidur Rahman Kohinoor | mdsaidur.r.kohinoor@ieee.org


LAB Report on Predictive Parsing using First and Follow Function Page 10

3.4 Sample Input/Output

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.

The sample Output-

Md. Saidur Rahman Kohinoor | mdsaidur.r.kohinoor@ieee.org


LAB Report on Predictive Parsing using First and Follow Function Page 11

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

Md. Saidur Rahman Kohinoor | mdsaidur.r.kohinoor@ieee.org

You might also like