• 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++;
        }
    
    }

  • 相关阅读:
    Python安装的库列表导出到文件和批量安装库文件
    Selenium之浏览器驱动下载和配置使用
    测试面试计算题--python
    软件质量模型
    用例要素和设计方法
    python的层级
    day 14:深浅copy,数据结构 ,函数,set集合,变量作用域、返回值
    day 8:open文件和with的使用
    day 1:计算机发展史和组成部分
    day 2:计算机的基础知识,编程语言分类
  • 原文地址:https://www.cnblogs.com/hanqilin/p/4878081.html
Copyright © 2020-2023  润新知