-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathparser.cpp
140 lines (103 loc) · 2.22 KB
/
parser.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
#include<iostream>
#include<conio.h>
#include<string.h>
using namespace std;
struct grammar{
char p[1];
char production[20];
}gmr[10];
int main(){
int iter=0, n;
cout<<"Enter the number of productions: "<<endl;
cin>>n;
char prods[10], input[100];
cout<<"\nEnter productions:"<<endl;
for(;iter<n;iter++){
cin>>prods;
strncpy(gmr[iter].p,prods,1);
strcpy(gmr[iter].production,&prods[3]);
}
cout<<"\nEnter the string to be parsed:"<<endl;
cin>>input;
int ip_length = strlen(input);
char stack[10], scann;
int start_position=0, position, k, j, f, l, m;
int state;
iter=0;
//Let us move the INPUT
scann=input[iter];
//Push the first character into the stack
stack[start_position]=scann;
//Actual move
iter++;
start_position++;
cout<<"\n\nStack\tInput\tAction"<<endl;
do
{
//Initialize the state State 2 = Reduced State 1 = Shift
state=1;
while(state!=0){
cout<<"\n";
for(position=0; position<start_position; position++){
cout<<stack[position];
}
cout<<"\t";
for(position=iter; position<ip_length; position++){
cout<<input[position];
}
if(state==2){
cout<<"\tReduced";
}
else{
cout<<"\tShifted";
}
state=0;
//Let's see if reduction is possible here
getch();
for(k=0;k<start_position;k++){
f=0;
for(j=0;j<10;j++)
{
prods[j]='\0';
}
int prods_pos=0;
for(j=k; j<start_position; j++){ //removing first Char
prods[prods_pos]=stack[j];
prods_pos++;
}
//Comparing each possibility with production
for(m=0 ;m<n; m++){
int cr = strcmp(prods,gmr[m].production);
//if cr is zero then match is found
if(cr==0)
{
for(l=k;l<10;l++) //removing matched part from our beloved stack
{
stack[l]='\0';
start_position--;
}
start_position=k;
//concatinate the string
strcat(stack,gmr[m].p);
start_position++;
state=2;
}
}
}
}
//moving input
scann=input[iter];
stack[start_position]=scann;
iter++;
start_position++;
}while(strlen(stack)!=1 && start_position!=ip_length+1);
if(strlen(stack)==1){
cout<<"\nString Accepted";
}
else{
cout<<"\nRejected";
}
getch();
//Success, HELL YEAH!
return 0;
}