讽刺:如何使 KeyTerm 优先于变量?

发布于 2024-11-01 02:22:16 字数 789 浏览 0 评论 0原文

Irony 语法的相关块:

var VARIABLE = new RegexBasedTerminal("variable", @"(?-i)\$?\w+");

variable.Rule = VARIABLE;
tag_blk.Rule = html_tag_kw + attr_args_opt + block;
term_simple.Rule = NUMBER | STRING | variable | boolean | "null" | term_list;
term.Rule = term_simple | term_filter;
block.Rule = statement_list | statement | ";";
statement.Rule = tag_blk | directive_blk | term;

问题是“标签”和“变量”都可以出现在同一个地方。我希望我的解析器更喜欢标签而不是变量,但它总是更喜欢变量。我怎样才能改变这一点?

我尝试将 tag_blk.Rule 更改为 PreferShiftHere() + html_tag_kw + attr_args_opt + block;ImplyPrecedenceHere(-100) + html_tag_kw + attr_args_opt + block;< /code> 但这没有任何帮助。解析器甚至不会抱怨歧义。

Relevant chunk of Irony grammar:

var VARIABLE = new RegexBasedTerminal("variable", @"(?-i)\$?\w+");

variable.Rule = VARIABLE;
tag_blk.Rule = html_tag_kw + attr_args_opt + block;
term_simple.Rule = NUMBER | STRING | variable | boolean | "null" | term_list;
term.Rule = term_simple | term_filter;
block.Rule = statement_list | statement | ";";
statement.Rule = tag_blk | directive_blk | term;

The problem is that both a "tag" and a "variable" can appear in the same place. I want my parser to prefer the tag over the variable, but it always prefers the variable. How can I change that?

I've tried changing tag_blk.Rule to PreferShiftHere() + html_tag_kw + attr_args_opt + block; and ImplyPrecedenceHere(-100) + html_tag_kw + attr_args_opt + block; but it doesn't help any. The parser doesn't even complain of an ambiguity.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

跨年 2024-11-08 02:22:16

尝试更改“tag_blk.Rule”和“variable.Rule”的顺序,因为标记器通常在第一个匹配之后进行,而变量位于列表中的第一个。

Try changing the order of 'tag_blk.Rule' and 'variable.Rule' as tokenisers usually go after first match, and variable is first in your list.

最佳男配角 2024-11-08 02:22:16

您可以根据您的目的增加 tag_blk TerminalPriority 或减少 variable 之一。 Terminal 类有一个 Priority 字段,默认为 0。根据上面的注释

// Priority is used when more than one terminal may match the input char. 
// It determines the order in which terminals will try to match input for a given char in the input.
// For a given input char the scanner uses the hash table to look up the collection of terminals that may match this input symbol. 
// It is the order in this collection that is determined by Priority property - the higher the priority, 
// the earlier the terminal gets a chance to check the input.

不幸的是,我目前无法测试它,因为提供的代码片段需要工作和大量工作的假设是可编译的。但从上面的描述来看,这应该就是您要找的。希望它能对某人有所帮助——即使是在问题提出 10 年后。

You can increase the Priority of the tag_blk Terminal or decrease the one of variable whichever suits your purpose. Terminal class has a Priority field defaulting to 0. According to the comment right above it

// Priority is used when more than one terminal may match the input char. 
// It determines the order in which terminals will try to match input for a given char in the input.
// For a given input char the scanner uses the hash table to look up the collection of terminals that may match this input symbol. 
// It is the order in this collection that is determined by Priority property - the higher the priority, 
// the earlier the terminal gets a chance to check the input.

Unfortunately I can't test this at the moment as the code fragment provided needs work and lots of assumptions to be made compilable. But from the description above this should be the one you are looking for. Hope it helps someone -even 10 years after the question aired.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文