<p>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.</p>

<p>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.</p>

<p>Does anyone know of any better yacc tricks to solve this problem?</p>

<p>Please don't tell me to choose a different parser.</p>

[{'answer_id': 65096, 'author': 'Ron', 'author_id': 9100, 'author_profile': 'https://Stackoverflow.com/users/9100', 'pm_score': 1, 'selected': False, 'text': '<p>If it suits your project, consider using the Boehm Garbage collector.  That way you can freely allocate new objects and let the collector handle the deletes.  Of course there are tradeoffs involved in using a garbage collector.  You would have to weigh the costs and benefits.</p>\n'}, {'answer_id': 65098, 'author': 'user7305', 'author_id': 7305, 'author_profile': 'https://Stackoverflow.com/users/7305', 'pm_score': -1, 'selected': False, 'text': '<p>Use <a href="http://www.boost.org/doc/libs/1_36_0/libs/smart_ptr/smart_ptr.htm" rel="nofollow noreferrer">smart pointers</a>!</p>\n\n<p>Or, if you\'re uncomfortable depending on yet another library, you can always use <a href="http://www.gotw.ca/publications/using_auto_ptr_effectively.htm" rel="nofollow noreferrer">auto_ptr</a> from the C++ standard library.</p>\n'}, {'answer_id': 65424, 'author': 'Michael L Perry', 'author_id': 7668, 'author_profile': 'https://Stackoverflow.com/users/7668', 'pm_score': 3, 'selected': True, 'text': "<p>I love Yacc, but the discriminating union stack does present a challenge.</p>\n\n<p>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.</p>\n\n<p>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.</p>\n\n<p>For example:</p>\n\n<pre><code>class IExpressionOwner\n{\npublic:\n    virtual ExpressionAdd *newExpressionAdd() = 0;\n    virtual ExpressionSubstract *newExpressionSubtract() = 0;\n    virtual ExpressionMultiply *newExpressionMultiply() = 0;\n    virtual ExpressionDivide *newExpressionDivide() = 0;\n};\n\nclass ExpressionAdd : public Expression, public IExpressionOwner\n{\nprivate:\n    std::auto_ptr&lt;Expression&gt; left;\n    std::auto_ptr&lt;Expression&gt; right;\n\npublic:\n    ExpressionAdd *newExpressionAdd()\n    {\n        ExpressionAdd *newExpression = new ExpressionAdd();\n        std::auto_ptr&lt;Expression&gt; autoPtr(newExpression);\n        if (left.get() == NULL)\n            left = autoPtr;\n        else\n            right = autoPtr;\n        return newExpression;\n    }\n\n    ...\n};\n\nclass Parser\n{\nprivate:\n    std::stack&lt;IExpressionOwner *&gt; expressionOwner;\n\n    ...\n};\n</code></pre>\n\n<p>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.</p>\n\n<p><strong>Update</strong></p>\n\n<p>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.</p>\n"}, {'answer_id': 73202, 'author': 'user12310', 'author_id': 12310, 'author_profile': 'https://Stackoverflow.com/users/12310', 'pm_score': -1, 'selected': False, 'text': "<p>Why is using a different parser such a problem? Bison is readily available, and (at least on linux) yacc is usually implemented as bison. You shouldn't need any changes to your grammar to use it (except for adding %destructor to solve your issue).</p>\n"}]
['https://Stackoverflow.com/questions/64958', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/8566/']