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

    #include<stdio.h>
    #include<string.h>  
    char str[10];
    int index=0;
    void E();            //E->TX;
    void X();            //X->+TX|-TX| e
    void T();            //T->FY
    void Y();            //Y->*FY |/fy| e
    void F();            //F->(E) | id
    int id();           //id
    int main()
    {
        int len;
        int m;
    
            printf("请输入算数表达式:");
            scanf("%s",str);
            len=strlen(str);
            str[len]='#';
            str[len+1]='\0';
            E();
            printf("正确语句!\n");
            strcpy(str,"");
            index=0;
        
        return 0;
    }
    void E()
    {
        T();
        X();
    }
    void X()
    {
        if(str[index]=='+')
        {
            index++;
            T();
            X();
        } 
        else if(str[index]=='-')
        {
            index++;
            T();
            X();
        } 
    }
    void T()
    {
        F();
        Y();
    }
    void Y()
    {
        if(str[index]=='*')
        {
            index++;
            F();
            Y();
        }
        else if(str[index]=='/')
        {
            index++;
            F();
            Y();
        }
    }
    void F()
    {
        if(id())
        {
            index++;
        }
        else if (str[index]=='(')
        {     
            index++;
            E();
            if(str[index]==')')
            {
                index++; 
            }else{
                printf("\n分析失败!\n");
                exit (0);
            }
        } 
        else{
            printf("分析失败!\n"); 
            exit(0);
        }
     }
    int id()
    {
         if(str[index]>='0'&&str[index]<='9')
        {
            while( str[index+1]>='0'&&str[index+1]<='9' )
            {
                index++;
            }
            if(str[index+1]>='a'&&str[index+1]<='z' )
                return 0;
    
            return 1;
        }
         else if(str[index]>='a'&&str[index]<='z' )
         {
             return 1;
         }
         else 
             return 0;
         
    }
  • 相关阅读:
    订餐系统
    throw和throws
    CF999E Solution
    CF1142B Solution
    CF965C Solution
    CF963B Solution
    CF999F Solution
    CF975D Solution
    CF997B Solution
    hdu 2553 N皇后
  • 原文地址:https://www.cnblogs.com/ken520/p/5070625.html
Copyright © 2011-2022 走看看