zoukankan      html  css  js  c++  java
  • 词法分析

    #include <stdio.h>
    #include <string.h>
    
    char string[80],simbol[8],ch;
    int wordID,index,m,n,sum;
    char *rwtab[6]={"begin","if","then","while","do","end"};
    
    void scaner(void);
    
    main()
    {
        int index=0;
        printf("请输入代码,并以串#号键结束:\n");
    
        do{
                scanf("%c",&ch);
                string[index++]=ch;
        }while(ch!='#');
    
        index=0;
        do{
                scaner();
                switch(wordID)
                {
                    case 11:
                        printf("( %-10d%5d )\n",sum,wordID);
                    break;
    
                    case -1:
                        printf("错误\n");
                        return 0;
                    break;
    
                    default:
                    printf("( %-10s%5d )\n",simbol,wordID);
                    break;
                }
            }while(wordID!=0);
     return 0;
     }
    
    void scaner(void)
    {
        sum=0;
    
        for(m=0;m<8;m++)
            simbol[m++]= NULL;
    
            ch=string[index++];
            m=0;
    
        while((ch==' ')||(ch=='\n'))
            ch=string[index++];
    
        if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))
         {
            while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='0')&&(ch<='9')))
            {
                simbol[m++]=ch;
                ch=string[index++];
            }
    
            index--;
            wordID=10;
    
            for(n=0;n<6;n++)
            if(strcmp(simbol,rwtab[n])==0)
            {
                wordID=n+1;
                break;
            }
         }
         else if((ch>='0')&&(ch<='9'))
         {
            while((ch>='0')&&(ch<='9'))
            {
                sum=sum*10+ch-'0';
                ch=string[index++];
            }
            index--;
            wordID=11;
        }
        else
        {
            switch(ch)
            {
            case '<':
                simbol[m++]=ch;
                ch=string[index++];
                if(ch=='=')
                {
                    wordID=22;
                    simbol[m++]=ch;
                }
                else
                {
                    wordID=20;
                    index--;
                }
            break;
    
            case '>':
                simbol[m++]=ch;
                ch=string[index++];
                if(ch=='=')
                {
                    wordID=24;
                    simbol[m++]=ch;
                }
                else
                {
                    wordID=23;
                    index--;
                }
            break;
    
            case '+':
                simbol[m++]=ch;
                ch=string[index++];
                if(ch=='+')
                {
                    wordID=17;
                    simbol[m++]=ch;
                }
                else
                {
                    wordID=13;
                    index--;
                }
            break;
    
            case '-':
                simbol[m++]=ch;
                ch=string[index++];
                if(ch=='-')
                {
                    wordID=29;
                    simbol[m++]=ch;
                }
                else
                {
                    wordID=14;
                    index--;
                }
            break;
    
            case '!':
                ch=string[index++];
                if(ch=='=')
                {
                    wordID=21;
                    simbol[m++]=ch;
                }
                else
                {
                    wordID=31;
                    index--;
                }
            break;
    
            case '=':
                simbol[m++]=ch;
                ch=string[index++];
                if(ch=='=')
                {
                    wordID=25;
                    simbol[m++]=ch;
                }
                else
                {
                    wordID=18;
                    index--;
                }
            break;
    
            case '*':
                wordID=15;
                simbol[m++]=ch;
            break;
    
            case '/':
                wordID=16;
                simbol[m++]=ch;
            break;
    
            case '(':
                wordID=27;
                simbol[m++]=ch;
            break;
    
            case ')':
                wordID=28;
                simbol[m++]=ch;
            break;
    
            case '{':
                wordID=5;
                simbol[m++]=ch;
            break;
    
            case '}':
                wordID=6;
                simbol[m++]=ch;
            break;
    
            case ';':
                wordID=26;
                simbol[m++]=ch;
            break;
    
            case '\"':
                wordID=30;
                simbol[m++]=ch;
            break;
    
            case '#':
                wordID=0;
                simbol[m++]=ch;
            break;
    
            case ':':
                wordID=17;
                simbol[m++]=ch;
            break;
    
            default:
                wordID=-1;
            break;
            }
        }
            simbol[m++]='\0';
    }

  • 相关阅读:
    What is the purpose of FormsAuthenticationTicket isPersistent property?
    Forms Authentication configurations
    What is the difference between localStorage, sessionStorage, session and cookies?
    Set-Cookie
    申威机器信息
    systemctl 关闭图形界面的办法
    sourcetree 使用
    在 Web 项目中应用 Apache Shiro
    移动web开发框架
    从0开始搭建symphony
  • 原文地址:https://www.cnblogs.com/wban48/p/5985188.html
Copyright © 2011-2022 走看看