zoukankan      html  css  js  c++  java
  • +递归下降语法分析程序设计

    消除左递归后的文法是:

    E→TE'

    E'→+TE'|-TE'|ε

    T→FT'

    T'→*FT'|/FT'|ε

    F→(E)|i

     
    #include<stdio.h>  
    #include<string>  
    char str[10];  
    int index=0;  
    void E();          
    void X();            
    void T();           
    void Y();          
    void F();             
    int main()  
    {  
        int len;  
        int m;  
        printf("请输入要测试的次数:");  
        scanf("%d",&m);  
        while(m--)  
        {  
            printf("请输入算数表达式:");  
            scanf("%s",str);  
            len=strlen(str);  
            str[len]='#';  
            str[len+1]='';  
            E();  
            printf("正确语句!
    ");  
            strcpy(str,"");  
            index=0;  
        }  
        return 0;  
    }  
    void E()  
    {  
        T();  
        X();  
    }  
    void X()  
    {  
        if(str[index]=='+'||str[index]=='-')  
        {  
            index++;  
            T();  
            X();  
        }   
    }  
    void T()  
    {  
        F();  
        Y();  
    }  
    void Y()  
    {  
        if(str[index]=='*'||str[index]=='/')  
        {  
            index++;  
            F();  
            Y();  
        }  
    }  
    void F()  
    {  
        if(str[index]=='i')  
        {  
            index++;  
        }  
        else if (str[index]=='(')  
        {      
            index++;  
            E();  
            if(str[index]==')')  
            {  
                index++;   
            }else{  
                printf("
    分析失败!
    ");  
                exit (0);  
            }  
        }   
        else{  
            printf("分析失败!
    ");   
            exit(0);  
        }  
     } 

      

  • 相关阅读:
    js 实现图片上传
    关于IOS不能使用JQUERY的ON事件
    js实现复制
    订单列表倒计时
    小程序实现倒计时
    微信小程序服务消息推送
    python爬虫七
    python爬虫六
    python爬虫五
    python爬虫四
  • 原文地址:https://www.cnblogs.com/caicaihong/p/5051582.html
Copyright © 2011-2022 走看看