需要帮助调试段错误

时间:2018-08-29 17:11:00

标签: c

我不知道我的代码有什么问题。它正在正确编译,但在输出时显示第二张图像。输出告诉我代码有什么问题,它必须提供第一张图所示的输出。

代码必须像这样运行: code must run like this

但显示如下: but shows this

我不知道哪个块有什么问题。这是用于检查输入字符串是否将被接受的代码。

#include<stdio.h>
#include<conio.h>
#include<string.h>
void main()
{
    char *arr[1][3]= {{"aABb","c","d"}};
    char input[15]= {'\0'};
    char temp[15]= {'\0'};
    char stack[15]= {'$','S'};
    int ip=0;
    int ct=0;
    int top,i;
    char x;
    clrscr();
    printf("\t\t\t Predictive parser\t\t\t\n");
    printf("___________________________________________________\n\n");
    printf("The grammar is :\n");
    printf("\t\tS-->aABb\n");
    printf("\t\tA-->c\n");
    printf("\t\tB-->d\n\n");
    printf("You have follow some rules\n");
    printf("The string must end with $\n");
    printf("Enter  the String  : ");
    gets(input);
    top=(strlen(stack))-1;
    x=stack[top];
    printf("_______________________________________________________\n");
    printf("stack\t\t\t input\t\t\t production\n");
    printf("________________________________________________________\n");
    while(x!='\0')
    {
        if(x=='S'&&input[ct]=='a')
        {
            printf("\n");
            for(i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t");
            for(i=ip; i<6; i++)
                printf("%c",input[i]);
            printf("\t\tderivation using S-->aABb\n");
            stack[top]='\0';
            strcpy(temp,strrev(arr[0][0]));
            strcat(stack,temp);
            top=strlen(stack)-1;
        }
        else if(x=='A'&&input[ct]=='c')
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            stack[top]='\0';
            strcpy(temp,strrev(arr[0][1]));
            strcat(stack,temp);
            top=strlen(stack)-1;
            printf("\t\t derivation usingA-->c\n");
        }
        else if(x=='B'&&input[ct]=='d')
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            stack[top]='\0';
            strcpy(temp,strrev(arr[0][2]));
            strcat(stack,temp);
            top=strlen(stack)-1;
            printf("\t\t derivation using B-->d\n");
        }
        else if(x=='a'&&input[ct]=='a')
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            printf("\t\t popping a from the stack");
            input[ct]=' ';
            ct++;
            stack[top]='\0';
            top--;
        }
        else if(x=='c'&&input[ct]=='c')
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            printf("\t\t popping c from the stack");
            input[ct]=' ';
            ct++;
            stack[top]='\0';
            top--;
        }
        else if(x=='d'&&input[ct]=='d')
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            printf("\t\t popping d from the stack");
            input[ct]=' ';
            ct++;
            stack[top]='\0';
            top--;
        }
        else if(x=='b'&&input[ct]=='b')
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            printf("\t\t popping b from the stack");
            input[ct]=' ';
            ct++;
            stack[top]='\0';
            top--;
        }
        else if(x=='$'&&input[ct]=='$')
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            stack[top]='\0';
            printf("\t\tSuccessfull\n");
        }
        else
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            stack[top]='\0';
            printf("\t\tUnsuccessfull\n");
            getch();
            exit(1);
        }
        x=stack[top];
    }
    printf("\n____________________ Exit program__________________");
    getch();
}

1 个答案:

答案 0 :(得分:0)

怀疑是conio.h的原因,而其他过时的标头是这个。

#include<stdio.h>
#include <stdlib.h>
// #include<conio.h>
#include<string.h>

char* strrev(char *str)
{
      char *p1;
      int i, j;
      //puts("Reversing");
      //puts(str);
      p1 = (char*)malloc(strlen(str) * sizeof(char));
      for( i = strlen(str)-1, j =0; i >=0; i--, j++) {
        p1[j] = str[i];
      }
      p1[j] = '\0';
      puts(p1);
      //puts("Done");
      return p1;
}

