标题:自己写了一个词法分析器。有错,求大神帮忙修改。
只看楼主
wannetta
Rank: 1
等 级:新手上路
帖 子:3
专家分:0
注 册:2013-10-24
结帖率:0
 问题点数:0 回复次数:1 
自己写了一个词法分析器。有错,求大神帮忙修改。
#include<iostream>
#include<stdio.h>
#include<fstream>
using namespace std;


enum statetype{INNUM, INID, INASSIGN, STRING, OPERATION, INCOMMENT, START, DONE};
char Token[100];
bool IsCout;
//int count = 0;
bool IsOPERATION;
bool IsINCOMMENT;

StateType state;


char type[10];
char keyword[14][20]={"main","cin","cout","int","char","bool","float","return","if","switch","while","do","case","break"};
char operation[9][3] = { "+", "-", "*",  "/","%", "++", "--", "+=", "-=" };
char incomment[8][3] = { "<<", ">>", "<=", ">=", "==", "!=", "&&", "||" };

void c_init()
{
    state = Strat;
//    count = -1;
    IsCout = true;
    IsOPERATION = false;
    IsINCOMMENT = false;
   
    memset(type, 0, sizeof(type));
    memset(Token, 0, sizeof(Token));
}

void c_out()
{    if( IsCout )
    {
        int i;
        for( i=0; i < count; i++ )
        {
            printf("%c", Token[i]);
        }  
        
        printf("  ");
        for( i=0; i < 10; i++ )
        {
            printf("%c", type[i]);
        }
        printf("\n");
    }
    c_init();
}


bool IsINASSIGN(char c[])
{
    for(int i=0; i < 14; i++)
    {
        if( !strcmp(keyword[i], c) ) return true;  
    }
    return false;
}

bool IsINCOMMENT(char c[])
{
    for(int i=0; i < 8; i++)
    {
        if( !strcmp(incomment[i], c) ) return true;
    }
    return false;
}

struct node
{
    bool flag;
    char c;
}

void c_calc()
{
    freopen("test.cpp", "r", stdin);

    node temp;
    temp.flag=false;//初始化为无存放临时值

    c_init();
   
    while( 1 )
    {         
        //count++;

        if( temp.flag )
        { Token[count]=temp.c;
            temp.flag = false; }
        
        else
        {
            //if( scanf("%c", &Token[count]) == EOF )
            //{ break; }
        }
        if( state == Start )            
        {
            if( isdigit(Token[count]) )
            { state = INNUM; }//数字
            else
            {
                if( isalpha(Token[count]) )
                { state = INID;  }//标识符
                else
                {
                    if( !strcmp(Token, "\'") || !strcmp(Token, "\"") )
                    { state = STRING; }
                    else
                    {
                        if( (Token[count] == ' ') || (Token[count] == '\n') )
                        { state = DONE;  IsCout = false; }
                        else
                        {
                            if( !strcmp(Token, "+") || !strcmp(Token, "-") || !strcmp(Token, "*") || !strcmp(Token, "%") || !strcmp(Token, "/") )
                            { state = OPERATION; }
                            else  state = INCOMMENT;   //特殊符号
                        }
                    }
                }
            }
        }

        switch( state )
        {
            case INNUM:
                {                        
                    if( !isdigit(Token[count]) )
                    {  
                        temp.flag = true;  temp.c = Token[count];     
                        
                        if( (Token[count] == ' ') || (Token[count] == '\n')  ){ temp.flag = false; }
                                                   
                        strcpy(type, "数");
                        state = DONE; break;
                    }
                    break;
                }
            case INID:
                {                        
                    if( !isdigit(Token[count]) && !isalpha(Token[count])  &&  (Token[count] != '.') )
                    {
                        temp.flag = true;  temp.c = Token[count];
               
                        if( (Token[count] == ' ') || (Token[count] == '\n') ){ temp.flag = false; }

                        Token[count] = '\0';   
                        if( IsINASSIGN(Token) ) strcpy(type, "关键字");
                        else strcpy(type, "标识符");

                        state = DONE; break;
                    }
                    break;
                }
            case STRING:
                {
                    if( Token[0] == '\'' )
                    {
                        if( count > 0  &&  Token[0] == Token[count] )
                        {
                            count++;
                            strcpy(type, "字符");
                            state = DONE; break;
                        }
                        break;
                    }
                    if( Token[0] == '\"' )
                    {
                        if( count > 0  &&  Token[0] == Token[count] )
                        {
                            count++;
                            strcpy(type, "串");
                            state = DONE; break;
                        }
                        break;
                    }
                    break;
                }
            case OPERATION:
                {
                    if( count > 0 )
                    {
                        if( isdigit(Token[count]) || isalpha(Token[count]) )
                        { temp.flag = true;  temp.c = Token[count]; }
                        else count++;

                        strcpy(type, "运算符号");
                        state = DONE; break;
                    }
                    break;
                }
            case INCOMMENT:
                {        
                    if( IsINCOMMENT )
                    {
                        if( !IsINCOMMENT(Token) )
                        { temp.flag = true; temp.c = Token[count];  count--;}
                    }

                    if( !strcmp(Token, "<") || !strcmp(Token, ">") || !strcmp(Token, "!") || !strcmp(Token, "=") || !strcmp(Token, "+") || !strcmp(Token, "-") || !strcmp(Token, "|") || !strcmp(Token, "&") )
                    { IsINCOMMENT = true; break; }
                    else
                    {
                        strcpy(type, "特殊符号");
                        //count++;
                        state = DONE; break;
                    }
                    break;
                }
            case DONE: break;
        }

        if( state == DONE ){ c_out(); }   
    }
}


void main()
{
    c_calc();
}
搜索更多相关主题的帖子: operation keyword include return Token 
2014-11-09 22:08
菜鸟,求帮忙
Rank: 4
等 级:业余侠客
帖 子:197
专家分:267
注 册:2015-7-11
得分:0 
吓我呢
2015-07-12 14:51



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-438350-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.577597 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved