编译原理(词法分析)

时间:2021-05-11 19:54:09
#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;
}