void main()
{
    char *arr[1][3]= {{"aABb","c","d"}};
    char input[15]= {'\0'};
    char temp[15]= {'\0'};
    char stack[15]= {'$','S'};
    int ip=0;
    int ct=0;
    int top,i;
    char x;

    printf("\t\t\t Predictive parser\t\t\t\n");
    printf("___________________________________________________\n\n");
    printf("The grammar is :\n");
    printf("\t\tS-->aABb\n");
    printf("\t\tA-->c\n");
    printf("\t\tB-->d\n\n");
    printf("You have follow some rules\n");
    printf("The string must end with $\n");
    printf("Enter  the String  : ");
    gets(input);
    top=(strlen(stack))-1;
    x=stack[top];
    printf("_______________________________________________________\n");
    printf("stack\t\t\t input\t\t\t production\n");
    printf("________________________________________________________\n");
    while(x!='\0')
    {
        if(x=='S'&&input[ct]=='a')
        {
            printf("\n");
            for(i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t");
            for(i=ip; i<6; i++)
                printf("%c",input[i]);
            printf("\t\tderivation using S-->aABb\n");
            stack[top]='\0';
            strcpy(temp,strrev(arr[0][0]));
            strcat(stack,temp);
            top=strlen(stack)-1;
        }
        else if(x=='A'&&input[ct]=='c')
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            stack[top]='\0';
            strcpy(temp,strrev(arr[0][1]));
            strcat(stack,temp);
            top=strlen(stack)-1;
            printf("\t\t derivation usingA-->c\n");
        }
        else if(x=='B'&&input[ct]=='d')
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            stack[top]='\0';
            strcpy(temp,strrev(arr[0][2]));
            strcat(stack,temp);
            top=strlen(stack)-1;
            printf("\t\t derivation using B-->d\n");
        }
        else if(x=='a'&&input[ct]=='a')
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            printf("\t\t popping a from the stack");
            input[ct]=' ';
            ct++;
            stack[top]='\0';
            top--;
        }
        else if(x=='c'&&input[ct]=='c')
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            printf("\t\t popping c from the stack");
            input[ct]=' ';
            ct++;
            stack[top]='\0';
            top--;
        }
        else if(x=='d'&&input[ct]=='d')
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            printf("\t\t popping d from the stack");
            input[ct]=' ';
            ct++;
            stack[top]='\0';
            top--;
        }
        else if(x=='b'&&input[ct]=='b')
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            printf("\t\t popping b from the stack");
            input[ct]=' ';
            ct++;
            stack[top]='\0';
            top--;
        }
        else if(x=='$'&&input[ct]=='$')
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            stack[top]='\0';
            printf("\t\tSuccessfull\n");
        }
        else
        {
            printf("\n");
            for (i=0; i<=strlen(stack); i++)
                printf("%c",stack[i]);
            printf("\t\t\t ");
            for (i=ip; i<6; i++)
                printf("%c",input[i]);
            stack[top]='\0';
            printf("\t\tUnsuccessfull\n");
            getchar();
            exit(1);
        }
        x=stack[top];
    }
    printf("\n____________________ Exit program__________________");
    getchar();
}

上面是您的代码,做了一些微小的修改

  1. 使用了自定义strrev,这是一种非常幼稚的实现,具有不良的内存实践,但是可以正常工作。
  2. 摆脱conio
  3. stdlib.h添加了malloc
  4. 摆脱getch的支持,getchar
  5. 摆脱clrscr()

没有人知道conio和旧的Borland标头中有什么错误。他们可能错误地重新定义了一些std函数。

当我用输入acdb$运行以上命令时,我得到了

                        Predictive parser
___________________________________________________

The grammar is :
                S-->aABb
                A-->c
                B-->d

You have follow some rules
The string must end with $
Enter  the String  : acdb$
_______________________________________________________
stack                    input                   production
________________________________________________________

$S                      acdb$           derivation using S-->aABb
bBAa

$bBAa                    acdb$           popping a from the stack
$bBA                      cdb$c
                 derivation usingA-->c

$bBc                      cdb$           popping c from the stack
$bB                        db$d
                 derivation using B-->d

$bd                        db$           popping d from the stack
$b                          b$           popping b from the stack
$                            $          Successfull

____________________ Exit program__________________

开始使用gcc / clang并使用gdb / lldb调试这些程序。

here是不错的指南。

如果使用这些选项进行编译,则可以看到您的程序充斥着不良的编程模式

gcc -Werror -Wall -Wextra -pedantic parser.c -o parser
parser.c: In function ‘strrev’:
parser.c:9:4: error: C++ style comments are not allowed in ISO C90 [-Werror]
parser.c:9:4: error: (this will be reported only once per input file) [-Werror]
parser.c:11:4: error: implicit declaration of function ‘malloc’ [-Werror=implicit-function-declaration]
parser.c:11:16: error: incompatible implicit declaration of built-in function ‘malloc’ [-Werror]
parser.c: At top level:
parser.c:21:6: error: return type of ‘main’ is not ‘int’ [-Werror=main]
parser.c: In function ‘main’:
parser.c:52:23: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
parser.c:66:24: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
parser.c:80:24: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
parser.c:94:24: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
parser.c:108:24: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
parser.c:122:24: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
parser.c:136:24: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
parser.c:150:24: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
parser.c:161:24: error: comparison between signed and unsigned integer expressions [-Werror=sign-compare]
parser.c:169:13: error: implicit declaration of function ‘exit’ [-Werror=implicit-function-declaration]
parser.c:169:13: error: incompatible implicit declaration of built-in function ‘exit’ [-Werror]
cc1: all warnings being treated as errors