-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathSLR_parse_table.cpp
429 lines (376 loc) · 11.1 KB
/
SLR_parse_table.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
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
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
#include<iostream>
#include<string.h>
#include<stdlib.h>
#include<stdio.h>
using namespace std;
char terminals[100]={};int no_t;
char non_terminals[100]={};int no_nt;
char goto_table[100][100];
char reduce[20][20];
char follow[20][20];char fo_co[20][20];
char first[20][20];
struct state{
int prod_count;
char prod[100][100]={{}};
};
void add_dots(struct state *I){
for(int i=0;i<I->prod_count;i++){
for (int j=99;j>3;j--)
I->prod[i][j] = I->prod[i][j-1];
I->prod[i][3]='.';
}
}
void augument(struct state *S,struct state *I){
if(I->prod[0][0]=='S')
strcpy(S->prod[0],"Z->.S");
else{
strcpy(S->prod[0],"S->.");
S->prod[0][4]=I->prod[0][0];}
S->prod_count++;
}
void get_prods(struct state *I){
cout<<"Enter the number of productions:\n";
cin>>I->prod_count;
cout<<"Enter the number of non terminals:"<<endl;
cin>>no_nt;
cout<<"Enter the non terminals one by one:"<<endl;
for(int i=0;i<no_nt;i++)
cin>>non_terminals[i];
cout<<"Enter the number of terminals:"<<endl;
cin>>no_t;
cout<<"Enter the terminals (single lettered) one by one:"<<endl;
for(int i=0;i<no_t;i++)
cin>>terminals[i];
cout<<"Enter the productions one by one in form (S->ABc):\n";
for(int i=0;i<I->prod_count;i++){
cin>>I->prod[i];
}
}
bool is_non_terminal(char a){
if (a >= 'A' && a <= 'Z')
return true;
else
return false;
}
bool in_state(struct state *I,char *a){
for(int i=0;i<I->prod_count;i++){
if(!strcmp(I->prod[i],a))
return true;
}
return false;
}
char char_after_dot(char a[100]){
char b;
for(int i=0;i<strlen(a);i++)
if(a[i]=='.'){
b=a[i+1];
return b;}
}
char* move_dot(char b[100],int len){
char a[100]={};
strcpy(a,b);
for(int i=0;i<len;i++){
if(a[i]=='.'){
swap(a[i],a[i+1]);
break;
}
}
return &a[0];
}
bool same_state(struct state *I0,struct state *I){
if (I0->prod_count != I->prod_count)
return false;
for (int i=0; i<I0->prod_count; i++)
{
int flag = 0;
for (int j=0; j<I->prod_count; j++)
if (strcmp(I0->prod[i], I->prod[j]) == 0)
flag = 1;
if (flag == 0)
return false;
}
return true;
}
void closure(struct state *I,struct state *I0){
char a={};
for(int i=0;i<I0->prod_count;i++){
a=char_after_dot(I0->prod[i]);
if(is_non_terminal(a)){
for(int j=0;j<I->prod_count;j++){
if(I->prod[j][0]==a){
if(!in_state(I0,I->prod[j])){
strcpy(I0->prod[I0->prod_count],I->prod[j]);
I0->prod_count++;
}
}
}
}
}
}
void goto_state(struct state *I,struct state *S,char a){
int time=1;
for(int i=0;i<I->prod_count;i++){
if(char_after_dot(I->prod[i])==a){
if(time==1){
time++;
}
strcpy(S->prod[S->prod_count],move_dot(I->prod[i],strlen(I->prod[i])));
S->prod_count++;
}
}
}
void print_prods(struct state *I){
for(int i=0;i<I->prod_count;i++)
printf("%s\n",I->prod[i]);
cout<<endl;
}
bool in_array(char a[20],char b){
for(int i=0;i<strlen(a);i++)
if(a[i]==b)
return true;
return false;
}
char* chars_after_dots(struct state *I){
char a[20]={};
for(int i=0;i<I->prod_count;i++){
if(!in_array(a,char_after_dot(I->prod[i]))){
a[strlen(a)]=char_after_dot(I->prod[i]);
}
}
return &a[0];
}
void cleanup_prods(struct state * I){
char a[100]={};
for(int i=0;i<I->prod_count;i++)
strcpy(I->prod[i],a);
I->prod_count=0;
}
int return_index(char a){
for(int i=0;i<no_t;i++)
if(terminals[i]==a)
return i;
for(int i=0;i<no_nt;i++)
if(non_terminals[i]==a)
return no_t+i;
}
void print_shift_table(int state_count){
cout<<endl<<"********Shift Actions*********"<<endl<<endl;
cout<<"\t";
for(int i=0;i<no_t;i++)
cout<<terminals[i]<<"\t";
for(int i=0;i<no_nt;i++)
cout<<non_terminals[i]<<"\t";
cout<<endl;
for(int i=0;i<state_count;i++){
int arr[no_nt+no_t]={-1};
for(int j=0;j<state_count;j++){
if(goto_table[i][j]!='~'){
arr[return_index(goto_table[i][j])]= j;
}
}
cout<<"I"<<i<<"\t";
for(int j=0;j<no_nt+no_t;j++){
if(i==1&&j==no_t-1)
cout<<"ACC"<<"\t";
if(arr[j]==-1||arr[j]==0)
cout<<"\t";
else{
if(j<no_t)
cout<<"S"<<arr[j]<<"\t";
else
cout<<arr[j]<<"\t";
}
}
cout<<"\n";
}
}
int get_index(char c,char *a){
for(int i=0;i<strlen(a);i++)
if(a[i]==c)
return i;
}
void add_dot_at_end(struct state* I){
for(int i=0;i<I->prod_count;i++){
strcat(I->prod[i],".");
}
}
void add_to_first(int n,char b){
for(int i=0;i<strlen(first[n]);i++)
if(first[n][i]==b)
return;
first[n][strlen(first[n])]=b;
}
void add_to_first(int m,int n){
for(int i=0;i<strlen(first[n]);i++){
int flag=0;
for(int j=0;j<strlen(first[m]);j++){
if(first[n][i]==first[m][j])
flag=1;
}
if(flag==0)
add_to_first(m,first[n][i]);
}
}
void add_to_follow(int n,char b){
for(int i=0;i<strlen(follow[n]);i++)
if(follow[n][i]==b)
return;
follow[n][strlen(follow[n])]=b;
}
void add_to_follow(int m,int n){
for(int i=0;i<strlen(follow[n]);i++){
int flag=0;
for(int j=0;j<strlen(follow[m]);j++){
if(follow[n][i]==follow[m][j])
flag=1;
}
if(flag==0)
add_to_follow(m,follow[n][i]);
}
}
void add_to_follow_first(int m,int n){
for(int i=0;i<strlen(first[n]);i++){
int flag=0;
for(int j=0;j<strlen(follow[m]);j++){
if(first[n][i]==follow[m][j])
flag=1;
}
if(flag==0)
add_to_follow(m,first[n][i]);
}
}
void find_first(struct state *I){
for(int i=0;i<no_nt;i++){
for(int j=0;j<I->prod_count;j++){
if(I->prod[j][0]==non_terminals[i]){
if(!is_non_terminal(I->prod[j][3])){
add_to_first(i,I->prod[j][3]);
}
}
}
}
}
void find_follow(struct state *I){
for(int i=0;i<no_nt;i++){
for(int j=0;j<I->prod_count;j++){
for(int k=3;k<strlen(I->prod[j]);k++){
if(I->prod[j][k]==non_terminals[i]){
if(I->prod[j][k+1]!='\0'){
if(!is_non_terminal(I->prod[j][k+1])){
add_to_follow(i,I->prod[j][k+1]);
}
}
}
}
}
}
}
int get_index(int *arr,int n){
for(int i=0;i<no_t;i++){
if(arr[i]==n)
return i;
}
return -1;
}
void print_reduce_table(int state_count,int *no_re,struct state *temp1){
cout<<"**********Reduce actions**********"<<endl<<endl;
cout<<"\t";
int arr[temp1->prod_count][no_t]={-1};
for(int i=0;i<no_t;i++){
cout<<terminals[i]<<"\t";
}
cout<<endl;
for(int i=0;i<temp1->prod_count;i++){
int n=no_re[i];
for(int j=0;j<strlen(follow[return_index(temp1->prod[i][0])-no_t]);j++){
for(int k=0;k<no_t;k++){
if(follow[return_index(temp1->prod[i][0])-no_t][j]==terminals[k])
arr[i][k]=i+1;
}
}
cout<<"I"<<n<<"\t";
for(int j=0;j<no_t;j++){
if(arr[i][j]!=-1&&arr[i][j]!=0&&arr[i][j]<state_count)
cout<<"R"<<arr[i][j]<<"\t";
else
cout<<"\t";
}
cout<<endl;
}
}
int main(){
struct state init;
struct state temp;struct state temp1;
int state_count=1;
get_prods(&init);
temp=init;
temp1=temp;
add_dots(&init);
for(int i=0;i<100;i++)
for(int j=0;j<100;j++)
goto_table[i][j]='~';
struct state I[50];
augument(&I[0],&init);
closure(&init,&I[0]);
cout<<"\nI0:\n";
print_prods(&I[0]);
char characters[20]={};
for(int i=0;i<state_count;i++){
char characters[20]={};
for(int z=0;z<I[i].prod_count;z++)
if(!in_array(characters,char_after_dot(I[i].prod[z])))
characters[strlen(characters)]=char_after_dot(I[i].prod[z]);
for(int j=0;j<strlen(characters);j++){
goto_state(&I[i],&I[state_count],characters[j]);
closure(&init,&I[state_count]);
int flag=0;
for(int k=0;k<state_count-1;k++){
if(same_state(&I[k],&I[state_count])){
cleanup_prods(&I[state_count]);flag=1;
cout<<"I"<<i<<" on reading the symbol "<<characters[j]<<" goes to I"<<k<<".\n";
goto_table[i][k]=characters[j];;
break;
}
}
if(flag==0){
state_count++;
cout<<"I"<<i<<" on reading the symbol "<<characters[j]<<" goes to I"<<state_count-1<<":\n";
goto_table[i][state_count-1]=characters[j];
print_prods(&I[state_count-1]);
}
}
}
int no_re[temp.prod_count]={-1};
terminals[no_t]='$';no_t++;
add_dot_at_end(&temp1);
for(int i=0;i<state_count;i++){
for(int j=0;j<I[i].prod_count;j++)
for(int k=0;k<temp1.prod_count;k++)
if(in_state(&I[i],temp1.prod[k]))
no_re[k]=i;
}
find_first(&temp);
for(int l=0;l<no_nt;l++){
for(int i=0;i<temp.prod_count;i++){
if(is_non_terminal(temp.prod[i][3])){
add_to_first(return_index(temp.prod[i][0])-no_t,return_index(temp.prod[i][3])-no_t);
}
}}
find_follow(&temp);
add_to_follow(0,'$');
for(int l=0;l<no_nt;l++){
for(int i=0;i<temp.prod_count;i++){
for(int k=3;k<strlen(temp.prod[i]);k++){
if(temp.prod[i][k]==non_terminals[l]){
if(is_non_terminal(temp.prod[i][k+1])){
add_to_follow_first(l,return_index(temp.prod[i][k+1])-no_t);}
if(temp.prod[i][k+1]=='\0')
add_to_follow(l,return_index(temp.prod[i][0])-no_t);
}
}
}
}
print_shift_table(state_count);
cout<<endl<<endl;
print_reduce_table(state_count,&no_re[0],&temp1);
}