How can I use a scanner I've written using Flex as part of a program I'm designing? Specifically, within a c++ class as a method of the class, and from a separate file with just a main method to perform testing.
我如何使用我使用Flex编写的扫描仪作为我正在设计的程序的一部分?具体来说,在一个c ++类中作为类的一个方法,并从一个单独的文件中只用一个main方法来执行测试。
I don't wish to use the %option c++, but will compile with g++.
我不想使用%选项c ++,但会用g ++编译。
To answer the problem of how to test the scanner from a separate file's main I attempted with the following code:
要回答如何从单独的文件主要测试扫描仪的问题,我尝试使用以下代码:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
extern "C" {
extern int yylex();
}
extern FILE* yyin;
int main(int argc, char *argv[]) {
if (argc > 1)
yyin = fopen(argv[1], "r");
yylex();
return 0;
}
I compile like so:
我这样编译:
flex mylexer.l++
g++ lex.mylexer.C myDriver.C -o myLexer
I get:
undefined reference to yyin
未定义的yyin引用
undefined reference to yylex
对yylex的未定义引用
What is the correct way to compile/setup the driver file? Thank you for reading and contributing anything!
编译/设置驱动程序文件的正确方法是什么?感谢您阅读和贡献任何东西!
4 个解决方案
#1
8
The simplist example I have is:
我的简单例子是:
mylex.l
%option noyywrap
%%
: return ':';
%%
main.cpp
#include <stdio.h>
#include <iostream>
extern "C"
{
extern int yylex(void);
extern FILE* yyin;
}
int main()
{
yyin = fopen("plop", "r");
std::cout << yylex() << "\n";
}
Then to build:
然后建立:
> flex -o mylex.c mylex.l
> gcc -c mylex.c
> g++ -c main.cpp
> g++ main.o mylex.o
Notice the gcc to compile the mylex.c
If you compile mylex.c with g++ it will be compiled as C++ (not C) and your extern "C" declarations in main would be wrong. Thus you need to compile the mylex.c and main.cpp with different compilers then link them together in separate steps.
注意gcc编译mylex.c如果用g ++编译mylex.c它将被编译为C ++(而不是C),并且你在main中的extern“C”声明是错误的。因此,您需要使用不同的编译器编译mylex.c和main.cpp,然后在不同的步骤中将它们链接在一起。
Version 2:
Alternatively you can compile the flex code as C++ and remove the extern "C" from main.
或者,您可以将flex代码编译为C ++,并从main中删除extern“C”。
main.cpp
#include <stdio.h>
#include <iostream>
extern int yylex(void);
extern FILE* yyin;
int main()
{
yyin = fopen("plop", "r");
std::cout << yylex() << "\n";
}
Now Build like this:
现在构建如下:
> flex -o mylex.c mylex.l
> g++ -c mylex.c
> g++ -c main.cpp
> g++ main.o mylex.o
Notice this time I used g++ to compile mylex.c (which you could call mylex.cpp now).
注意这次我使用g ++来编译mylex.c(你现在可以调用mylex.cpp)。
Now that you are using the same compiler it can be a one liner:
现在你使用的是同一个编译器,它可以是一个单行程序:
> flex -o mylex.c mylex.l
> g++ mylex.c main.cpp
#2
2
You have to link the flex library with your programm, that is, you have to add -lfl
to your g++compiler invocation.
您必须将flex库与您的程序链接,也就是说,您必须将-lfl添加到您的g ++编译器调用中。
flex mylexer.l++
g++ lex.mylexer.C myDriver.C -o myLexer -lfl
#3
0
You need to include the file generated by flex on your g++ command line -- both yyin and yylex are defined there. If lex.mylexer.C is supposed to be that file, its possible that you're getting a flex error you're ignoring, or otherwise not running flex properly to generate the file -- check it to make sure that it actually contains the flex output and isn't an empty file.
您需要在g ++命令行中包含flex生成的文件 - yyin和yylex都在那里定义。如果lex.mylexer.C应该是那个文件,你可能会得到一个你忽略的flex错误,或者没有正确运行flex来生成文件 - 检查它以确保它实际上包含flex输出并且不是空文件。
#1
8
The simplist example I have is:
我的简单例子是:
mylex.l
%option noyywrap
%%
: return ':';
%%
main.cpp
#include <stdio.h>
#include <iostream>
extern "C"
{
extern int yylex(void);
extern FILE* yyin;
}
int main()
{
yyin = fopen("plop", "r");
std::cout << yylex() << "\n";
}
Then to build:
然后建立:
> flex -o mylex.c mylex.l
> gcc -c mylex.c
> g++ -c main.cpp
> g++ main.o mylex.o
Notice the gcc to compile the mylex.c
If you compile mylex.c with g++ it will be compiled as C++ (not C) and your extern "C" declarations in main would be wrong. Thus you need to compile the mylex.c and main.cpp with different compilers then link them together in separate steps.
注意gcc编译mylex.c如果用g ++编译mylex.c它将被编译为C ++(而不是C),并且你在main中的extern“C”声明是错误的。因此,您需要使用不同的编译器编译mylex.c和main.cpp,然后在不同的步骤中将它们链接在一起。
Version 2:
Alternatively you can compile the flex code as C++ and remove the extern "C" from main.
或者,您可以将flex代码编译为C ++,并从main中删除extern“C”。
main.cpp
#include <stdio.h>
#include <iostream>
extern int yylex(void);
extern FILE* yyin;
int main()
{
yyin = fopen("plop", "r");
std::cout << yylex() << "\n";
}
Now Build like this:
现在构建如下:
> flex -o mylex.c mylex.l
> g++ -c mylex.c
> g++ -c main.cpp
> g++ main.o mylex.o
Notice this time I used g++ to compile mylex.c (which you could call mylex.cpp now).
注意这次我使用g ++来编译mylex.c(你现在可以调用mylex.cpp)。
Now that you are using the same compiler it can be a one liner:
现在你使用的是同一个编译器,它可以是一个单行程序:
> flex -o mylex.c mylex.l
> g++ mylex.c main.cpp
#2
2
You have to link the flex library with your programm, that is, you have to add -lfl
to your g++compiler invocation.
您必须将flex库与您的程序链接,也就是说,您必须将-lfl添加到您的g ++编译器调用中。
flex mylexer.l++
g++ lex.mylexer.C myDriver.C -o myLexer -lfl
#3
0
You need to include the file generated by flex on your g++ command line -- both yyin and yylex are defined there. If lex.mylexer.C is supposed to be that file, its possible that you're getting a flex error you're ignoring, or otherwise not running flex properly to generate the file -- check it to make sure that it actually contains the flex output and isn't an empty file.
您需要在g ++命令行中包含flex生成的文件 - yyin和yylex都在那里定义。如果lex.mylexer.C应该是那个文件,你可能会得到一个你忽略的flex错误,或者没有正确运行flex来生成文件 - 检查它以确保它实际上包含flex输出并且不是空文件。