C Programs
A finite automata is implemented using C programming language which can detect:
1. Unsigned numbers
C program to implement the transition diagram for unsigned numbers
/* C program to implement the transition diagram for unsigned numbers */
#include <stdio.h>
#include <ctype.h>
/* Use a DFA approach. state indicates the state of the DFA. */
enum Statetype {NORMAL, NUM, END};
enum Statetype start(int);
enum Statetype state1(int);
/*------------------------------------------------------------*/
/* main: Read text from stdin. Check for the unsigned numbers */
/* and return the result to stdout. Return 0 */
/*------------------------------------------------------------*/
int main()
{
int c;
enum Statetype state = NORMAL;
for( ; ; ) {
c = getchar();
if (c == EOF)
break;
switch (state)
{
case NORMAL:
state = start(c);
break;
case NUM:
state = state1(c);
break;
case END:
printf("Unsigned number detected.\n");
return 0;
}
}
return 0;
}
/*------------------------------------------------------------*/
/* start: Implement the state 0 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype start(int c)
{
enum Statetype state;
if(isdigit(c))
state = NUM;
return state;
}
/*------------------------------------------------------------*/
/* state1: Implement the state 1 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype state1(int c)
{
enum Statetype state;
if (isdigit(c))
state = NUM;
else
state = END;
return state;
}
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
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
2. Integers
C program to implement the transition diagram for integer
/* C program to implement the transition diagram for integer */
#include <stdio.h>
#include <ctype.h>
/* Use a DFA approach. state indicates the state of the DFA. */
enum Statetype {NORMAL, FSIGN, INT, FINT};
enum Statetype start(int);
enum Statetype state1(int);
enum Statetype state2(int);
/*------------------------------------------------------------*/
/* main: Read text from stdin. Check for the integer and */
/* and return the result to stdout. Return 0 */
/*------------------------------------------------------------*/
int main()
{
int c;
enum Statetype state = NORMAL;
for( ; ; ) {
c = getchar();
if (c == EOF)
break;
switch (state)
{
case NORMAL:
state = start(c);
break;
case FSIGN:
state = state1(c);
break;
case INT:
state = state2(c);
break;
case FINT:
printf("Integer detected.\n");
return 0;
}
}
return 0;
}
/*------------------------------------------------------------*/
/* start: Implement the state 0 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype start(int c)
{
enum Statetype state;
if (c == '+' || c == '-')
state = FSIGN;
else if(isdigit(c))
state = INT;
return state;
}
/*------------------------------------------------------------*/
/* state1: Implement the state 1 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype state1(int c)
{
enum Statetype state;
if (isdigit(c))
state = INT;
return state;
}
/*------------------------------------------------------------*/
/* state2: Implement the state 2 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype state2(int c)
{
enum Statetype state;
if (isdigit(c))
state = INT;
else
state = FINT;
return state;
}
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
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
3. Real numbers
C program to implement the transition diagram for integer
/* C program to implement the transition diagram for real number */
#include <stdio.h>
#include <ctype.h>
/* Use a DFA approach. state indicates the state of the DFA. */
enum Statetype {NORMAL, FSIGN, INT, DOT, REAL, SCI, SSIGN, SNOT, FINT, FREAL, FSCI};
enum Statetype start(int);
enum Statetype state1(int);
enum Statetype state2(int);
enum Statetype state3(int);
enum Statetype state4(int);
enum Statetype state5(int);
enum Statetype state6(int);
enum Statetype state7(int);
/*------------------------------------------------------------*/
/* main: Read text from stdin. Check for the floating point */
/* number and return the result to stdout. Return 0 */
/*------------------------------------------------------------*/
int main()
{
int c;
enum Statetype state = NORMAL;
for( ; ; ) {
c = getchar();
if (c == EOF)
break;
switch (state)
{
case NORMAL:
state = start(c);
break;
case FSIGN:
state = state1(c);
break;
case INT:
state = state2(c);
break;
case DOT:
state = state3(c);
break;
case REAL:
state = state4(c);
break;
case SCI:
state = state5(c);
break;
case SSIGN:
state = state6(c);
break;
case SNOT:
state = state7(c);
break;
case FINT:
printf("Integer detected.\n");
return 0;
case FREAL:
printf("Real number detected.\n");
return 0;
case FSCI:
printf("Scientific Notation detected.\n");
return 0;
}
}
return 0;
}
/*------------------------------------------------------------*/
/* start: Implement the state 0 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype start(int c)
{
enum Statetype state;
if (c == '+' || c == '-')
state = FSIGN;
else if(isdigit(c))
state = INT;
return state;
}
/*------------------------------------------------------------*/
/* state1: Implement the state 1 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype state1(int c)
{
enum Statetype state;
if (isdigit(c))
state = INT;
return state;
}
/*------------------------------------------------------------*/
/* state2: Implement the state 2 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype state2(int c)
{
enum Statetype state;
if (isdigit(c))
state = INT;
else if (c == '.')
state = DOT;
else if (c == 'e' || c == 'E')
state = SCI;
else
state = FINT;
return state;
}
/*------------------------------------------------------------*/
/* state3: Implement the state 3 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype state3(int c)
{
enum Statetype state;
if (isdigit(c))
state = REAL;
return state;
}
/*------------------------------------------------------------*/
/* state4: Implement the state 4 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype state4(int c)
{
enum Statetype state;
if (isdigit(c))
state = REAL;
else if (c == 'e' || c == 'E')
state = SCI;
else
state = FREAL;
return state;
}
/*------------------------------------------------------------*/
/* state5: Implement the state 5 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype state5(int c)
{
enum Statetype state;
if (isdigit(c))
state = SNOT;
else if (c == '+' || c == '-')
state = SSIGN;
return state;
}
/*------------------------------------------------------------*/
/* state6: Implement the state 6 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype state6(int c)
{
enum Statetype state;
if (isdigit(c))
state = SNOT;
return state;
}
/*------------------------------------------------------------*/
/* state7: Implement the state 7 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype state7(int c)
{
enum Statetype state;
if (isdigit(c))
state = SNOT;
else
state = FSCI;
return state;
}
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
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
4. Identifiers
C program to implement the transition diagram for Identifiers
/* C program to implement the transition diagram for identifier */
#include <stdio.h>
#include <ctype.h>
/* Use a DFA approach. state indicates the state of the DFA. */
enum Statetype {NORMAL, INWORD, END};
enum Statetype start(int);
enum Statetype state1(int);
/*------------------------------------------------------------*/
/* main: Read text from stdin. Check for the identifier and */
/* return the result to stdout. Return 0 */
/*------------------------------------------------------------*/
int main()
{
int c;
enum Statetype state = NORMAL;
for( ; ; ) {
c = getchar();
if (c == EOF)
break;
switch (state)
{
case NORMAL:
state = start(c);
break;
case INWORD:
state = state1(c);
break;
case END:
printf("Identifier detected.\n");
return 0;
}
}
return 0;
}
/*------------------------------------------------------------*/
/* start: Implement the state 0 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype start(int c)
{
enum Statetype state;
if (isalpha(c))
state = INWORD;
return state;
}
/*------------------------------------------------------------*/
/* state1: Implement the state 1 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
enum Statetype state1(int c)
{
enum Statetype state;
if (isalnum(c))
state = INWORD;
else
state = END;
return state;
}
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
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
5. Relational Operators
C program to implement the transition diagram for Relational Operators
/* C program to implement the transition diagram for relational operators */
#include <stdio.h>
/* Use a DFA approach. state indicates the state of the DFA. */
int state = 0;
int start(int);
int state1(int);
int state6(int);
/*------------------------------------------------------------*/
/* main: Read text from stdin. Check for the relop and return */
/* the result to stdout. Return 0 */
/*------------------------------------------------------------*/
int main()
{
int c;
for( ; ; ) {
c = getchar();
if (c == EOF)
break;
switch (state)
{
case 0:
state = start(c);
break;
case 1:
state = state1(c);
break;
case 2:
printf("RELOP, LE\n");
return 0;
case 3:
printf("RELOP, NE\n");
return 0;
case 4:
printf("RELOP, LT\n");
return 0;
case 5:
printf("RELOP, EQ\n");
return 0;
case 6:
state = state6(c);
break;
case 7:
printf("RELOP, GE\n");
return 0;
case 8:
printf("RELOP, GT\n");
return 0;
}
}
return 0;
}
/*------------------------------------------------------------*/
/* start: Implement the state 0 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
int start(int c)
{
if (c == '<')
state = 1;
else if (c == '=')
state = 5;
else if (c == '>')
state = 6;
return state;
}
/*------------------------------------------------------------*/
/* state1: Implement the state 1 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
int state1(int c)
{
if (c == '=')
state = 2;
else if (c == '>')
state = 3;
else
state = 4;
return state;
}
/*------------------------------------------------------------*/
/* state6: Implement the state 6 of the DFA. c is the current */
/* DFA character. Return the next state. */
/*------------------------------------------------------------*/
int state6(int c)
{
if (c == '=')
state = 7;
else
state = 8;
return state;
}
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
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