forked from rounakdatta/15CS314J
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathleftFactoring.cpp
61 lines (52 loc) · 1.45 KB
/
leftFactoring.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
#include <iostream>
#include <cstring>
#include <vector>
using namespace std;
string getString(char x) {
string s(1, x);
return s;
}
int main() {
int t;
printf("Enter the number of test cases: ");
cin >> t;
for (int u = 0; u < t; u++) {
int n;
printf("\nEnter the no of productions: ");
cin >> n;
string lhs;
printf("\nEnter LHS: ");
cin >> lhs;
vector <string> productions;
for (int i = 0; i < n; i++) {
string foo;
cin >> foo;
productions.push_back(foo);
}
int counter = 3;
string commonFactor = "";
while (productions[0][counter] == productions[1][counter]) {
commonFactor += productions[0][counter];
counter += 1;
}
string newProduction1 = lhs + "->" + commonFactor + lhs + "\'";
for (int i = 2; i < n; i++) {
newProduction1 += ("|" + productions[i].substr(3));
}
cout << newProduction1 << endl;
string newProduction2 = lhs + "\'->";
for (int i = 0; i < n; i++) {
string bar;
try {
bar = productions[i].substr(counter);
if (bar == "") {
bar = "ε";
}
} catch(...) {
bar = "";
}
newProduction2 += (bar + "|");
}
cout << newProduction2 << endl;
}
}