柔性++不良字符错误等等。刚接触弹性
我们刚刚开始使用 flex 为项目构建词法分析器,但我们不知道如何让它工作。我复制教程中给出的示例代码,并尝试使用 tut 文件作为参数运行 flex++,但每次都收到错误。例如
输入文件 (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;
}
与我得到的代码
flex++ calc.l
calc.l:1: 错误字符: % calc.l:1: 未知错误处理部分 1
calc.l:1: 未知错误处理部分 1
calc.l:1: 未知错误处理部分 1
calc.l:2: 无法识别的“%”指令
任何人都可以帮助我理解我在这里做错了什么吗?干杯
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.
input file (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 '%' directive
could anyone help me understand what im doing wrong here? cheers
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以尝试以下操作:
%{ ... %}
添加到文件的前几行,#include
和using namespace std;
(而不是尝试定义扫描仪)yylex( )
(而不是尝试调用不存在的扫描仪的方法)对于您的示例,它可能看起来像这样:
You might try something like:
%{ ... %}
to the first couple of lines in your file#include <iostream>
andusing namespace std;
(instead of trying to define Scanner)%option noyywrap
above the rules sectionyylex()
(instead of trying to call the method of a non-existant Scanner)With your example, it could look something like this:
你使用的flex++版本是什么?
我使用 '功能:快速词法分析器生成器 C/C++ V2.3.8-7 (flex++),基于 2.3.8 并由 [email protected] for c++' (-? 选项) 并且您的 cacl.c 已完美处理..
对于 Win32,此版本的 Flex++/Bison++ 是 此处
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 [email protected] for c++' (-? option) and your cacl.c is processed perfectly..
For Win32, this version of Flex++/Bison++ is here