如何从语法规则中删除左递归
How to remove left-recursion from grammar rules
以下一组 antlr 语法行给出了 number_operation & number_argument 的错误,如下所示
The following sets of rules are mutually left-recursive
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
;
?
以下一组 antlr 语法行给出了 number_operation & number_argument 的错误,如下所示
The following sets of rules are mutually left-recursive
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
;
?