用于解析算术表达式的最佳数据结构
假设我们要为解析算术表达式的解析器编写计算机代码,简单来说,就是一个检查算术表达式有效性的程序。例如,给定算术表达式 (a / ( b + c) ) xd,我们希望查看括号是否正确匹配。哪种数据结构最适合用于匹配表达式中的正确括号。
Suppose that we are going to write a computer code for a parser that parses arithmetic expressions, in simple terms, a program that inspects arithmetic expression for validity. For example, given an arithmetic expression (a / ( b + c) ) x d we would like to see if the parenthesis are matching properly. Which data structure would be the best that can be used to match correct parenthesis in the expression.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
大多数解析器的输出要么是显示字符串结构的解析树,要么是显示输入字符串语义的抽象语法树。就您而言,如果您正在寻找“哪个括号与另一个括号匹配”的效果,您可能需要一棵解析树。然后,您可以通过查找哪个解析树节点将其作为子节点来将括号与其灵魂伴侣进行匹配。
The output of most parsers is either a parse tree showing how the string is structured or an abstract syntax tree showing the semantic meaning of the input string. In your case, if you’re looking for something to the effect of “what parenthesis matches this other one,” you’d probably want a parse tree. You could then match the parenthesis with its soul mate by finding which parse tree node has it as a child.