?? scan.c
字號:
/* File: scan.c
The scanner implementation for the TINY compiler
*/
#include "globals.h"
#include "util.h"
#include "scan.h"
/* states in scanner DFA */
typedef enum
{ START, INASSIGN, INCOMMENT, INNUM, INID, 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 the buffer string */
static int lastPos = 0;
static int first = TRUE;
/* getNextChar gets the next non-blank character from lineBuf, reading in a new
line if lineBuf is exhausted */
/*
static char 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 return EOF;
}
else return lineBuf[linepos++];
}
*/
static void printLine(void)
{
char c;
int i;
lineno++;
fprintf(listing,"%d: ",lineno);
do{
c=getc(source);
fprintf(listing,"%c", c);
}while(c!='\n'&&c!=EOF);
rewind(source);
for(i=0;i<lastPos;i++)
getc(source);
}
static int getNextChar(void)
{
char c;
if(first){
printLine();
first=FALSE;
}
c=getc(source);
lastPos++;
if(c){
if(c=='\n')
printLine();
return c;
}
else
return EOF;
}
/* ungetNextChar backtracks one character in lineBuf */
static void ungetNextChar(int c)
{ ungetc(c, source);}
/* look table of reserved words */
static struct
{
char* str;
TokenType tok;
}reservedWords[MAXRESERVED]
={{"if",IF},{"then",THEN},{"else",ELSE},{"end",END},{"repeat",REPEAT},
{"until",UNTIL},{"read",READ},{"write",WRITE}};
/* look up an identifier to see if it is a reserved word
use 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;
}
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -