13

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.RulePreferShiftHere() + html_tag_kw + attr_args_opt + block;ImplyPrecedenceHere(-100) + html_tag_kw + attr_args_opt + block;没有任何帮助。解析器甚至不会抱怨歧义。

4

2 回答 2

2

尝试更改 'tag_blk.Rule' 和 'variable.Rule' 的顺序,因为标记器通常在第一次匹配之后进行,并且变量在您的列表中排在第一位。

于 2011-04-18T21:08:56.140 回答
1

您可以根据您的目的增加Priority或减少tag_blk Terminal其中一个。类有一个默认为 0 的字段。根据它上面的评论variableTerminalPriority

// 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 年后。

于 2021-08-13T09:55:55.763 回答