-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathppr.cpp
More file actions
161 lines (147 loc) · 4.87 KB
/
ppr.cpp
File metadata and controls
161 lines (147 loc) · 4.87 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
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
#include <bits/stdc++.h>
using namespace std;
struct Prod{
char left;
string right;
};
map<char, map<char, string> > generateparsingtable(map<char, vector<char> > first, map<char, vector<char> > follow, struct Prod Pr[], int p){
map<char, map<char, string> > pars_table;
for(int i=0; i<p; i++){
string s = Pr[i].right;
if((Pr[i].right[0] < 'A' || Pr[i].right[0] > 'Z' ) && Pr[i].right[0] != '#'){
//cout << s << endl;
pars_table[Pr[i].left][Pr[i].right[0]] = s;
}
if(Pr[i].right[0] >= 'A' && Pr[i].right[0] <= 'Z'){
//cout << s << endl;
vector<char> temp = first[Pr[i].right[0]];
for(int c=0; c<temp.size(); c++){
pars_table[Pr[i].left][temp[c]] = s;
}
}
if(Pr[i].right[0] == '#'){
vector<char> temp = follow[Pr[i].left];
//cout << s << endl;
for(int c=0; c<temp.size(); c++){
pars_table[Pr[i].left][temp[c]] = s;
}
}
}
return pars_table;
}
int main(){
int p;
int ch;
char start;
struct Prod Pr[p];
map<char, vector<char> > first;
map<char, vector<char> > follow;
cout << "Enter 1 for user input and 2 for default input: ";
cin >> ch;
switch(ch){
case 1:
cout << "Enter starting symbol: ";
cin >> start;
cout << "Enter the no of Productions: ";
cin >> p;
cout << "Enter the Productions: ";
for(int i=0; i<p; i++){
cin >> Pr[i].left;
cin >> Pr[i].right;
}
int n;
for(int i=0;i<n;i++){
char nonTerminal;
cout << "Enter Non-terminal: ";
cin >> nonTerminal;
vector<char> firstSet;
vector<char> followSet;
cout << "Enter the no of elements in first set: ";
cin >> n;
cout << "Enter the elements of first set: ";
for(int i=0; i<n; i++){
char c;
cin >> c;
firstSet.push_back(c);
}
first[nonTerminal] = firstSet;
cout << "Enter the no of elements in follow set: ";
cin >> n;
cout << "Enter the elements of follow set: ";
for(int i=0; i<n; i++){
char c;
cin >> c;
followSet.push_back(c);
}
follow[nonTerminal] = followSet;
}
break;
case 2:
p = 8;
start='E';
Pr[0].left = 'E';
Pr[0].right = "TX";
Pr[1].left = 'X';
Pr[1].right = "+TX";
Pr[2].left = 'X';
Pr[2].right = "#";
Pr[3].left = 'T';
Pr[3].right = "FY";
Pr[4].left = 'Y';
Pr[4].right = "*FY";
Pr[5].left = 'Y';
Pr[5].right = "#";
Pr[6].left = 'F';
Pr[6].right = "(E)";
Pr[7].left = 'F';
Pr[7].right = "i";
first['E'] = { '(', 'i', '}' };
first['F'] = { '(', 'i', '}' };
first['T'] = { '(', 'i', '}' };
first['X'] = { '+', '#', '}' };
first['Y'] = { '*', '#', '}' };
follow['E'] = { '$', ')' };
follow['F'] = { '*', '+', '$', ')' };
follow['T'] = { '+', '$', ')' };
follow['X'] = { '$', ')' };
follow['Y'] = { '+', '$', ')' };
break;
}
cout << "Productions: " << endl;
for(int i=0;i<p;i++)
{
cout<<Pr[i].left<<"->"<<Pr[i].right<<endl;
}
cout << "First sets: " << endl;
for(map<char, vector<char> >::iterator it = first.begin(); it != first.end(); it++){
cout << it->first << " = { ";
for(int i=0; i<it->second.size(); i++){
cout << it->second[i] << ", ";
}
cout << " }" << endl;
}
cout<<endl;
cout << "Follow sets: " << endl;
for(map<char, vector<char> >::iterator it = follow.begin(); it != follow.end(); it++){
cout << it->first << " = { ";
for(int i=0; i<it->second.size(); i++){
cout << it->second[i] << ", ";
}
cout << " }" << endl;
}
map<char, map<char, string> > pars_table = generateparsingtable(first, follow, Pr, p);
cout << "\nParsing Table: "<<endl;
for(map<char, map<char, string> >::iterator it = pars_table.begin(); it != pars_table.end(); it++){
cout << it->first << "\t";
map <char, string> temp = it->second;
for( map<char, string>::iterator it2 = temp.begin(); it2 != temp.end(); it2++){
cout << it2->first << " : " << it2->second << "\t";
}
cout << endl;
}
return 0;
}