如何获取 Antlr 解析器规则以从默认通道和隐藏通道读取
我在隐藏通道中使用正常的空白分隔,但我有一个规则,我想包含任何空白以供以后处理,但我发现的任何示例都需要一些非常奇怪的手动编码。
是否没有简单的选项可以从多个通道读取内容,例如从头开始将空格放置在那里的选项。
前任。这是 WhiteSpace 词法分析器规则
WS : ( ' '
| '\t'
| '\r'
| '\n'
) {$channel=HIDDEN;}
;
这是我的规则,我想在其中包含空格
raw : '{'? (~('{'))*;
基本上,这是一个捕获所有规则,用于捕获与要由另一个模式处理的其他规则不匹配的任何内容,因此我需要原始流。
我希望有一个 {$channel==DEFAULT || $channel==HIDDEN}
语法示例,但找不到任何示例。
我的目标是 C#,但如果需要,我可以重写 Java 示例。
I use the normal whitespace separation into the hidden channel but I have one rule where I would like to include any whitespace for later processing but any example I have found requires some very strange manual coding.
Is there no easy option to read from multiple channels like the option to put the whitespace there from the beginning.
Ex. this is the WhiteSpace lexer rule
WS : ( ' '
| '\t'
| '\r'
| '\n'
) {$channel=HIDDEN;}
;
And this is my rule where I would like to include whitespace
raw : '{'? (~('{'))*;
Basically it's a catch all rule to capture any content that does not match other rules to be processed by another pattern and therefore I need the original stream.
I was hoping for a {$channel==DEFAULT || $channel==HIDDEN}
syntax example but cannot find any.
My target will be C# but I can rewrite Java examples if required.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
AFAIK,那是不可能的。但是,您可以扩展
UnbufferedTokenStream
以在解析期间更改channel
。您不能使用CommonTokenStream
,因为它缓冲可变数量的令牌(并且缓冲区中可能存在位于错误通道上的令牌!)。请注意,您至少需要 ANTLR 3.3:在以前的版本中,尚未包含UnbufferedTokenStream
。假设您想要解析(并显示)小写或大写字母。大写字母被放在
HIDDEN
通道上,因此默认情况下,只会解析小写字母。然而,当解析器偶然发现小写的“q”
时,我们想要更改为HIDDEN
通道。在HIDDEN
通道上进行解析后,我们希望"Q"
将我们再次带回DEFAULT_CHANNEL
。因此,在解析源代码
"aAbBcqCdDQeE"
时,首先是"a"
、"b"
和"c"
打印,然后更改通道,然后打印"C"
和"D"
,然后再次更改通道,最后"e" 被打印到控制台。
这是执行此操作的 ANTLR 语法:
ChannelDemo.g
这是自定义令牌流类:
ChangeableChannelTokenStream.java
和一个用于测试所有内容的小 Main 类:
Main.java
最后,生成词法分析器/解析器 (1),编译所有源文件(2) 并运行 Main 类 (3):
1
2
3 (*nix)
3 (Windows)
这将导致以下内容打印到控制台:
编辑
您可以将该类包含在您的像这样的语法文件:
从上面的语法生成的解析器在遇到
"*"
时将启用从所有通道的读取。因此,在解析"aAbB*cCdDeE"
时:将打印以下内容:
AFAIK, that is not possible. However, you could extend the
UnbufferedTokenStream
to change thechannel
during parsing. You can't use theCommonTokenStream
since it buffers a variable amount of tokens (and there can be tokens in the buffer that are on the wrong channel!). Note that you need at least ANTLR 3.3: in previous versions theUnbufferedTokenStream
wasn't included yet.Let's say you want to parse (and display) either lower- or upper case letters. Upper case letters are put on the
HIDDEN
channel, so by deafult, only lower case letters will be parsed. However, when the parser stumbles upon a lower case"q"
, we want to change to theHIDDEN
channel. Once parsing on theHIDDEN
channel, we want the"Q"
to bring us back to theDEFAULT_CHANNEL
again.So when parsing the source
"aAbBcqCdDQeE"
, first"a"
,"b"
and"c"
are printed, then the channel is changed, then"C"
and"D"
get printed, then the channel is changed again, and finally"e"
is printed to the console.Here's an ANTLR grammar that does this:
ChannelDemo.g
And here's the custom token stream class:
ChangeableChannelTokenStream.java
And a small Main class to test it all:
Main.java
Finally, generate a lexer/parser (1), compile all source files (2) and run the Main class (3):
1
2
3 (*nix)
3 (Windows)
which will cause the following to be printed to the console:
EDIT
You can include the class in your grammar file like this:
The parser that gets generated from the grammar above will enable reading from all channels when it encounters a
"*"
. So when parsing"aAbB*cCdDeE"
:the following gets printed:
也许您应该考虑将空格作为语法的一部分。但为什么要用如此不重要的信息来扰乱你的语法呢?好吧,因为这并不不重要。换行符在某些情况下有意义。当您需要 IDE 支持(例如来自 Visual Studio 语言服务器)时,您需要指定一个语言语法器,而不需要低级 ANTLR 自定义的所有花哨功能。
Maybe you should consider making whitespace a part of your gramar instead. But why clutter up your gramar with such unimportant information? Well, because it is NOT unimportant. The newline has meaining in certain contexts. When you want IDE support, e.g. from visual studio language server, you need to specify a language grammer without all the bells and whistles of low-level ANTLR customization.
在
Antler 4
中,我使用了一个简单的解决方案。我没有在Antlr 3
中测试它。它是 C#,但您可以轻松地将其转换为 Java。将
parser1.g4
更改如下:将
lexer1.g4
更改为下一个:将
Parser1
类扩展为下一个:将
Lexer1
类扩展为下一个:现在
Main
函数作为下一个:In
Antler 4
I'm using a simple solution. I didn't test it inAntlr 3
. It's C# but you can translate it to Java easily.Change
parser1.g4
as next:Change
lexer1.g4
as next:Extend
Parser1
class as next:Extend
Lexer1
class as next:Now the
Main
function as next: