forked from niyasc/Compiler-Design-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ5Cfg.cpp
More file actions
117 lines (99 loc) · 2.58 KB
/
Q5Cfg.cpp
File metadata and controls
117 lines (99 loc) · 2.58 KB
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
#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int i,j,k,l,m,n=0,o,p,nv,z=0,t,x=0;
char str[10],temp[20],temp2[20],temp3[20];
struct prod
{
char lhs[10],rhs[10][10];
int n;
}pro[10];
void findter()
{
for(k=0;k<n;k++)
{
if(temp[i]==pro[k].lhs[0])
{
for(t=0;t<pro[k].n;t++)
{
for(l=0;l<20;l++)
temp2[l]='\0';
for(l=i+1;l<strlen(temp);l++)
temp2[l-i-1]=temp[l];
for(l=i;l<20;l++)
temp[l]='\0';
for(l=0;l<strlen(pro[k].rhs[t]);l++)
temp[i+l]=pro[k].rhs[t][l];
strcat(temp,temp2);
if(str[i]==temp[i])
return;
else if(str[i]!=temp[i] && temp[i]>=65 && temp[i]<=90)
break;
}
break;
}
}
if(temp[i]>=65 && temp[i]<=90)
findter();
}
int main()
{
FILE *f;
for(i=0;i<10;i++)
pro[i].n=0;
f=fopen("q5.txt","r");
while(!feof(f))
{
fscanf(f,"%s",pro[n].lhs);
if(n>0)
{
if( strcmp(pro[n].lhs,pro[n-1].lhs) == 0 )
{
pro[n].lhs[0]='\0';
fscanf(f,"%s",pro[n-1].rhs[pro[n-1].n]);
pro[n-1].n++;
continue;
}
}
fscanf(f,"%s",pro[n].rhs[pro[n].n]);
pro[n].n++;
n++;
}
n--;
printf("\nThe grammar detected is:\n");
for(i=0;i<n;i++)
for(j=0;j<pro[i].n;j++)
printf("%s -> %s\n",pro[i].lhs,pro[i].rhs[j]);
for(l=0;l<10;l++)
str[0]=NULL;
printf("\n\nEnter a string:\n");
scanf("%s",str);
for(j=0;j<pro[0].n;j++)
{
for(l=0;l<20;l++)
temp[l]=NULL;
strcpy(temp,pro[0].rhs[j]);
m=0;
for(i=0;i<strlen(str);i++)
{
if(str[i]==temp[i])
m++;
else if(str[i]!=temp[i] && temp[i]>=65 && temp[i]<=90)
{
findter();
if(str[i]==temp[i])
m++;
}
else if( str[i]!=temp[i] && (temp[i]<65 || temp[i]>90) )
break;
}
if(m==strlen(str) && strlen(str)==strlen(temp))
{
printf("\nIt satisfies the Grammar.");
break;
}
}
if(j==pro[0].n)
printf("\nIt does not satisfy the Grammar.");
return 0;
}