-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcfg2pda.cpp
76 lines (63 loc) · 2.19 KB
/
cfg2pda.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
#include <bits/stdc++.h>
using namespace std;
int main() {
int n;
cout << "Enter the number of Production Rules of CFG: ";
cin >> n;
string s1;
string s2;
vector<pair<string, string>> v;
cout << "Please enter the Production rules of CFG:\n";
for (int i = 0; i < n; i++) {
cin >> s1 >> s2;
v.push_back(make_pair(s1, s2));
}
set<string> terminalSymbols;
set<string> nonTerminalSymbols;
for (int i = 0; i < n; i++) {
string nonTerminal = v[i].first;
nonTerminalSymbols.insert(nonTerminal); // Populate nonTerminalSymbols
string production = v[i].second;
// Split the production string by '|' to handle multiple production rules
istringstream productionStream(production);
string rule;
while (getline(productionStream, rule, '|')) {
for (char ch : rule) {
if (isalpha(ch) && isupper(ch)) {
nonTerminalSymbols.insert(string(1, ch));
} else if (isalnum(ch)) {
terminalSymbols.insert(string(1, ch));
}
}
}
if (production == "#") {
terminalSymbols.insert("#"); // Use '#' to represent epsilon
}
}
cout << "The Corresponding Production Rules For PDA are:" << endl;
cout << "Rules For Non-Terminal Symbols are:" << endl;
for (const string &nonTerminal : nonTerminalSymbols) {
int flag = 0;
cout << "dl(q,null," << nonTerminal << ") --> ";
for (int i = 0; i < n; i++) {
if (v[i].first == nonTerminal) {
if (flag == 1) {
cout << " | ";
}
cout << "dl(q," << v[i].second << ")";
flag = 1;
}
}
if (flag != 0) {
cout << endl;
}
}
cout << "Rules For Terminal Symbols are:" << endl;
for (const string &terminal : terminalSymbols) {
if (terminal == "#") {
cout << "dl(q,null,null) --> dl(q,null)" << endl; // Handle epsilon production
} else {
cout << "dl(q," << terminal << "," << terminal << ") --> dl(q,null)" << endl;
}
}
}