如何解决 ANTLR 错误“词法分析器操作中不允许属性引用”
读完《The Definitive ANTLR 4 Reference》第10章后,我尝试编写一个简单的分析器来获取词法属性,但出现错误。如何获取词汇属性?
lexer grammar TestLexer;
SPACE: [ \t\r\n]+ -> skip;
LINE: INT DOT [a-z]+ {System.out.println($INT.text);};
INT: [0-9]+;
DOT: '.';
[INFO]
[INFO] --- antlr4-maven-plugin:4.9.2:antlr4 (antlr) @ parser ---
[INFO] ANTLR 4: Processing source directory /Users/Poison/IdeaProjects/parser/src/main/antlr4
[INFO] Processing grammar: me.tianshuang.parser/TestLexer.g4
[ERROR] error(128): me.tianshuang.parser/TestLexer.g4:5:65: attribute references not allowed in lexer actions: $INT.text
[ERROR] /Users/Poison/IdeaProjects/parser/me.tianshuang.parser/TestLexer.g4 [5:65]: attribute references not allowed in lexer actions: $INT.text
ANTLR4版本:4.9.2。
参考:
antlr4/actions.md at master · antlr/antlr4 · GitHub
如何获取 Antlr-4 词法分析器规则操作中的标记属性 · 问题 #1946 · antlr/antlr4 · GitHub
After reading Chapter 10 of "The Definitive ANTLR 4 Reference", I tried to write a simple analyzer to get lexical attributes, but I got an error. How can I get the lexical attributes?
lexer grammar TestLexer;
SPACE: [ \t\r\n]+ -> skip;
LINE: INT DOT [a-z]+ {System.out.println($INT.text);};
INT: [0-9]+;
DOT: '.';
[INFO]
[INFO] --- antlr4-maven-plugin:4.9.2:antlr4 (antlr) @ parser ---
[INFO] ANTLR 4: Processing source directory /Users/Poison/IdeaProjects/parser/src/main/antlr4
[INFO] Processing grammar: me.tianshuang.parser/TestLexer.g4
[ERROR] error(128): me.tianshuang.parser/TestLexer.g4:5:65: attribute references not allowed in lexer actions: $INT.text
[ERROR] /Users/Poison/IdeaProjects/parser/me.tianshuang.parser/TestLexer.g4 [5:65]: attribute references not allowed in lexer actions: $INT.text
ANTLR4 version: 4.9.2.
Reference:
antlr4/actions.md at master · antlr/antlr4 · GitHub
How to get the token attributes in Antlr-4 lexer rule's action · Issue #1946 · antlr/antlr4 · GitHub
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你不能:词法分析器规则根本不支持标签。您可能会说,“好吧,但我没有使用任何标签!”。但以下:
只是以下的简写符号:
其中
some_var_name
称为标签。如果删除嵌入的代码(
{
和}
之间的内容),在INT
之前添加标签,然后生成词法分析器,您将请参阅打印到 stderr 的以下警告:最后一部分意味着您可以像这样抓取词法分析器规则的整个文本:
但是从词法分析器的各个部分抓取文本规则是不可能的。
You can't: labels are simply not supported in lexer rules. You might say, "well, but I'm not using any labels!". But the following:
is just a shorthand notation for:
where
some_var_name
is called a label.If you remove the embedded code (the stuff between
{
and}
), add a label beforeINT
and then generate a lexer, you'll see the following warning being printed to stderr:The last part means that you can grab the entire text of the lexer rule like this:
But grabbing text from individual parts of a lexer rule is not possible.
尝试分离词法分析器和其他输出问题的关注点:这是 Antlr 的主要焦点VS 野牛/Flex。例如,您可以使用本书其他章节中的访问者/听众模式。
Try to separate the concerns of the lexer and other output matters: that's a main focus point of Antlr VS Bison/Flex. You can use for example visitor/listener patterns from the other chapters of the book.