views:

151

answers:

5
#include<stdio.h>

/* this is a lexer which recognizes constants , variables ,symbols, identifiers , functions , comments and also header files . It stores the lexemes in 3 different files . One file contains all the headers and the comments . Another file will contain all the variables , another will contain all the symbols. */

int main()
{
    int i;
    char a,b[20],c;
    FILE *fp1;

    fp1=fopen("source.txt","r"); //the source file is opened in read only mode which will passed through the lexer

    //now lets remove all the white spaces and store the rest of the words in a file 

    if(fp1==NULL)
    {
        perror("failed to open source.txt");
        //return EXIT_FAILURE;
    }
    i=0;
    while(1)
    {


        a=fgetc(fp1);

        if(a !="")
        {
            b[i]=a;
        }
        else
        {


            fprintf(fp1, "%.20s\n", b);
            i=0;
            continue;
        }
        i=i+1;                  

        /*Switch(a)
        {
            case EOF :return eof;
            case '+':sym=sym+1;

            case '-':sym=sym+1;

            case '*':sym=sym+1;

            case '/':sym=sym+1;

            case '%':sym=sym+1;

            case '
        */
    }
return 0;
}

how does this code end up in segmentation fault?

A: 

There is no check to test the End of File.

Change

while(1) 

to

while (!feof(fp1))

EDIT:

But the reason for seg-fault is the missing NULL char at the end of b. You can add the NULL char at the end of b just before writing it to the file:

b[i] = 0; // i must <=19
fprintf(fp1, "%.20s\n", b);

This way you also need to ensure that you don't stuff more than 19 char into b that way you always have space to write the NULL char.

codaddict
+2  A: 

And also, the string b is probably not null terminated. At some point in your code you need:

 b[i] = '\0';
anon
A: 

Use your while like this:

...
 while((a=fgetc(fp1))!=EOF)
    {
        if(a !="")
        {
            b[i]=a;
        }
...

You will check every-time if a is different from EOF. If a equals EOF, the processing will stop.

I would consider reading a bigger buffer with fread() / fgets(), and parse the resulting string, instead of reading character by character. I think this may improve the overall performance of your parser.

Andrei Ciobanu
A: 

SegFault?? usually INFINITE LOOP...


Use the following...

#include<stdio.h>

/* this is a lexer which recognizes constants , variables ,symbols, identifiers , functions , comments and also header files . It stores the lexemes in 3 different files . One file contains all the headers and the comments . Another file will contain all the variables , another will contain all the symbols. */

int main()
{
    int i;
    char a,b[20],c;
    FILE *fp1;

    fp1=fopen("source.txt","r"); //the source file is opened in read only mode which will passed through the lexer

    //now lets remove all the white spaces and store the rest of the words in a file 

    if(fp1==NULL)
    {
        perror("failed to open source.txt");
        //return EXIT_FAILURE;
    }
    i=0;
    while(!feof(fp1))
    {


        a=fgetc(fp1);

        if(a !="")
        {
            b[i]=a;
        }
        else
        {

            b[i]='\0';
            fprintf(fp1, "%.20s\n", b);
            i=0;
            continue;
        }
        i=i+1;                  

        /*Switch(a)
        {
            case EOF :return eof;
            case '+':sym=sym+1;

            case '-':sym=sym+1;

            case '*':sym=sym+1;

            case '/':sym=sym+1;

            case '%':sym=sym+1;

            case '
        */
    }
return 0;
}

GoodLUCK!!

CVS @ 2600Hertz

CVS-2600Hertz
You don't understand how feof() works. And sigs are generally considered a bad idea here.
anon
+2  A: 

It looks like it needs a check to be sure it is not writing beyond the end of array b. If it reads over 20 characters, it will write past it and corrupt the stack.

Mark Wilkins
+1 While the underlying problem is the infinite loop (mentioned in other answers), what is causing the segfault is that the infinite loop causes writing beyond the end of the space allocated to `b`
Tyler McHenry