Photolog

Through the Looking-Glass
2010-10-12: Through the Looking-Glass
My radio speaks is binary!
2010-10-10: My radio speaks is binary!
Gigaminx: (present for my birthday)
2010-09-16: Gigaminx: (present for my birthday)
Trini on bike
2010-09-05: Trini on bike
Valporquero
2010-08-28: Valporquero
My new bike!
2010-08-22: My new bike!
Mario and Ana's wedding
2010-08-13: Mario and Ana's wedding
Canyoning in Guara
2010-08-07: Canyoning in Guara
Trini and Mari in Marbella
2010-08-05: Trini and Mari in Marbella
Trini and Chelo in Tabarca
2010-08-03: Trini and Chelo in Tabarca
Valid XHTML 1.1
Log in
Back to list of problems

Loglan-A Logical Language

134.c

/* 134 - Loglan-A Logical Language */
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int nw;
char **words = NULL;

#if DEBUG
void
debug(char *str, int start, int end) {
	int i;
	printf("DEBUG: %s (", str);
	for (i=start; i<end; i++) {
		printf("%s ", words[i]);
	}
	printf(")\n");
}
#endif

int
vowel(char c) {
	if (c=='a' || c=='e' || c=='i' || c=='o' || c=='u') {
		return 1;
	} else {
		return 0;
	}
}

int
A(int start, int *end) {
	if (start>=nw) return 0;
	if (!strcmp(words[start], "a") ||
			!strcmp(words[start], "e") ||
			!strcmp(words[start], "i") ||
			!strcmp(words[start], "o") ||
			!strcmp(words[start], "u")) {
		*end = start+1;
#if DEBUG
		debug("A", start, *end);
#endif
		return 1;
	} else {
		return 0;
	}
}

int
MOD(int start, int *end) {
	if (start>=nw) return 0;
	if (!strcmp(words[start], "ga") ||
			!strcmp(words[start], "ge") ||
			!strcmp(words[start], "gi") ||
			!strcmp(words[start], "go") ||
			!strcmp(words[start], "gu")) {
		*end = start+1;
#if DEBUG
		debug("MOD", start, *end);
#endif
		return 1;
	} else {
		return 0;
	}
}

int
BA(int start, int *end) {
	if (start>=nw) return 0;
	if (!strcmp(words[start], "ba") ||
			!strcmp(words[start], "be") ||
			!strcmp(words[start], "bi") ||
			!strcmp(words[start], "bo") ||
			!strcmp(words[start], "bu")) {
		*end = start+1;
#if DEBUG
		debug("BA", start, *end);
#endif
		return 1;
	} else {
		return 0;
	}
}

int
DA(int start, int *end) {
	if (start>=nw) return 0;
	if (!strcmp(words[start], "da") ||
			!strcmp(words[start], "de") ||
			!strcmp(words[start], "di") ||
			!strcmp(words[start], "do") ||
			!strcmp(words[start], "du")) {
		*end = start+1;
#if DEBUG
		debug("DA", start, *end);
#endif
		return 1;
	} else {
		return 0;
	}
}

int
LA(int start, int *end) {
	if (start>=nw) return 0;
	if (!strcmp(words[start], "la") ||
			!strcmp(words[start], "le") ||
			!strcmp(words[start], "li") ||
			!strcmp(words[start], "lo") ||
			!strcmp(words[start], "lu")) {
		*end = start+1;
#if DEBUG
		debug("LA", start, *end);
#endif
		return 1;
	} else {
		return 0;
	}
}

int
NAM(int start, int *end) {
	if (start>=nw) return 0;
	if (vowel(words[start][strlen(words[start])-1])) {
		return 0;
	} else {
		*end = start+1;
#if DEBUG
		debug("NAM", start, *end);
#endif
		return 1;
	}
}

