⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 scan.c

📁 tiny 编译器程序的设计 修改了原作者的程序 现在能生成中间代码
💻 C
字号:
#include"globals.h"
#include"util.h"
#include"scan.h"

char tokenString[MAXTOKENLEN+1];
#define BUFLEN 256
int state,start;
static char lineBuf[BUFLEN];
static int linepos=0;
static int bufsize=0;
static char getNextChar(void)
{  if(!(linepos<bufsize))
      { lineno++;
        if(fgets(lineBuf,BUFLEN-1,source))
        {fprintf(listing,"%4d:%s",lineno,lineBuf);
         bufsize=strlen(lineBuf);
         linepos=0;
         return lineBuf[linepos++];
        }
    else return EOF;
    }
   else return lineBuf[linepos++];
}  

static  void ungetNextChar(void)
{linepos--;}

static struct
{ char *str;
   TokenType tok;
}reservedWords[MAXRESERVED]
={{"if",IF},{"then",THEN},{"else",ELSE},{"end",END},
{"repeat",REPEAT},{"until",UNTIL},{"read",READ},
{"write",WRITE}};

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;
}

int fail()
{
	switch(state){
	case 0: state=13;break;
	case 13:state=16;break;
	default:state=19;break;
	}
	return state;
}
TokenType getToken()
{ TokenType currentToken;
    int notfind=TRUE;
	int i=0;
	char c;
	state=0;
	 while(notfind) {
		switch(state) {
		case 0: c=getNextChar();
			if(c==' '||c=='\t'||c=='\n') {
				state=0;
			}
			else if(c=='<') state=1;
			else if(c=='=') state=2;
			else if(c=='+')  state=3;
			else if(c=='*') state=4;
			else if(c=='-') state=5;
			else if(c=='/') state=6;
			else if(c=='(') state=7;
			else if(c==')') state=8;
			else if(c=='{') state=9;
            else if(c==';') state=10;
            else if(c==':') state=11;
			else if(c==EOF) state=12;
			else {ungetNextChar();fail();}
			break;
		case 1:  currentToken=LT;notfind=FALSE;break;
	   	case 2:  currentToken=EQ;notfind=FALSE;break;
	    case 3:  currentToken=PLUS;notfind=FALSE;break;
        case 4:  currentToken=TIMES;notfind=FALSE;break;
        case 5:  currentToken=MINUS;notfind=FALSE;break;
        case 6:  currentToken=OVER;notfind=FALSE;break;
        case 7:  currentToken=LPAREN;notfind=FALSE;break;
        case 8:  currentToken=RPAREN;notfind=FALSE;break;
        case 9:   do{state=0;
                         c=getNextChar();
                        } while(c!='}');break;
        case 10:  currentToken=SEMI;notfind=FALSE;break;
		case 11:  c=getNextChar();
                if(c=='=') currentToken=ASSIGN;notfind=FALSE;break;
		case 12:  currentToken=ENDFILE;notfind=FALSE;break;
	    case 13:c=getNextChar();tokenString[i++]=c;
                if(isalpha(c)) state=14;
	  	        else {ungetNextChar();i=0;fail();};
		        break;
		case 14:c=getNextChar();tokenString[i++]=c;
			if(isalpha(c)) state=14;
			else if(isdigit(c)) state=14;
			else state=15;
			break;
		case 15:ungetNextChar();tokenString[i-1]='\0';
                        currentToken=reservedLookup(tokenString);notfind=FALSE;break;
		case 16:c=getNextChar();tokenString[i++]=c;
			if(isdigit(c)) state=17;
			else  state=fail();
			break;
		case 17:c=getNextChar();tokenString[i++]=c;
			if(isdigit(c)) state=17;
			else state=18;
			break;
		case 18:ungetNextChar();tokenString[i-1]='\0';
            currentToken=NUM;notfind=FALSE;break;
		case 19:currentToken=ERROR;notfind=FALSE;break;
	    default: currentToken=ERROR;notfind=FALSE;break;
                }
	}
                fprintf(listing,"\t%d:",lineno);
                printToken(currentToken,tokenString);
                return currentToken;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -