[Solved] Need help debugging segfault


The reason is suspected conio.h and other outdated headers is this.

#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();
}

The above is your code with some tiny modifications

  1. Used a custom strrev , very naive implementation with bad memory practices, but it works.
  2. got rid of conio
  3. Added stdlib.h for malloc
  4. got rid of getch in favor of getchar
  5. got rid of clrscr()

Nobody knows what bugs conio and the old Borland headers have. They might erroneously redescribe some std functions.

When I run the above with input acdb$ I get

                        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__________________

Start using either gcc/clang and use gdb/lldb to debug these programs.

here‘s a nice guide.

If you compile with these options you can see your program is rife with bad programming patterns

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

solved Need help debugging segfault