• 词法分析


    ---恢复内容开始---

    词法分析程序的功能

    输入:所给文法的源程序字符串

    输出:二元组(syn,token或sum)构成的序列

    其中syn为单词种别码;token为存放的单词自身字符串;sum为整形常数。

    例如:对源程序begin x=9;if x>9 then x:=2*x+|/3;end#的源文件,经过词法分析后输出如下序列:

    (1,begin)(10,x)(18,:=)(11,9)(26,;)(2,if).......

     

     

    #include <stdio.h>  
    #include <string.h>  
    #include<stdlib.h>  
    char prog[80],token[8],ch;  
    int syn,p,m,n,sum;  
    char *rwtab[6]={"begin","if","then","while","do","end"};  
       
    void scaner();  
      
    void main()  
    {  
        p=0;  
        printf("
     please input a string(end with'#'):
    ");  
          
        do{  
                scanf("%c",&ch);  
                prog[p++]=ch;  
        }while(ch!='#');  
          
        p=0;  
        do{  
                scaner();  
                switch(syn)  
                {  
                    case 11:  
                        printf("( %-10d%5d )
    ",sum,syn);  
                    break;  
                      
                    case -1:  
                        printf("you have input a wrong string
    ");    
                        getchar(); 
                   exit(0);  
                      
                    default:   
                    printf("( %-10s%5d )
    ",token,syn);  
                    break;  
                }  
            }while(syn!=0);
        getchar();
     }  
      
    void scaner()  
    {    
        sum=0;  
          
        for(m=0;m<8;m++)  
            token[m++]=NULL;  
          
            ch=prog[p++];  
            m=0;  
              
        while((ch==' ')||(ch=='
    '))  
            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++]='';  
    } 

    ---恢复内容结束---

  • 相关阅读:
    Elasticsearch集群使用ik分词器
    ES Docs-3:Modifying Data
    ES Docs-2:Exploring ES cluster
    ES Docs-1:Installation Elasticsearch-2.3.1
    Ubuntu的apt-get本地源搭配——根据需要自己添加软件作源
    Linux中Root用户密码变更、密码忘记
    Eclipse安装SVN插件
    【转】Subversion快速入门教程-动画演示
    【转】‘svn’不是内部或外部命令,也不是可运行的程序
    pt-summary pt-mysql-summary工具
  • 原文地址:https://www.cnblogs.com/ashh/p/5925041.html
Copyright © 2020-2023  润新知