再不开始来不及了,可咱没学过编译结构啊,只能把已知的存储的模式写了,经测试目前是没发现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();
}