忽略标记字符中的标记?
我的词法分析器中有以下标记定义,定义了一个字符字符串(例如“abcd”):
CharacterString:
Apostrophe
(Alphanumeric)*
Apostrophe
;
是否可以忽略两个撇号,然后能够在词法分析器中没有它们的情况下获取标记字符串(通过 $CharacterString.text->字符)?
我尝试过……
CharacterString:
Apostrophe { $channel = HIDDEN; }
(Alphanumeric)*
Apostrophe { $channel = HIDDEN; }
;
但没有成功……这种情况甚至不再匹配我的字符串(例如“oiu”将在解析器中失败 - 不匹配设置异常)。
谢谢 :)
I have the following token definition in my lexer defining a CharacterString (e.g. 'abcd'):
CharacterString:
Apostrophe
(Alphanumeric)*
Apostrophe
;
Is it possible to ignore the two apostrophes to then be able to get the token string without them in the lexer (via $CharacterString.text->chars)?
I tried ...
CharacterString:
Apostrophe { $channel = HIDDEN; }
(Alphanumeric)*
Apostrophe { $channel = HIDDEN; }
;
... without success... This case does not even match my string anymore (e.g. 'oiu' will fail in the parser - Missmatched Set Exception).
Thank you :)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
内联代码
{$channel=HIDDEN;}
会影响整个CharacterString
,因此您无法按照您尝试的方式进行操作。您将需要添加一些自定义代码并自行删除引号。这是一个小型 C 演示:
和一个小测试函数:
测试
input.txt
文件包含:如果您现在 1) 生成词法分析器和解析器,2) 编译所有
.c 源文件,以及 3) 运行
main
:您将看到
abc
(不带引号)被打印到控制台。The inline code
{$channel=HIDDEN;}
affects the entireCharacterString
, so you can't do it like the way you tried.You will need to add some custom code and remove the quotes yourself. Here's a small C demo:
and a little test function:
and the test
input.txt
file contains:If you now 1) generate the lexer and parser, 2) compile all
.c
source files, and 3) runmain
:you'll see that
abc
(without the quotes) is being printed to the console.您可以通过词法分析器的
RecognizerSharedState state
属性影响令牌构建:You can influence token construction via
RecognizerSharedState state
attribute of your lexer: