FLEX:有没有办法一次返回多个令牌
在 Flex 中,我想为正则表达式的一次匹配返回多个标记。 有没有办法做到这一点?
In flex, I want to return multiple tokens for one match of a regular expression. Is there a way to do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我这样做的方法是创建一个要返回的令牌队列,并在 yylex() 的开头检查令牌并返回它们。
The way I've been doing this is to create a queue of to-be-returned tokens, and at the beginning of
yylex()
, check for tokens and return them.您是指所有匹配项吗? 您使用的是正则表达式函数还是字符串函数? 使用全局标志。至于flex,我认为你无法做到这一点。 您一次测试与一种模式的匹配,因此这可能超出了范围。 你为什么想要那个? 作为优化? 范围界定问题?
Do you mean all matches? Are you using regex functions or string functions? Use the global flag.As for flex, I don't think you can do that. You test for a match with one pattern at a time so that's probably out of scope. Why'd you want that? As an optimization? Scoping issues?
通常,这是由扫描器顶部的解析器处理的,它可以为您提供更清晰的代码。 您可以在某种程度上用状态来模拟这一点:
但是一旦您的语法变得更加复杂,这就会变得非常混乱。
Usually, this is handled by a parser on top of the scanner which gives you much cleaner code. You can emulate that to some degree with states:
but this will get very messy as soon as your grammar gets more complex.