Ejemplo n.º 1
0
/* La función getToken el siguiente token
 * en el archivo fuente
 */
TokenType getToken(void)
{  /* indice para guardar en tokenString */
   int tokenStringIndex = 0;
   /* guarda el token actual para ser retornado */
   TokenType currentToken;
   /* estado actual - siempre comienza con START */
   StateType state = START;
   /* flag para indicar el guardado en tokenString */
   int save;
   /* flags para indicar el estado de un NUM */ 
   int real = 0;
   int negativo = 0;
   int cientifico = 0;

   while (state != DONE)
   { int c = getNextChar();
     save = TRUE;
     switch (state)
     { case START:
         if (isdigit(c) || (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 == '/')
         { 
			c = getNextChar();
			if (c == '/')
			{
				save = FALSE;
				state = INCOMMENT;
			}
			else
			{
				ungetNextChar();
				state = START;
			}
         }
         else
         { state = DONE;
           switch (c)
           { case EOF:
               save = FALSE;
               currentToken = ENDFILE;
               break;
             case '<':
               currentToken = LT;
               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;
             default:
               currentToken = ERROR;
               break;
           }
         }
         break;
       case INCOMMENT:
         save = FALSE;
         if (c == EOF)
         { state = DONE;
           currentToken = ENDFILE;
         }
         else if (c == '\n') state = START;
         break;
       case INASSIGN:
         state = DONE;
         if (c == '=')
               currentToken = EQ;
         else
         { 
           ungetNextChar();
           currentToken =ASSIGN;
        }
         break;
       case INNUM:
         if (!isdigit(c))
         { 
			if(c=='-' && negativo == 0){
				negativo = 1;
			}else if (c=='.' && real == 0){
				real = 1;
			}else if (c=='e' && cientifico == 0){
				cientifico = 1;
				c = getNextChar();
				real = 0;
				if (c=='-'){
					negativo = 0;
				}else{
					ungetNextChar();
				}	
			}else{
					/* backup en el input */
					ungetNextChar();
					save = FALSE;
					state = DONE;
					currentToken = NUM;
			}
         }
         break;
       case INID:
         if (!isalpha(c))
         { /* backup en el input */
           ungetNextChar();
           save = FALSE;
           state = DONE;
           currentToken = ID;
         }
         break;
       case DONE:
       default: /* nunca deberia suceder */
         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;
} /* fin del getToken */
Ejemplo n.º 2
0
Archivo: scan.c Proyecto: cocojk/major
/* 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 = INLESS;
            else if (c == '>')
                state = INGREATER;
            else if (c == '!')
                state = INNOTEQ;
            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 = SEMI;
                    break;
                case ',':
                    currentToken = COMMA;
                    break;
                case '(':
                    currentToken = LPAREN;
                    break;
                case ')':
                    currentToken = RPAREN;
                    break;
                case '[':
                    currentToken = SLBRACKET;
                    break;
                case ']':
                    currentToken = SRBRACKET;
                    break;
                case '{':
                    currentToken = LBRACKET;
                    break;
                case '}':
                    currentToken = RBRACKET;
                    break;
                default:
                    currentToken = ERROR;
                    break;
                }
            }
            break;


        case INCOMMENT1:
            save = FALSE;
            if (c == '*')
            {

                state = INCOMMENT2;
            }
            else
            {
                state = DONE;
                ungetNextChar();
                currentToken = OVER;

            }
            break;

        case INCOMMENT2:
            save = FALSE;
            if(c == EOF)
            {
                state = DONE;
                currentToken = ENDFILE;
            }
            else if (c == '*')
            {
                state = INCOMMENT3;

            }

            break;

        case INCOMMENT3:
            save = FALSE;
            if (c == '/')
            {
                state = START;

            }
            else
            {
                state = INCOMMENT2;
            }
            break;



        case INASSIGN:
            state = DONE;
            if (c == '=')
                currentToken = EQ;
            else
            {   /* backup in the input */
                ungetNextChar();
                save = FALSE;
                currentToken = ASSIGN;
            }
            break;

        case INLESS:
            state = DONE;
            if (c == '=')
                currentToken = LESSEQ;
            else
            {   /* backup in the input */
                ungetNextChar();
                save = FALSE;
                currentToken = LESS;
            }
            break;

        case INGREATER:
            state = DONE;
            if (c == '=')
                currentToken = GREATEREQ;
            else
            {   /* backup in the input */
                ungetNextChar();
                save = FALSE;
                currentToken = GREATER;
            }
            break;

        case INNOTEQ:
            state = DONE;
            if (c == '=')
                currentToken = NOTEQ;
            else
            {   /* backup in the input */
                ungetNextChar();
                save = FALSE;
                currentToken = ERROR;
            }
            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 */
Ejemplo n.º 3
0
/* 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 = INLE;
         else if (c == '>')
           state = INGE;
         else if (c == '=')
           state = INEQ;
         else if (c == '!')
           state = INNE;
         else if ((c == ' ') || (c == '\t') || (c == '\n'))
           save = false;
         else if (c == '/')
         { //save = FALSE;
           state = ENTERING_COMMENT;
         } 
         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 = ENTERING_COMMENT;
               break;
             case '(':
               currentToken = LPAREN;
               break;
             case ')':
               currentToken = RPAREN;
               break;
             case ';':
               currentToken = SEMI;
               break;
             case '[':
               currentToken = LBRACKET;
               break;
             case ']':
               currentToken = RBRACKET;
               break;
             case '{':
               currentToken = LBRACE;
               break;
             case '}':
               currentToken = RBRACE;
               break;
             default:
               currentToken = ERROR;
               break;
           }
         }
         break;
       /*case ENTERING_COMMENT:
         save = false;
         if (c == EOF)   //????????????????
         { state = DONE;
           currentToken = ENDFILE;
         }
         else if (c == '*') state = INCOMMENT;
         else { 
           currentToken = TIMES;
           save = true;
           state = DONE;
           }
         break; */
       case ENTERING_COMMENT:
            save = false;
            if(c == '*') state = INCOMMENT;
            else {
                 ungetNextChar();
                 currentToken = OVER;
                 state = DONE;
                 save = true;
                 }
            break;
       case INCOMMENT:
            save = false;
            if(c == '*') state = EXITING_COMMENT;
            break;
       case EXITING_COMMENT:
            save = false;
            if(c == '/') state = START;
            else if (c == '*') state = EXITING_COMMENT;
            else state = INCOMMENT;
            break;
                 
       case INLE:
         state = DONE;
         if (c == '=')
           currentToken = LE;
         else
         { /* backup in the input */
           ungetNextChar();
           //save = FALSE;
           currentToken = LT;
         }
         break;
         case INGE:
         state = DONE;
         if (c == '=')
           currentToken = GE;
         else
         { /* backup in the input */
           ungetNextChar();
           //save = FALSE;
           currentToken = GT;
         }
         break;
         case INEQ:
         state = DONE;
         if (c == '=')
           currentToken = EQ;
         else
         { /* backup in the input */
           ungetNextChar();
           //save = FALSE;
           currentToken = ASSIGN;
         }
         break;
         case INNE:       //不等号。。。。。。。。。。。。 
         state = DONE;
         if (c == '=')
           currentToken = NE;
         else
         { /* backup in the input */
           ungetNextChar();
           save = false;
           currentToken = ERROR;
         }
         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;
       //tokenIndex[tokenStringIndex] = currentToken;
     }
       
     if (state == DONE)
     { tokenString[tokenStringIndex] = '\0';
       if (currentToken == ID) { 
         currentToken = reservedLookup(tokenString);
         }
     }
   }
  /* if (TraceScan) {
     fprintf(listing,"\t%d: ",lineno);
     printToken(currentToken,tokenString);
   }*/
   
   //printf("YES\n");
   //fputs(ansToken[currentToken],target);
    printf("%s\n",ansToken[currentToken]);
   return currentToken;
} /* end getToken */ 
Ejemplo n.º 4
0
/* function getToken returns the
 * next token in source file
 */
