zoukankan      html  css  js  c++  java
  • 简易的词法分析程序

    #include <stdio.h>
    #include <string.h>
    char prog[666],token[8],ch;
    int syn,p,m,n,sum;
    char *rwtab[6]={"begin","if","then","while","do","end"};
    scaner();
    main()
    {
        p=0;
        printf("\n please input a string(end with '#'):\n");
        do{
            scanf("%c",&ch);
            prog[p++]=ch;
        }while(ch!='#');
        p=0;
        do{
            scaner();
            switch(syn)
            {
                case 11:printf("( %-10d%5d )\n",sum,syn);
                break;
                case -1:printf("you have input a wrong string\n");
                getch();
                exit(0);
                default: printf("( %-10s%5d )\n",token,syn);
                break;
            }
        }while(syn!=0);
        getch();
    }
        scaner()
        {  
            sum=0;
        for(m=0;m<8;m++)token[m++]=NULL;
        ch=prog[p++];
        m=0;
        while((ch==' ')||(ch=='\n'))ch=prog[p++];
        if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
        { 
            while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='0')&&(ch<='9')))
            {
                token[m++]=ch;
                ch=prog[p++];
            }
            p--;
            syn=10;
            for(n=0;n<6;n++)
                if(strcmp(token,rwtab[n])==0)
                    { 
                        syn=n+1;
                        break;
                    }
        }
        else if((ch>='0')&&(ch<='9'))
        { 
            while((ch>='0')&&(ch<='9'))
            { 
                sum=sum*10+ch-'0';
                ch=prog[p++];
            }
            p--;
            syn=11;
        }
        else switch(ch)
        { 
            case '<':token[m++]=ch;
                ch=prog[p++];
                if(ch=='=')
                {  
                    syn=22;
                    token[m++]=ch;
                }
                else
                {  
                    syn=20;
                    p--;
                }
                break;
            case '>':token[m++]=ch;
                ch=prog[p++];
                if(ch=='=')
                { 
                    syn=24;
                    token[m++]=ch;
                }
                else
                { 
                    syn=23;
                    p--;
                }
                break;
            case '+': token[m++]=ch;
                ch=prog[p++];
                if(ch=='+')
                { 
                    syn=17;
                    token[m++]=ch;
                }
                else
                { 
                    syn=13;
                    p--;
                }
                break;
            case '-':token[m++]=ch;
                ch=prog[p++];
                if(ch=='-')
                { 
                    syn=29;
                    token[m++]=ch;
                }
                else
                { 
                    syn=14;
                    p--;
                }
                break;
            case '!':ch=prog[p++];
                if(ch=='=')
                { 
                    syn=21;
                    token[m++]=ch;
                }
                else
                { 
                    syn=31;
                    p--;
                }
                break;
            case '=':token[m++]=ch;
                ch=prog[p++];
                if(ch=='=')
                {    
                    syn=25;
                    token[m++]=ch;
                }
                else
                { 
                    syn=18;
                    p--;
                }
                break;
            case '*': syn=15;
                token[m++]=ch;
                break;
            case '/': syn=16;
                token[m++]=ch;
                break;
            case '(': syn=27;
                token[m++]=ch;
                break;
            case ')': syn=28;
                token[m++]=ch;
                break;
            case '{': syn=5;
                token[m++]=ch;
                break;
            case '}': syn=6;
                token[m++]=ch;
                break;
            case ';': syn=26;
                token[m++]=ch;
                break;
            case '\"': syn=30;
                token[m++]=ch;
                break;
            case '#': syn=0;
                token[m++]=ch;
                break;
            case ':':syn=17;
                token[m++]=ch;
                break;
            default: syn=-1;
                break;
                }
        token[m++]='\0';
        printf("感谢您使用本程序!");
    }

  • 相关阅读:
    Android测试:从零开始3—— Instrumented单元测试1
    Android测试:从零开始2——local单元测试
    自定义封装 banner 组件
    常用表单 组件封装
    ContentProvider域名替换小工具
    接口回调封装
    日常记录-代码中Background后Padding 失效
    EditText 限制输入整数和小数 的位数
    (求助)对某一颜色,设置透明度 alpha 后,其他使用该颜色的地方 受到影响!!!!原因未知
    图片选择器ImageEditContainer
  • 原文地址:https://www.cnblogs.com/bin763043666/p/5924516.html
Copyright © 2011-2022 走看看