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

    #include <stdio.h>  
    #include <string.h>
    #define max 100  
    char a[max],b[8],ch;  
    int syn,p,m,n,sum;  
    char word[][6]={"begin","if","then","while","do","end"};   
    void mor()
    {    
        sum=0;  
        ch=a[p++];  
        m=0;      
        while((ch==' ')||(ch=='
    ')||(ch=='	'))  
            ch=a[p++];  
        if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))  
        {
            while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='0')&&(ch<='9')))  
            {  
                b[m++]=ch;  
                ch=a[p++];  
            }    
            p--; 
            syn=10;                    
            for(n=0;n<6;n++)  
               if(strcmp(b,word[n])==0)   
               {
                   syn=n+1;  
                   break;  
               }  
        }                                   
        else if((ch>='0')&&(ch<='9'))  
        {   
           while((ch>='0')&&(ch<='9'))  
           {  
               sum=sum*10+ch-'0';    
               ch=a[p++];  
           }  
           p--;  
           syn=11;  
        }  
        else                  
        {  
            switch(ch)  
            {  
            case '+':  
                syn=13;  
                b[m++]=ch;          
                break;  
            case '-':   
                syn=14;
                b[m++]=ch;   
                break; 
            case '*':  
                syn=15;  
                b[m++]=ch;  
                break;  
            case '/':  
                syn=16;  
                b[m++]=ch;  
                break;  
            case ':':  
                b[m++]=ch;  
                ch=a[p++];  
                if(ch=='=')  
                {  
                    syn=18;  
                    b[m++]=ch;  
                }  
                else  
                {   
                    syn=18;  
                    p--;  
                }  
                break; 
            case '<':
                b[m++]=ch;  
                ch=a[p++];  
                if(ch=='=')  
                {   
                    syn=21;  
                    b[m++]=ch;  
                }  
                else  
                {    
                    syn=20;  
                    p--;  
                }  
                break;  
            case '>':  
                b[m++]=ch;  
                ch=a[p++];  
                if(ch=='=')  
                {  
                    syn=24;  
                    b[m++]=ch;  
                }  
                else  
                {   
                    syn=23;  
                    p--;  
                }  
                break;
            case '=':  
                syn=25;  
                b[m++]=ch;  
                break;  
            case ';':  
                syn=26;  
                b[m++]=ch;  
                break;  
            case '(':   
                syn=27;  
                b[m++]=ch;  
                break;  
            case ')':  
                syn=28;  
                b[m++]=ch;  
                break;  
            case '#':   
                syn=0;  
                b[m++]=ch;  
                break; 
            case '!': 
                syn=-2;
                break;
            default:  
                syn=-1;  
                break;  
            }  
        }  
        b[m++]='';  
    }
    main()  
    {
        p=0;  
        printf("请输入一段程序段,便于词法分析(以!作为结束符):"); 
        do{
            scanf("%c",&a[p++]);    
        }while(a[p-1]!='!');     
        p=0;  
        do{  
            mor();  
            switch(syn)  
            {  
                case 11:  
                    printf("( %-5d%10d )
    ",syn,sum);  
                    break;  
                case -1:  
                    printf("你输入一个错的字符
    ");    
                    return 0;  
                    break;
                case -2:
                    break;
                default:   
                    printf("( %-5d%10s )
    ",syn,b);  
                    break;  
            }  
        }while(syn!=-2);  
    }
  • 相关阅读:
    java内部类
    java接口静态方法
    java接口默认方法
    多人协作项目如何保持代码风格统一
    美帝程序员薪资大曝光!2年经验google facebook员工真实薪资揭秘【2020年版】
    go并发编程by examples
    解决macbook外接显示屏vscode文字变模糊的问题
    zookeeper和kafka集群源码搭建
    zookeeper命令
    kafka简介和术语
  • 原文地址:https://www.cnblogs.com/04JC/p/5936863.html
Copyright © 2011-2022 走看看