1

So, I was wondering what makes a parser like:

line        :   expression EOF;
expression  :   m_expression (PLUS m_expression)?;
m_expression:   basic (TIMES basic)?;
basic       :   NUMBER | VARIABLE | (OPENING expression CLOSING) | expression;

left recursive and invalid, while a parser like

line        :   expression EOF;
expression  :   m_expression (PLUS m_expression)?;
m_expression:   basic (TIMES basic)?;
basic       :   NUMBER | VARIABLE | (OPENING expression CLOSING);

is valid and works even though the definition of 'basic' still refers to 'expression'. In particular, I'd like to be able to parse expressions in the form of

a+b+c

without introducing operations acting on more than two operands.

4

1 回答 1

1

line 调用 expression 调用 m_expression 调用 basic 调用表达式...这对于 v3 antlr 和 v4 来说都是间接递归和不好的。左递归的定义意味着您可以在不消耗令牌的情况下返回相同的规则。在第二个实例中,表达式前面有 OPENING 标记。

于 2013-10-08T22:27:57.627 回答