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); } }

  结果如下:

原文地址:https://www.cnblogs.com/jjy520/p/5091996.html