Mercurial > hg > Members > nobuyasu > CbC
comparison automaton/fautomaton1.cbc~ @ 3:4e98faa1d831
add automaton
author | Nobuyasu Oshiro <dimolto@cr.ie.u-ryukyu.ac.jp> |
---|---|
date | Thu, 19 Apr 2012 13:41:11 +0900 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
2:396046c5b0b0 | 3:4e98faa1d831 |
---|---|
1 /* | |
2 * Implementation of the fininte automat by CbC | |
3 * test program | |
4 * | |
5 * Finite automaton M. | |
6 * M = ({q1,q2},{0,1}, delta, q1, {q2}) | |
7 * | |
8 * delta = | |
9 * |0 1 | |
10 * ----------- | |
11 * q1|q1 q2 | |
12 * q1|q1 q2 | |
13 */ | |
14 | |
15 #include <stdio.h> | |
16 #include <stdlib.h> // exit() | |
17 #include <string.h> // strlen() | |
18 | |
19 __code q1(char *c, int size, int count); | |
20 __code q2(char *c, int size, int count); | |
21 | |
22 | |
23 __code print_error() | |
24 { | |
25 printf("error\n"); | |
26 exit(0); | |
27 } | |
28 | |
29 __code no_recognize() | |
30 { | |
31 printf("no recognize\n"); | |
32 } | |
33 | |
34 __code recognize() | |
35 { | |
36 printf("recognize\n"); | |
37 } | |
38 | |
39 int _isspace(char c) | |
40 { | |
41 return c == ' ' || c == '\t'; | |
42 } | |
43 | |
44 char *skipchar(char *c) | |
45 { | |
46 | |
47 while(_isspace(*c)) { | |
48 c++; | |
49 } | |
50 | |
51 return c; | |
52 } | |
53 | |
54 __code q1(char *c, int size, int count) | |
55 { | |
56 // char *ch = skipchar(c); | |
57 char *ch = c; | |
58 ch++; | |
59 count++; | |
60 switch(*c) { | |
61 case '0': | |
62 goto q1(ch, size, count); | |
63 break; | |
64 case '1': | |
65 goto q2(ch, size, count); | |
66 break; | |
67 default: | |
68 if(count >= size) | |
69 goto no_recognize(); | |
70 else | |
71 goto recognize(); | |
72 | |
73 } | |
74 | |
75 } | |
76 | |
77 __code q2(char *c, int size, int count) | |
78 { | |
79 // char *ch = skipchar(c); | |
80 char *ch = c; | |
81 ch++; | |
82 count++; | |
83 switch(*c) { | |
84 case '0': | |
85 goto q1(ch, size, count); | |
86 break; | |
87 case '1': | |
88 goto q2(ch, size, count); | |
89 break; | |
90 default: | |
91 if(count >= size) | |
92 goto recognize(); | |
93 else | |
94 goto no_recognize(); | |
95 } | |
96 | |
97 } | |
98 | |
99 | |
100 int main(int argc, char *argv[]) | |
101 { | |
102 if(argc == 1 ) goto print_error(); | |
103 int size = strlen(argv[1]); | |
104 char *c = argv[1]; | |
105 goto q1(c, size, 0); | |
106 return 0; | |
107 } |