求助用C程序对给定文法进行递归下降分析问题

大王娟 学生  2017-11-12 10:42:06
学习编译原理 ,老师让用C语言写了一个程序,识别一个文法,在VC++6.0中运行结果老是显示cpp1.exe停止工作,新人才疏学浅,希望得到大家的帮助,谢谢谢谢!!
文法:S->(T) | aM
T->SN
N->bSN | ε
M->+S | ε (ε 代表空符号)
代码:#include <stdio.h>
#include<dos.h>
#include<stdlib.h>
#include<string.h>
char v[50], u[50], d[200], e[10];
char ch;
int n1,i1=0,flag=1,n=7;
int S();
int M();
int T();
int N();
void input();
void input1();
void output();

//================================================
void main() /*递归分析*/
{
int f,p,j=0;
char x;
d[0]='S';
d[1]='=';
d[2]=' ';
d[3]='(';
d[4]='T';
d[5]=')';
d[6]='#';

printf("请输入字符串(长度<50,以#号结束)\n");
do{
scanf("%c",&ch);
v[j]=ch;
j++;
} while(ch!='#');

n1=j;
ch=u[0]=v[0];
printf("文法\t分析串\t\t分析字符\t剩余串\n");
f=S();
if (f==0) return;
if (ch=='#')
{
printf("accept\n");
p=0;
x=d[p];
while(x!='#')
{
printf("%c",x);p=p+1;x=d[p]; /*输出推导式*/
}
}
else
{
printf("error\n");
printf("回车返回\n");
getchar();getchar();
return;
}
printf("\n");
printf("回车返回\n");
getchar();
getchar();
}


//================================================
int S()
{ int f;
if(ch=='(') {
u[i1]=ch;printf("S->(T)\t");
e[0]='S';e[1]='=';e[2]=' ';e[3]='(';e[4]='T';e[5]=')';e[6]='#';
output();
flag=0;
input();input1();
ch=v[++i1];
f=T();
if (f==0) return(0);
if(ch==')')
{
u[i1]=ch;printf("S--(T)\t");
flag=0;input();input1();
ch=v[++i1];
}
else {
printf("error\n");
return(0);
}

}
if(ch=='a')
{
u[i1]=ch;printf("S->aM\t");
e[0]='S';e[1]='=';e[2]=' ';e[3]='a';e[4]='M';e[5]='#';
output();
flag=0;input();input1();
ch=v[++i1];
f=M();
if (f==0) return(0);
return(1);
}
else {printf("error\n");return(0);}
return(1);
}

//================================================
int T()
{ int f,t;
printf("T->SN\t");
e[0]='T';e[1]='=';e[2]=' ';e[3]='S';e[4]='N';e[5]='#';
output();
flag=1;
input();
input1();
f=S();
if (f==0) return(0);
t=N();
if (t==0) return(0);
else return(1);
}

//================================================
int N()
{
int f,t;
if(ch=='b') {
u[i1]=ch;printf("N->bSN\t");
e[0]='N';e[1]='=';e[2]=' ';e[3]='b';e[4]='S';e[5]='N';e[6]='#';
output();
flag=0;
input();input1();
ch=v[++i1];
f=S();
if (f==0) return(0);
t=N();
if (t==0) return(0);
else return(1);}

printf("N->^\t");
e[0]='N';e[1]='=';e[2]=' ';e[3]='^';e[4]='#';
output();
flag=1;
v[i1]=ch;
input();input1();
return(1);

}
//================================================


int M()
{ int f;
if(ch=='+') {
u[i1]=ch;
printf("M->+S\t");
e[0]='M';e[1]='=';e[2]=' ';e[3]='+';e[4]='S';e[5]='#';
output();
flag=0;
input();input1();
ch=v[++i1];
f=S();
if (f==0) return(0);
return(1);
}
printf("M->^\t");
e[0]='M';e[1]='=';e[2]=' ';e[3]='^';e[4]='#';
output();
flag=1;
input();input1();
return(1);
}

