Professional Documents
Culture Documents
First Follow Instruction
First Follow Instruction
3
Title:
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.
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$
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