/* Very Simple Compiler Parser part $Id: s-compile.c,v 1.2 96/11/16 01:15:20 kono Exp Locker: kono $ */ #include < stdio.h> void error(char *); void expr(); int token(); char *ptr,*last_ptr,*before; int value,lvalue; int last_token; int token() { int c,d; last_ptr = ptr; /* for error position */ c= *ptr; if(!c) { last_token = EOF; return last_token; } ptr++; if (c< =' ') { /* comment */ while(*ptr++); ptr--; last_token = EOF; last_ptr = ptr; return last_token; } if('0'< =c && c< ='9') { /* Decimal */ d = c-'0'; while(c= *ptr++) { if('0'< =c && c< ='9') { d = d*10 + (c - '0'); } else { break; } } c && ptr--; value = d; last_token = '0'; return last_token; } else if ('a'< =c && c< ='z') { /* variable */ value = c-'a'; /* return variable reference */ last_token = 'v'; return last_token; } else { last_token = c; return last_token; return c; } } void expr() { do { token(); switch(last_token) { case '0': printf("%d\tvalue\n",value); break; case 'v': printf("%c\tvariable\n",value+'a'); break; case EOF: break; default: printf("%c\ttoken\n",last_token); break; } } while(last_token!=EOF); } int main() { int d; char buf[BUFSIZ]; while (fgets(buf,BUFSIZ,stdin)) { ptr = buf; before = buf; printf("#### %s",buf); expr(); } return 0; } void error(s) char *s; { fprintf(stderr,"%s on %s\n",s,last_ptr); } /* end */