AsmTokenType TAsmAnalyze::getToken(void)
{
	int tokenStrIdx = 0;		/* index for storing into tokenString */
    tokenString = "";
	AsmTokenType curToken;			/* holds current token to be returned */
	StateType state = START;	/* current state - always begins at START */
	int save;					/* flag to indicate save to tokenString */

	//linepos=0;
	//bufsize=0;
	//cout<<linepos<<endl;
	while (state != DONE)
	{
		char c = getNextChar();

		save = TRUE;
		switch (state)
		{
			case START:
				 if (IsName(c))        // Is Name
   					  state = INID;
				 else if (isdigit(c))    // Is Hex Number
					  state = INHEXNUM;
				 else if (c == '#')
					  state = INSHARP;
				 else if (c == ',')
					  state = INXCHAR;
				 else if ((c == ' ') || (c == '\t'))
					  save = FALSE;
				 else if (c == '[')
                 {
                      save = FALSE;
					  state = INSTRING;
                 }
				 else if (c == '{')
				 {
					  save = FALSE;
					  state = INCOMMENT;
				 }
				 else if (c == ';')
				 {
					  save = FALSE;
					  state = INCOMMENT_SINGLE;
				 }
				 else
				 {
					  state = DONE;
					  switch (c)
					  {
						case EOF:
							save = FALSE;
							curToken = ENDFILE;
						break;
						case ':':
							curToken = COLON;
						break;
						case '=':
							curToken = EQUAL;
						break;
						case '@':
							curToken = ATCHAR;
						break;
						case '\n':
							curToken = ENDLINE;
						break;
						default:
							curToken = TOKENERROR;
					  }
				 }
			break;

			case INCOMMENT:
				save = FALSE;
                if (c == EOF)
                    state = DONE;
				else if (c == '}')
					state = START;
			break;

			case INCOMMENT_SINGLE:
				save = FALSE;
				if (c == '\n')
				{
                	state = DONE;
 				    curToken = ENDLINE;
                }
			break;

			case INXCHAR:
				state = DONE;
				if (c == 'X')
					curToken = XCHAR;
				else
				{
					ungetNextChar();	/* backup in the input */
					save = FALSE;
					curToken = TOKENERROR;
				}
			break;

			case INSHARP:
				if (!isxdigit(c))
				{
					ungetNextChar();	/* backup in the input */
					save = FALSE;
					state = DONE;
                    if (tokenStrIdx > 1)
					    curToken = SHARP;
                    else
					    curToken = TOKENERROR;
				}
			break;

			case INSTRING:
                save = TRUE;
				if (c == ']')
                {
					ungetNextChar();	/* backup in the input */
					save = FALSE;
					state = DONE;
                    curToken = STRING;
                }
			break;

			case INID:
				if (!IsName(c) && !isdigit(c))
				{
					ungetNextChar();	/* backup in the input */
					save = FALSE;
					state = DONE;
					curToken = ID;
				}
			break;

			case INHEXNUM:
				if (!isxdigit(c))
				{
					ungetNextChar();	/* backup in the input */
					save = FALSE;
					state = DONE;
					curToken = HEXNUM;
				}
			break;

			case DONE:
			default:
				//cout<<"Scanner Bug: state= "<<state<<endl;
				state = DONE;
				curToken = TOKENERROR;
			break;
		}
		if ((save) && (tokenStrIdx <= MAXTOKENLEN))
		{
			//tokenString[tokenStrIdx++] = c;
            tokenString+=c;
            tokenStrIdx++;
			//cout<<tokenStrIdx<<" "<<c<<"\n";
		}

		if (state == DONE)
		{
			//tokenString[tokenStrIdx++] = '\0';
			if (curToken == ID) curToken = reservedLookup(tokenString);
			/*if (curToken == ID)
			{
				if (IsHex(tokenString))
					curToken = HEXNUM;
				else
					curToken = ID;
			}*/
		}
	}

	TokenPos++;
	//cout<<"\t"<<lineno<<": ";
	//cout<<tokenString<<" "<<TokenPos<<endl;
	//printToken(curToken, tokenString);

	CurrentToken = curToken;
	return curToken;
}
Ejemplo n.º 5
0
// 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) {
		char 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 == '{') {
				save = FALSE;
				state = INCOMMENT;
			} else {
				state = DONE;
				switch (c) {
				case EOF:
					save = FALSE;
					currentToken = ENDFILE;
					break;
				case '=':
					currentToken = EQ;
					break;
				case '<':
					currentToken = LT;
					break;
				case '+':
					currentToken = PLUS;
					break;
				case '-':
					currentToken = MINUS;
					break;
				case '*':
					currentToken = TIMES;
					break;
				case '/':
					currentToken = OVER;
					break;
				case '(':
					currentToken = LPAREN;
				case ')':
					currentToken = RPAREN;
				case ';':
					currentToken = SEMI;
					break;
				default:
					currentToken = ERROR;
					break;
				}
			}
			break;
		case INCOMMENT:
			save = FALSE;
			if (c == '}') state = START;
			break;
		case INASSIGN:
			state = DONE;
			if (c == '=')
				currentToken = ASSIGN;
			else {
				// backup in the input
				ungetNextChar();
				save = FALSE;
				currentToken = ERROR;
			}
			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 ++ ] = 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