/* * Input scanner. * * Function are SetSrc() to redirect input, and GetToken() to * get a token. This automatically adds the token to the * symbol table. GetToken uses ; as a statement separator. * */ #include #include #include #include "glob.h" static FILE *src = NULL; static int nextch = 0; int SetSrc(char *fn) { if (src != NULL) fclose(src); if (strcmp(fn,"stdin")) { src = fopen(fn,"r"); if (src == NULL) eprintf("An error occurred opening '%s' for input.",fn,0,0); } else src = NULL; nextch = 0; return 0; } #define SYMB "!&()-,:;<=>?{|}\[]^" #define MAXW 80 #define more nextch=((src == NULL) ? getchar() : putchar(getc(src))) #define eof (nextch==-1 || ((src == NULL) ? feof(stdin) : feof(src))) int GetToken(void) { static char buffer[MAXW]; static int i; while (nextch <= ' ') if (eof) src==NULL||printf("eof in stdin\n"),SetSrc("stdin"); else more; if (strchr(SYMB,nextch) != NULL) { buffer[0] = nextch; buffer[1] = 0; more; return AddSymbol(buffer); } for (i=0; i