patterncMinor
Computing first and following sets (compiler design)
Viewed 0 times
designfollowingfirstcompilerandsetscomputing
Problem
I have made this program to find out the first and following sets of the productions:
Here epsilon(NULL) is taken as 0:
Please review this.
E->TA
A->+TA
A->0
T->FB
B->*FB
B->0
F->(E)
F->#Here epsilon(NULL) is taken as 0:
#include
#include
char a[8][8];
struct firTab
{
int n;
char firT[5];
};
struct folTab
{
int n;
char folT[5];
};
struct folTab follow[5];
struct firTab first[5];
int col;
void findFirst(char,char);
void findFollow(char,char);
void folTabOperation(char,char);
void firTabOperation(char,char);
void main()
{
int i,j,c=0,cnt=0;
char ip;
char b[8];
printf("\nFIRST AND FOLLOW SET \n\nenter 8 productions in format A->B+T\n");
for(i=0;iTA
A->+TA
A->0
T->FB
B->*FB
B->0
F->(E)
F->#
*/Please review this.
Solution
To start at the top, your two structures
combined. And given a name than means something.
Your embedded constants 8 and 5 throughout should be replaced by #defined
constants to make changing them easier.
You would also do well to restructure
logically complete and separate into functions. And place
avoid the need for prototypes. Every other function can be static.
On compiling the code, every array subscript gives a warning: array subscript
is of type 'char'. char subscripts are generally best avoided because char
can be signed or unsigned according to the implementation.
On the details of the code, I can't claim to have followed it through. But
looking at the first 30 lines of
Your first action is to obtain the 8 rules you expect. The code would be
better is it did not assume a fixed number (8) of rules or a fixed number (5)
of first/follow.
The first line of main defines some variables, but they would be better defined
at the point of first use (where possible) or one per line.
not sufficiently meaningful.
You then define array
assumption of the number of input lines.
will contain junk (whatever is at that stack location).
You then read the 8 input lines into
in the array entries. Then follows a loop that reads through these eight
lines and puts the first character of each line into array
duplication. There are numerous issues with these lines, the main one being
that it is such a convoluted way of doing this. Here are some more:
only works by chance.
a particular letter.
-
variables
-
you should add some spaces to make expressions more readable, for example
after
-
your layout is inconsistent (eg placement of
opening braces)
This whole loop should have been combined with the input loop so that each
time you read a new line into
whether it was in
make it a nul terminated string, you can check this easily with
where
I've only covered 50 lines of the code, but I think there is enough there for you to think about. It looks as if you have disappeared, so I'm not sure it is worth digging any further. If you reappear, I might :-)
firTab and folTab could probably becombined. And given a name than means something.
Your embedded constants 8 and 5 throughout should be replaced by #defined
constants to make changing them easier.
You would also do well to restructure
main to extract parts that are arelogically complete and separate into functions. And place
main last toavoid the need for prototypes. Every other function can be static.
On compiling the code, every array subscript gives a warning: array subscript
is of type 'char'. char subscripts are generally best avoided because char
can be signed or unsigned according to the implementation.
On the details of the code, I can't claim to have followed it through. But
looking at the first 30 lines of
main...Your first action is to obtain the 8 rules you expect. The code would be
better is it did not assume a fixed number (8) of rules or a fixed number (5)
of first/follow.
The first line of main defines some variables, but they would be better defined
at the point of first use (where possible) or one per line.
c and cnt arenot sufficiently meaningful.
You then define array
b[8], again with an embedded constant and anassumption of the number of input lines.
b is left uninitialized and hencewill contain junk (whatever is at that stack location).
You then read the 8 input lines into
a[][] without any checks that they fitin the array entries. Then follows a loop that reads through these eight
lines and puts the first character of each line into array
b withoutduplication. There are numerous issues with these lines, the main one being
that it is such a convoluted way of doing this. Here are some more:
- array
ais badly named
- array
bis badly named and is used before being initialized. The loop
only works by chance.
- flag
cis badly named. It is a boolean indicating thatbalready holds
a particular letter.
-
variables
i and j should be declared as part of their loops:for (int i = 0; i < 8; i++)-
you should add some spaces to make expressions more readable, for example
after
if and for, after ;, around = and + etc-
your layout is inconsistent (eg placement of
c=0; compared to otheropening braces)
This whole loop should have been combined with the input loop so that each
time you read a new line into
a, you checked its first character to seewhether it was in
b and if not added it. If we rename b as letters andmake it a nul terminated string, you can check this easily with
if (!strchr(letters, line[0])) {
// append new letter
}where
line is the new input line.I've only covered 50 lines of the code, but I think there is enough there for you to think about. It looks as if you have disappeared, so I'm not sure it is worth digging any further. If you reappear, I might :-)
Code Snippets
for (int i = 0; i < 8; i++)if (!strchr(letters, line[0])) {
// append new letter
}Context
StackExchange Code Review Q#32199, answer score: 7
Revisions (0)
No revisions yet.