/* * scanner.c */ #include "scanner.h" #include #include static long needchar = 1; static char ch; long gettoken (Token* tok) { long state = 0; double sign = 1.0; double numval; long valint; if (needchar) { ch = getchar(); needchar = 0; } while (state >= 0) { switch (state) { case 0: if (ch == '(' ) { tok -> tokentype = lprtok; needchar = 1; state = -1; } else if (ch == ')' ) { tok -> tokentype = rprtok; needchar = 1; state = -1; } else if ((ch == '+') || (ch == '-') ) { tok -> tokenval.numval = 0.0; if (ch == '-' ) sign = -1.0; ch = getchar(); state = 3; } else if (isdigit(ch) ) { tok -> tokenval.numval = (double) (ch - '0'); ch = getchar(); state = 5; } else if (isalpha(ch) ) { valint = 0; tok -> tokenval.alfval[valint] = ch; ch = getchar(); state = 7; } else if ((ch == ' ') || (ch == '\n') ) { ch = getchar(); state = 0; } else { printf("ILLEGAL CHARACTER:%c\n", ch); ch = getchar(); state = 0; } break; case 3: if (isdigit(ch) ) { tok -> tokenval.numval = (double) (ch - '0'); ch = getchar(); state = 5; } else { printf("ILLEGAL CONSTANT\n"); tok -> tokenval.numval= 0.0; state = 5; } break; case 5: if (isdigit(ch) ) { tok -> tokenval.numval = tok -> tokenval.numval*10.0 + (ch -'0'); ch = getchar(); } else { tok -> tokentype = numtok; tok -> tokenval.numval = tok -> tokenval.numval*sign; state = -1; } break; case 7: if (isalnum(ch) ) { valint = valint + 1; if (valint < ALF - 1 ) { tok -> tokenval.alfval[valint] = ch; ch = getchar(); } else { printf("ATOM TOO LARGE\n"); ch = getchar(); } } else { tok -> tokentype = alftok; valint = valint + 1; tok -> tokenval.alfval[valint] = '\0'; state = -1; } break; } /* end of switch */ } /* end of while */ }