int
PREDA(int start, int *end) {
	if (start>=nw) return 0;
	if (strlen(words[start]) != 5) {
		return 0;
	}
	if (!vowel(words[start][0]) &&
		!vowel(words[start][1]) &&
		vowel(words[start][2]) &&
		!vowel(words[start][3]) &&
		vowel(words[start][4])) {
			*end = start+1;
#if DEBUG
		debug("PREDA", start, *end);
#endif
			return 1;
	}
	if (!vowel(words[start][0]) &&
		vowel(words[start][1]) &&
		!vowel(words[start][2]) &&
		!vowel(words[start][3]) &&
		vowel(words[start][4])) {
			*end = start+1;
#if DEBUG
		debug("PREDA", start, *end);
#endif
			return 1;
	}
	return 0;
}

int
predstring(int start, int *end) {
	int a;
	if (!PREDA(start, &a)) {
		return 0;
	}
	while (1) {
		int b;
		if (PREDA(a, &b)) {
			a = b;
		} else {
			break;
		}
	}
	*end = a;
#if DEBUG
		debug("predstring", start, *end);
#endif
	return 1;
}

int
preds(int start, int *end) {
	int a;
	if (!predstring(start, &a)) {
		return 0;
	}
	while (1) {
		int b;
		if (A(a,&b)) {
			if (predstring(b,&b)) {
				a = b;
			} else {
				break;
			}
		} else {
			break;
		}
	}
	*end = a;
#if DEBUG
		debug("preds", start, *end);
#endif
	return 1;
}

int
predname(int start, int *end) {
	int a;
	if (LA(start, &a)) {
		if (predstring(a, &a)) {
			*end = a;
#if DEBUG
		debug("predname", start, *end);
#endif
			return 1;
		}
	}
	if (NAM(start, &a)) {
		*end = a;
#if DEBUG
		debug("predname", start, *end);
#endif
		return 1;
	}
	return 0;
}

int
predclaim(int start, int *end) {
	int a;
	if (predname(start, &a)) {
		if (BA(a, &a)) {
			if (preds(a, &a)) {
				*end = a;
#if DEBUG
		debug("predclaim", start, *end);
#endif
				return 1;
			}
		}
	}
	if (DA(start, &a)) {
		if (preds(a, &a)) {
			*end = a;
#if DEBUG
		debug("predclaim", start, *end);
#endif
			return 1;
		}
	}
	return 0;
}

int
verbpred(int start, int *end) {
	int a;
	if (!MOD(start, &a)) {
		return 0;
	}
	if (!predstring(a, &a)) {
		return 0;
	}
	*end = a;
#if DEBUG
		debug("verbpred", start, *end);
#endif
	return 1;
}

int
statement(int start, int *end) {
	int a,b;
	if (!predname(start, &a)) {
		return 0;
	}
	if (!verbpred(a, &a)) {
		return 0;
	}
	if (predname(a, &b)) {
		*end = b;
	} else {
		*end = a;
	}
#if DEBUG
		debug("statement", start, *end);
#endif
	return 1;
}

int
sentence(int start, int *end) {
	if (statement(start, end)) {
#if DEBUG
		debug("sentence", start, *end);
#endif
		return 1;
	}
	if (predclaim(start, end)) {
#if DEBUG
		debug("sentence", start, *end);
#endif
		return 1;
	}
	return 0;
}

void
doit(void) {
	int a;
	if (sentence(0, &a) && a==nw) {
		printf("Good\n");
	} else {
		printf("Bad!\n");
	}
}

int
main(void) {

	while (1) {
		char buf[1024];
		int fin=0;
		nw = 0;
		while (!fin) {
			scanf(" %s", buf);
			if (buf[0]=='#') {
				return 0;
			}
			if (buf[strlen(buf)-1]=='.') {
				fin=1;
				buf[strlen(buf)-1] = '\0';
			}
			words = realloc(words, (nw+1)*sizeof(char*));
			words[nw] = malloc(strlen(buf)+1);
			strcpy(words[nw],buf);
			nw++;
		}
		doit();
		fgets(buf, 1024, stdin);
	}
	return 0;
}