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

问题描述

我使用 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