forked from spandey1296/Learn-Share-Hacktoberfest2021
-
Notifications
You must be signed in to change notification settings - Fork 0
/
firstfollow.cpp
99 lines (82 loc) ยท 2.22 KB
/
firstfollow.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
#include <bits/stdc++.h>
using namespace std;
void findFollow(char & find, int & l, vector<string> & prod, map<char, int> & ans);
void findFirst(char & find, int & l, vector<string> & prod, map<char, int> & ans){
if(find >= 'a' && find <= 'z'){
ans[find] = 1;
return;
}
for(int i = 0; i < l; i++){
if(prod[i][0] == find){
// Calc first.
if(prod[i][2] >= 'a' && prod[i][2] <= 'z'){
ans[prod[i][2]] = 1;
}
else if(prod[i][2] >= 'A' && prod[i][2] <= 'Z'){
findFirst(prod[i][2], l, prod, ans);
}
else if(prod[i][2] == '#'){
findFollow(find, l, prod, ans);
}
}
}
}
void findFollow(char & find, int & l, vector<string> & prod, map<char, int> & ans){
// For 'S'.
if(prod[0][0] == find){
ans['$'] = 1;
}
for(int i = 0; i < l; i++){
for(int j = 2; j < prod[i].size(); j++){
// Character matches.
if(prod[i][j] == find){
// If it's last character then call first Follow.
if(j == prod[i].size() - 1 && prod[i][0] != find){
findFollow(prod[i][0], l, prod, ans);
}
else{
findFirst(prod[i][j + 1], l, prod, ans);
}
}
}
}
}
void solve(){
// cout<<0<<endl;
int l;
cout<<"Enter total productions : ";
cin>>l;
vector<string> prod(l);
for(int i = 0; i < l; i++){
cout<<"Enter production "<< (i + 1) <<" : ";
cin>>prod[i];
}
while(true){
char find;
cout<<"Enter character to find First & Follow (Z to exit):";
cin>>find;
if(find == 'Z'){
break;
}
map<char, int> ans;
findFirst(find, l, prod, ans);
cout<<"First :=> ";
for(auto itr:ans){
cout<<itr.first<<" , ";
}
cout<<endl;
ans.clear();
findFollow(find, l, prod, ans);
cout<<"Follow :=> ";
for(auto itr:ans){
cout<<itr.first<<" , ";
}
cout<<endl;
}
}
signed main() {
int q = 1;
while(q--){
solve();
}
}