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

📄 scan.cpp

📁 CMinus 小型编译器的词法分析与语法分析部分
💻 CPP
字号:
/****************************************************/
/* File: scan.c                                     */
/* The scanner implementation for the TINY compiler */
/* Compiler Construction: Principles and Practice   */
/* Kenneth C. Louden                                */
/****************************************************/

#include "globals.h"
#include "util.h"
#include "scan.h"

/* states in scanner DFA */
/* comments */
typedef enum
   { START,INASSIGN,INCOMMENT,INOVER,INNUM,INID,INLT,INGT,INNOEQ,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 = (int)strlen(lineBuf);
      linepos = 0;
      return lineBuf[linepos++];
    }
    else
    { EOF_flag = TRUE;
      return EOF;
    }
  }
  else return lineBuf[linepos++];
}

static void ungetNextChar(void)
{ if (!EOF_flag) linepos--;}

static struct
    { char* str;
      TokenType tok;
    } reservedWords[MAXRESERVED]
   = {{"if",IF},{"else",ELSE},{"while",WHILE},{"return",RETURN},{"int",INT},{"void",VOID}};

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  */
/****************************************/

TokenType getToken(void)
{
   int tokenStringIndex = 0;
   TokenType currentToken;
   StateType state = START;
   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 == ' ') || (c == '\t') || (c == '\n'))
           save = FALSE;
		 else if (c == '<')
		   state = INLT;
		 else if (c == '>')
		   state = INGT;
		 else if (c == '!')
		   state = INNOEQ;
         else if (c == '/')
           state = INOVER;
         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 = LPAREN;
               break;
             case ')':
               currentToken = RPAREN;
               break;
             case '[':
               currentToken = LBRA;
               break;
             case ']':
               currentToken = RBRA;
               break;
			 case '{':
               currentToken = LBRACE;
               break;
			 case '}':
               currentToken = RBRACE;
               break;
             case ';':
               currentToken = SEMI;
               break;
			 case ',':
               currentToken = COMMA;
               break;
             default:
               currentToken = ERROR;
               break;
           }
         }
         break;
       case INCOMMENT:
         save = FALSE;
         if (c == EOF)
         { state = DONE;
           currentToken = ENDFILE;
         }
		 else if ((c == '*') && (getNextChar( ) =='/')) {
			 	 save = FALSE;
				 state = START;
		 }
         break;
	   case INOVER:
		   if (c == '*')
		   {
			   save = FALSE;
			   tokenStringIndex --;
			   state = INCOMMENT;
		   }
		   else 
		   {
			   state = DONE;
			   currentToken = OVER;
			   ungetNextChar();
		   }
		   break;
       case INASSIGN:
         state = DONE;
         if (c == '=')
           currentToken = EQ;
         else
         { 
		   currentToken = ASSIGN;
           ungetNextChar();
           save = FALSE;
         }
         break;
       case INNUM:
         if (!isdigit(c))
         { 
           ungetNextChar();
           save = FALSE;
           state = DONE;
           currentToken = NUM;
         }
         break;
       case INID:
         if (!isalpha(c))
         { 
           ungetNextChar();
           save = FALSE;
           state = DONE;
           currentToken = ID;
         }
         break;
	   case INLT:
		   state = DONE;
		   if (c == '=')
			   currentToken = LTEQ;
		   else {
			   currentToken = LT;
			   ungetNextChar();
			   save = FALSE;
		   }
		   break;
	   case INGT:
		   state = DONE;
		   if (c == '=')
			   currentToken = GTEQ;
		   else {
			   currentToken = GT;
		   	   ungetNextChar();
			   save = FALSE;
		   }
		   break;
	   case INNOEQ:
		   state = DONE;
		   if (c == '=')
			   currentToken = NOEQ;
		   else
		   {
			   ungetNextChar();
			   save = FALSE;
			   currentToken = ERROR;
		   }
		   break;
       case DONE:
       default: 
         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;
}

⌨️ 快捷键说明

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