-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathll1parser.cpp
More file actions
292 lines (278 loc) · 6.81 KB
/
ll1parser.cpp
File metadata and controls
292 lines (278 loc) · 6.81 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
/**
Author - Anant Kumar Singh
Description : Reads a given context free grammar
from a text file and build a LL(1) parser
out of it by computing first and follow
sets to check whether the input
string can be parsed from given
grammar or not.
Grammar example : S -> TG
G -> +TG
G -> ^
T -> FU
U -> *FU|^
F -> (S)|e
*/
#include <iostream>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <fstream>
#include <stack>
using namespace std;
//util function to prevent repetitions while inserting
void uniqueInsert(vector<char> &v, char c){
for(int i=0;i<v.size();++i)
if(v[i]==c)return;
v.push_back(c);
}
void printSet(const set<char> &s,bool newline = 1){
set < char > :: iterator ft;
for(ft=s.begin();ft!=s.end();++ft)
cout<<*ft<<' ';
if(s.size()>0 && newline == 1)cout<<endl;
}
// returns true when first(V) contains ε / ^
// false otherwise
bool First(char V, map<char,vector<string> >& gr, map<char,vector<char> >& F){
if(F[V].size() > 0){
for(int i=0;i<F[V].size();++i)if(F[V][i]=='^')return 1;
return 0;
}
if(!isupper(V)){
F[V].push_back(V);
return 0;
}
//else
int prods = gr[V].size();
bool hasnull = 0;
for(int p=0;p < prods;++p){
if(gr[V][p] == "^"){
F[V].push_back('^');
hasnull = 1;
}
else{
int len = gr[V][p].length();
for(int i=0;i<len;++i){
char C = gr[V][p][i];
bool N = First(C,gr,F);
// add elements of First(C) in First(V)
for(int j = 0; j < F[C].size(); ++j)
uniqueInsert(F[V],F[C][j]);
// if null is in First(C) continue looking for next symbol
if(N)continue;
else break;
}
}
}
if(F[V].size() == 0){
F[V].push_back('^');
hasnull = 1;
}
return hasnull;
}
// finds follow of V by searching in all of the grammar
void Follow(char V, map<char,vector<string> >& gr, map<char,vector<char> >& first, map<char,set<char> >& follow){
map <char,vector < string > >:: iterator it,it2;
set < char > :: iterator ft;
for(it = gr.begin();it!=gr.end();++it){
for(int p=0; p < it->second.size();++p){
int len = it->second[p].size();
string s = it->second[p];
char next;
for(int i=0;i<len;++i){
if(s[i]==V){
g:;
bool hasnull = 0;
if(i+1<len){
next = s[i+1];
if(isupper(next)!=0){
// insert first of s[i+1]
for(int j=0;j<first[next].size();++j)
if(first[next][j] == '^') hasnull = 1;
else follow[V].insert(first[next][j]);
++i;
if(hasnull) goto g;
}
else
follow[V].insert(next);
}
// last in production
else
for(ft=follow[it->first].begin(); ft != follow[it->first].end(); ++ft)
if(*ft != '^')
follow[V].insert(*ft);
}
}
}
}
}
vector<char> firstOfString(map<char,vector<char> >& first, string prod){
vector<char> ans;
int len = prod.length();
bool hadnull = 1;
for(int i=0;i<len;++i){
if(!hadnull) break;
//else
hadnull = 0;
if(isupper(prod[i])){
for(int j=0;j<first[prod[i]].size();++j){
if(first[prod[i]][j]=='^')hadnull=1;
uniqueInsert(ans,first[prod[i]][j]);
}
}
else
uniqueInsert(ans,prod[i]);
}
return ans;
}
#define pcc pair<char,char>
void createParseTable(map<pcc,string >&ptable, map <char,vector<string> >& gr, map<char,vector<char> >& first, map<char,set<char> >& follow){
for(map <char,vector < string > >:: iterator it = gr.begin();it!=gr.end();++it){
int n = it->second.size();
for(int p=0;p<n;++p){
vector<char> v = firstOfString(first,it->second[p]);
bool null = 0;
for(int i=0;i<v.size();++i)
if(v[i]!='^')
ptable[make_pair(it->first,v[i])]=it->second[p];
else null=1;
if(null){
set < char > :: iterator ft, z = follow[it->first].end();
for(ft=follow[it->first].begin();ft!=z;++ft)
ptable[make_pair(it->first,*ft)]=it->second[p];
}
}
}
}
void print(map <char,vector < string > >& gr, map<char,vector<char> >& firstMap, map<char,set<char> >& followMap){
// Printing grammar
map <char,vector < string > >:: iterator it;
map <char,vector < char > > :: iterator it1;
for(it = gr.begin();it!=gr.end();++it){
cout<<it->first<<" --> ";
int n = it->second.size();
for(int i=0;i<n;++i)
cout<<it->second[i]<< "\t";
cout<<endl;
}
cout<<endl;
// Printing First
for(it=gr.begin();it!=gr.end();it++){
cout<<"First("<<it->first<<") = { ";
it1=firstMap.find(it->first);
int sz = it1->second.size();
for(int i=0;i<sz-1;++i) cout<<it1->second[i]<< " , ";
cout<<it1->second[sz-1]<<' ';
cout<<"}"<<endl;
}
cout<<endl;
// Printing Follow
for(it=gr.begin();it!=gr.end();it++){
cout<<"Follow("<<it->first<<") = { ";
printSet(followMap[it->first],0);
cout<<"}"<<endl;
}
}
void reversePush(string s, stack<char> &st){
int len = s.length();
for(int i=len-1;i>=0;--i)
st.push(s[i]);
}
bool parse(string s, char start, map<pcc,string> & ptable){
stack<char>st;
st.push('$');
st.push(start);
s+="$";
int i=0,len = s.length();
while(i<len)
if(isupper(st.top())){
char Top = st.top();
if(ptable[make_pair(Top,s[i])]=="")return 0;
st.pop();
if(ptable[make_pair(Top,s[i])] != "^")
reversePush(ptable[make_pair(Top,s[i])],st);
}
else
if(s[i]!=st.top())return 0;
else{
st.pop();
++i;
}
return 1;
}
int main(int argc,char * argv[]){
ifstream G;
G.open(argv[1]);
string prod,x,a;
map <char,vector < string > > gr;
map <char,vector < char > > firstMap;
map <char,set < char > > followMap;
map <char,vector < string > > :: iterator it;
char c;
int n,st,flag=0;
if(G.is_open()){
while(!G.eof()){
G>>c;
if(isupper(c)){
G>>x;
if( x == "->" ){
G>>prod;
n=prod.size();st=0;
// cout<<n<<'_'<<prod<<endl;
while(st<n){
a="";
while(st<n && prod[st]!='|'){
a += prod[st++];
}
st++;
gr[c].push_back(a);
}
}
else{
cout<<"grammer is not correct"<<endl;
flag=1;
break;
}
}
else if(c=='_')
break;
else{
cout<<"grammer is not correct"<<endl;
flag=1;
break;
}
}
}
else{
cout<<"File does not exist"<<endl;
flag=1;
}
char start = gr.begin()->first;
G.close();
if(flag) return 0;
// Find first of all non terminals
for(it=gr.begin();it!=gr.end();it++) First(it->first,gr,firstMap);
// Find follow of all non terminals
followMap[start].insert('$');
int times = 2;
while(times--)
for(it=gr.begin();it!=gr.end();++it)
Follow(it->first,gr,firstMap,followMap);
// Printing grammar, first and follow
print(gr,firstMap,followMap);
//Parse Table construction
map<pair<char,char>,string> parseTable;
createParseTable(parseTable,gr,firstMap,followMap);
cout<<"Enter number of strings to be checked : ";
int tests;
cin>>tests;
while(tests--){
cout<<"Enter the string to be checked : ";
string in;
cin>>in;
if(parse(in,start,parseTable))cout<<"Input string parsed successfully!\n";
else cout<<"Input string cannot be parsed!\n";
}
}