gpt4 book ai didi

intellij-plugin - 在没有左递归的情况下重写解析表达式语法 (PEG)

转载 作者:行者123 更新时间:2023-12-02 03:34:19 28 4
gpt4 key购买 nike

使用 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 子节点的解析树属性: enter image description here

最佳答案

这个问题引导正确的方向: 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/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com