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

#include <iostream>
#include<bits/stdc++.h>
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; i<len; i++)
    {
        if(!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; i<len; i++)
        {
            if(data[i]==' ')continue;
            chu+=data[i];
            //cout<<chu<<endl;
            //特判 == <= >= !=符号
            if(i+1<len&&data[i]=='='&&data[i+1]=='=')
            {
                i++;
                chu+=data[i];
            }
            if(i+1<len&&data[i]=='!'&&data[i+1]=='=')
            {
                i++;
                chu+=data[i];
            }
            if(i+1<len&&data[i]=='<'&&data[i+1]=='=')
            {
                i++;
                chu+=data[i];
            }
            if(i+1<len&&data[i]=='>'&&data[i+1]=='=')
            {
                i++;
                chu+=data[i];
            }
            /*  去除/t及空格 chu字符串  */
            int le =chu.size();
            int f =0;
            for(int i=0;i<le;i++)
            {
                if(chu[i]!=' '&&chu[i]!='\t')
                {
                    f= 1;
                    break;
                }
            }
            if(!f)chu="";

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

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

            //  cout<<chu<<endl;
            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;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值