sdut 编译原理 小C语言--词法分析程序

#include 
#include
using namespace std;
map<string,string>mp;
void coo(string k,int id)
{
    if(id==1)
    {
        mp[k]="keyword";
    }
    else if(id==2)
    {
        mp[k]="boundary";
    }
    else if(id==3)
    {

        mp[k] = "identifier";
    }
    else if(id==4)
    {
        mp[k] = "operator";
    }
    else
    {
        mp[k] = "integer";
    }
}
void init()
{
    coo("main",1);
    coo("if",1);
    coo("int",1);
    coo("for",1);
    coo("while",1);
    coo("else",1);
    coo("(",2);
    coo(")",2);
    coo("{",2);
    coo("}",2);
    coo(",",2);
    coo(";",2);
    coo("=",4);
    coo("+",4);
    coo("-",4);
    coo("*",4);
    coo("/",4);
    coo(">=",4);
    coo("<=",4);
    coo("<",4);
    coo(">",4);
    coo("==",4);
    coo("!=",4);

}
bool isalnuM(char p)
{
    if(p>='0'&&p<='9')return 1;
    else return 0;
}
bool Is(string h)
{
    int len = h.size();
    for(int i=0; iif(!isalnuM(h[i]))return false;
    }
    return true;
}
int main()
{

  // freopen("in.txt","r",stdin);
    string tmp;
    init();
    while(getline(cin,tmp))
    {
        //stringstream s(tmp);
        string data = tmp;
        int len = tmp.size();
        string chu="";
        for(int i=0; iif(data[i]==' ')continue;
            chu+=data[i];
            //cout<
            //特判 == <= >= !=符号
            if(i+1'='&&data[i+1]=='=')
            {
                i++;
                chu+=data[i];
            }
            if(i+1'!'&&data[i+1]=='=')
            {
                i++;
                chu+=data[i];
            }
            if(i+1'<'&&data[i+1]=='=')
            {
                i++;
                chu+=data[i];
            }
            if(i+1'>'&&data[i+1]=='=')
            {
                i++;
                chu+=data[i];
            }
            /*  去除/t及空格 chu字符串  */
            int le =chu.size();
            int f =0;
            for(int i=0;iif(chu[i]!=' '&&chu[i]!='\t')
                {
                    f= 1;
                    break;
                }
            }
            if(!f)chu="";

            if(i+11])&&Is(chu)&&chu!="")
            //Is 函数区别 inta1 和纯数字
            {
                printf("(integer,%s)\n",chu.c_str());
                chu="";
            }
            //不是纯数字的肯定就是合法的标识符了,mp[chu]判定不是关键字
            //判定下一位不是字母也不是数字,说明chu就是标识符
            if(i+1isalpha(data[i+1])&&!isalnuM(data[i+1])&&mp[chu]==""&&chu!="")
            {

                printf("(identifier,%s)\n",chu.c_str());
                chu="";
            }

            //  cout<
            if(mp[chu]!=""&&chu!="")
            {
                //将inta1 和int区别开
                if(mp[chu]=="keyword")
                {

                    if(isalnuM(data[i+1])||isalpha(data[i+1]))
                    {
                        continue;
                        //如果当前下一位还是字母或数字,说明当前这个字符串是标识符
                    }
                    else
                    {
                        printf("(%s,%s)\n",mp[chu].c_str(),chu.c_str());
                        chu = "";
                    }
                    continue;

                }

                printf("(%s,%s)\n",mp[chu].c_str(),chu.c_str());
                chu = "";
            }

        }

    }

    return 0;
}

你可能感兴趣的:(vim,acm,编译原理,sdut)