-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsrp.c
142 lines (139 loc) · 4.08 KB
/
srp.c
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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char ip[30], stack[30], temp[15], action[15];
int len, iptr = 0, sptr = 0, i;
void check()
{
int flag = 0;
while (1)
{
if (stack[sptr] == 'd' && stack[sptr - 1] == 'i')
{
stack[sptr - 1] = 'F';
stack[sptr] = '\0';
sptr--;
flag = 1;
printf("\n$%s\t\t%s$\t\tReduce F->id", stack, ip);
}
if (stack[sptr] == ')' && stack[sptr - 1] == 'E' && stack[sptr - 2] == '(')
{
stack[sptr - 2] = 'F';
stack[sptr - 1] = '\0';
sptr -= 2;
flag = 1;
printf("\n$%s\t\t%s$\t\tReduce F->(E)", stack, ip);
}
if (stack[sptr] == 'F' && stack[sptr - 1] == '*' && stack[sptr - 2] == 'T')
{
stack[sptr - 1] = '\0';
sptr -= 2;
flag = 1;
printf("\n$%s\t\t%s$\t\tReduce T->T*F", stack, ip);
}
else if (stack[sptr] == 'F')
{
stack[sptr] = 'T';
flag = 1;
printf("\n$%s\t\t%s$\t\tReduce T->F", stack, ip);
}
if (stack[sptr] == 'T' && stack[sptr - 1] == '+' && stack[sptr - 2] == 'E' && ip[iptr] != '*')
{
stack[sptr - 1] = '\0';
sptr -= 2;
flag = 1;
printf("\n$%s\t\t%s$\t\tReduce E->E+T", stack, ip);
}
else if ((stack[sptr] == 'T' && ip[iptr] == ')') || (stack[0] == 'T' && ip[iptr] == '\0') || (stack[sptr] == 'T' && ip[iptr] == '+'))
{
stack[sptr] = 'E';
flag = 1;
printf("\n$%s\t\t%s$\t\tReduce E->T", stack, ip);
}
if ((stack[sptr] == 'T' && ip[iptr] == '*') || (stack[sptr] == 'E' && ip[iptr] == '+') || (stack[sptr] == 'E' && ip[iptr] == ')') ||
(stack[sptr] == '(' && ip[iptr] == 'i' && ip[iptr + 1] == 'd') || (stack[sptr] == '(' && ip[iptr] == '(') ||
(stack[sptr] == '+' && ip[iptr] == 'i' && ip[iptr + 1] == 'd') || (stack[sptr] == '+' && ip[iptr] == '(') ||
(stack[sptr] == '*' && ip[iptr] == 'i' && ip[iptr + 1] == 'd') || (stack[sptr] == '*' && ip[iptr] == '('))
{
flag = 2;
}
if (!strcmp(stack, "E") && ip[iptr] == '\0')
{
printf("\n$%s\t\t%s$\t\tACCEPT", stack, ip);
exit(0);
}
if (flag == 0)
{
printf("\n$%s\t\t%s$\t\tREJECT", stack, ip);
exit(0);
}
if (flag == 2)
return;
flag = 0;
}
}
int main()
{
printf("\t\t\tSHIFT RECDUCE PARSING\t\t\n\n");
printf("Grammar\n");
printf("E->E+T|T\nT->T*F|F\nF->( E )|id\n");
printf("Enter the input string\n");
scanf("%s", ip);
printf("\n\nSTACK Implementation Table\n");
printf("\nSTACK\t\tINPUT\t\tACTION\n");
printf("_______\t\t_____\t\t______\n");
printf("\n$\t\t%s$\t\t--");
strcpy(action, "Shift ");
if (ip[iptr] == '(')
{
temp[0] = ip[iptr];
temp[1] = '\0';
}
else
{
temp[0] = ip[iptr];
temp[1] = ip[iptr + 1];
temp[2] = '\0';
}
strcat(action, temp);
len = strlen(ip);
for (i = 0; i < len; i++)
{
if (ip[iptr] == 'i' && ip[iptr + 1] == 'd')
{
stack[sptr] = ip[iptr];
sptr++;
ip[iptr] = ' ';
iptr++;
stack[sptr] = ip[iptr];
stack[sptr + 1] = '\0';
ip[iptr] = ' ';
iptr++;
}
else
{
stack[sptr] = ip[iptr];
stack[sptr + 1] = '\0';
ip[iptr] = ' ';
iptr++;
}
printf("\n$%s\t\t%s$\t\t%s", stack, ip, action);
strcpy(action, "Shift ");
if (ip[iptr] == '(' || ip[iptr] == ')' || ip[iptr] == '+' || ip[iptr] == '*')
{
temp[0] = ip[iptr];
temp[1] = '\0';
}
else
{
temp[0] = ip[iptr];
temp[1] = ip[iptr + 1];
temp[2] = '\0';
}
strcat(action, temp);
check();
sptr++;
}
sptr++;
check();
}