📄 scan.cpp
字号:
#include "globals.h"
#include "util.h"
#include "scan.h"
#define BUFLEN 256
//static int bufsize = 0; /* current size of buffer string */
//static int EOF_flag = FALSE; /* corrects ungetNextChar behavior on EOF */
char tokenString[MAXTOKENLEN+1];
/* states in scanner DFA */
typedef enum
{ START,INASSIGN,INCOMMENT,INNUM,INID,DONE,INEQ,INLE,INGE,INCOMMENT_S1,INCOMMENT_S2,INNOEQ}
StateType;
int getNextChar(void)
{ if (!(linepos < bufsize))
{ lineno++;
if (fgets(lineBuf,BUFLEN-1,source))
{ if (EchoSource) fprintf(scan,"%4d: %s",lineno,lineBuf);
bufsize = strlen(lineBuf);
linepos = 0;
return lineBuf[linepos++];
}
else
{ EOF_flag = TRUE;
return EOF;
}
}
else return lineBuf[linepos++];
}
void ungetNextChar(void)
{ if (!EOF_flag) linepos-- ;}
/* lookup table of reserved words */
static struct
{ char* str;
TokenType tok;
} reservedWords[MAXRESERVED]
= {{"if",IF},{"else",ELSE},
{"int",INT},{"return",RETURN},{"void",VOID},
{"while",WHILE}};
/* lookup an identifier to see if it is a reserved word */
/* uses linear search */
static TokenType reservedLookup (char * s)
{ int i;
for (i=0;i<MAXRESERVED;i++)
if (!strcmp(s,reservedWords[i].str))
return reservedWords[i].tok;
return ID;
}
TokenType getToken(void)
{ /* index for storing into tokenString */
int tokenStringIndex = 0;
/* holds current token to be returned */
TokenType currentToken;
/* current state - always begins at START */
StateType state = START;
/* flag to indicate save to tokenString */
int save;
while (state != DONE)
{ char c = getNextChar();
save = TRUE;
switch (state)
{ case START:
if (isdigit(c))
state = INNUM;
else if (isalpha(c))
state = INID;
else if (c == '=')
state = INEQ;
else if (c == '<')
state = INLE;
else if (c == '>')
state = INGE;
else if (c=='!')
state=INNOEQ;
else if ((c == ' ') || (c == '\t') || (c == '\n'))
save = FALSE;
else if (c == '/')
{ save=false;
state = INCOMMENT_S1;
}
else
{ state = DONE;
switch (c)
{ case EOF:
save = FALSE;
currentToken = ENDFILE;
break;
case '+':
currentToken = PLUS;
break;
case '-':
currentToken = MINUS;
break;
case '*':
currentToken = TIMES;
break;
case',':
currentToken = COMMA;
break;
case '(':
currentToken = LPAREN;
break;
case ')':
currentToken = RPAREN;
break;
case '[':
currentToken = LINDEX;
break;
case ']':
currentToken = RINDEX;
break;
case '{':
currentToken = LBRACE;
break;
case '}':
currentToken = RBRACE;
break;
case ';':
currentToken = SEMI;
break;
default:
currentToken = ERROR;
Error=TRUE;
break;
}
}
break;
case INCOMMENT_S1:
save = FALSE;
if (c == '*')
{
state = INCOMMENT;
}
else
{ ungetNextChar();
save = FALSE;
state = DONE;
currentToken = OVER;}
break;
case INCOMMENT:
save = FALSE;
if (c == EOF)
{ state = DONE;
currentToken = ENDFILE;
}
else if (c == '*') state = INCOMMENT_S2;
break;
case INCOMMENT_S2:
save = FALSE;
if (c == '/')
{
state = START;
}
else
{
state = INCOMMENT;
currentToken = OVER;}
break;
case INEQ:
state = DONE;
if (c == '=')
currentToken = EQ;
else
{ /* backup in the input */
ungetNextChar();
save = FALSE;
state = DONE;
currentToken = ASSIGN;
}
break;
case INNOEQ:
state = DONE;
if (c == '=')
currentToken = NOEQ;
else
{ /* backup in the input */
ungetNextChar();
save = FALSE;
state = DONE;
currentToken = ERROR;
}
break;
case INLE:
state = DONE;
if (c == '=')
currentToken = LE;
else
{ /* backup in the input */
ungetNextChar();
save = FALSE;
state = DONE;
currentToken = LT;
}
break;
case INGE:
state = DONE;
if (c == '=')
currentToken = GE;
else
{ /* backup in the input */
ungetNextChar();
save = FALSE;
state = DONE;
currentToken = GT;
}
break;
case INNUM:
if (!isdigit(c))
{ /* backup in the input */
ungetNextChar();
save = FALSE;
state = DONE;
currentToken = NUM;
}
break;
case INID:
if (!isalpha(c))
{ /* backup in the input */
ungetNextChar();
save = FALSE;
state = DONE;
currentToken = ID;
}
break;
case DONE:
default: /* should never happen */
fprintf(scan,"Scanner Bug: state= %d\n",state);
state = DONE;
currentToken = ERROR;
break;
}
if ((save) && (tokenStringIndex <= MAXTOKENLEN))
tokenString[tokenStringIndex++] = (char) c;
if (state == DONE)
{ tokenString[tokenStringIndex] = '\0';
if (currentToken == ID)
currentToken = reservedLookup(tokenString);
}
}
if (TraceScan)
{
fprintf(scan,"\t%d: ",lineno);
// printf("%d ",tokenno++);
printToken(currentToken,tokenString);
}
return currentToken;
} /* end getToken */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -