下面的一组antlr语法行给出了number_operation &Number_argument如下所示
以下规则集是相互左递归的
number_funtion : COUNT LEFT_PAREN number_argument RIGHT_PAREN
number_operation :
number_argument (number_operator number_argument)+ | LEFT_PAREN number_argument (number_operator number_argument)+ RIGHT_PAREN
| prefix_operator number_argument | LEFT_PAREN prefix_operator number_argument RIGHT_PAREN;
number_argument : number_column | number_function | digit_constant | number_operation ;
为了避免左递归,用number_argument的每个元素的所有可能组合来修改number_operation可以像下面这样做,但是会导致更长的规则。
number_operation :
number_column (number_operator number_argument)+ | LEFT_PAREN number_column (number_operator number_column)+ RIGHT_PAREN
| prefix_operator number_column | LEFT_PAREN prefix_operator number_column RIGHT_PAREN
//and other combinations
谁能告诉我去掉左边递归的最好方法是什么?
这样如何:
number_operation
: number_operation (number_operator number_argument)+
| number_argument_2 (number_operator number_argument)+
| LEFT_PAREN number_argument (number_operator number_argument)+ RIGHT_PAREN
| prefix_operator number_argument
| LEFT_PAREN prefix_operator number_argument RIGHT_PAREN
;
number_argument
: number_column
| number_function
| digit_constant
| number_operation
;
number_argument_2
: number_column
| number_function
| digit_constant
;
?