-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathq8.cpp
312 lines (247 loc) · 8 KB
/
q8.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
#include<iostream>
#include<string>
#include<map>
#include<fstream>
using namespace std;
string terminals = "e", nonterminals; // e is epsilon
string dupliRemove( string a )
{
string w;
for( int i=0; i<a.length();i++)
{
if ( w.find(a[i]) == string::npos)
{
w += a[i];
}
}
return w;
}
//FIRST SET
string first( string a,multimap<char,string>& map)
{
string first_ans;
for (multimap<char, string>::iterator it = map.begin();it != map.end();++it)
{
if ( it->first == a[0])
{
//rhs is non-terminal
for( int k = 0 ; k < (it->second).length(); k++) //searching rhs of production
{
// multimap<char,string> cfg;
bool repeat = false;
string rhs;
rhs += (it->second)[k];
string tmp ;
tmp = first(rhs, map);
const int length = tmp.length();
//searching tmp for epsilon
for( int i=0;i<length;i++)
{
if ( tmp[i] == 'e')
{
if ( k != (it->second).length()) //last symbol on rhs dont remove
tmp.erase(i,1);
first_ans += tmp;
repeat = true; //epsilon found go to next symbol
break;
}
}
//epsilon not found leave the loop
if ( repeat == false)
{
first_ans += tmp;
break;
}
}
}
else //terminal is part of loop not necessary but keep this way
{
for( int j=0 ; j< terminals.length();j++)
{
// cout << "terminal " << terminals[j] << endl;
if ( a[0] == terminals[j] )
{
first_ans += a[0];
// cout << first_ans << endl;
break;
}
}
}
}
//
return first_ans;
}
string rhs_cfg(string a)
{
int i=0;
while ( a[i] != '-')i++;
i+= 2;
if ( i >= a.length()) return "-1";
return a.substr(i,a.length()-i); // rhs of production
}
string firstMain( string a , multimap<char,string>& map )
{
string first_ans,temp,e = "e";
for( int i=0 ; i < a.length(); i++)
{
string in;
in = a[i];
temp = first(in , map);
if ( temp.find(e) != string::npos)
{
temp.erase(temp.find(e),1);
first_ans += temp;
if ( i == a.length()-1)
{
first_ans += e;
}
}
else
{
first_ans += temp;
break;
}
}
string ans = dupliRemove(first_ans);
return ans;
}
//FOLLOW SET
string follow( string a , multimap<char,string>& map) //enter cfg
{
string follow_ans;
for (multimap<char,string>::iterator it = map.begin();it != map.end();++it)
{
//means it is the start symbol
if ( it == map.begin() && a[0] == it->first)
{
follow_ans += "$";
}
//scanning rhs to apply 2 rule to find follow
if ( (it->second).find(a[0]) != string::npos) // we found a[0]
{
int length = (it->second).length();
int pos = (it->second).find(a[0]);
string e = "e";
if ( (pos+1) < length ) //not the last symbol
{
string p,tmp;
//p += (it->second)[pos+1]; //change this to accomaodate whole till end
p = (it->second).substr(pos+1,length-pos-1);
tmp = firstMain(p,map);
while( tmp.find(e) != string::npos)
tmp.erase((tmp.find(e)),1); //removing epsilon from first
follow_ans += tmp;
}
string tmp2;
tmp2 = (it->second).substr(pos+1,length-pos-1); //need to modify this rule
cout << "POS " << pos+2 << " length " << length << " FOLLOW OF " << a[0] << " rhs " << it->second << endl;
//applying 3 rule
if ( (pos+1) == length )
{
string tmp1;
tmp1 += it->first;
if ( tmp1[0] == a[0])
{
continue;
}
cout << "FOLLOW " << follow(tmp1,map) << " to be added in " << a[0] << endl;
follow_ans += follow(tmp1,map);
cout << "Which is " << follow_ans << endl;
}
else if ( firstMain(tmp2,map).find(e) != string::npos ) //first has epsilon
{
string a1;
a1 += it->first;
if ( a1[0] == a[0]) continue;
follow_ans += follow(a1,map);
}
}
}
return follow_ans;
}
int main()
{
multimap<char,string> cfg;
map<string,string> firstSet;
map<string,string> followSet;
fstream in("q8test.txt"); //Input File
int i;
string input,temp;
while(true)
{
getline(in,input);
//add to multimap
if ( rhs_cfg(input) != "-1")
cfg.insert(pair<char,string>(input[0],rhs_cfg(input)));
if ( in.eof()) break;
}
cout << ".................CONTEXT FREE GRAMMAR.......................\n" << endl;
for (multimap<char, string>::iterator it = cfg.begin();it != cfg.end();++it)
{
//store first set in a array
cout << (*it).first << " - " << (*it).second << endl;
}
//asking for terminals and nonterminals
cout << "Enter TERMINALS in the CFG .Press q to exit" << endl;
while(true)
{
cin >> temp;
if ( temp == "q" ) break;
terminals += temp;
}
cout << endl;
cout << "Enter NON-TERMINALS in the CFG" << endl;
while(true)
{
cin >> temp;
if ( temp == "q" ) break;
nonterminals += temp;
}
cout << "\n Terminals " << terminals << " \n Nonterminals " << nonterminals << endl;
cout << "....................FIRST sets........................." << endl;
char duplicateChecker = NULL ;
for (multimap<char, string>::iterator it = cfg.begin(); it != cfg.end(); ++it)
{
if ( duplicateChecker != it->first)
{
string lhs;
lhs += it->first;
string ans = dupliRemove(first(lhs,cfg));
duplicateChecker = it->first;
firstSet.insert(pair<string,string>(lhs,ans)); //adding to first hashtable
cout << "FIRSTMAIN " << dupliRemove(firstMain(lhs,cfg)) << endl;
}
}
for (multimap<string, string>::iterator it = firstSet.begin(); it != firstSet.end(); ++it)
{
cout << "FIRST( " << it->first << " ) = " << it->second << endl;
}
// cout << "first " << first("A",cfg);
cout << "....................FOLLOW sets........................." << endl;
duplicateChecker = NULL ;
for (multimap<char, string>::iterator it = cfg.begin(); it != cfg.end(); ++it)
{
if ( duplicateChecker != it->first)
{
string lhs;
lhs += it->first;
string ans = dupliRemove(follow(lhs,cfg));
duplicateChecker = it->first;
followSet.insert(pair<string,string>(lhs,ans)); //adding to follow hashtable
}
}
for (multimap<string, string>::iterator it = followSet.begin(); it != followSet.end(); ++it)
{
cout << "FOLLOW( " << it->first << " ) = " << it->second << endl;
}
while(true)
{
string input;
cout << "Enter the string to find the FIRST set. Press 1 to exit" << endl;
cin >> input;
if ( input == "1")break;
cout << "FIRSTMAIN " << dupliRemove(firstMain(input,cfg)) << endl;
}
in.close();
return 0;
}