Download as doc, pdf, or txt
Download as doc, pdf, or txt
You are on page 1of 4

Practical No.

3
Title:

Write a program to compute FIRST and FOLLOW of the Nonterminal

Logic:
A)
To compute FIRST(X) for all grammar symbols X, apply the following rules
until no more terminals or can be added to any FIRST set:
1.
2.
3.

If X is terminal, then FIRST(X) is {X}.


If X is a production, then add to FIRST(X).
If X is nonterminal and X Y1 Y2 ... Yk. is a production, then place a in FIRST(X) if
for some i, a is in FIRST(Yi), and is in all of FIRST(Y1), ... , FIRST(Yi-1); that is,
Y1, ... ,Yi-1 . If is in FIRST(Yj) for all j = 1, 2, ... , k, then add to FIRST(X).
For example, everything in FIRST(Y1) is surely in FIRST(X). If Y1 does not derive ,
then we add nothing more to FIRST(X), but if Y1, then we add
FIRST(Y2) and so on.

B) To compute FOLLOW(A) for all nonterminals A, apply the following rules until
nothing can be added to any FOLLOW set:
1. Place $ in FOLLOW(S), where S is the start symbol and $ is the input right
endmarker.
2. If there is a production A , then everything in FIRST(), except for is
placed in FOLLOW(B).
3. If there is a production A , or a production A where FIRST()
contains (i.e., then everything in FOLLOW(A) is in FOLLOW(B).
Example : Consider the expression grammar given below :
E T E
E+ T E |
T F T
T* F T |
F ( E ) | id
Then:
FIRST(E) = FIRST(T) = FIRST(F) = {( , id}
FIRST(E) = {+, }
FIRST(T) = {*, }
FOLLOW(E) = FOLLOW(E) = {) , $}
FOLLOW(T) = FOLLOW(T) = {+, ), $}
FOLLOW(F) = {+, *, ), $}

Logic:
Step 1: Accept the list of terminals and nonterminals in an array.
Step 2: Accept production rule for every nonterminal in an array and terminate the
string on RHS of rule by $( endmarker)
eg.

S-> aeA$

Step 3: You can use e as epsilon.


Step 4: Apply the rules for finding First(X) and display it.
Step 5: Use First ( X) to find Follow(X) and display it.
Instructions for Writing Journal
1)
2)
3)
4)

Practical No. , Title on LHS and RHS


Write the theory of First and Follow. Illustrate with example.
Attach the print copy of the program you have executed.
Write Output of the program on LHS

Practical No. 2
Title:
Write a program to find tokens of the input file.

Logic:
Step1: Declare the necessary variables
Step2: Declare an array/ file / direct for the keywords/ operators/ special symbols
Step3: Open the input file in read open

Step4: Read the string from the file till the end of the file
If the first character in the string is # then print the string as header file
If the string matches with any of the keywords print that string as a keyword
If the string matches with operator and special symbols print the corresponding
message.
If the string is not a keyword/ operator then print that as an identifier with and
validate it .
If constant is declared eg: j=16 than read constant value and define its type
whether it is Integer / Float.

Instructions:
A. There are 3 ways to match the keywords / Operators
1) Maintain file of keywords / Operators when input string is matched read both the
files.
2) Maintain keywords / Operators as Arrays.
3) Otherwise match all keywords/ Operators directly in the construct using
If- Else along with relational operator or Switch case
Instructions for Journal Writing
1) Practical No. , Title on LHS and RHS
2) Write the theory of Lexical analyzer i.e rules for finding Keywords/ Operator/
Identifier on RHS
3) Mention in the Journal the list of operators/ keywords for which your program
works
4) Write program on RHS and attach print copy of Output

Example Program for Keyword Checking for few keywords:


#include<string.h>
#include<ctype.h>
#include<stdio.h>
void keyword(char str[10])
{
if(strcmp("for",str)==0||strcmp("while",str)==0||strcmp("do",str)==0||
strcmp("int",str)==0||strcmp("float",str)==0||strcmp("char",str)==0||
strcmp("double",str)==0||strcmp("static",str)==0||strcmp("switch",str)==0||
strcmp("case",str)==0)
printf("\n%s is a keyword",str);
else
printf("\n%s is an identifier",str);
}

You might also like