词法分析优化

来源:互联网 发布:python if 不等于 编辑:程序博客网 时间:2024/06/05 11:01
<pre name="code" class="cpp">#include <iostream>#include <cstring>#include <string>using namespace std;class Node{public:    string name;    int type;};struct word{    Node node[1000];    int ans,t;    string sstack;    void lian10_11()    {        if(sstack[0]>='0'&&sstack[0]<='9')            //cout<<"11 "<<sstack<<endl;        {            node[t].name=sstack;            node[t++].type=11;        }        if(sstack[0]>='a'&&sstack[0]<='z')        {            node[t].name=sstack;            node[t++].type=10;        }        sstack.clear();    }    void  getarray(string s)    {        t=0;        int n=s.length();        sstack.clear();        for(int i=0; i<n; i++)        {            if(s[i]=='b')            {                if(s.substr(i,5)=="begin"&&s[i+5]==' ')                {                    lian10_11();                    {                        node[t].name="begin";                        node[t++].type=1;                    }                    i+=4;                }                else sstack+=s[i];            }            else if(s[i]=='i')            {                if(s.substr(i,2)=="if"&&s[i+2]==' ')                {                    lian10_11();                    {                        node[t].name="if";                        node[t++].type=2;                    }                    i+=1;                }                else sstack+=s[i];            }            else  if(s[i]=='t')            {                if(s.substr(i,4)=="then"&&s[i+4]==' ')                {                    lian10_11();                    {                        node[t].name="then";                        node[t++].type=3;                    }                    i+=3;                }                else sstack+=s[i];            }            else if(s[i]=='w')            {                if(s.substr(i,5)=="while"&&s[i+5]==' ')                {                    lian10_11();                    {                        node[t].name="while";                        node[t++].type=4;                    }                    i+=4;                }                else sstack+=s[i];            }            else if(s[i]=='d')            {                if(s.substr(i,2)=="do"&&s[i+2]==' ')                {                    lian10_11();                    {                        node[t].name="do";                        node[t++].type=5;                    }                    i+=1;                }                else sstack+=s[i];            }            else if(s[i]=='e')            {                if(s.substr(i,3)=="end"&&s[i+3]==' ')                {                    lian10_11();                    {                        node[t].name="end";                        node[t++].type=6;                    }                    i+=2;                }                else sstack+=s[i];            }            else if(s[i]=='+')            {                lian10_11();                {                    node[t].name="+";                    node[t++].type=13;                }            }            else if(s[i]=='-')            {                lian10_11();                {                    node[t].name="-";                    node[t++].type=14;                }            }            else if(s[i]=='*')            {                lian10_11();                {                    node[t].name="*";                    node[t++].type=15;                }            }            else if(s[i]=='/')            {                lian10_11();                {                    node[t].name="/";                    node[t++].type=16;                }            }            else if(s[i]==':')            {                lian10_11();                if(s[i+1]=='=')                {                    {                        node[t].name=":=";                        node[t++].type=18;                    }                    i++;                }                else                {                    node[t].name=":";                    node[t++].type=17;                }            }            else if(s[i]=='<')            {                lian10_11();                if(s[i+1]=='>')                {                    {                        node[t].name="<>";                        node[t++].type=21;                    }                    i++;                }                else if(s[i+1]=='=')                {                    {                        node[t].name="<=";                        node[t++].type=22;                    }                    i++;                }                else                {                    node[t].name="<";                    node[t++].type=20;                }            }            else if(s[i]=='>')            {                lian10_11();                if(s[i+1]=='=')                {                    {                        node[t].name=">=";                        node[t++].type=24;                    }                    i++;                }                else                {                    node[t].name=">";                    node[t++].type=23;                }            }            else if(s[i]=='=')            {                lian10_11();                {                    node[t].name="=";                    node[t++].type=15;                }            }            else if(s[i]==';')            {                lian10_11();                {                    node[t].name=";";                    node[t++].type=26;                }            }            else if(s[i]=='(')            {                lian10_11();                {                    node[t].name="(";                    node[t++].type=27;                }            }            else if(s[i]==')')            {                lian10_11();                {                    node[t].name=")";                    node[t++].type=28;                }            }            else if(s[i]=='#')            {                lian10_11();                {                    node[t].name="#";                    node[t++].type=0;                }            }            else if((s[i]>='0'&&s[i]<='9')||(s[i]>='a'&&s[i]<='z'))                sstack+=s[i];        }        if(!sstack.empty())        {            lian10_11();        }        ans=t;    }};int main(){    string str;    getline(cin,str);    word w;    w.getarray(str);    cout<<w.ans<<endl;    for(int i=0; i<w.ans; i++)        cout<<i<<" :  "<<w.node[i].name<<" "<<w.node[i].type<<endl;    return 0;}


                                             
0 0
原创粉丝点击