递归树顶不起作用
我正在尝试使用 Treetop 创建一个有点递归的解析器。表达式可以是数字,但也可以是表达式的加法,所以我这样写:
grammar Language
rule expression
"(" _ expression _ ")" / addition / integer
end
rule addition
expression _ "+" _ expression
/
expression _ "-" _ expression
end
rule integer
'-'? _ [0-9]+
end
# space
rule _
' '*
end
end
那是行不通的。每当我尝试解析任何内容时,我都会收到异常“SystemStackError:堆栈级别太深”(堆栈溢出!耶!)。有什么想法吗?使用 Treetop 指定此类递归定义的正确方法是什么?
I'm trying to create a parser using Treetop that is somewhat recursive. An expression can be a number but it also can be an addition of expressions, so I wrote this:
grammar Language
rule expression
"(" _ expression _ ")" / addition / integer
end
rule addition
expression _ "+" _ expression
/
expression _ "-" _ expression
end
rule integer
'-'? _ [0-9]+
end
# space
rule _
' '*
end
end
That just doesn't work. Any time I'm trying to parse anything I get an exception "SystemStackError: stack level too deep" (stack overflow! yay!). Any ideas why? what's the correct way to specify such a recursive definition with Treetop?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的语法是左递归的:即
表达式
可以立即成为加法
,而加法
又可以是表达式
等。导致解析器进入无限循环。尝试这样的事情(未经测试!):
You grammar is left-recursive: i.e. a
expression
can immediately be anaddition
which in its turn can be anexpression
etc. causing the parser to go in an infinite loop.Try something like this instead (untested!):