【发布时间】:2015-05-07 14:24:32
【问题描述】:
我正在编写一种递归下降解析器。在我的语法树的第一阶段,我将正在解析的模型片段保存在一个向量中。第一部分是左侧部分(lhs),其余部分是右侧部分(rhs)。 Lhs 加上 rhs 件构成生产规则 (pr)。任何 rhs 片段本身可能是 pr,其中它的第一个片段是 lhs 片段。那个 pr 的 lhs 片段也是它在我称之为“sub”类型的 pr 中占据的片段。例如,让 pr1 = lhs10 rhs11 sub12 rhs13 和 pr2 = lhs20 rhs21 rhs22。 lhs20 和 sub12 将是同一块。我收到“内部向量”错误可能是因为我正在对与周围对象相同类型的对象执行 push_back,但是有没有办法使用向量等容器中的智能指针对象来实现这种递归?
#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;
}
【问题讨论】:
标签: c++ parsing recursion vector