//================================================

void input()
{
int j=0;
for (;j<=i1-flag;j++)
printf("%c",u[j]); /*输出分析串*/

printf("\t\t");
printf("%c\t\t",ch); /*输出分析字符*/
}

//================================================
void input1()
{
int j;
for (j=i1+1-flag;j<n1;j++)
printf("%c",v[j]); /*输出剩余字符*/

printf("\n");
}



//================================================
void output(){ /*推导式计算*/
int m,k,j,q;
int i=0;
m=0;k=0;q=0;
i=n;
d[n]='='; d[n+1]=' '; d[n+2]='#'; n=n+2; i=n;
i=i-2;
while(d[i]!=' '&&i!=0)
i=i-1;
i=i+1;
while(d[i]!=e[0])
i=i+1;
q=i;
m=q;k=q;
while(d[m]!=' ')
m=m-1;
m=m+1;
while(m!=q)
{
d[n]=d[m];m=m+1;n=n+1;
}
d[n]='#';
for(j=3;e[j]!='#';j++)
{
d[n]=e[j];
n=n+1;
}
k=k+1;
while(d[k]!='=')
{
d[n]=d[k]; n=n+1; k=k+1;
}
d[n]='#';
}


...全文
114 3 点赞 打赏 收藏 举报
写回复
3 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
大王娟 2017-11-13
谢谢帮助,问题解决了
  • 打赏
  • 举报
回复
hongwenjun 2017-11-12
#include <stdio.h>
#include<dos.h>
#include<stdlib.h>
#include<string.h>
char v[50], u[50], d[200], e[10];
char ch;
int n1, i1 = 0, flag = 1, n = 7;
int S();
int M();
int T();
int N();
void input();
void input1();
void output();

//================================================
int main()                      /*递归分析*/
{
    int f, p, j = 0;
    char x;
    d[0] = 'S';
    d[1] = '=';
    d[2] = ' ';
    d[3] = '(';
    d[4] = 'T';
    d[5] = ')';
    d[6] = '#';

    printf("请输入字符串(长度<50,以#号结束)\n");
    do {
        scanf("%c", &ch);
        v[j] = ch;
        j++;
    } while (ch != '#');

    n1 = j;
    ch = u[0] = v[0];
    printf("文法\t分析串\t\t分析字符\t剩余串\n");
    f = S();
    if (f == 0)
        return -1;
    if (ch == '#') {
        printf("accept\n");
        p = 0;
        x = d[p];
        while (x != '#') {
            printf("%c", x);
            p = p + 1;
            x = d[p];        /*输出推导式*/
        }
    } else {
        printf("error\n");
        printf("回车返回\n");
        getchar();
        getchar();
        return -1;
    }
    printf("\n");
    printf("回车返回\n");
    getchar();
    getchar();
}


//================================================
int S()
{
    int f;
    if (ch == '(') {
        u[i1] = ch;
        printf("S->(T)\t");
        e[0] = 'S';
        e[1] = '=';
        e[2] = ' ';
        e[3] = '(';
        e[4] = 'T';
        e[5] = ')';
        e[6] = '#';
        output();
        flag = 0;
        input();
        input1();
        ch = v[++i1];
        f = T();
        if (f == 0)
            return (0);
        if (ch == ')') {
            u[i1] = ch;
            printf("S--(T)\t");
            flag = 0;
            input();
            input1();
            ch = v[++i1];
        } else {
            printf("error\n");
            return (0);
        }

    }
    if (ch == 'a') {
        u[i1] = ch;
        printf("S->aM\t");
        e[0] = 'S';
        e[1] = '=';
        e[2] = ' ';
        e[3] = 'a';
        e[4] = 'M';
        e[5] = '#';
        output();
        flag = 0;
        input();
        input1();
        ch = v[++i1];
        f = M();
        if (f == 0)
            return (0);
        return (1);
    } else {
        printf("error\n");
        return (0);
    }
    return (1);
}

