• 0917 实验一 词法分析程序


    #include <stdio.h>
    #include <string.h>
    #include <iostream.h>
    #define BEGIN 1
    #define IF 2 
    #define THEN 3
    #define WHILE 4
    #define DO 5
    #define END 6
    #define INT 11
    #define LT 20
    #define LE 21
    #define EQ 24
    #define NE 22
    #define GT 12
    #define GE 24
    #define IS 18
    #define PL 13
    #define MI 14
    #define MU 15
    #define DI 16
    #define SS 17
    #define ID 10
    #define AI  26
    #define BI 27
    #define HI 58
    #define JI  0
    
    char *keyword[8]={"begin","end","if","then","do","while"};
    int i=0,j=0,k=0,t=0;
    char ch,dck[20];
    char * chr_form[100];
    char * int_form[100];
    char form[1000];
    int q=0,temp;
    void GetChar()
    {
        ch=form[k];
        k++;
    }
    void getbc()
    {
        while(ch==' ')
        {
            
            GetChar();
        }
    }
    void concat()
    {
        dck[i]=ch;
        i++;
    }
    bool isletter()
    {
        if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
            return(1);
        else
            return(0);
    }
    bool isdigit()
    {
    if(((ch)<='9')&&((ch)>='0'))
    return (1);
    else
    return (0);
    }
    int reserve()
    {
        for(int q=0;q<5;q++)
        {
            if(strcmp(keyword[q],dck)==0)
                return q;
            if(q==4)
                return -1;
        }
    }
    void retract()
    {
        k--;
        ch=NULL;
    }
    char *insertld()
    {
        chr_form[j]=dck;
        j++;
        return chr_form[0];
    }
    char * insertconst()
    {
        int_form[t]=dck;
        t++;
        return int_form[0];
    }
    int code;
    void output(int a,char *p1,char *p2)
    {
        cout<<"	种别码:"<<a<<"	 单词值:";
            while(p1<=p2)
            {
                printf("%s",*p1);
                p1++;
            }
            cout<<endl;
    }
    
    void analyze()
    {
        GetChar();
        getbc();
        if(isletter())
        {
            while (isletter()||isdigit())
            {
                concat();
            GetChar();
            }
            retract();
            code=reserve();
            switch(code)
            {
            case 0:cout<<"单词符号:" <<dck<<"  种别码为: "<<BEGIN<<endl;break;
    
            case 1:cout<<"单词符号:" <<dck<<"  种别码为: "<<END<<endl;break;
        
            case 2:cout<<"单词符号:" <<dck<<"  种别码为: "<<IF<<endl;break;
    
            case 3:cout<<"单词符号:" <<dck<<"  种别码为: "<<THEN<<endl;break;
            case 4:cout<<"单词符号:" <<dck<<"  种别码为: "<<DO<<endl;break;
            case 5:cout<<"单词符号:" <<dck<<"  种别码为: "<<WHILE<<endl;break;
            
          default:
              cout<<"单词符号:" <<dck<<"  种别码为: "<<ID<<endl;break;
    
    
        }
        }
        else
        {
            if( isdigit())
            {
              while(isdigit()||ch=='.')
              {
                  concat();
                  GetChar();
              }
              retract();
              cout<<"单词符号:"<<dck<<"  种别码为:"<<INT<<endl;
            }
            else
            {
                switch(ch)
                {
                case'+': cout<<"单词符号:+  种别码为: "<<PL<<endl;break;
                case'-': cout<<"单词符号:-  种别码为: "<<MI<<endl;break;
                case'*': cout<<"单词符号:*  种别码为: "<<MU<<endl;break;
                case'/': cout<<"单词符号:/  种别码为: "<<DI<<endl;break;
                case';': cout<<"单词符号:;  种别码为: "<<AI<<endl;break;
                case'(': cout<<"单词符号:(  种别码为: "<<BI<<endl;break;
                case')': cout<<"单词符号:)  种别码为: "<<HI<<endl;break;
                case'#': cout<<"单词符号:#  种别码为: "<<JI<<endl;break;
            
                case':':GetChar();
                    if(ch=='=')
                    {
                        cout<<"单词符号::=  种别码为: "<<IS<<endl;break;
                    }
                    else
                    {
                        retract();
                        cout<<"单词符号::   种别码为: "<<SS<<endl;break;
                    }
                case'=':cout<<"单词符号:=  种别码:"<<EQ<<endl; break;
                case'>':GetChar();switch(ch)
                        {
                case'=':cout<<"单词符号:>=  种别码:"<<GE<<endl;break;
                default: retract;
                    cout<<"单词符号:=> 种别码:"<<GT<<endl;break;
                        }
                case'<':GetChar();
                    switch(ch)
                    {
                    case'=':cout<<"单词符号:<=  种别码:"<<LE<<endl;break;
                    case'>':cout<<"单词符号:<>  种别码:"<<NE<<endl;break;
                   default: retract();
                        cout<<"单词符号:<  种别码:"<<LT<<endl;break;
                }
            }
        }
        }
    
        while(k<q)
        {
            for(int p=0;p<50;p++)
                dck[p]='';
            i=0;
            analyze();
        }
    }
    
    void main ()
    {
        printf("输入一段程序,以^结束:");
        form[0]=cin.get();
        for(q=1;form[q-1]!='!!';q++)
        {
            form[q]=cin.get();
            if(form[q]=='^')
            {
                printf("所输入的程序段为:");
                printf("
    ");
                cout.write(form,q);
                break;
            }
        }
        cout<<endl;
        analyze();
    
    }

  • 相关阅读:
    团队冲刺第五天
    团队冲刺第四天
    学习进度条---第八周
    团队冲刺第三天
    团队冲刺第二天
    团队冲刺第一天
    学习进度条--第七周
    课堂练习--第7周(两人结对)
    学习进度条--第六周
    第一次 在Java课上的编程
  • 原文地址:https://www.cnblogs.com/l549023320/p/4827189.html
Copyright © 2020-2023  润新知