• 1014编译原理第二次作业(修改版1)


    #include<stdio.h>
    #include<string.h>
    #define max 100
    int n=0,i=0;
    char token[max];
    char a;
    char word[max];
    
    void fuhao();
    void shuzi();
    
    
    
    main()
    {
        char *rwtab[6]={"begin","if","then","while","do","end"};
        int p=0,j=0,k=0,m=0,q=1;
        int key=0;
        printf("请输入源程序(以#键结束):");
        do{
            scanf("%c",&a);
            word[p]=a;
            p++;
        }while(a!='#');
    
        //关键字的判断    
        
        while(word[i]!='#')
        {    
        while((word[i]>='a'&&word[i]<='z')||(word[i]>='A'&&word[i]<='Z')||(word[i]>='0'&&word[i]<='9'))
        {
            if((word[i]>='a'&&word[i]<='z')||(word[i]>='A'&&word[i]<='Z')||(word[i]>='0'&&word[i]<='9'))
            {
                token[n]=word[i];
                n++;
                token[n]='';
                i++; 
                if(word[i]>='0'&&word[i]<='9')
                    q++;
            }
    
        for(k=0;k<6;k++)
        {
            key=0;
            if(strcmp(rwtab[k],token)==0)
            {
                printf("%s   %d
    ",rwtab[k],k+1);
                key=1;
                break;
            }
        }    }
         if((word[i-q]>='a'&&word[i-q]<='z')||(word[i-q]>='A'&&word[i-q]<='Z'))
         {
        if(key==0)
        {
                printf("%s   10
    ",token);
        }
            }
        key=1;
        i=i+1;
        n=0;
        }
        fuhao();
        shuzi();
    }
    //特殊符号的判断
    void fuhao()
    {
        int p=0,sign=0;
        for(p=0;p<max;p++)
        {
            switch(word[p])
            {
            case '+':
                printf("+    13
    ");
                break;
            case '-':
                printf("-    14
    ");
                break;
            case '*':
                printf("*    15
    ");
                break;
            case '/':
                printf("/    16
    ");
                break;
            case ':':
                if(word[p+1]=='=')
                {
                    printf(":=   18
    ");
                    p++;
                }
                else
                    printf(":    17
    ");
                break;
            case '<':
                if(word[p+1]=='=')
                {
                    printf("<=     21
    ");
                    sign=1;
                    p++;
                }
                else if(word[p+1]=='>')
                {
                    printf("<>     22
    ");
                    sign=1;
                    p++;
                }
                else if(sign==0)
                    printf("<    20
    ");
                break;
            case '>':
                if(word[p+1]=='=')
                {
                    printf(">=    24
    ");
                    p++;
                }
                else
                    printf(">    23
    ");
                break;
            case '=':
                printf("=    25
    ");
                break;
            case ';':
                printf(";    26
    ");
                break;
            case '(':
                printf("(    27
    ");
                break;
            case ')':
                printf(")    28
    ");
                break;
            case '#':
                printf("#    29
    ");
                break;
            }
        }
    }
    //数字的判断
    void shuzi()
    {
        int p=0,j=0,t=0,key=0,q=1;
        char Shu[100];
        while(word[p]!='#')
        {
            key=0;
            j=0;
            
        while(word[p]>='0'&&word[p]<='9')
        {
            Shu[j]=word[p];
            p++;
            j++;
            key=1;
            q++;
        }
        if((word[p-q]>='a'&&word[p-q]<='z')||(word[p-q]>='A'&&word[p-q]<='Z'))
        {
            key=0;
            q=1;
        }
    
        if(key==1)
        {
            for(t=0;t<j;t++)
            {
                printf("%c",Shu[t]);
            }
            printf("    11
    ");
            j=0;
        }
        p++;
        }
    
    }

  • 相关阅读:
    【HDU
    【HDU
    【AtCoder
    【POJ
    Apache、Nginx、IIS日志记录分析&详解
    在Windows Server 2008 R2上安装IIS
    SQL 注入漏洞研究
    利用Nginx实现反向代理web服务器(Linux+Nginx+Mysql+PHP)
    CentOS 7 LAMP搭建并且部署sqli-labs
    Kali 下配置Nessus-2020年3月新版
  • 原文地址:https://www.cnblogs.com/hanqilin/p/4878081.html
Copyright © 2020-2023  润新知