Antlr ANTL4上的相互左递归lexer规则?
我想写一篇快速的语言亮点。此外,我还想强调一些语言构造的标记。对以下规则有问题:Antlr ANTL4上的相互左递归lexer规则?,antlr,antlr4,lexer,Antlr,Antlr4,Lexer,我想写一篇快速的语言亮点。此外,我还想强调一些语言构造的标记。对以下规则有问题: Type : '[' Type ']' | '[' Type ':' Type ']' | (Attributes? Function_type_argument_clause 'throws'? '->' Type | Attributes? Function_type_argument_clause 'rethrows' '->' Type) | (Type_name
Type
: '[' Type ']'
| '[' Type ':' Type ']'
| (Attributes? Function_type_argument_clause 'throws'? '->' Type | Attributes? Function_type_argument_clause 'rethrows' '->' Type)
| (Type_name Generic_argument_clause? | Type_name Generic_argument_clause? '.' Type)
| Tuple_type
| Type '?'
| Type '!'
| (Type_name Generic_argument_clause? | Type_name Generic_argument_clause? '.' Type) '&' Protocol_composition_continuation
| (Type '.' 'Type' | Type '.' 'Protocol')
| 'Any'
| 'Self'
| '(' Type ')'
;
错误:以下规则集是相互左递归的[Type]
在规则中尝试离开,仅在以下情况下:
Type
: Type '?'
| 'Any'
| 'Self'
;
但问题仍然存在:以下规则集是相互左递归的[Type]您将
Type
定义为lexer规则。Lexer规则不能是递归的<代码>类型应该是解析器规则
见:
请注意,现有的Swift语法有:
Type
。您必须重写它,这样就不再有左递归了
例如,假设简化的类型
规则如下所示:
Type
: '[' Type ']'
| '[' Type ':' Type ']'
| Type '?'
| Type '!'
| 'Any'
| 'Self'
| '(' Type ')'
;
Type
: TypeStart TypeTrailing?
;
fragment TypeStart
: '[' Type ']'
| '[' Type ':' Type ']'
| 'Any'
| 'Self'
| '(' Type ')'
;
fragment TypeTrailing: [?!];
那么你应该这样重写它:
Type
: '[' Type ']'
| '[' Type ':' Type ']'
| Type '?'
| Type '!'
| 'Any'
| 'Self'
| '(' Type ')'
;
Type
: TypeStart TypeTrailing?
;
fragment TypeStart
: '[' Type ']'
| '[' Type ':' Type ']'
| 'Any'
| 'Self'
| '(' Type ')'
;
fragment TypeTrailing: [?!];
我理解你。如何处理语法突出显示?我仍然无法从词汇分析的角度理解它——找到所需的标记并突出显示。如何使用解析器规则执行此操作?@jeudesprit检查我的编辑