-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathff.cpp
150 lines (142 loc) · 3.7 KB
/
ff.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#include<stdio.h>
#include<string.h>
#include<conio.h>
#define max 20
using namespace std;
char prod[max][10];
char ter[10],nt[10];
char first[10][10],follow[10][10];
int eps[10];
int count=0;
int findpos(char ch)
{
int n;
for(n=0;nt[n]!='\0';n++)
if(nt[n]==ch)
break;
if(nt[n]=='\0')
return 1;
return n;
}
int IsCap(char c)
{
if(c >= 'A' && c<= 'Z')
return 1;
return 0;
}
void add(char *arr,char c)
{
int i,flag=0;
for(i=0;arr[i]!='\0';i++){
if(arr[i] == c){
flag=1;
break;
}
}
if(flag!=1)
arr[strlen(arr)] = c;
}
void addarr(char *s1,char *s2) {
int i,j,flag=99;
for(i=0;s2[i]!='\0';i++){
flag=0;
for(j=0;;j++){
if(s2[i]==s1[j]){
flag=1;break;
}
if(j==strlen(s1) && flag!=1){
s1[strlen(s1)] = s2[i];
break;
}
}
}
}
void addprod(char *s){
int i;
prod[count][0] = s[0];
for(i=3;s[i]!='\0';i++){
if(!IsCap(s[i]))
add(ter,s[i]);
prod[count][i-2] = s[i];
}
prod[count][i-2] = '\0';
add(nt,s[0]);
count++;
}
void findfirst(){
int i,j,n,k,e,n1;
for(i=0;i<count;i++){
for(j=0;j<count;j++){
n = findpos(prod[j][0]);
if(prod[j][1] == (char)238)
eps[n] = 1;
else {
for(k=1,e=1;prod[j][k]!='\0' && e==1;k++){
if(!IsCap(prod[j][k])){
e=0;
add(first[n],prod[j][k]);
}else{
n1 = findpos(prod[j][k]);
addarr(first[n],first[n1]);
if(eps[n1] == 0)
e=0;
}
}
if(e==1)
eps[n]=1;
}
}
}
}
void findfollow(){
int i,j,k,n,e,n1;
n = findpos(prod[0][0]);
add(follow[n],'#');
for(i=0;i<count;i++){
for(j=0;j<count;j++){
k = strlen(prod[j])-1;
for(;k>0;k--){
if(IsCap(prod[j][k])){
n=findpos(prod[j][k]);
if(prod[j][k+1] == '\0'){
if(prod[j][k+1] == '\0') {
n1 = findpos(prod[j][0]);
addarr(follow[n],follow[n1]);
}
if(IsCap(prod[j][k+1])){
n1 = findpos(prod[j][k+1]);
addarr(follow[n],first[n1]);
if(eps[n1]==1){
n1=findpos(prod[j][0]);
addarr(follow[n],follow[n1]);
}
} else if(prod[j][k+1] != '\0'){
add(follow[n],prod[j][k+1]);
}
}
}
}
}
}
}
void main() {
char s[max],i;
printf("\nEnter the productions(type 'end' at the last of the production)\n");
scanf("%s",s);
while(strcmp("end",s)){
addprod(s);
scanf("%s",s);
}
findfirst();
findfollow();
for(i=0;i<strlen(nt);i++){
printf("%c\t",nt[i]);
printf("%s",first[i]);
if(eps[i]==1)
printf("%c\t",(char)238);
else
printf("\t");
printf("%s\n",follow[i]);
}
getch();
}