in Education by
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. JavaScript questions and answers, JavaScript questions pdf, JavaScript question bank, JavaScript questions and answers pdf, mcq on JavaScript pdf, JavaScript questions and solutions, JavaScript mcq Test , Interview JavaScript questions, JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)

1 Answer

0 votes
by
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 left; std::auto_ptr right; public: ExpressionAdd *newExpressionAdd() { ExpressionAdd *newExpression = new ExpressionAdd(); std::auto_ptr autoPtr(newExpression); if (left.get() == NULL) left = autoPtr; else right = autoPtr; return newExpression; } ... }; class Parser { private: std::stack 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.

Related questions

0 votes
    __________ is the best way for communicating the results of data analysis using the R language. (a) Single ... of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    Where are memory leaks found? (a) Client side objects (b) Server side objects (c) Both Client ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Oct 23, 2021 in Education by JackTerrance
0 votes
    This question's answers are a community effort. Edit existing answers to improve this post. It is not ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 20, 2022 in Education by JackTerrance
0 votes
    ______ finds K best paths in a given graph. (a) kBestShortestPaths (b) kcirt (c) ktrees (d) kmap The ... Regression of R Programming Select the correct answer from above options...
asked Feb 10, 2022 in Education by JackTerrance
0 votes
    I have 2 large files (each about 500k lines or 85mb) containing the checksum of the file and the ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I would like to use CHOLMOD's GPU acceleration, and have found several simple examples on how to use ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Apr 16, 2022 in Education by JackTerrance
0 votes
    I had a program working good (without prompting errors) using references, but I decided to use a pointer-to-pointer array ... that it is there because I have put two flags (cout...
asked Apr 6, 2022 in Education by JackTerrance
0 votes
    Suppose you have a fairly large (~2.2 MLOC), fairly old (started more than 10 years ago) Windows ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 22, 2022 in Education by JackTerrance
0 votes
    Suppose you have a fairly large (~2.2 MLOC), fairly old (started more than 10 years ago) Windows ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 21, 2022 in Education by JackTerrance
0 votes
    Suppose you have a fairly large (~2.2 MLOC), fairly old (started more than 10 years ago) Windows ... JavaScript Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 17, 2022 in Education by JackTerrance
0 votes
    I am working on a very large scale computing library that is using STL heavily. The library is being ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 12, 2022 in Education by JackTerrance
0 votes
    I am inputting a 200mb file in my application and due to a very strange reason the memory usage of ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    I am inputting a 200mb file in my application and due to a very strange reason the memory usage of ... Questions for Interview, JavaScript MCQ (Multiple Choice Questions)...
asked Mar 5, 2022 in Education by JackTerrance
0 votes
    What is the memory limit in R for 64 bit system? (a) 8 TB (b) 9TB (c) 10TB (d) 16TB The ... Functions and Debugging of R Programming Select the correct answer from above options...
asked Feb 15, 2022 in Education by JackTerrance
0 votes
    What is the memory limit in R for 32 bit system? (a) 8 TB (b) 9TB (c) 10TB (d) 3GB The ... Functions and Debugging of R Programming Select the correct answer from above options...
asked Feb 13, 2022 in Education by JackTerrance
...