-
Notifications
You must be signed in to change notification settings - Fork 10
/
6_ll1_parsing.c
93 lines (90 loc) · 2.11 KB
/
6_ll1_parsing.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
#include <stdio.h>
#include <string.h>
#include <process.h>
char s[20], stack[20];
int main()
{
char m[5][6][4] = {"tb", " ", " ", "tb", " ", " ", " ", "+tb", " ", " ", "n", "n", "fc", " ", " ", "fc", " ", " ", " ", "n", "*fc", " a", "n", "n", "i", " ", " ", "(e)", " ", " "};
int size[5][6] = {2, 0, 0, 2, 0, 0, 0, 3, 0, 0, 1, 1, 2, 0, 0, 2, 0, 0, 0, 1, 3, 0, 1, 1, 1, 0, 0, 3, 0, 0};
int i, j, k, n, str1, str2;
printf("Enter the input string: ");
scanf("%s", s);
strcat(s, "$");
n = strlen(s);
stack[0] = '$';
stack[1] = 'e';
i = 1;
j = 0;
printf("Stack\t\tInput\n");
printf("_________________________\n");
while ((stack[i] != '$') && (s[j] != '$'))
{
if (stack[i] == s[j])
{
i--;
j++;
}
switch (stack[i])
{
case 'e':
str1 = 0;
break;
case 'b':
str1 = 1;
break;
case 't':
str1 = 2;
break;
case 'c':
str1 = 3;
break;
case 'f':
str1 = 4;
break;
}
switch (s[j])
{
case 'i':
str2 = 0;
break;
case '+':
str2 = 1;
break;
case '*':
str2 = 2;
break;
case '(':
str2 = 3;
break;
case ')':
str2 = 4;
break;
case '$':
str2 = 5;
break;
}
if (m[str1][str2][0] == '\0')
{
printf("\nERROR");
exit(0);
}
else if (m[str1][str2][0] == 'n')
i--;
else if (m[str1][str2][0] == 'i')
stack[i] = 'i';
else
{
for (k = size[str1][str2] - 1; k >= 0; k--)
stack[i++] = m[str1][str2][k];
i--;
}
for (k = 0; k <= i; k++)
printf("%c", stack[k]);
printf("\t\t");
for (k = j; k <= n; k++)
printf("%c", s[k]);
printf("\n");
}
printf("\nSUCCESS");
return 0;
}