实验三、 递归下降分析程序实验

Posted on 2016-12-17 22:08  170何强  阅读(114)  评论(0编辑  收藏  举报
#include<stdlib.h>
#include<stdio.h>
#include<string.h>
char s[10];   
int x=0;
void A();          
void B();           
void C();          
void D(); 
void E();
 
int main()
{
    int len;
    printf("请输入算术表达式:(以#为结束)\n");
    scanf("%s",s);
    len=strlen(s);
    s[len]='#';
    s[len+1]='\0';
    A();
    printf("True!\n");
    strcpy(s,"");
    x=0;
    return 0;
}
 
void A()
{
    C();
    B();
}
 
void B()
{
    if(s[x]=='+'||s[x]=='-')
    {
        x++;
        C();
        B();
    } 
}
 
void C()
{
    E();
    D();
}
 
void D()
{
    if(s[x]=='*'||s[x]=='/')
    {
        x++;
        E();
        D();
    }
}
 
void E()
{
    if(s[x]>='a'&&s[x]<='z')
    {
        x++;
    }
    else if(s[x]>=0&&s[x]<=9)
    {
        x++;
    }
    else if (s[x]=='(')
    {     
        x++;
        A();
        if(s[x]==')')
        {
            x++; 
        }
        else
        {
            printf("Error!\n");
            exit(0);
        }
    } 
    else
    {
        printf("Error!\n"); 
        exit(0);
    }
}

 

Copyright © 2024 170何强
Powered by .NET 8.0 on Kubernetes