如何区分ANTLR 4中的子规则?
我有这样的规则:如何区分ANTLR 4中的子规则?,antlr,antlr4,Antlr,Antlr4,我有这样的规则: type : '[' type ']' | '[' type ':' type ']' | type 'throws'? arrow_operator type | type 'rethrows' arrow_operator type | type_identifier | tuple_type | type '?' | type '!' | protocol_composition_type | type '.' 'Type' | type '.' '
type
: '[' type ']'
| '[' type ':' type ']'
| type 'throws'? arrow_operator type
| type 'rethrows' arrow_operator type
| type_identifier
| tuple_type
| type '?'
| type '!'
| protocol_composition_type
| type '.' 'Type'
| type '.' 'Protocol'
;
我需要在解析器应用程序中区分规则。我知道我可以标记每个选项,但我不想修改语法,因为我使用grammars-v4存储库来保持语法的最新
是否有其他易于使用的选项?或者我应该承认,如果不修改语法,我就无法制作我的应用程序?当然可以,但如果没有其他标签,就更难了。 在visitor或listener方法中,您可以通过以下方式区分它们:
if (context.arrow_operator() != null)
{
if (context.GetChild(1).GetText() == "rethrows")
{
// rule 4
}
else
{
// rule 3
}
}
else if (context.type_identifier() != null)
{
// rule 5
}
else
...
else if (context.type().Length == 2)
{
// rule 2
}
else
{
...
}