我正在尝试在 lex 中编写一个简单的解析器并在 c++ 中执行它。我按照这里的指示开始
WIKILINK \133{2}[^\135]+\135{2}
%option noyywrap
%option c++
%{
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <stdio.h>
#include <FlexLexer.h>
class xxFlexLexer : public yyFlexLexer
{
public:
virtual int yylex()
{
std::cout << "hello" << std::endl;
}
private:
vector<string> listOfLinks;
};
using namespace std;
%}
%%
{WIKILINK} {
int size = YYLeng();
string link(YYText());
link.erase(YYLeng()-3, 2);
link.erase(0, 2);
listOfLinks.push_back(link);
}
%%
int main()
{
ifstream in;
ofstream out;
in.open("in.txt");
out.open("out.txt");
yyFlexLexer lexer(&in, &out);
lexer.yylex();
}
当我尝试编译程序 lex.yy.cc 时,我收到以下错误 -
In file included from tmp.cpp:12:0:
/usr/include/FlexLexer.h:112:7: error: redefinition of ‘class yyFlexLexer’
/usr/include/FlexLexer.h:112:7: error: previous definition of ‘class yyFlexLexer’
tmp.cpp: In member function ‘virtual int yyFlexLexer::yylex()’:
tmp.cpp:33:29: error: ‘listOfLinks’ was not declared in this scope
我不明白这个错误。怎么了?