forked from niyasc/Compiler-Design-Lab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQ2moore.c
More file actions
90 lines (84 loc) · 1.45 KB
/
Q2moore.c
File metadata and controls
90 lines (84 loc) · 1.45 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
#include<stdio.h>
#include<stdbool.h>
#include<string.h>
int m,n,itable[10][10];
int getState(int current_state,int current_symbol)
{
if(current_symbol-48<n)
{
if(itable[current_state][current_symbol-48]=='-')
return -1;
else
return itable[current_state][current_symbol-48]-48;
}
else
{
return -1;
}
}
int main()
{
system("COLOR F0");
char osymbol[10];
int i=0,j=0;
FILE *ptr=fopen("moore.txt","r");
char c,string[10];;
while(!feof(ptr))
{
fscanf(ptr,"%c",&c);
switch(c)
{
case ' ':
j++;
n=j;
break;
case '\n':
i++;
j=0;
break;
default:
itable[i][j]=c;
}
}
m=i+1;
for(i=0;i<m;i++)
{
osymbol[i]=itable[i][n];
}
printf("Enter string : ");
scanf("%s",string);
i=0;
j=0;
char output[10];
int current_state=0;
int current_symbol=string[i++];
output[j++]=osymbol[current_state];
bool flag=false;
printf("current_state %d,output %c\n",current_state,osymbol[current_state]);
while(1)
{
int next_state=getState(current_state,current_symbol);
printf("current_state %d,current_symbol %c,next_state %d,output %c\n",current_state,current_symbol,next_state,osymbol[next_state]);
if(next_state<0)
{
flag=false;
break;
}
current_state=next_state;
output[j++]=osymbol[current_state];
if(i<strlen(string))
{
current_symbol=string[i++];
}
else
{
flag=true;
break;
}
}
if(flag)
printf("Output %s\n",output);
else
printf("Invalid input\n");
return 0;
}