首页 > 解决方案 > 删除ANTLR中的左递归?

问题描述

我想知道如何解决这个错误?

以下几组规则是相互左递归的 [type, array_type]

目标是实现这样的目标:

(数组类型)类型->类型[ expr ]

应该包含在类型参数中。任何帮助,将不胜感激

以下代码:

// Types
type
  : atomic_type
  | named_type
  | pointer_type
  | record_type
  | enclosed_type
  | array_type
  ;

atomic_type
  : VOID
  | CHAR
  | INTEGER
  | BOOLEAN
  ;

named_type
  : IDENTIFIER
  ;

// LEFT SIDE RECURSION, SHOULD BE INCLUDED IN TYPE ABOVE
array_type
  : type MLBRACE expr MRBRACE
  ;

pointer_type
  : CARET type
  ;

record_type
  : BLBRACE IDENTIFIER COLON type BLBRACE (COMMA? IDENTIFIER COLON type) BRBRACE BRBRACE
  ;

enclosed_type
  : SLBRACE type SRBRACE 
  ;

编辑:我已经在:

type
  : atomic_type
  | named_type
  | pointer_type
  | record_type
  | enclosed_type
  | type MLBRACE expr MRBRACE
  ;

但我的问题是,是否有可能array_type代替type MLBRACE expr MRBRACE

标签: antlr

解决方案


不,那是不可能的。您不能在 ANTLR4 中使用间接左递归。


推荐阅读