讽刺:如何使 KeyTerm 优先于变量?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尝试更改“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.
您可以根据您的目的增加
tag_blk
Terminal
的Priority
或减少variable
之一。Terminal
类有一个Priority
字段,默认为 0。根据上面的注释不幸的是,我目前无法测试它,因为提供的代码片段需要工作和大量工作的假设是可编译的。但从上面的描述来看,这应该就是您要找的。希望它能对某人有所帮助——即使是在问题提出 10 年后。
You can increase the
Priority
of thetag_blk
Terminal
or decrease the one ofvariable
whichever suits your purpose.Terminal
class has aPriority
field defaulting to 0. According to the comment right above itUnfortunately 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.