I am writing a kind of recursive descent parser. In the first stage of my syntax tree, I hold the model pieces I am parsing in a vector. The first piece is the left hand side piece (lhs) and the remaining pieces are right hand side pieces (rhs). Lhs plus the rhs pieces constitutes a production rule (pr). Any of the rhs pieces may themselves be a pr where its first piece is an lhs piece. That pr's lhs piece is also the piece it occupies in the pr whose type I call 'sub'. For example, let pr1 = lhs10 rhs11 sub12 rhs13 and pr2 = lhs20 rhs21 rhs22. lhs20 and sub12 would be the same piece. I am getting an 'inside vector' error probably because I am doing a push_back on the same kind of object as the surrounding object, but is there a way to achieve this kind of recursivity using smart-pointered objects in containers like vector?
我正在写一种递归下降解析器。在我的语法树的第一阶段,我持有我在向量中解析的模型片段。第一件是左手侧件(lhs),其余件是右手件(rhs)。 Lhs加上rhs碎片构成生产规则(pr)。任何rhs件本身可以是pr,其第一件是lhs件。那个pr的lhs片段也是它在pr中占据的片段,我称之为'sub'。例如,令pr1 = lhs10 rhs11 sub12 rhs13和pr2 = lhs20 rhs21 rhs22。 lhs20和sub12将是同一块。我得到一个'内部向量'错误可能是因为我在与周围对象相同类型的对象上进行push_back,但是有没有办法在像vector这样的容器中使用智能指针对象实现这种递归?
#include "stdafx.h"
#include <memory>
#include <vector>
struct AbstractSyntaxTree;
typedef std::shared_ptr< AbstractSyntaxTree > ModelPiece;
typedef std::vector< ModelPiece > ModelPiecesVect;
struct AbstractSyntaxTree
{
enum LexState
{
LHS = 0x0, RHS = 0x1, SUB = 0x2
};
ModelPiecesVect modelPiecesVect;
LexState lexState;
};
int main()
{
ModelPiece mp;
ModelPiece lhs10, rhs11, sub12, rhs13;
ModelPiece lhs20, rhs21, rhs22;
lhs10 = std::make_shared< AbstractSyntaxTree >();
mp->modelPiecesVect.push_back( lhs10 ); // fails here with '... _Ptr points inside vector' (see code fragment below)
mp->modelPiecesVect.push_back( rhs11 );
mp->modelPiecesVect.push_back( sub12 );
mp->modelPiecesVect.push_back( rhs13 );
mp->modelPiecesVect[ 2 ]->modelPiecesVect.push_back( lhs20 );
mp->modelPiecesVect[ 2 ]->modelPiecesVect.push_back( rhs21 );
mp->modelPiecesVect[ 2 ]->modelPiecesVect.push_back( rhs22 );
/*
bool _Inside( const value_type *_Ptr ) const
{ // test if _Ptr points inside vector
return ( _Ptr < this->_Mylast && this->_Myfirst <= _Ptr ); // 'this' is null
}
*/
return 0;
}
2 个解决方案
#1
With
typedef std::shared_ptr< AbstractSyntaxTree > ModelPiece;
This:
ModelPiece mp;
is a null pointer. You have to assign something to it, or mp->modelPiecesVect
causes undefined behavior. Similarly, these:
是一个空指针。您必须为其分配内容,或者mp-> modelPiecesVect导致未定义的行为。同样,这些:
ModelPiece lhs10, rhs11, sub12, rhs13;
ModelPiece lhs20, rhs21, rhs22;
have to be initialized (sub12
in particular), or later mp->modelPiecesVect[ 2 ]->modelPiecesVect
will fail in the same manner.
必须初始化(特别是sub12),或者稍后mp-> modelPiecesVect [2] - > modelPiecesVect将以相同的方式失败。
Apart from this, what you're doing should work.
除此之外,你所做的应该是有效的。
#2
mp->modelPiecesVect.push_back( lhs10 );
mp
is not initialized here
mp没有在这里初始化
#1
With
typedef std::shared_ptr< AbstractSyntaxTree > ModelPiece;
This:
ModelPiece mp;
is a null pointer. You have to assign something to it, or mp->modelPiecesVect
causes undefined behavior. Similarly, these:
是一个空指针。您必须为其分配内容,或者mp-> modelPiecesVect导致未定义的行为。同样,这些:
ModelPiece lhs10, rhs11, sub12, rhs13;
ModelPiece lhs20, rhs21, rhs22;
have to be initialized (sub12
in particular), or later mp->modelPiecesVect[ 2 ]->modelPiecesVect
will fail in the same manner.
必须初始化(特别是sub12),或者稍后mp-> modelPiecesVect [2] - > modelPiecesVect将以相同的方式失败。
Apart from this, what you're doing should work.
除此之外,你所做的应该是有效的。
#2
mp->modelPiecesVect.push_back( lhs10 );
mp
is not initialized here
mp没有在这里初始化