Back to list of problems
FORCAL
309.c
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> void token_error(void) { char buf[10240]; printf("TOKEN ERROR\n"); while(fgets(buf, 1024, stdin)) { if (buf[0]=='\n') { printf("\n"); break; } } } char *buf; int main(void) { int i; buf = malloc(1048577); if (!buf) { abort(); } while(fgets(buf, 1048576, stdin)) { int s = strlen(buf); if (buf[0]=='\n') { printf("\n"); } for(i=0; i<s; i++) { if (buf[i]==' ' || buf[i]=='\t' || buf[i]=='\n') { continue; } if (isalnum(buf[i]) || buf[i]=='_') { int j,k; char word[50]; int all_digits; word[0]=buf[i]; for(j=1; j<40; j++) { if (isalnum(buf[i+j]) || buf[i+j]=='_') { word[j]=buf[i+j]; } else { break; } } word[j]=0; if (j >= 32) { token_error(); goto next_while; } all_digits=1; for(k=0; isdigit(word[k]); k++); if (!word[k] && k>=8) { token_error(); goto next_while; } printf("%s\n", word); i += j; i--; continue; } switch(buf[i]) { case '+': printf("+\n"); break; case '(': printf("(\n"); break; case ')': printf(")\n"); break; case ';': printf(";\n"); break; case ',': printf(",\n"); break; case '-': if (buf[i+1]=='-') { goto next_while; } else { printf("-\n"); break; } case ':': if (buf[i+1]=='=') { printf(":=\n"); i++; break; } else { token_error(); goto next_while; } default: token_error(); goto next_while; } } next_while: ; } exit(0); }