编译原理(词法分析)

#include <iostream>
using namespace std;
int main()
{
    string s;
    while(cin>>s)
    {
        int n=s.length();
        string sstack="";
        for(int i=0; i<n; i++)
        {
            if(s[i]=='b')
            {
                if(s.substr(i,5)=="begin")
                {
                    if(sstack[0]>='0'&&sstack[0]<='9')
                        cout<<"11 "<<sstack<<endl;
                    if(sstack[0]>='a'&&sstack[0]<='z')
                        cout<<"10 "<<sstack<<endl;
                    sstack.clear();
                    cout<<"1 "<<"begin"<<endl;
                    i+=4;
                }
                else sstack+=s[i];
            }
            else if(s[i]=='i')
            {
                if(s.substr(i,2)=="if")
                {
                    if(sstack[0]>='0'&&sstack[0]<='9')
                        cout<<"11 "<<sstack<<endl;
                    if(sstack[0]>='a'&&sstack[0]<='z')
                        cout<<"10 "<<sstack<<endl;
                    sstack.clear();
                    cout<<"2 "<<"if"<<endl;
                    i+=1;
                }
                else sstack+=s[i];
            }
            else  if(s[i]=='t')
            {
                if(s.substr(i,4)=="then")
                {
                    if(sstack[0]>='0'&&sstack[0]<='9')
                        cout<<"11 "<<sstack<<endl;
                    if(sstack[0]>='a'&&sstack[0]<='z')
                        cout<<"10 "<<sstack<<endl;
                    sstack.clear();
                    cout<<"3 "<<"then"<<endl;
                    i+=3;
                }
                else sstack+=s[i];
            }
            else if(s[i]=='w')
            {
                if(s.substr(i,5)=="while")
                {
                    if(sstack[0]>='0'&&sstack[0]<='9')
                        cout<<"11 "<<sstack<<endl;
                    if(sstack[0]>='a'&&sstack[0]<='z')
                        cout<<"10 "<<sstack<<endl;
                    sstack.clear();
                    cout<<"4 "<<"while"<<endl;
                    i+=4;
                }
                else sstack+=s[i];
            }
            else if(s[i]=='d')
            {
                if(s.substr(i,2)=="do")
                {
                    if(sstack[0]>='0'&&sstack[0]<='9')
                        cout<<"11 "<<sstack<<endl;
                    if(sstack[0]>='a'&&sstack[0]<='z')
                        cout<<"10 "<<sstack<<endl;
                    sstack.clear();
                    cout<<"5 "<<"do"<<endl;
                    i+=1;
                }
                else sstack+=s[i];
            }
            else if(s[i]=='e')
            {
                if(s.substr(i,3)=="end")
                {
                    if(sstack[0]>='0'&&sstack[0]<='9')
                        cout<<"11 "<<sstack<<endl;
                    if(sstack[0]>='a'&&sstack[0]<='z')
                        cout<<"10 "<<sstack<<endl;
                    sstack.clear();
                    cout<<"6 "<<"end"<<endl;
                    i+=2;
                }
                else sstack+=s[i];
            }
            else if(s[i]=='+')
            {

                if(sstack[0]>='0'&&sstack[0]<='9')
                    cout<<"11 "<<sstack<<endl;
                if(sstack[0]>='a'&&sstack[0]<='z')
                    cout<<"10 "<<sstack<<endl;
                sstack.clear();
                cout<<"13 "<<"+"<<endl;

            }
            else if(s[i]=='-')
            {

                if(sstack[0]>='0'&&sstack[0]<='9')
                    cout<<"11 "<<sstack<<endl;
                if(sstack[0]>='a'&&sstack[0]<='z')
                    cout<<"10 "<<sstack<<endl;
                sstack.clear();
                cout<<"14 "<<"-"<<endl;

            }
            else if(s[i]=='*')
            {

                if(sstack[0]>='0'&&sstack[0]<='9')
                    cout<<"11 "<<sstack<<endl;
                if(sstack[0]>='a'&&sstack[0]<='z')
                    cout<<"10 "<<sstack<<endl;
                sstack.clear();
                cout<<"15 "<<"*"<<endl;

            }
            else if(s[i]=='/')
            {

                if(sstack[0]>='0'&&sstack[0]<='9')
                    cout<<"11 "<<sstack<<endl;
                if(sstack[0]>='a'&&sstack[0]<='z')
                    cout<<"10 "<<sstack<<endl;
                sstack.clear();
                cout<<"16 "<<"/"<<endl;

            }
            else if(s[i]==':')
            {

                if(sstack[0]>='0'&&sstack[0]<='9')
                    cout<<"11 "<<sstack<<endl;
                if(sstack[0]>='a'&&sstack[0]<='z')
                    cout<<"10 "<<sstack<<endl;
                sstack.clear();
                if(s[i+1]=='=')
                {
                    cout<<"18 "<<":="<<endl;
                    i++;
                }
                else
                    cout<<"17 "<<":"<<endl;

            }
            else if(s[i]=='<')
            {

                if(sstack[0]>='0'&&sstack[0]<='9')
                    cout<<"11 "<<sstack<<endl;
                if(sstack[0]>='a'&&sstack[0]<='z')
                    cout<<"10 "<<sstack<<endl;
                sstack.clear();
                if(s[i+1]=='>')
                {
                    cout<<"21 "<<"<>"<<endl;
                    i++;
                }
                else if(s[i+1]=='=')
                {
                    cout<<"22 "<<"<="<<endl;
                    i++;
                }
                else
                    cout<<"20 "<<"<"<<endl;

            }
            else if(s[i]=='>')
            {

                if(sstack[0]>='0'&&sstack[0]<='9')
                    cout<<"11 "<<sstack<<endl;
                if(sstack[0]>='a'&&sstack[0]<='z')
                    cout<<"10 "<<sstack<<endl;
                sstack.clear();
                if(s[i+1]=='=')
                {
                    cout<<"24 "<<"<="<<endl;
                    i++;
                }

                else
                    cout<<"23 "<<">"<<endl;

            }
            else if(s[i]=='=')
            {

                if(sstack[0]>='0'&&sstack[0]<='9')
                    cout<<"11 "<<sstack<<endl;
                if(sstack[0]>='a'&&sstack[0]<='z')
                    cout<<"10 "<<sstack<<endl;
                sstack.clear();
                cout<<"15 "<<"="<<endl;

            }
            else if(s[i]==';')
            {

                if(sstack[0]>='0'&&sstack[0]<='9')
                    cout<<"11 "<<sstack<<endl;
                if(sstack[0]>='a'&&sstack[0]<='z')
                    cout<<"10 "<<sstack<<endl;
                sstack.clear();
                cout<<"26 "<<";"<<endl;

            }
            else if(s[i]=='(')
            {

                if(sstack[0]>='0'&&sstack[0]<='9')
                    cout<<"11 "<<sstack<<endl;
                if(sstack[0]>='a'&&sstack[0]<='z')
                    cout<<"10 "<<sstack<<endl;
                sstack.clear();
                cout<<"27 "<<"("<<endl;

            }
            else if(s[i]==')')
            {

                if(sstack[0]>='0'&&sstack[0]<='9')
                    cout<<"11 "<<sstack<<endl;
                if(sstack[0]>='a'&&sstack[0]<='z')
                    cout<<"10 "<<sstack<<endl;
                sstack.clear();
                cout<<"28 "<<")"<<endl;

            }
            else if(s[i]=='#')
            {

                if(sstack[0]>='0'&&sstack[0]<='9')
                    cout<<"11 "<<sstack<<endl;
                if(sstack[0]>='a'&&sstack[0]<='z')
                    cout<<"10 "<<sstack<<endl;
                sstack.clear();
                cout<<"0 "<<"#"<<endl;

            }

            else if((s[i]>='0'&&s[i]<='9')||(s[i]>='a'&&s[i]<='z'))
                sstack+=s[i];
                /*
            else
            {
                if(sstack[0]>='0'&&sstack[0]<='9')
                    cout<<"11 "<<sstack[0]<<endl;
                if(sstack[0]>='a'&&sstack[0]<='z')
                    cout<<"10 "<<sstack[0]<<endl;
                sstack.clear();

            }
            */


        }
        if(sstack[0]>='0'&&sstack[0]<='9')
                        cout<<"11 "<<sstack<<endl;
                    if(sstack[0]>='a'&&sstack[0]<='z')
                        cout<<"10 "<<sstack<<endl;

    }
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值