我正在尝试使用bison,flex和llvm为某种语言生成代码。 以下是联合和规则部分的代码的一部分(点是指与问题无关的代码):
%union {
Node *node;
NBlock *block;
NMethodCall* methodcall;
.
.
.
std::vector<NIdentifier*> idetListvec;
std::vector<NRecordItem*> recdListvec;
.
.
}
.
.
type <expr> numeric
%type <expr> CondExpression
%type <idetListvec> IdentifierList
%type <recdListvec> RecordItemList VarDefnList FormalParmSecList VarDefns FormalParameterList
.
.
%%
.
.
FormalParameterList : TLPAREN FormalParmSecList TRPAREN {$$ = $2;}
;
FormalParmSecList : FormalParmSecList TSEMICOLON FormalParmSec { $1->push_back($3);}
| FormalParmSec {$$ = new NRecordItemList(); $$->push_back($1);}
;
FormalParmSec : ValueParmSpec {$$ = $1;}
| VariableParmSpec {$$ = $1;}
;
%%
我有一个单独的文件用于所谓的抽象语法树(AST)来实现类。以下是我想问的课程:
#include <iostream>
#include <vector>
class NStatement;
.
classNRecordItem;
classModule;
.
.
typedef std::vector<NRecordItem*> NRecordItemList;
typedef std::vector<NConstantDeclaration*> NConstantDeclarationList;
typedef std::vector<NTypDefn> NTypDefnList;
typedef std::vector<Module> NModuleList;
class Node {
public:
virtual ~Node() {}
//virtual llvm::Value* codeGen(CodeGenContext& context) { return NULL; }
};
class NExpression : public Node {
};
class NStatement : public Node {
};
class NInteger : public NExpression {
public:
int value;
NInteger(int value) : value(value) { }
//virtual llvm::Value* codeGen(CodeGenContext& context);
};
.
.
class NRecordItem : public NStatement {
public:
const NIdentifier& type;
NIdentifierList arguments;
NRecordItem(NIdentifierList& arguments, const NIdentifier& type ) :
type(type), arguments(arguments) { }
//virtual llvm::Value* codeGen(CodeGenContext& context);
};
运行代码时,它给出了以下错误:
parser.y:256:26: error: no match for ‘operator=’ (operand types are ‘std::vector<NRecordItem*>’ and ‘NRecordItemList* {aka int*}’)
| FormalParmSec {$$ = new NRecordI
^
In file included from /usr/include/c++/5/vector:69:0,
from node.h:2,
from parser.y:2:
/usr/include/c++/5/bits/vector.tcc:167:5: note: candidate: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(const std::vector<_Tp, _Alloc>&) [with _Tp = NRecordItem*; _Alloc = std::allocator<NRecordItem*>]
vector<_Tp, _Alloc>::
^
/usr/include/c++/5/bits/vector.tcc:167:5: note: no known conversion for argument 1 from ‘NRecordItemList* {aka int*}’ to ‘const std::vector<NRecordItem*>&’
In file included from /usr/include/c++/5/vector:64:0,
from node.h:2,
from parser.y:2:
/usr/include/c++/5/bits/stl_vector.h:448:7: note: candidate: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::vector<_Tp, _Alloc>&&) [with _Tp = NRecordItem*; _Alloc = std::allocator<NRecordItem*>]
operator=(vector&& __x) noexcept(_Alloc_traits::_S_not
^
/usr/include/c++/5/bits/stl_vector.h:448:7: note: no known conversion for argument 1 from ‘NRecordItemList* {aka int*}’ to ‘std::vector<NRecordItem*>&&’
/usr/include/c++/5/bits/stl_vector.h:470:7: note: candidate: std::vector<_Tp, _Alloc>& std::vector<_Tp, _Alloc>::operator=(std::initializer_list<_Tp>) [with _Tp = NRecordItem*; _Alloc = std::allocator<NRecordItem*>]
operator=(initializer_list<value_type> __l)
^
我的问题是关于规则&#39; FormalParmSecList&#39;。我将参数的值存储在矢量类中但它给出了该错误。 任何想法!
答案 0 :(得分:2)
您正试图将std::vector
个对象直接放在<{1}}中。这不会起作用,因为它们不会被正确地构造和破坏。您需要更改%union
以使用指针:
%union
你已经在尝试存储指向行为中向量的指针,所以看起来你已经尝试这样做了,你只是错过了声明。
答案 1 :(得分:1)
$$ = new NRecordItemList();
此语句创建指向NRecordItemList
对象的指针,并将其分配给$$
。但是,在某处您声明$$
是NRecordItemList
,而不是指向一个new
。您需要从此语句中删除$$
或更改此语法规则的类型声明,以便function ReadSecuredFile
{
[SecureString] $SecuredString;
$SecuredString = ConvertTo-SecureString 'Testing123' -asplaintext -force;
return $SecuredString;
}
$a = ReadSecuredFile;
具有正确的类型。我在这里发布的代码中没有看到您为此特定规则声明类型的位置。您需要找到并修复它。 (自从我使用野牛以来已经有一段时间了。)
答案 2 :(得分:0)
您正在尝试为std::vector
分配一个指向std::vector
(又名std::vector*
)的指针。
从错误行中移除new
以获取{$$ = NRecordItemList(); $$->push_back($1)}