-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredictiveparsing.c
105 lines (103 loc) · 2.09 KB
/
predictiveparsing.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
char table[10][10] = {"NT", "a", "b", "A", "aBa", "Error", "B", "@", "bB"};
char buffer[10], stack[10];
int top = -1;
char pop()
{
return stack[top--];
}
void push(int e)
{
stack[++top] = e;
}
void display_stack()
{
int i = top;
while (i >= 0)
{
printf("%c", stack[i]);
i--;
}
printf("\n");
}
char *parse_table(char stack_top, char input_val)
{
switch (stack_top)
{
case 'A':
switch (input_val)
{
case 'a':
return table[4];
case 'b':
return table[5];
}
break;
case 'B':
switch (input_val)
{
case 'a':
return table[7];
case 'b':
return table[8];
}
default:
return table[5];
}
}
int main()
{
int ptr = 0, i = 0, j, k, w = 0;
char *str;
for (j = 0; j < 3; j++)
{
for (k = 0; k < 3; k++)
{
printf("%s\t", table[w++]);
}
printf("\n");
}
printf("Enter string\n");
scanf("%s", buffer);
if (buffer[strlen(buffer) - 1] != ';')
{
printf("\nString should end with:");
exit(0);
}
push('$');
push('A');
while (stack[top] != '$' && (ptr < strlen(buffer)))
{
if (stack[top] == buffer[ptr])
{
ptr++;
printf("1.Element popped is %c\n", pop());
}
else if (stack[top] == '@')
{
printf("2.Element popped is %c\n", pop());
}
else
{
str = parse_table(stack[top], buffer[ptr]);
if (strcmp(str, "Error") == 0)
{
printf("Error in parsing\n");
break;
}
printf("3.Element popped is %c\n", pop());
for (i = strlen(str) - 1; i >= 0; i--)
push(*(str + i));
}
display_stack();
}
if (stack[top] == '$' && buffer[ptr] == ';')
printf("String is accepted\n");
else
{
printf("String is no accepted\n");
}
return 0;
}