-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpp-str.cpp
More file actions
161 lines (144 loc) · 3.89 KB
/
pp-str.cpp
File metadata and controls
161 lines (144 loc) · 3.89 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;
};
bool allnotvisited(vector<bool> visited){
for(int i=0; i<visited.size(); i++){
if(visited[i] == false)
return true;
}
return false;
}
string printstack(stack<char> st){
string s = "";
while(!st.empty()){
s = st.top() + s;
st.pop();
}
return s;
}
bool checkstring(char start, string in, map<char, map<char, string> > pars_table){
stack<char> st;
st.push('$');
st.push(start);
int i=0;
cout << "$" << start << " \t::\t " << in << endl;
while(i < in.length()){
if(st.top() == '$' && in[i] == '$'){
return true;
}
else if(st.top() == '$'){
return false;
}
else if(st.top() == in[i]){
i++;
st.pop();
}
else if( pars_table[st.top()].find(in[i]) != pars_table[st.top()].end()){
if(pars_table[st.top()][in[i]] == "#"){
st.pop();
}
else{
string s = pars_table[st.top()][in[i]] ;
st.pop();
for(int j=s.length()-1; j>-1; j-- ){
st.push(s[j]);
}
}
}
else{
return false;
}
cout << printstack(st) << " \t::\t " << in.substr(i, in.length()-i) << endl;
}
return true;
}
int main(){
int p=8;
int ch;
char start;
vector<Prod> Pr(p);
map<char, vector<char> > first;
map<char, vector<char> > follow;
map<char, map<char, string> > pars_table;
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'] = { '+', '$', ')' };
pars_table = {
{'E', { {'(', "TX"}, {'i', "TX"}, {'}', "TX"} }},
{'F', { {'(', "(E)"}, {'i', "i"} }},
{'T', { {'(', "FY"}, {'i', "FY"}, {'}', "FY"} }},
{'X', { {'$', "#"}, {')', "#"}, {'+', "+TX"} }},
{'Y', { {'$', "#"}, {')', "#"}, {'*', "*FY"}, {'+', "#"} }}
};
cout << "Productions: " << endl;
for(int i=0;i<p;i++)
{
cout<<Pr[i].left<<"->"<<Pr[i].right<<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;
}
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;
}
string in;
cout << "Enter the input string: ";
cin >> in;
in += '$';
if(checkstring(start, in, pars_table)){
printf("String is accepted..");
}
else
printf("String is not accepted...");
return 0;
}