forked from niyasc/Compiler-Design-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ1sregx.c
More file actions
85 lines (83 loc) · 1.34 KB
/
Q1sregx.c
File metadata and controls
85 lines (83 loc) · 1.34 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
#include<stdio.h>
#include<string.h>
int main()
{
system("COLOR F0");
FILE *fp=fopen("regex.txt","r");
char regex[100];
fscanf(fp,"%s",regex);
char string[100];
int m,n,i,j=0,k=0;
char components[10][10];
for(i=0;i<strlen(regex);i++)
{
if(regex[i]=='(')
{/*ignore*/}
else if(regex[i]=='+')
{
components[j][k]=0;
j++;
k=0;
}
else if(regex[i]==')')
{
components[j][k]=0;
break;
}
else
{
components[j][k++]=regex[i];
}
}
m=j+1;
printf("Components are \n");
for(i=0;i<m;i++)
printf("-%s- ",components[i]);
printf("\n");
printf("Enter the string:\n");
scanf("%s",string);
//check string
if(regex[strlen(regex)-1]!='*')
{
for(i=0;i<m;i++)
{
if(strcmp(components[i],string)==0)
{
printf("String accepted\n");
return 0;
}
}
printf("String rejected\n");
return 0;
}
for(i=0;i<strlen(string);)
{
int p=i;
for(j=0;j<m;j++)
{
printf("i=%d j=%d\n",i,j);
if(string[i]==components[j][0])
{
int t=i;
for(k=0;k<strlen(components[j]);k++)
{
printf("k=%d \n",k);
if(components[j][k]==string[t])
t++;
else
break;
}
printf("-t=%d i=%d length=%d-",t,i,strlen(components[j]));
if(t==strlen(components[j])+i)
i=t;
}
}
if(p==i)
{
printf("String rejected\n");
return 0;
}
}
printf("String accepted\n");
return 0;
}