-
Notifications
You must be signed in to change notification settings - Fork 0
/
prog3.y
153 lines (130 loc) · 2.73 KB
/
prog3.y
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
#include <string.h>
#include <stdio.h>
#include <stdlib.h>
char STACK[20]="\0";
int TOP=-1,flag=0;
int B_ptr = 0;
char BUFFER[20],G_prod[20];
char table [3][3][10] = {
"NT", "a","b",
"A", "aBa","Error",
"B", "Îμ","bB",
};
char pop()
{
char ch;
ch = STACK[TOP--];
return ch;
}
void push(char ch)
{
STACK[++TOP] = ch;
}
void stack_content()
{
if (TOP != -1)
{
int i = 0;
printf("\nstack content: ");
while(i <= TOP)
{
printf("%c",STACK[i++]);
}
printf("\n");
}
return;
}
int isterm(char c)
{
if (c >= 'a' && c <= 'z')
return 1;
else
return 0;
}
int Parser_table(char stack_top,char buf_value,int flag)
{
int r,c;
switch(stack_top)
{
case 'A' : r = 1; break;
case 'B' : if(flag<=5) r = 2; else r = 3;
}
switch(buf_value)
{
case 'a' : c = 1; break;
case 'b' : c = 2;
}
if (strcmp(table[r][c],"error") == 0)
return 0;
if (strcmp(table[r][c],"Îμ") != 0)
{
strcpy(G_prod,table[r][c]);
}
return 1;
}
int main()
{
int i,j,stln;
printf("LL(1) PARSER TABLE \n");
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
printf("%s\t",table[i][j]);
}
printf("\n");
}
printf("\n");
printf("ENTER THE STRING into the Buffer and also give a ';' as the terminator: ");
scanf("%s",BUFFER);
printf("\n THE STRING in the Buffer is %s",BUFFER);
if(BUFFER[strlen(BUFFER)-1] != ';')
{
printf("END OF STRING MARKER SHOULD BE ';'");
exit(0);
}
push('$');
push('A');
while(STACK[TOP] != '$') // Stack is not Empty
{
flag++;
if (STACK[TOP] == BUFFER[B_ptr]) // X is a
{
printf("\n1.The poped item is - %c,",pop());
B_ptr++;
printf("\t buffer cont - %.*s",strlen(BUFFER),BUFFER+B_ptr);
}
else if(isterm(STACK[TOP])) // is X is terminal
{
printf("\n2. $ %c",STACK[TOP]);
printf("\t Error in Parsing \n");
}
else
if (!Parser_table(STACK[TOP],BUFFER[B_ptr],flag))
printf("3. Error Entry in Parse Table ");
else
if (Parser_table(STACK[TOP],BUFFER[B_ptr],flag))
{
if (flag < 6 && strcmp(G_prod,"Îμ") != 0)
{
printf("\n4.1 flag = %d, prod id- %s*\t",flag,G_prod);
pop();
stln = strlen(G_prod);
for(i=stln-1;i>=0;i--)
push(G_prod[i]);
stack_content();
}
else
{
stack_content();
printf("\n4.2 flag = %d *reduce by %s*",flag,"B->Îμ");
pop();
printf("\t buffer content is %c",BUFFER[B_ptr]);
}
}
}
if (STACK[TOP] == '$' && BUFFER[B_ptr] == ';')
printf("\n** The string is accepted **");
else
printf("\n** The string is not accepted **");
}