Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9197925
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T22:10:47+00:00 2026-06-17T22:10:47+00:00

I have a problem with Bison’s error handling. I have the following grammar (I

  • 0

I have a problem with Bison’s error handling. I have the following grammar (I have cut out only the relevant part). Flex sends its tokens to Bison and returns the terminal symbol “KW_CONFIGPARAM” if the string “ConfigParam” is parsed. IDENT is a pointer to a C++ string object.

statementlist   : statement ';'               { $$ = new string("statementlist"); }
                | statementlist statement ';' { $$ = new string("statementlist"); }
;
statement       : KW_CONFIGPARAM IDENT        { $$ = new string("statement"); /* use $2, IDENT is used in main program */ }
;

I have the following destructor specified for strings like IDENT.

%destructor { printf (“free at line %d: %s\n”,@$.first_line, $$->c_str()); delete($$); }

Now I have the following input. The first line is valid, the second line isn’t (identifier is missing):

ConfigParam p;
ConfigParam;

Output:

In input 2:12 - 2:12 : syntax error
free at line 2: p
free at line 1: statementlist

Because of an error in the second line, Bison complains, returns a syntax error and calls the destructors of all objects on the parsing stack.
Now I don’t understand why the destructor of identifier “p” from the first line is called? It belongs to the first line and it was successfully parsed. The problem is that p is used in the main program and should be not removed by Bison.

If I choose as an (invalid) statement an arbitrary string (“foo”) p is not removed by Bison.

ConfigParam p;
foo;

In input 2:1 - 2:3 : syntax error
free at line 1: statementlist
free at line 2: foo

Why this works?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T22:10:48+00:00Added an answer on June 17, 2026 at 10:10 pm

    It’s not easy to tell the exact problem without seeing more of your file, but it’s certainly not the case that

    The first line was successfully parsed and should be popped off the stack

    The stack at the point of the error looks like this:

    statementlist KW_CONFIGPARAM
    

    where statementlist was created by the reduction of the first line, as reported by bison.

    You don’t seem to be specifying a value for statement_list; if you excerpt is literal, then it will have been compiled as though it had been written:

    statementlist   : statement ';'                 {$0 = $1;}
    

    I can’t tell what the value corresponding to statement was, but it is certainly conceivable that it includes the string "p". That depends on what you set $0 to during { /* do some stuff, create a new ConfigParam object */ }.

    (Moved from a comment once it became evident that it’s somewhat close to the right answer.)
    My other idea is this: Perhaps you’ve made all of your terminals and non-terminals have a type which triggers that destructor. And maybe your lexer does not modify yylval when it returns KW_CONFIGPARM. In that case, the bison parser cannot know that yylval is irrelevant, so it will use the left over yylval (which is a pointer to "p").

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following bison grammar (as part of a more complex grammar): classDeclaration
In larger program I have given the following (flex/bison) In flex: pn [\+|\-] dig
Problem: I have a project which I'm porting from Solaris/Lex/Yacc to Linux/Flex/Bison + Autotools.
I have this problem with bison/flex (I've seen other posts, but I don't define
I'm writing a grammar in YACC (actually Bison), and I'm having a shift/reduce problem.
I want to use Flex and Bison together. I have declared a union in
I'm having trouble following the steps to make my bison/flex parser reentrant with a
I have been looking at flex and bison tutorials online trying to solve my
Okay I have the following code in Bison .l file. By the way I
I have a project for school where we need to use flex and bison.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.