具有多个缓冲区的 lex flex 扫描仪
我想使用 yy_scan_bytes() 因为我有定义规则的空字符。我的问题是我的字符串可以匹配多个规则。我想掌握所有匹配的规则。我想一次向 yylex() 函数提供一个字符,并检查是否有匹配的内容。我尝试了以下代码进行测试,但这不起作用。
for(int i=0;i<length;i++)
{
yy_scan_bytes(&temp[i],1 );
index=TomsonTalkslex();
}
为简单起见,我只返回从扫描仪匹配的规则的索引。 temp 是一个字符缓冲区。我尝试使用 yy_switch_to_buffer(yy_scan_bytes(&temp[i],1 ));但这没有用。
我如何告诉扫描仪不要重置其状态并继续处理具有相同状态的后续缓冲区。
I want to use the yy_scan_bytes() as I have null characters defining a rule. My problem is that my string can match more than one rule. I want to get hold of all the rules matched. I want to feed the yylex() function one character at a time and check if something matched. I tried the following code for testing but this doesnt work.
for(int i=0;i<length;i++)
{
yy_scan_bytes(&temp[i],1 );
index=TomsonTalkslex();
}
For simplicity I just return the index of the rule matched from scanner. temp is a char buffer. I tried to use the yy_switch_to_buffer(yy_scan_bytes(&temp[i],1 )); but this didnt work.
How can I tell the scanner not to reset its state and continue processing subsequent buffers with the same state.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
好吧,这只是对 lex/flex 工作原理的误解。默认情况下,yylex 挂钩到 stdin,读取直到收到 EOF,并匹配每个规则。这就是为什么它是一个标记器。因此,下面的示例程序将从 stdin 读取数据,直到您输入 -c 发送 EOF。
要编译上述内容,请使用:
执行此操作时,将源文件另存为 Scanner.l。编译后,您将在同一目录中获得一个名为 flexer.exe 的文件。从终端运行它,您将得到一个等待输入的空白提示。您输入的所有内容都会尝试根据规则进行匹配,直到您只找到一个匹配项。然后它将执行与该规则关联的代码。
因此,如果您输入
abcd
,那么您将同时匹配ab
和cd
规则。我建议阅读 lex 和 yacc 以获取更详细的介绍。
Ok, This is just a misunderstanding of how lex/flex works. By default, yylex hooks into stdin, reading until it receives EOF, and matching each rule. That's why it's a tokenizer. So, the sample program below will read from stdin until you enter -c to send an EOF.
To compile the above, use:
Save the source file as scanner.l when you do this. Once you compile, you will get a file called flexer.exe in the same directory. Run it from a terminal, and you will get a blank prompt waiting for input. Everything you type will try and match against the rules, until you find only one match. It will then execute the code associated with the rule.
Therefore, if you type
abcd
, then you will match both theab
andcd
rules.I suggest reading lex and yacc for a more detailed introduction.