-
Notifications
You must be signed in to change notification settings - Fork 0
/
regextonfa1.c
116 lines (116 loc) · 3.03 KB
/
regextonfa1.c
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>
struct node
{
int start,end;
}arr[15];
char *postfix(char str[])
{
int i,j,k,l=0;
char *temp,op;
start:
for(i=0;str[i]!=')';i++);
for(j=i;str[j]!='(';j--);
for(k=j+1;k<i;k++)
{
if(isalpha(str[k]))
temp[l++]=str[k];
else
op=str[k];
}
temp[l++]=op;
for(k=j;k<=i;k++)
str[k]='#';
if(str[0]!='#')
goto start;
else
temp[l]='\0';
return temp;
}
void main()
{
char *postregex;
char regex[15];
int i=0,n=0,no=0,beg=0,clo=0;
printf("\nEnter Regex:- ");
scanf("%s",regex);
postregex=postfix(regex);
printf("\nPostfix Expression:- %s\n",postregex);
printf("START----------------->END on INPUT\n");
while(postregex[i]!='\0')
{
if(isalpha(postregex[i]))
{
arr[i].start=++no;
arr[i].end=++no;
printf(" %d -----------------> %d on %c\n",arr[i].start,arr[i].end,postregex[i]);
}
else
{
switch(postregex[i])
{
case '+':
arr[i].start=++no;
arr[i].end=++no;
if(clo==0)
{
if(beg==0)
beg=arr[i].start;
clo=arr[i].end;
printf(" %d -----------------> %d on %s\n",arr[i].start,arr[i-2].start,"Ɛ");
printf(" %d -----------------> %d on %s\n",arr[i].start,arr[i-1].start,"Ɛ");
printf(" %d -----------------> %d on %s\n",arr[i-2].end,arr[i].end,"Ɛ");
printf(" %d -----------------> %d on %s\n",arr[i-1].end,arr[i].end,"Ɛ");
}
else
{
printf(" %d -----------------> %d on %s\n",arr[i].start,beg,"Ɛ");
printf(" %d -----------------> %d on %s\n",arr[i].start,arr[i-1].start,"Ɛ");
printf(" %d -----------------> %d on %s\n",arr[i-1].end,arr[i].end,"Ɛ");
printf(" %d -----------------> %d on %s\n",clo,arr[i].end,"Ɛ");
beg=arr[i].start;
clo=arr[i].end;
}
break;
case '.':
if(clo==0)
{
printf(" %d -----------------> %d on %s\n",arr[i-2].end,arr[i-1].start,"Ɛ");
clo=arr[i-1].end;
if(beg==0)
beg=arr[i-2].start;
}
else
{
printf(" %d -----------------> %d on %s\n",clo,arr[i-1].start,"Ɛ");
clo=arr[i-1].end;
}
break;
case '*':
arr[i].start=++no;
arr[i].end=++no;
if(clo==0)
{
printf(" %d -----------------> %d on %s\n",arr[i].start,arr[i-1].start,"Ɛ");
printf(" %d -----------------> %d on %s\n",arr[i].start,arr[i].end,"Ɛ");
printf(" %d -----------------> %d on %s\n",arr[i-1].end,arr[i-1].start,"Ɛ");
printf(" %d -----------------> %d on %s\n",arr[i-1].end,arr[i].end,"Ɛ");
beg=arr[i].start;
clo=arr[i].end;
}
else
{
printf(" %d -----------------> %d on %s\n",arr[i].start,beg,"Ɛ");
printf(" %d -----------------> %d on %s\n",clo,beg,"Ɛ");
printf(" %d -----------------> %d on %s\n",clo,arr[i].end,"Ɛ");
printf(" %d -----------------> %d on %s\n",arr[i].start,arr[i].end,"Ɛ");
beg=arr[i].start;
clo=arr[i].end;
}
break;
}
}
++i;
}
printf("\nStart State:- %d",beg);
printf("\nEnd State:- %d\n",clo);
}