这是parser.ypp文件的开头
%{
#include <iostream>
#include "hw3_output.hpp"
using namespace std;
using namespace output;
extern int yylineno;
extern int yylex();
int yyparse();
void output::errorSyn(int lineno);
void yyerror(char const* s);
output::TableStack* SymboleTableStack = new output::TableStack();
SymboleTableStack->makeTable();
output::offsetStack* st = new output::offsetStack();
st->push(0);
%}
这是我的hw3_output.hpp文件。我只显示重要部分:
namespace output{
class SymbolTable {
vector<std::shared_ptr<SymbolNode>> Symbols;
public:
SymbolTable() : Symbols() {}
void AddSymbolToTable(std::shared_ptr<SymbolNode> symbolNode) {
Symbols.push_back(symbolNode);
}
....
class TableStack {
public:
vector<SymbolTable*> stack;
TableStack() :stack() {}
SymbolTable* topTable()
{
return *(stack.begin() + stack.size() - 1);
}
class offsetStack
{
public:
vector<int> stack;
offsetStack() :stack() {}
};
}
I keep getting the following error (and the same for the offsetStack)
您粘贴的代码位于生成的文件的顶层(例如,可以使用#include
指令)。回想一下,C ++程序的顶层只能包含声明和定义,而不能包含可执行语句。和