forked from niyasc/Compiler-Design-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ7Cfgconv.cpp
More file actions
496 lines (438 loc) · 15.4 KB
/
Q7Cfgconv.cpp
File metadata and controls
496 lines (438 loc) · 15.4 KB
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
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
#include <algorithm>
#include <iostream>
#include <vector>
#include <fstream>
#include <string>
using namespace std;
int num_of_productions;
vector<string> productions;
vector<char> terminals;
vector<char> nonTerminals;
vector<string> newProductions;
vector<string> deterministicProductions;
int isInTerminal(char c){
if(find(terminals.begin(),terminals.end(),c)!=terminals.end()){
return 1;
}
return 0;
}
int isInNonTerminal(char c){
if(find(nonTerminals.begin(),nonTerminals.end(),c)!=nonTerminals.end()){
return 1;
}
return 0;
}
int isTerminal(char tem){
if(tem>='A' && tem<='Z'){
return 0;
}else{
return 1;
}
}
int prepareListOfCharacter(){//Prepare List Of Valid Characters
for(int i=0;i<productions.size()-1;i++){
string tempString=productions[i];
int tempStringLen = tempString.length();
for(int j=0;j<tempStringLen;j++){
char t = tempString[j];
if(!(t==' ')){
if(isInNonTerminal(t)||isInTerminal(t)){
continue;
}else{
//Check if Terminal Or Non Terminal.
// Add in the respective Vector
if(isTerminal(t)){
terminals.push_back(t);
}else{
nonTerminals.push_back(t);
}
}
}
}
}
return 1;
}
int readProductions(){
//Function to read the productions from the input file
//READING BEGINS
ifstream input("input2.txt");
string tempString;
input>>num_of_productions;
getline(input, tempString, '\n');
while (getline(input, tempString, '\n')) {
productions.push_back(tempString);
}
cout<<"\n\t\tinput frammar "<<endl;
//cout<<"\n\t\ttotal Productions : "<<num_of_productions<<endl;
cout<<"\t\t\tProductions\n";
for(int i=0;i<productions.size()-1;i++){
cout<<productions[i]<<endl;
}
// cout<<"DATA FROM INPUT FILE ENDS: "<<endl<<endl;
//READING ENDS
prepareListOfCharacter();
cout<<"\nTerminal Chars: ";
for(int i=0;i<terminals.size();i++){
cout<<terminals[i]<<" ";
}
cout<<"\nNon Terminals : ";
for(int i=0;i<nonTerminals.size();i++){
cout<<nonTerminals[i]<<" ";
}
cout<<"\n";
return 1;
}
char nonTermGen(){
//Generate A Non Terminal To Replace
char nonTerm = 'A';
while(true){
if(isInNonTerminal(nonTerm)){
//cout<<"\nNon Term Mai Hai";
nonTerm += 1;
}else{
//cout<<"\nNon Term mai Ghanta";
return nonTerm;
}
if(nonTerm==('Z'+1)){
return '$';
}
}
}
int isLeftRecursive(char c){
//cout<<"\nChar => "<<c;
//cout<<"\n Productions : \n";
for(int j=0;j<productions.size();j++){
string tempString = productions[j];
//cout<<"\nTempString["<<j<<"] => "<<tempString;
if(tempString[0]==c){
if(!isInNonTerminal(tempString[2])){
//First Character Is A Terminal
//cout<<"\n Not L Rec => "<<tempString;
newProductions.push_back(tempString);
}else{
//First Character Is A Non Terminal
if(tempString[2]==c){
//Direct Recursion
return 1;
}else{
//Indirect Recursion IGNORED FOR NOW
newProductions.push_back(tempString);
}
}
}else{
//cout<<"\n First Character not "<<c;
}
}
return 0;
}
int rectifyLeftRecursion(char c){
// A->Aα|β
// ||
// A->βA'
// A'->αA'|ε
//Adding Beta Productions
char newNonTerm = nonTermGen();
if(newNonTerm=='$'){
///////////// cout<<"Ran Out Of Non Terminals To Assign. Too Many Left Recursive Entries.";
exit;
}
vector<string> beta;
for(int k=0;k<productions.size();k++){
if(productions[k][0]==c){
if(productions[k][2]!=c){
beta.push_back(productions[k]);
}
}
}
for(int k=0;k<beta.size();k++){
//Add Beta Productions
string betaProduction = beta[k]+newNonTerm;
if(!isInNonTerminal(newNonTerm)){
nonTerminals.push_back(newNonTerm);
}
newProductions.push_back(betaProduction);
}
//Dealing With A' Productions
for(int j=0;j<productions.size();j++){
if(productions[j][0]==c){
string tempString = productions[j];
if(tempString[2]==c){
//Direct Left Recursion Present
string alpha = tempString.substr(3);
//Add A' Productions
string alphaProduction = "";
alphaProduction += newNonTerm;
alphaProduction += (" "+alpha);
alphaProduction += newNonTerm;
if(!isInNonTerminal(newNonTerm)){
nonTerminals.push_back(newNonTerm);
}
newProductions.push_back(alphaProduction);
}
}
}
string epsilonProduction = "";
epsilonProduction += newNonTerm;
epsilonProduction += " ε";
newProductions.push_back(epsilonProduction);
terminals.push_back(newNonTerm);
return 1;
}
int printFinalGrammar(){
cout<<"Output left recursion free gramamr is "<<endl;
//cout<<"\nFINAL LEFT RECURSION FREE GRAMMAR IS : "<<endl;
for(int i=0;i<newProductions.size();i++){
cout<<newProductions[i]<<endl;
}
return 1;
}
int clearProductions(char c){
vector<string> tempProds=productions;
int j=0;
while(true){
if(tempProds[j][0]==c){
tempProds.erase(tempProds.begin()+j);
}else{
j++;
}
if(j==tempProds.size()){
break;
}
}
productions = tempProds;
return 1;
}
int isNonDeterministic(char c){
//return 0;
vector<string> tempProductions;
for(int j=0;j<productions.size();j++){
string tempString = productions[j];
if(productions[j][0]==c){
tempProductions.push_back(productions[j]);
}
}
for(int j=0;j<tempProductions.size();j++){
string tempString = tempProductions[j];
//cout<<"\n\n\nTemp String => "<<tempString;
for(int k=0;k<tempProductions.size();k++){
//cout<<"\nTempProduction[k] => "<<k<<"=> "<<tempProductions[k];
if(tempString==tempProductions[k]){
//cout<<"\t 1";
continue;
}else{
if(tempString.length()>tempProductions[k].length()){
//cout<<"\t 2";
for(int l=tempProductions[k].length();l>1;l--){
size_t found = tempString.find(tempProductions[k].substr(1,l));
if(found!=string::npos){
//Find the Element in tempString
if(found==1){
//Element At The Beginning This is the case We're interested in
//cout<<"\n ND 1 "<<tempProductions[k].substr(2,l);
return 1;
}
}
}
}else{
//cout<<"\t 3";
for(int l=tempString.length();l>1;l--){
//cout<<"\n\tLooking For => "<<tempString.substr(1,l)<<"\t l = "<<l;
size_t found = tempProductions[k].find(tempString.substr(1,l));
if(found!=string::npos){
//cout<<"\t=> Found => "<<tempString.substr(1,l)<<" At => "<<found;
//Find the Element in tempProduction
if(found==1){
//Element At The Beginning This is the case We're interested in
//cout<<"\n ND 2 "<<tempProductions[k].substr(1,l);
return 1;
}
}
}
}
}
}
}
return 0;
}
int exists(string s){
for(int m=0;m<deterministicProductions.size();m++){
if(s == deterministicProductions[m]){
return 1;
}
}
return 0;
}
int rectifyNonDeterminism(char c){
//return 0;
char newNonTerm = nonTermGen();
if(newNonTerm=='$'){
cout<<"Ran Out Of Non Terminals To Assign. Too Many Non Deterministic Entries.";
exit;
}
vector<string> tempProductions;
for(int j=0;j<productions.size();j++){
string tempString = productions[j];
if(productions[j][0]==c){
tempProductions.push_back(productions[j]);
}
}
//cout<<"\nChar => "<<c;
for(int j=0;j<tempProductions.size();j++){
string tempString = tempProductions[j];
//cout<<"\n\tTempString => "<<tempString;
int flag=0;
for(int k=0;k<tempProductions.size();k++){
//cout<<"\n\tTempProduction["<<k<<"] => "<<tempProductions[k];
if(tempString==tempProductions[k]){
//cout<<"\n Same";
continue;
}else{
//cout<<"\n\t\tDiffer";
if(tempString.length()>tempProductions[k].length()){
for(int l=tempProductions[k].length();l>1;l--){
size_t found = tempString.find(tempProductions[k].substr(1,l));
//cout<<"\n\t\tLooking For => "<<tempProductions[k].substr(1,l);
if(found!=string::npos){
//Find the Element in tempString
if(found==1){
flag=1;
//Element At The Beginning This is the case We're interested in
//cout<<"\n\t ND 1 Found => "<<tempProductions[k].substr(1,l)<<"\t l = "<<l;
if(!isInNonTerminal(newNonTerm)){
nonTerminals.push_back(newNonTerm);
}
string newProd1 = "";
newProd1+=c;
newProd1 += (" "+tempString.substr(1,l));
newProd1 += newNonTerm;
string newProd2 = "";
newProd2+=newNonTerm;
newProd2 += (" "+tempString.substr(l+1));
if(!exists(newProd1)){
//cout<<"\n\t\tnewProd1 => "<<newProd1;
deterministicProductions.push_back(newProd1);
}
if(!exists(newProd2)){
//cout<<"\n\t\tnewProd2 => "<<newProd2;
deterministicProductions.push_back(newProd2);
}
}
}
}
}else{
for(int l=tempString.length();l>1;l--){
size_t found = tempProductions[k].find(tempString.substr(1,l));
//cout<<"\n\t\tLooking For => "<<tempProductions[k].substr(1,l);
if(found!=string::npos){
//Find the Element in tempProduction
if(found==1){
flag=1;
//Element At The Beginning This is the case We're interested in
//cout<<"\n ND 2 Found => "<<tempString.substr(1,l)<<"\t l = "<<l;
if(!isInNonTerminal(newNonTerm)){
nonTerminals.push_back(newNonTerm);
}
string newProd1 = "";
newProd1+=c;
newProd1 += tempString.substr(1,l);
newProd1 += newNonTerm;
//cout<<"\n\tnewProd1 => "<<newProd1;
string newProd2 = "";
newProd2+=newNonTerm;
newProd2 += (" "+tempString.substr(l+1));
//cout<<"\n\tnewProd2 => "<<newProd2;
if(!exists(newProd1)){
//cout<<"\n\t\tnewProd1 => "<<newProd1;
deterministicProductions.push_back(newProd1);
}
if(!exists(newProd2)){
//cout<<"\n\t\tnewProd2 => "<<newProd2;
deterministicProductions.push_back(newProd2);
}
}
}
}
}
}
}
if(flag==0){
if(!exists(tempString)){
deterministicProductions.push_back(tempString);
}
}
}
return 1;
}
int printNDGrammar(){
cout<<"\nFINAL NON DETERMINISTIC FREE GRAMMAR IS : "<<endl;
//return 1;
for(int i=0;i<productions.size();i++){
cout<<productions[i]<<endl;
}
return 1;
}
int main(){
//Check For:
//Unambiguous -- undecidable
//Left Recursion
//Non Determinism
//Read All The Productions
readProductions();
int flag=0;
//NON DETERMINISM CHECK BEGINS
for(int i=0;i<nonTerminals.size();i++){
if(isNonDeterministic(nonTerminals[i])){
//cout<<"\nInside ND\n";
rectifyNonDeterminism(nonTerminals[i]);
clearProductions(nonTerminals[i]);
flag=1;
}else{
//cout<<"\nInside Dete\n ";
for(int j=0;j< productions.size();j++){
if(productions[j][0]==nonTerminals[i]){
deterministicProductions.push_back(productions[j]);
}
}
}
}
if(flag==0){
cout<<"Grammar Is Free Of Non Determinism";
}else{
productions = deterministicProductions;
printNDGrammar();
}
//NON DETERMINISM CHECK ENDS
cout<<"\nAll The Terminals : ";
for(int i=0;i<terminals.size();i++){
cout<<terminals[i]<<" ";
}
cout<<"\nAll The Non Terminals : ";
for(int i=0;i<nonTerminals.size();i++){
cout<<nonTerminals[i]<<" ";
}
cout<<"\n";
// cout<<"\n Productions : \n";
// for(int i=0;i<productions.size();i++){
// cout<<productions[i]<<endl;
// }
flag=0;
//LEFT RECURSION CHECK BEGINS
for(int i=0;i<nonTerminals.size();i++){
if(isLeftRecursive(nonTerminals[i])){
//Left Recursion Found
cout<<"Grammar Has Left Recursion In Production Of : "<<nonTerminals[i]<<endl;
rectifyLeftRecursion(nonTerminals[i]);
clearProductions(nonTerminals[i]);
flag=1;
}
}
if(flag==0){
cout<<"Grammar IsConverted free Of Left Recursion"<<endl;
}else{
printFinalGrammar();
productions = newProductions;
}
//LEFT RECURSION CHECK FINISH
flag=0;
return 1;
}