使用https://github.com/JetBrains/Grammar-Kit如何在没有左递归的情况下重写语法?

grammar ::= exprs
exprs::= (sum_expr (';')?)*
private sum_expr::= sum_expr_infix | sum_expr_prefix
sum_expr_infix ::= number sum_expr_prefix


left sum_expr_prefix::= op_plus number


private op_plus ::= '+'
number ::= float | integer
float ::= digit+ '.' digit*
integer ::= digit+
private digit ::=('0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9')

输入样例:
10+20+30.0;
10+20+30.0

答案应维护节点包含2/3个子节点的解析树属性:

最佳答案

这个问题导致正确的方向:
Parsing boolean expression without left hand recursion

grammar ::= e*
e ::=  math separator?

math ::= add
add ::=
    mul op_plus math
 |  mul op_minus math
 |  mul


mul ::=
    factorial op_mul mul
  | factorial op_div mul
  | factorial

factorial ::= term op_factorial space* | term
op_factorial ::= '!'

term ::= parentheses | space* number space*
parentheses ::= '(' math ')'


op_minus ::= '-'
op_plus ::= '+'
op_div ::= '/'
op_mul ::= '*'
number ::= float | integer
float ::= (digit+'.') digit*
integer ::=digit+
digit ::= '0'|'1'|'2'|'3'|'4'|'5'|'6'|'7'|'8'|'9'
space ::= ' ' | '\t'
separator ::= ';'

测试输入:
1!
3*2+1
3*2+1+3.0!
3*2+1 + 3.0!
1+1+(1+1)!

关于intellij-plugin - 重新编写解析表达式语法(PEG),而无需左递归,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/24688484/

10-11 01:27