we are just getting started using flex to build a lexer for a project, but we cant figure out how to get it to work. I copy the example code given in tutorials and try to run flex++ with the tut file as its argument however I just receive an error each time. e.g.
我们刚刚开始使用flex为一个项目构建一个lexer,但是我们不知道如何让它工作。我复制了教程中给出的示例代码,并尝试使用tut文件运行flex++,但是每次都只接收一个错误。如。
input file (calc.l)
输入文件(calc.l)
%name Scanner
%define IOSTREAM
DIGIT [0-9]
DIGIT1 [1-9]
%%
"+" { cout << "operator <" << yytext[0] << ">" << endl; }
"-" { cout << "operator <" << yytext[0] << ">" << endl; }
"=" { cout << "operator <" << yytext[0] << ">" << endl; }
{DIGIT1}{DIGIT}* { cout << " number <" << yytext << ">" << endl; }
. { cout << " UNKNOWN <" << yytext[0] << ">" << endl; }
%%
int main(int argc, char ** argv)
{
Scanner scanner;
scanner.yylex();
return 0;
}
with this code i get
我得到了这个代码。
flex++ calc.l
calc.l:1: bad character: % calc.l:1: unknown error processing section 1
calc.l:1: unknown error processing section 1
calc.l:1: unknown error processing section 1
calc.l:2: unrecognised '%' directive1:未知错误处理第1节calc.l:1:未知错误处理第1节calc.l:1:未知错误处理第1节calc.l:2:未确认的“%”指令。
could anyone help me understand what im doing wrong here? cheers
有人能帮我理解我在这里做错了什么吗?干杯
2 个解决方案
#1
3
You might try something like:
你可以试试:
- adding
%{ ... %}
to the first couple of lines in your file - 添加% {…%}在您的文件的前几行。
- adding
#include <iostream>
andusing namespace std;
(instead of trying to define Scanner) -
添加#包括
和使用名称空间std;(而不是试图定义扫描仪) - adding
%option noyywrap
above the rules section - 在规则部分中添加%选项noyywrap。
- using just
yylex()
(instead of trying to call the method of a non-existant Scanner) - 使用yylex()(而不是尝试调用非存在扫描器的方法)
With your example, it could look something like this:
举个例子,它可以是这样的:
%{
#include <iostream>
using namespace std;
%}
DIGIT [0-9]
DIGIT1 [1-9]
/* read only one input file */
%option noyywrap
%%
"+" { cout << "operator <" << yytext[0] << ">" << endl; }
"-" { cout << "operator <" << yytext[0] << ">" << endl; }
"=" { cout << "operator <" << yytext[0] << ">" << endl; }
{DIGIT1}{DIGIT}* { cout << " number <" << yytext << ">" << endl; }
. { cout << " UNKNOWN <" << yytext[0] << ">" << endl; }
%%
int main(int argc, char** argv)
{
yylex();
return 0;
}
#2
0
What is the version of flex++ you using? I uses 'Function: fast lexical analyzer generator C/C++ V2.3.8-7 (flex++), based on 2.3.8 and modified by coetmeur@icdc.fr for c++' (-? option) and your cacl.c is processed perfectly..
你使用的flex++的版本是什么?我使用了“功能:快速词汇分析器生成器C/ c++ V2.3.8-7 (flex++),基于2.3.8,并由coetmeur@icdc.fr修改为c++”(-?选项)和cacl。c处理完美. .
For Win32, this version of Flex++/Bison++ is here
对于Win32,这个版本的Flex++/Bison++在这里。
#1
3
You might try something like:
你可以试试:
- adding
%{ ... %}
to the first couple of lines in your file - 添加% {…%}在您的文件的前几行。
- adding
#include <iostream>
andusing namespace std;
(instead of trying to define Scanner) -
添加#包括
和使用名称空间std;(而不是试图定义扫描仪) - adding
%option noyywrap
above the rules section - 在规则部分中添加%选项noyywrap。
- using just
yylex()
(instead of trying to call the method of a non-existant Scanner) - 使用yylex()(而不是尝试调用非存在扫描器的方法)
With your example, it could look something like this:
举个例子,它可以是这样的:
%{
#include <iostream>
using namespace std;
%}
DIGIT [0-9]
DIGIT1 [1-9]
/* read only one input file */
%option noyywrap
%%
"+" { cout << "operator <" << yytext[0] << ">" << endl; }
"-" { cout << "operator <" << yytext[0] << ">" << endl; }
"=" { cout << "operator <" << yytext[0] << ">" << endl; }
{DIGIT1}{DIGIT}* { cout << " number <" << yytext << ">" << endl; }
. { cout << " UNKNOWN <" << yytext[0] << ">" << endl; }
%%
int main(int argc, char** argv)
{
yylex();
return 0;
}
#2
0
What is the version of flex++ you using? I uses 'Function: fast lexical analyzer generator C/C++ V2.3.8-7 (flex++), based on 2.3.8 and modified by coetmeur@icdc.fr for c++' (-? option) and your cacl.c is processed perfectly..
你使用的flex++的版本是什么?我使用了“功能:快速词汇分析器生成器C/ c++ V2.3.8-7 (flex++),基于2.3.8,并由coetmeur@icdc.fr修改为c++”(-?选项)和cacl。c处理完美. .
For Win32, this version of Flex++/Bison++ is here
对于Win32,这个版本的Flex++/Bison++在这里。