Thread: Compile error: fatal error C1026: parser stack overflow, program too complex

  1. #1
    Registered User
    Join Date
    Apr 2011
    Posts
    308

    Compile error: fatal error C1026: parser stack overflow, program too complex

    Here's my code. I'm sorry it's so big. I can't compile it. I read on the microsoft page about this error and it said:

    "Simplify the program by decreasing the complexity of expressions. Decrease the level of nesting in for and switch statements by putting some of the more deeply nested statements in separate functions. Break up very long expressions involving comma operators or function calls."

    So I'm hoping you can show me how to compile this.

    <<< snip bloat - download the 500KB source file if you're that interested >>>
    <<< remove .txt extension, then unzip >>>
    Attached Files Attached Files

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Your compiler should just tell you: "Use arrays, you big dummy!", buuut I guess it's too polite...

    EDIT: Let me explain. By using an array of pointers instead of this mayhem of variables, your code would be much smaller, and it would compile! Imagine replacing this chaos with a for loop and 5 or 6 if statements. What would you prefer?
    Last edited by GReaper; 11-26-2011 at 08:47 AM.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    The variable declarations in the find pattern function wouldn't use arrays, and I have no idea on how the while loop would use arrays.

  4. #4
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Well, how do you label your variables *_1, *_2, etc? Can't you come up with an array for each of these groups? Then, by using some for loops, your code would be ~7500 lines less!
    Devoted my life to programming...

  5. #5
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by GReaper View Post
    Well, how do you label your variables *_1, *_2, etc? Can't you come up with an array for each of these groups? Then, by using some for loops, your code would be ~7500 lines less!
    Actually he needs to do more than using arrays... he needs to get all that stuff off the program's stack, too.... If it's crashing his compiler's call stack it's almost certainly going to crash the runtime stack as well.

    All those strings need to be created on the heap with malloc(), stored in arrays (a single multi-dimensional array would be fastest), checked for duplicates, and read into memory from a file at program startup.

    @Jeramy ... THIS is what happens when you try to write C programs with almost no knowledge of programming. I'm doing my best not say "We told you so"... but we did. The odds are a good programmer could reduce your code to less than 150 lines.
    Last edited by CommonTater; 11-26-2011 at 09:19 AM.

  6. #6
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,739
    Or he could use constant pointers instead of arrays.
    Devoted my life to programming...

  7. #7
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    I got this code to work:

    Code:
    /* function to find pattern, useful to identify how the words parallel a syllogism */
    int find_pattern(void)
    {
    	/* declare variable arrays */
    	int period = 0;
    	int one = 1;
    	char arr[2][52]= { "point\nautofire\npoint\nshoot\npoint\nreload\npoint", "point\nautofire\npoint\nshoot\nreload\nreload\npoint" };
    	
        char buffer[LINES];
    	char pch[LINES] = {0};
    
    	/* Read the list made by the percentage function */
    
        FILE *book=fopen("writelist.txt", "r");
        if(!book) {printf("Error: unable to open input file!\n"); return 1;}
    	while(fgets(buffer, sizeof(buffer), book)!=NULL)
    	{
    		/* Find the word then add it the the pch string */
    		if (strstr(buffer, "point"))
    		{
    			strcat(pch, buffer);
    		}
    		if (strstr(buffer, "shoot"))
    		{
    			strcat(pch, buffer);
    		}
    		if (strstr(buffer, "reload"))
    		{
    			strcat(pch, buffer);
    		}
    		if (strstr(buffer, "autofire"))
    		{
    			strcat(pch, buffer);
    		}
    		
    		/* counting the periods in the input text file*/
    		if (strstr(buffer, "."))
    		{
    			period++;
    		}
            
            /* After two periods are counted the while loop is exited */
            if(period > one)
            {
    			if(strstr(pch, arr[1]))
                {
                printf("\nType Camestros (AEO-2) Syllogism pattern found\nP are M\nNo S is M\n\n");
                }
    
                /* resetting the pch array to zero, so there is room to fill it again */
                
                if(period > one)
                {
                memset(&pch[0], 0, sizeof(pch));
                }
                period = 0;
            }
    	}
       fclose(book);
       
       return 0;
    }
    CommonTater, look at this code and say if it will still crash if I put 256 strings in an array like this:
    Code:
    char arr[256][52]

  8. #8
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Trash this stuff... no joke, I'm serious... just delete all this crap you're working on from your computer...

    Sit down with a proper C language textbook ... start on page 1 ... read all the words, over and over until they sink in, type up all the exercises, compile them, change them, break them, fix them until you understand what's going on... then turn to page 2 and repeat the process, until you get to the end of the book. LEARN C... LEARN PROGRAMMING....

    Then in 6 months or a year once you've written a few smaller programs --chequebook balancing, household inventory, the basicis-- you can pull this out of the back of your mind and revisit it.

    What you did, wasting all that time, was a first class example of "the bull in the china shop"...
    You got yourself a bright idea, turned off your brain, and charged ahead with about 1/10th the required knowledge to do the job.

  9. #9
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by GReaper View Post
    Or he could use constant pointers instead of arrays.
    Take a close look at his strings... all 2000 of them... they're nothing but various combinations of 5 words ... He then wastes another 3000 lines of if() - else if() to pick out a specific combination.

  10. #10
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    Quote Originally Posted by CommonTater View Post
    Actually he needs to do more than using arrays... he needs to get all that stuff off the program's stack, too.... If it's crashing his compiler's call stack it's almost certainly going to crash the runtime stack as well.

    All those strings need to be created on the heap with malloc(), stored in arrays (a single multi-dimensional array would be fastest), checked for duplicates, and read into memory from a file at program startup.
    Show me a code example of what your talking about.
    Then I will Google the rest. I want to get this done with. Just show me a example then I will have a hand at it.

  11. #11
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    No sir... I'm not going to give you code examples...

    You will not learn a bloody thing if people keep giving you code or helping you with failed projects.

    I will however give you THIS which should get you started in the right direction.

  12. #12
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    Here is my current code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    #include <ctype.h>
    #define LINES 4096
    
    char *Camestros_1()
    {
    	char *Camestros_1 = "point\nautofire\npoint\nshoot\nreload\nreload\npoint";
    	return Camestros_1;
    }
    
    char red;
    char* reverse;
    
    /* The function to split the input into a sentence per line */
    int split_by_sentence(void)
    {
        int character, file_character=0;
        char buffer[1024];
        FILE *book=fopen("readtext1.txt", "r");
        FILE *book2=fopen("readtext.txt", "a+");
        if(!book) {printf("Error: unable to open input file!\n"); return 1;}
        if(!book2) {printf("Error: unable to open output file!\n"); return 1;}
    
        while(file_character!=EOF)
        {
            buffer[0]='\0';
            for(character=0;character<sizeof(buffer);character++) 
            {
                file_character=fgetc(book);
                if(file_character==EOF)
                break;
                
                if(file_character=='.')
                {
                    buffer[character]='\0';  
                    break;
                }
                
                if(file_character=='?')
                {
                    buffer[character]='\0';  
                    break;
                }
                
                if(file_character=='!')
                {
                    buffer[character]='\0';  
                    break;
                }
                
                buffer[character]=file_character;
            }
            
            if(file_character==EOF)
            break;
            fprintf(book2, "%s.\n", buffer);
        }
        fclose(book);
        fclose(book2);
        putchar('\n');
        
        return 0;
    }
    
    /* The function to find the first letter in the word */
    char find_letter (char* a, char* b)
    {
        char string = strlen(b);
        strncpy (a,b,1);
        a[1]='\0';
        return 0;
    }
    
    /* The function to reverse the characters in a string */
    char* rev(char* str)
    {
        int end= strlen(str)-1;
        int start = 0;
        
        while( start<end )
        {
            char temp = str[ start ];
            str[ start ] = str[ end ];
            str[ end ] = temp;
            
            ++start;
            --end;
        }
        
        return str;
    }
    
    /* The function for percentage calculation */
    void percentage_calculation_numbers(char* a_pch, char* a_b)
    {
        FILE *fp;
        char str2[7];
        char period[] = ".";
        char vowels[] = "aeiouyAEIOUY";
        char letters[] = "bcdfghjklmnpqrstvwxzBCDFGHJKLMNPQRSTVWXYZ";
        char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char sentence[4096] = {0};
        float one;
        float two;
        float three;
        float four;
        float total;
        float five;
        float six;
        float seven;
        float eight;
        float total_2;
        float percentage_1;
        float percentage_2;
        float percentage_3;
        float percentage_4;
        
        one = 0;
        two = 0;
        three = 0;
        four = 0;
        total = 0;
        five = 0;
        six = 0;
        seven = 0;
        eight = 0;
        total_2 = 0;
        percentage_1 = 0;
        percentage_2 = 0;
        percentage_3 = 0;
        percentage_4 = 0;
        
        /* open text file or report error */
        fp = fopen("writelist.txt", "a+");
    
        if(!fp) {printf("Error: unable to open input file!\n");}
        
        /* The while loop gives value to five, six, seven, eight, which is used for the percentage calculation */
        while(a_pch != NULL)
        {
            strcat(sentence, a_pch);
            strcat(sentence, " ");
            /* identify the first letter in the word begin */
            
            red = find_letter(str2, a_pch);
            
            if(strpbrk(str2, letters))
            {
                one++;
            }
            
            if(strpbrk(str2, vowels))
            {
                two++;
            }
            
            /* identify the first letter in the word end */
            
            /* identify the last letter in the word begin */
            
            reverse = rev(a_pch);
            red = find_letter(str2, a_pch);
            
            if(strpbrk(str2, letters))
            {
                three++;
            }
            
            if(strpbrk(str2, vowels))
            {
                four++;
            }
            
            /* identify the last letter in the word end */
    
            /* The math using the first and last letter begin */
    
            /* pch is backwards, so I use rev to reverse it back */
            /*printf("%s uncomment to test to see if the string is backwards", pch);*/
            reverse = rev(a_pch);
    
            /*The math using the first and last letter begin
            Here I give the word a new string value, based on how the vowel is situated in the word.*/
    
            if(total = (one && three))
            {
                five++;
                fprintf(fp, "point\n", a_pch);
            }
            
            else if(total = (one && four))
            {
                six++;
                fprintf(fp, "shoot\n", a_pch);
            }
            
            else if(total = (two && three))
            {
                seven++;
                fprintf(fp, "reload\n", a_pch);
            }
            
            else if(total = (two && four))
            {
                eight++;
                fprintf(fp, "autofire\n", a_pch);
            }
            
            /* The math using the first and last letter end */
    
            /* Reset the pointers */
            
            one = 0;
            two = 0;
            three = 0;
            four = 0;
            total = 0;
            
            a_pch = strtok (NULL, " .");
        }
        /* Add separation between lines in console window, to identify sentence separation*/
        putchar('\n');
    
        /* pch is NULL, so I find alphabet in b and pass that into pch */
        a_pch = strpbrk(a_b, alphabet);
        
        /* Count the pointers */
        total_2 = (five + six + seven + eight);
        percentage_1 = ((five)/ total_2);
        percentage_2 = ((six)/ total_2);
        percentage_3 = ((seven)/ total_2);
        percentage_4 = ((eight)/ total_2);
        
        /* puts a period into the textfile after a sentence is processed
    This way I can count the number of periods in the text file */
        fprintf(fp, ".\n");
        
        fclose(fp);
    
        /* Displays the sentence above the list.*/
        printf("%s %s", sentence, period);
        memset(&sentence[0], 0, sizeof(sentence));
    }
    
    /* function to find pattern, useful to identify how the words parallel a syllogism */
    int find_pattern(void)
    {
        /* declare variable arrays */
        int period = 0;
        int one = 1;
    	
    	
        char buffer[LINES];
        char pch[LINES] = {0};
    
        /* Read the list made by the percentage function */
    
        FILE *book=fopen("writelist.txt", "r");
        if(!book) {printf("Error: unable to open input file!\n"); return 1;}
        while(fgets(buffer, sizeof(buffer), book)!=NULL)
        {
            /* Find the word then add it the the pch string */
            if (strstr(buffer, "point"))
            {
                strcat(pch, buffer);
            }
            if (strstr(buffer, "shoot"))
            {
                strcat(pch, buffer);
            }
            if (strstr(buffer, "reload"))
            {
                strcat(pch, buffer);
            }
            if (strstr(buffer, "autofire"))
            {
                strcat(pch, buffer);
            }
            
            /* counting the periods in the input text file*/
            if (strstr(buffer, "."))
            {
                period++;
            }
            
            /* After two periods are counted the while loop is exited */
            if(period > one)
            {
                if(strstr(pch, Camestros_1()))
                {
                    printf("\nType Camestros (AEO-2) Syllogism pattern found\nP are M\nNo S is M\n\n");
                }
                //////////////////////////////////////////////////////////////////////////////////
                /* resetting the pch array to zero, so there is room to fill it again */
                
                if(period > one)
                {
                    memset(&pch[0], 0, sizeof(pch));
                }
                period = 0;
            }
        }
        fclose(book);
    
        return 0;
    }
    
    /* function so I don't have to type getch all over the place */
    void MyExit(void) { system("pause"); }
    
    /* the main program */ 
    int main ()
    {   
        /* declaring and initiaizing variables */
        FILE *book;
        char * pch = malloc(300);
        char alphabet[] = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
        char buffer[LINES];
    
        /* The function is called so I don't have to type getch */
        atexit(MyExit);    
        
        /* The function is called and creates the text file */
        split_by_sentence();
        
        /* The text file just created is opened */
        book = fopen("readtext.txt", "r");
        
        if(!book)   
        {
            perror("Error: file readtext.txt was not found or opened");   
            return 0;   
        }
        
        /* read from file */
        while(fgets(buffer, sizeof(buffer), book)!=NULL)
        { 
            /* I tokenize the input string into individual words */
            pch = strtok (buffer, " ~!@#$%^&*()_+`1234567890-=[]\'\"/;/|,./{}:<>?");
            
            /* I find the alphabet in pch after it's run through strtok */
            if(strpbrk(pch, alphabet))
            {
                /* I run the percentage function which also creates the new terms for the words processed */
                percentage_calculation_numbers(pch, buffer);
            }
        }
        fclose(book);
        find_pattern();
        return 0;
    }
    It works. I will now convert my variable arrays to functions and call them in the if statements as seen in the code.

    I'm not arguing, CommonTater. I understand programming people have been known to argue though, so your attitude is to be expected. I'm not a programming person though, so I have no bias like they do, yet anyway.

  13. #13
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    Quote Originally Posted by jeremy duncan View Post
    I'm not arguing, CommonTater. I understand programming people have been known to argue though, so your attitude is to be expected. I'm not a programming person though, so I have no bias like they do, yet anyway.
    My god... how thick are you?

    If you want to program... LEARN HOW!

    This is what your third or 4th major failure?
    Doesn't that tell you *anything*?

    I'm not arguing. I'm trying to get past whatver wall of stupid you've built around yourself to try to get you to listen to a little common sense.

    So... I give up, you're in my ignore file, and there you will stay.

  14. #14
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Careful, you're wandering into personal attack territory.

    Jeremy Duncan, Whilst you could throw this away, my advice would be to keep it and to spend a lot of time figuring out how to simplify and shorten it. Learning from your mistakes rather than starting over and making the same ones again.
    For example, the variable "total" is written to but is never read from, so out it goes. Same goes for "red", "reverse", and "string" inside that find_letter function.
    Don't return something if you don't need to. Declare the function as void.
    percentage_1 .. percentage_4 make it an array. Actually do you even need those? You assign values to them and then just let them go out of scope. The best thing you can do is get rid of everything that is not required.
    You've used initialisation for cases like this:
    Code:
        char period[] = ".";
    So why not do the same for other variables, like:
    Code:
        float total_2 = 0;
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  15. #15
    Registered User
    Join Date
    Apr 2011
    Posts
    308
    That code I posted before works. I posted it in my artificial intelligence thread here; link. I don't know why CommonTater is so stuffy to me. Chill out man.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. stack overflow error
    By azarue in forum C Programming
    Replies: 22
    Last Post: 06-29-2010, 01:22 PM
  2. [Linker Fatal Error] Fatal: Expected a file name
    By Checco in forum C++ Programming
    Replies: 1
    Last Post: 07-27-2005, 05:34 AM
  3. visual c++ .net compile problem... odd fatal error
    By tyouk in forum Windows Programming
    Replies: 3
    Last Post: 09-29-2004, 11:31 AM
  4. error trying to compile stack program
    By KristTlove in forum C++ Programming
    Replies: 2
    Last Post: 11-03-2003, 06:27 PM
  5. Program Fatal Error- Please Explain
    By luckygold6 in forum C++ Programming
    Replies: 9
    Last Post: 03-06-2003, 08:56 PM