LookAhead 在 JFlex 中不起作用

发布于 2024-12-10 07:57:35 字数 357 浏览 5 评论 0原文

我正在尝试使用 JFlex 构建解析器,但遇到一个非常基本的问题。 我想要拥有这个令牌

[A-Za-z]+_N$ { System.out.println("Noun"); }

并让它将“car_N”识别为“名词”而不识别“car_NN”。 但它不起作用,如果我使用

[A-Za-z]+_N { System.out.println("Noun"); } 

“car_N”得到识别,但“car_NN”也返回“car_N”,这不是我想要的。

我想知道这里有人知道如何提供帮助吗?

JFlex 和 Flex 前瞻符号“$”似乎不起作用

I am trying to use JFlex to build a parser but encounter a very basic issue.
I want to have this token

[A-Za-z]+_N$ { System.out.println("Noun"); }

and have it recognize "car_N" as a "Noun" and NOT recognize "car_NN".
but it does not work, if I use

[A-Za-z]+_N { System.out.println("Noun"); } 

"car_N" get recognize but "car_NN" returns "car_N" as well, this is not what I want.

I wonder anyone here knows how to help?

JFlex and Flex lookahead symbol '$' does not seem to be working

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

離殇 2024-12-17 07:57:35

我认为您误解了“$”的含义。作为前瞻,它仅在文本行的末尾匹配(硬换行)。因此它工作得很好。但它无法区分 car_N 和 car_NN,除了位于行最末端的 car_N 实例。也许你真的想要一个像这样的前瞻:

[A-Za-z]+_N/[^A-Za-z]

I think you're misunderstanding the meaning of '$'. As a lookahead, it matches only at the end of a line of text (hard-line break). As such it works perfectly fine. But it cannot differentiate between car_N and car_NN, except for instances of car_N that are at the very end of a line. Maybe you really want a lookahead something like:

[A-Za-z]+_N/[^A-Za-z]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文