• 词法分析程序(C)


    #include <stdio.h>  
    #include <string.h>  
      
    char string[80],word[8],ch;  
    int type,p,i,n,sum;  
    char keyword[][6]={"begin","if","then","while","do","end"};    //关键字数组
       
    void scaner(void);  
      
    main()  
    {  
        p=0;  
        printf("
    
    请输入一个程序段(以'#'结束):
    ");  
        do{  
                scanf("%c",&ch);  
                string[p++]=ch;  
        }while(ch!='#');  
         printf("单词符号	种别码 
    ");  
        p=0;  
        do{  
                scaner();  
                switch(type)  
                {  
                    case 11:  
                        printf("   %-10d%5d
    ",sum,type);  
                    break;  
                    case -1:  
                        printf("有无法识别的字符
    ");    
                        return 0;  
                    break;       
                    default:   
                    printf("   %-10s%5d 
    ",word,type);  
                    break;  
                }  
            }while(type!=0);  
     }  
      
    void scaner(void)  
    {    
        sum=0;  
        for(i=0;i<8;i++)  
           word[i++]= NULL;  
          
    
        ch=string[p++];  
        i=0;  
              
        while((ch==' ')||(ch=='
    '))  
            ch=string[p++];  
          
        if(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A')))  
         {   
            while(((ch<='z')&&(ch>='a'))||((ch<='Z')&&(ch>='A'))||((ch>='0')&&(ch<='9')))  
            {  
                word[i++]=ch;  
                ch=string[p++];  
            }
              
         p--; 
         type=10;                     //先将以字母开头的字符识别为标识符
      
            for(n=0;n<6;n++)  
            if(strcmp(word,keyword[n])==0)    //关键字数组比较,相同则表示为关键字,种别码即为下标值加一
            {   
                type=n+1;  
                break;  
            }  
        }                                   
        
        
        else if((ch>='0')&&(ch<='9'))  
         {   
            while((ch>='0')&&(ch<='9'))  
            {  
                sum=sum*10+ch-'0';    //将字符型转化为整形sum
                ch=string[p++];  
            }  
            p--;  
            type=11;  
         }  
    
    
        else                   //else语句内识别除关键字、标识符、数字以外的其他字符
        {  
            switch(ch)  
            {  
            case '+':  
                type=13;  
                word[i++]=ch;          
            break;  
      
            case '-':   
                type=14;
                word[i++]=ch;   
            break; 
            
             case '*':  
                type=15;  
                word[i++]=ch;  
            break;  
      
            case '/':  
                type=16;  
                word[i++]=ch;  
            break;  
    
            case ':':  
                word[i++]=ch;  
                ch=string[p++];  
             if(ch=='=')  
               {  
                   type=18;  
                   word[i++]=ch;  
               }  
                else  
                {   
                    type=18;  
                    p--;  
                }  
            break; 
            
            case '<':  
                word[i++]=ch;  
                ch=string[p++];  
                if(ch=='=')  
                {   
                    type=21;  
                    word[i++]=ch;  
                }  
                else  
                {    
                    type=20;  
                    p--;  
                }  
            break;  
      
            case '>':  
                word[i++]=ch;  
                ch=string[p++];  
                if(ch=='=')  
                {  
                    type=24;  
                    word[i++]=ch;  
                }  
                else  
                {   
                    type=23;  
                    p--;  
                }  
            break;
            
            case '=':  
                    type=25;  
                    word[i++]=ch;  
            break;  
      
            case ';':  
                type=26;  
                word[i++]=ch;  
            break;  
    
            case '(':   
                type=27;  
                word[i++]=ch;  
            break;  
      
            case ')':  
                type=28;  
                word[i++]=ch;  
            break;  
      
            case '#':   
                type=0;  
                word[i++]=ch;  
            break;  
      
            default:  
                type=-1;  
            break;  
            }  
        }  
            word[i++]='';                      //word字符数组变字符串
    }
  • 相关阅读:
    J2EE规范标准
    怎样用Google APIs和Google的应用系统进行集成(4)----获得Access Token以通过一些Google APIs的OAuth2认证
    [BestCoder Round #3] hdu 4908 BestCoder Sequence (计数)
    大数据存储之分布式文件系统(一)
    List与array的相互转换
    TRIZ系列-创新原理-31-多孔材料原理
    关于app.FragmentManager和v4包的FragmentPagerAdapter冲突
    POJ 1163 The Triangle
    ChinaVis2015 第一天会议
    JSON初入门
  • 原文地址:https://www.cnblogs.com/Ming-jay/p/5936830.html
Copyright © 2020-2023  润新知