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

    #include<stdio.h>
    #include<string>
    char str[10];   //记录要分析的字符串
    int x=0;        //记录第一个字符
    
    void E();           
    void X();           
    void T();           
    void Y(); 
    void F(); 
    
    int main()
    {
        int len;
        printf("请输入算数表达式:");
        scanf("%s",str);
        len=strlen(str);
        str[len]='#';
        str[len+1]='';
        E();
        printf("
    True!
    ");
        strcpy(str,"");
        x=0;
        return 0;
    }
    
    void E()
    {
        T();
        X();
    }
    
    void X()
    {
        if(str[x]=='+'||str[x]=='-')
        {
            x++;
            T();
            X();
        } 
    }
    
    void T()
    {
        F();
        Y();
    }
    
    void Y()
    {
        if(str[x]=='*'||str[x]=='/')
        {
            x++;
            F();
            Y();
        }
    }
    
    void F()
    {
        if(str[x]>='a'&&str[x]<='z')
        {
            x++;
        }
        else if(str[x]>=0&&str[x]<=9)
        {
            x++;
        }
        else if (str[x]=='(')
        {     
            x++;
            E();
            if(str[x]==')')
            {
                x++; 
            }
            else
            {
                printf("
    Error!
    ");
                exit(0);
            }
        } 
        else
        {
            printf("
    Error!
    "); exit(0); } }

      结果如下:

  • 相关阅读:
    塔 · 第 二 条 约 定
    nyoj 325
    塔 · 第 一 条 约 定
    大一上
    Django之ORM
    mysql概念
    数据库索引
    使用pymysql进行数据库的增删改查
    sql注入攻击
    pymysql
  • 原文地址:https://www.cnblogs.com/jjy520/p/5091996.html
Copyright © 2011-2022 走看看