-
Notifications
You must be signed in to change notification settings - Fork 9
/
Lead-Trail.cpp
190 lines (183 loc) · 2.91 KB
/
Lead-Trail.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
#include<iostream>
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
int vars,terms,i,j,k,m,rep,count,temp=-1;
char var[10],term[10],lead[10][10],trail[10][10];
struct grammar
{
int prodno;
char lhs,rhs[20][20];
}gram[50];
void get()
{
cout<<"\nLEADING AND TRAILING\n";
cout<<"\nEnter the no. of variables : ";
cin>>vars;
cout<<"\nEnter the variables : \n";
for(i=0;i<vars;i++)
{
cin>>gram[i].lhs;
var[i]=gram[i].lhs;
}
cout<<"\nEnter the no. of terminals : ";
cin>>terms;
cout<<"\nEnter the terminals : ";
for(j=0;j<terms;j++)
cin>>term[j];
cout<<"\nPRODUCTION DETAILS\n";
for(i=0;i<vars;i++)
{
cout<<"\nEnter the no. of production of "<<gram[i].lhs<<":";
cin>>gram[i].prodno;
for(j=0;j<gram[i].prodno;j++)
{
cout<<gram[i].lhs<<"->";
cin>>gram[i].rhs[j];
}
}
}
void leading()
{
for(i=0;i<vars;i++)
{
for(j=0;j<gram[i].prodno;j++)
{
for(k=0;k<terms;k++)
{
if(gram[i].rhs[j][0]==term[k])
lead[i][k]=1;
else
{
if(gram[i].rhs[j][1]==term[k])
lead[i][k]=1;
}
}
}
}
for(rep=0;rep<vars;rep++)
{
for(i=0;i<vars;i++)
{
for(j=0;j<gram[i].prodno;j++)
{
for(m=1;m<vars;m++)
{
if(gram[i].rhs[j][0]==var[m])
{
temp=m;
goto out;
}
}
out:
for(k=0;k<terms;k++)
{
if(lead[temp][k]==1)
lead[i][k]=1;
}
}
}
}
}
void trailing()
{
for(i=0;i<vars;i++)
{
for(j=0;j<gram[i].prodno;j++)
{
count=0;
while(gram[i].rhs[j][count]!='\x0')
count++;
for(k=0;k<terms;k++)
{
if(gram[i].rhs[j][count-1]==term[k])
trail[i][k]=1;
else
{
if(gram[i].rhs[j][count-2]==term[k])
trail[i][k]=1;
}
}
}
}
for(rep=0;rep<vars;rep++)
{
for(i=0;i<vars;i++)
{
for(j=0;j<gram[i].prodno;j++)
{
count=0;
while(gram[i].rhs[j][count]!='\x0')
count++;
for(m=1;m<vars;m++)
{
if(gram[i].rhs[j][count-1]==var[m])
temp=m;
}
for(k=0;k<terms;k++)
{
if(trail[temp][k]==1)
trail[i][k]=1;
}
}
}
}
}
void display()
{
for(i=0;i<vars;i++)
{
cout<<"\nLEADING("<<gram[i].lhs<<") = ";
for(j=0;j<terms;j++)
{
if(lead[i][j]==1)
cout<<term[j]<<",";
}
}
cout<<endl;
for(i=0;i<vars;i++)
{
cout<<"\nTRAILING("<<gram[i].lhs<<") = ";
for(j=0;j<terms;j++)
{
if(trail[i][j]==1)
cout<<term[j]<<",";
}
}
}
int main()
{
get();
leading();
trailing();
display();
}
// LEADING AND TRAILING
// Enter the no. of variables : 3
// Enter the variables :
// E
// T
// F
// Enter the no. of terminals : 5
// Enter the terminals : )
// (
// *
// +
// i
// PRODUCTION DETAILS
// Enter the no. of production of E:2
// E->E+T
// E->T
// Enter the no. of production of T:2
// T->T*F
// T->F
// Enter the no. of production of F:2
// F->(E)
// F->i
// LEADING(E) = (,*,+,i,
// LEADING(T) = (,*,i,
// LEADING(F) = (,i,
// TRAILING(E) = ),*,+,i,
// TRAILING(T) = ),*,i,
// TRAILING(F) = ),i,