//================================================
int T()
{
    int f, t;
    printf("T->SN\t");
    e[0] = 'T';
    e[1] = '=';
    e[2] = ' ';
    e[3] = 'S';
    e[4] = 'N';
    e[5] = '#';
    output();
    flag = 1;
    input();
    input1();
    f = S();
    if (f == 0)
        return (0);
    t = N();
    if (t == 0)
        return (0);
    else
        return (1);
}

//================================================
int N()
{
    int f, t;
    if (ch == 'b') {
        u[i1] = ch;
        printf("N->bSN\t");
        e[0] = 'N';
        e[1] = '=';
        e[2] = ' ';
        e[3] = 'b';
        e[4] = 'S';
        e[5] = 'N';
        e[6] = '#';
        output();
        flag = 0;
        input();
        input1();
        ch = v[++i1];
        f = S();
        if (f == 0)
            return (0);
        t = N();
        if (t == 0)
            return (0);
        else
            return (1);
    }

    printf("N->^\t");
    e[0] = 'N';
    e[1] = '=';
    e[2] = ' ';
    e[3] = '^';
    e[4] = '#';
    output();
    flag = 1;
    v[i1] = ch;
    input();
    input1();
    return (1);

}
//================================================


int  M()
{
    int f;
    if (ch == '+') {
        u[i1] = ch;
        printf("M->+S\t");
        e[0] = 'M';
        e[1] = '=';
        e[2] = ' ';
        e[3] = '+';
        e[4] = 'S';
        e[5] = '#';
        output();
        flag = 0;
        input();
        input1();
        ch = v[++i1];
        f = S();
        if (f == 0)
            return (0);
        return (1);
    }
    printf("M->^\t");
    e[0] = 'M';
    e[1] = '=';
    e[2] = ' ';
    e[3] = '^';
    e[4] = '#';
    output();
    flag = 1;
    input();
    input1();
    return (1);
}

//================================================

void input()
{
    int j = 0;
    for (; j <= i1 - flag; j++)
        printf("%c", u[j]);               /*输出分析串*/

    printf("\t\t");
    printf("%c\t\t", ch);                 /*输出分析字符*/
}

//================================================
void input1()
{
    int j;
    for (j = i1 + 1 - flag; j < n1; j++)
        printf("%c", v[j]);                /*输出剩余字符*/

    printf("\n");
}



//================================================
void output()                               /*推导式计算*/
{
    int m, k, j, q;
    int i = 0;
    m = 0;
    k = 0;
    q = 0;
    i = n;
    d[n] = '=';
    d[n + 1] = ' ';
    d[n + 2] = '#';
    n = n + 2;
    i = n;
    i = i - 2;
    while (d[i] != ' ' && i != 0)
        i = i - 1;
    i = i + 1;
    while (d[i] != e[0])
        i = i + 1;
    q = i;
    m = q;
    k = q;
    while (d[m] != ' ')
        m = m - 1;
    m = m + 1;
    while (m != q) {
        d[n] = d[m];
        m = m + 1;
        n = n + 1;
    }
    d[n] = '#';
    for (j = 3; e[j] != '#'; j++) {
        d[n] = e[j];
        n = n + 1;
    }
    k = k + 1;
    while (d[k] != '=') {
        d[n] = d[k];
        n = n + 1;
        k = k + 1;
    }
    d[n] = '#';
}
这样可以编译,
  • 打赏
  • 举报
回复
大王娟 2017-11-12
对文法使用了递归下降分析方法,手残打错了。。
  • 打赏
  • 举报
回复
相关推荐
发帖
C语言
加入

6.5w+

社区成员

C语言相关问题讨论
申请成为版主
帖子事件
创建了帖子
2017-11-12 10:42
社区公告
暂无公告