数据结构课程设计--------文法分析中推导的建立(第一天进度)

再不开始来不及了,可咱没学过编译结构啊,只能把已知的存储的模式写了,经测试目前是没发现BUG啦

开发环境:WINTC 1.9.1

#include<stdio.h>
#include<conio.h>
#define maxsize 10
struct RuleNode
{
    char symbol;
    struct RuleNode *nextRule;
    struct RuleNode *next;
};
struct RuleBegin
{
    char beginsymbol;
    struct RuleNode *equal;
};
void RuleBuilder(struct RuleBegin *Rule)           /*对每一条规则进行存储*/
{
    struct RuleNode *newRule;
    struct RuleNode *nowRule;
    struct RuleNode *firstRule;
    char input;
    printf("Please input the rule with the end #:");
    input=getchar();
    input=getchar();
    Rule->beginsymbol=input;
    input=getchar();
    input=getchar();
    newRule=(struct RuleNode*)malloc(sizeof(struct RuleNode));
    newRule->symbol=input;
    Rule->equal=newRule;
    firstRule=newRule;
    nowRule=newRule;
    newRule=(struct RuleNode*)malloc(sizeof(struct RuleNode));
    nowRule->next=newRule;
    input=getchar();
    while(input!='#')
    {
        if(input!='|');
        {
            newRule->symbol=input;
            nowRule=newRule;
            newRule=(struct RuleNode*)malloc(sizeof(struct RuleNode));
            nowRule->next=newRule;
        }
        if(input=='|')
        {
            nowRule->next=NULL;
            newRule=(struct RuleNode*)malloc(sizeof(struct RuleNode));
            firstRule->nextRule=newRule;
            firstRule=newRule;
        }
        input=getchar();
    }
    newRule->next=NULL;
    firstRule->nextRule=NULL;
}
void RuleShow(struct RuleBegin *Rule)      /*对规则进行输出*/
{
    struct RuleNode *nextRule;
    struct RuleNode *firstRule;
    printf("%c::=",Rule->beginsymbol);
    nextRule=Rule->equal;
    firstRule=nextRule;
    while(nextRule->next!=NULL)
    {
        printf("%c",nextRule->symbol);
        nextRule=nextRule->next;
    }
    while(firstRule->nextRule!=NULL)
    {
        printf("|");
        firstRule=firstRule->nextRule;
        nextRule=firstRule;
        while(nextRule->next!=NULL)
        {
            printf("%c",nextRule->symbol);
            nextRule=nextRule->next;
        }
    }
    printf("/n");
}
main()
{
    int i,n;
    struct RuleBegin *Rule[maxsize];
    printf("Please input the Rule's number:");
    scanf("%d",&n);
    for(i=0;i<n;i++)
    {
        Rule[i]=(struct RuleBegin*)malloc(sizeof(struct RuleBegin));
        RuleBuilder(Rule[i]);
    }
    for(i=0;i<n;i++)
    {
        RuleShow(Rule[i]);
    }
    getch();
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值