?? scan.cpp
字號:
/****************************************************/
/* File: scan.c */
/* Main program for c- compiler */
/* Compiler Construction: Principles and Practice */
/* Maker:Liukai */
/****************************************************/
#include "globals.h"
#include "util.h"
#include "scan.h"
/* states in scanner DFA */
typedef enum
{ START , INASSIGN , INCOMMENT1 , INCOMMENT2 , INCOMMENT3 , INCOMMENT4 , INNUM , INID , INLT , INMT , INNEQ , DONE }
StateType;
/* lexeme of identifier or reserved word */
char tokenString[MAXTOKENLEN+1];
/* BUFLEN = length of the input buffer for
source code lines */
#define BUFLEN 256
static char lineBuf[BUFLEN]; /* holds the current line */
static int linepos = 0; /* current position in LineBuf */
static int bufsize = 0; /* current size of buffer string */
static int EOF_flag = FALSE; /* corrects ungetNextChar behavior on EOF */
/* getNextChar fetches the next non-blank character
from lineBuf, reading in a new line if lineBuf is
exhausted */
static int getNextChar(void)
{ if (!(linepos < bufsize))
{ lineno++;
if (fgets(lineBuf,BUFLEN-1,source))
{ if (EchoSource) fprintf(listing,"%4d: %s",lineno,lineBuf);
bufsize = strlen(lineBuf);
linepos = 0;
return lineBuf[linepos++];
}
else
{ EOF_flag = TRUE;
return EOF;
}
}
else return lineBuf[linepos++];
}
/* ungetNextChar backtracks one character
in lineBuf */
static void ungetNextChar(void)
{ if (!EOF_flag) linepos-- ;}
/* lookup table of reserved words */
static struct
{ char* str;
TokenType tok;
} reservedWords[MAXRESERVED]
= {{"if",IF},{"else",ELSE},{"while",WHILE},
{"int",INT},{"return",RETURN},{"void",VOID},
};
/* 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;
}
/****************************************/
/* the primary function of the scanner */
/****************************************/
/* function getToken returns the
* next token in source file
*/
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)
{ int c = getNextChar();
save = TRUE;
switch (state)
{ case START:
if (isdigit(c))
state = INNUM;
else if (isalpha(c))
state = INID;
else if (c == '=')
state = INASSIGN;
else if (c == '<')
state = INLT;
else if (c == '>')
state = INMT;
else if (c == '!')
state = INNEQ;
else if ((c == ' ') || (c == '\t') || (c == '\n'))
save = FALSE;
else if (c == '/')
{ save = FALSE;
state = INCOMMENT1;
}
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 = OVER;
break;
*/
case '(':
currentToken = LPAREN;
break;
case ')':
currentToken = RPAREN;
break;
case ';':
currentToken = SEMI;
break;
case ',':
currentToken = DOUHAO;
break;
case '[':
currentToken = ZLPAREN;
break;
case ']':
currentToken = ZRPAREN;
break;
case '{':
currentToken = DLPAREN;
break;
case '}':
currentToken = DRPAREN;
break;
default:
currentToken = ERROR;
break;
}
}
break;
case INCOMMENT1:
save = false;
// c = getNextChar();
if (c == '*')
state = INCOMMENT2;
else
{
ungetNextChar();
ungetNextChar();
ungetNextChar();
ungetNextChar();
state = INCOMMENT4;
c = getNextChar();
}
/* if (c == EOF)
{ state = DONE;
currentToken = ENDFILE;
}
*/
break;
case INCOMMENT2:
save = false;
if(c == '*')
{
state = INCOMMENT3;
}
break;
case INCOMMENT4:
save = true ;
c = getNextChar();
state = DONE;
currentToken = OVER;
break;
case INCOMMENT3:
save = false ;
if(c == '/')
{
state = START ;
currentToken = OVER;
}
else
{
state = INCOMMENT2;
}
break;
case INLT:
state = DONE;
if (c == '=')
currentToken = LET;
else
{ /* backup in the input */
ungetNextChar();
save = false ;
currentToken = LT;
}
break;
case INMT:
state = DONE;
if (c == '=')
currentToken = MET;
else
{ /* backup in the input */
ungetNextChar();
save = false ;
currentToken = MT;
}
break;
case INNEQ:
state = DONE;
if (c == '=')
currentToken = NEQ;
else
{ /* backup in the input */
ungetNextChar();
save = false ;
currentToken = ERROR;
}
break;
case INASSIGN:
state = DONE;
if (c == '=')
currentToken = EQ;
else
{ /* backup in the input */
ungetNextChar();
save = false ;
currentToken = ASSIGN;
}
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(listing,"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(listing,"\t%d: ",lineno);
printToken(currentToken,tokenString);
}
return currentToken;
} /* end getToken */
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -