GNU Bison parser throws segmentation error 11 when specifying a non-empty file

Whenever I call yyparse() with a valid file, I get a seg error that seems to be caused by this line (around line 1789) of the code:

 if (yyss + yystacksize - 1 <= yyssp){ 

I came to this conclusion by printing debug messages before and after this line of code. Messages before this line were printed, but there was no line after this line.

It is strange that if I call yyparse() with an empty file, the error will not be selected, but it will be selected if there is at least one character in the file.

The analyzer itself was compiled without errors. What could be the reason behind this seg failure?

Parser File: https://gist.github.com/SamTebbs33/bffb72517f174af679ef

Debug message code:

 cout << "before if" << endl; if (yyss + yystacksize - 1 <= yyssp){ cout << "after if" << endl; cout.flush(); 

The first debug message is printed 3 times before the error is reset.

Edit: The error actually occurs in the switch statement when marker 55 matches the yyreduce label:

 case 55: #line 219 "grammar/grammar.y" /* yacc.c:1661 */ { cout << "processing token 55" << endl; (yyval.id) = new TIdentifier(*(yyvsp[0].string)); cout << "processed token 55" << endl; } #line 2228 "grammar/parser.cpp" /* yacc.c:1661 */ break; 

Before the switch statement is reached, I print the integer value of the variable that switches, and its value is 55, so the error code must be in the above code, since the “processed token 55” is not printed, and the “processing token” is 55 " The following is the TIdentifier constructor code :

 TIdentifier(std::string name) : name(name) { } 

This means that an error should occur when dereferencing (yyvsp[0].string)

+5
source share
1 answer

( Answered by OP in the editing question. Converted in response to the community wiki, which is more suitable for the Q & A StackOverflow format ).

OP wrote (a):

After further debugging, I realized that my flexible grammar does not save the lines found in the file, as it should, and tried to access a nonexistent element in yylval, and now the parser works!

However, it would be better that corect material was included in the question, as @rici noted:

It would be more useful (or at least easier) to see your input bison (.y) file, rather than the resulting parser.

0
source

All Articles