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

  • Home
  • SEARCH
  • 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 48959
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T16:21:42+00:00 2026-05-10T16:21:42+00:00

Yacc does not permit objects to be passed around. Because the %union can only

  • 0

Yacc does not permit objects to be passed around. Because the %union can only contain POD types, complex objects must be new’d and passed around by pointer. If a syntax error occurs, the yacc parser just stops running, and references to all of those created objects are lost.

The only solution I’ve come up with is that all new’d object inherit a particular base class, be added to a container when allocated, and if there is an error everything in that container can be deleted.

Does anyone know of any better yacc tricks to solve this problem?

Please don’t tell me to choose a different parser.

  • 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. 2026-05-10T16:21:43+00:00Added an answer on May 10, 2026 at 4:21 pm

    I love Yacc, but the discriminating union stack does present a challenge.

    I don’t know whether you are using C or C++. I’ve modified Yacc to generate C++ for my own purposes, but this solution can be adapted to C.

    My preferred solution is to pass an interface to the owner down the parse tree, rather than constructed objects up the stack. Do this by creating your own stack outside of Yacc’s. Before you invoke a non-terminal that allocates an object, push the owner of that object to this stack.

    For example:

    class IExpressionOwner { public:     virtual ExpressionAdd *newExpressionAdd() = 0;     virtual ExpressionSubstract *newExpressionSubtract() = 0;     virtual ExpressionMultiply *newExpressionMultiply() = 0;     virtual ExpressionDivide *newExpressionDivide() = 0; };  class ExpressionAdd : public Expression, public IExpressionOwner { private:     std::auto_ptr<Expression> left;     std::auto_ptr<Expression> right;  public:     ExpressionAdd *newExpressionAdd()     {         ExpressionAdd *newExpression = new ExpressionAdd();         std::auto_ptr<Expression> autoPtr(newExpression);         if (left.get() == NULL)             left = autoPtr;         else             right = autoPtr;         return newExpression;     }      ... };  class Parser { private:     std::stack<IExpressionOwner *> expressionOwner;      ... }; 

    Everything that wants an expression has to implement the IExpressionOwner interface and push itself to the stack before invoking the expression non-terminal. It’s a lot of extra code, but it controls object lifetime.

    Update

    The expression example is a bad one, since you don’t know the operation until after you’ve reduced the left operand. Still, this technique works in many cases, and requires just a little tweaking for expressions.

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

Sidebar

Ask A Question

Stats

  • Questions 90k
  • Answers 90k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer What is your build process? The Maven release process actually… May 11, 2026 at 6:05 pm
  • Editorial Team
    Editorial Team added an answer You should probably try to read about 'Temporal Database' handling.… May 11, 2026 at 6:05 pm
  • Editorial Team
    Editorial Team added an answer Use (?:) to get rid of the unnamed group: r"(?:(?P<a>1)=(?P<b>2))|(?P<c>3)"… May 11, 2026 at 6:05 pm

Related Questions

Does anyone know any resources (books/websites/whatever) related to parsing. I'm not so much interested
Since I've gotten no answer at all to my question Is there an alternative
I'm currently working on a Mips Code Generator for my Pascal Parser (Written in
I am looking for a clear definition of what a tokenizer, parser and lexer

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.