广播令牌在不同的频道上

huangapple go评论80阅读模式
英文:

Broadcasting Tokens on Different Channels

问题

我正在(暂时性地)处理一个语言翻译问题,我想在翻译的代码中包括源代码中的注释。

我正在研究ANTLR 4书籍《The Definitive ANTLR4 Reference》中的第12.1节“在不同通道上广播标记”,似乎解决了这个问题。

不幸的是,当我尝试这个示例时,它不起作用!我收到以下错误消息:

错误(177):Cymbol.g4:59:30:WHITESPACE不是一个已识别的通道名错误(177):Cymbol.g4:62:33:COMMENTS不是一个已识别的通道名

快速搜索发现Antlr问题#1555,该问题将问题视为工具正常工作。

我的问题仍然是,如何将注释与AST合并?

TIA

我复制了示例代码lexmagic/Cymbol.g4:

...
WS  :   [ \t\n\r]+ -> channel(WHITESPACE) ;
...

并收到以下错误:

错误(177):Cymbol.g4:59:30:WHITESPACE不是一个已识别的通道名错误(177):Cymbol.g4:62:33:COMMENTS不是一个已识别的通道名

英文:

I'm (tentatively) working on a language translation problem where I would like to include the comments in the source code in the translated code.

I'm working through the ANTLR 4 book "The Definitive Aantlr4 Reference" and section 12.1 "Broadcasting Tokens on Different Channels" seems to address this issue.

Unfortunately, when I try this example this it doesn't work! I get the error:

> error(177): Cymbol.g4:59:30: WHITESPACE is not a recognized channel
> name error(177): Cymbol.g4:62:33: COMMENTS is not a recognized channel
> name

A quick search unveils Antlr issue #1555, which loses the issue as the tool working correctly.

My question remains, how do I merge the comments with the AST?

TIA

I copied the sample code lexmagic/Cymbol.g4

...
WS  :   [ \t\n\r]+ -> channel(WHITESPACE) ;
...

and got the error:

> error(177): Cymbol.g4:59:30: WHITESPACE is not a recognized channel
> name error(177): Cymbol.g4:62:33: COMMENTS is not a recognized channel
> name

答案1

得分: 1

你必须声明你想要使用的通道,除了2个内置通道(DEFAULTHIDDEN)。

在你的语法顶部(在任何规则之前)添加一个通道块:

channels {
    WHITESPACE
}

请记住,通道和标记共享相同的命名空间。你不能在通道和标记中使用相同的标识符。

英文:

You have to declare the channels you want to used, except for the 2 built-in channels (DEFAULT and HIDDEN).

At the top of your grammar (before any rule) add a channels block:

channels {
    WHITESPACE
}

Keep in mind that channels and tokens share the same namespace. You cannot use the same identifier for a channel and a token.

huangapple
  • 本文由 发表于 2023年7月17日 11:28:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/76701325.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定