diff 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
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/automaton/fautomaton1.cbc~	Thu Apr 19 13:41:11 2012 +0900
@@ -0,0 +1,107 @@
+/*
+ * Implementation of the fininte automat by CbC
+ * test program
+ *
+ *  Finite automaton M.
+ *  M = ({q1,q2},{0,1}, delta, q1, {q2})
+ *  
+ *  delta = 
+ *    |0  1
+ *  -----------
+ *  q1|q1 q2
+ *  q1|q1 q2
+ */
+
+#include <stdio.h> 
+#include <stdlib.h> // exit()
+#include <string.h> // strlen()
+
+__code q1(char *c, int size, int count);
+__code q2(char *c, int size, int count);
+
+
+__code print_error()
+{
+	printf("error\n");
+	exit(0);
+}
+
+__code no_recognize()
+{
+	printf("no recognize\n");
+}
+
+__code recognize()
+{
+	printf("recognize\n");
+}
+
+int _isspace(char c)
+{
+	return c == ' ' || c == '\t';
+}
+
+char *skipchar(char *c)
+{
+	
+	while(_isspace(*c)) {
+		c++;
+	}
+
+	return c;
+}
+
+__code q1(char *c, int size, int count)
+{
+//	char *ch = skipchar(c);
+	char *ch = c;
+	ch++;
+	count++;
+	switch(*c) {
+	case '0':
+		goto q1(ch, size, count);
+		break;
+	case '1':
+		goto q2(ch, size, count);
+		break;
+	default:
+		if(count >= size) 
+			goto no_recognize();
+		else 
+			goto recognize();
+		
+	}
+
+}
+
+__code q2(char *c, int size, int count)
+{
+//	char *ch = skipchar(c);
+	char *ch = c;
+	ch++;
+	count++;
+	switch(*c) {
+	case '0':
+		goto q1(ch, size, count);
+		break;
+	case '1':
+		goto q2(ch, size, count);
+		break;
+	default:
+		if(count >= size)
+			goto recognize();
+		else 
+			goto no_recognize();
+	}
+	
+}
+
+
+int main(int argc, char *argv[])
+{
+	if(argc == 1 ) goto print_error();
+	int size = strlen(argv[1]);
+	char *c = argv[1];
+	goto q1(c, size, 0);
+	return 0;
+}