本文介绍了我的词法分析器令牌操作未调用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我将antlr4与javascript目标一起使用.

I use antlr4 with javascript target.

这是示例语法:

P : T ;
T : [a-z]+ {console.log(this.text);} ;
start: P ;

运行生成的解析器时,尽管输入已匹配,但未打印任何内容.如果将动作移至令牌P,则会调用该令牌.为什么会这样?

When I run the generated parser, nothing is printed, although the input is matched. If I move the action to the token P, then it gets invoked. Why is that?

推荐答案

在引用的规则中将忽略操作.这是ANTLR 4的原始行为,当时该词法分析器仅支持每个令牌一个操作(并且该操作必须出现在令牌的末尾).

Actions are ignored in referenced rules. This was the original behavior of ANTLR 4, back when the lexer only supported a single action per token (and that action must appear at the end of the token).

后来又发布了几个规则,取消了每个规则一个动作的限制,从而允许为令牌执行任意数量的动作.但是,我们发现许多现有用户依赖于原始行为,并假设忽略了参考规则中的操作,编写了他们的语法.这些语法中有许多在这些规则中使用了复杂的逻辑,因此更改行为将是一个重大的突破性变化,这将阻止人们使用新版本的ANTLR 4.

Several releases later the limitation of one-action-per-rule was lifted, allowing any number of actions to be executed for a token. However, we found that many existing users relied on the original behavior, and wrote their grammars assuming that actions in referenced rules were ignored. Many of these grammars used complicated logic in these rules, so changing the behavior would be a severe breaking change that would prevent people from using new versions of ANTLR 4.

我们没有破坏很多现有的ANTLR 4词法分析器,而是决定保留原始行为,并仅执行与匹配令牌以相同规则出现的动作.较新的版本确实允许您在每个规则中放置多个操作.

Rather than break so many existing ANTLR 4 lexers, we decided to preserve the original behavior and only execute actions that appear in the same rule as the matched token. Newer versions do allow you to place multiple actions in each rule though.

tl; dr:我们考虑过允许执行其他规则中的动作,但决定不这样做,因为这会破坏人们已经编写和使用的许多语法.

tl;dr: We considered allowing actions in other rules to execute, but decided not to because it would break a lot of grammars already written and used by people.

这篇关于我的词法分析器令牌操作未调用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-24 14:08