如何将令牌流转换为解析树 [英] How to turn a token stream into a parse tree
问题描述
我构建了一个词法分析器,该词法分析器从输入中输出令牌,但是我不确定如何构建该过程的下一步-解析树.是否有人有很好的资源或如何实现此目的的示例?
I have a lexer built that streams out tokens from in input but I'm not sure how to build the next step in the process - the parse tree. Does anybody have any good resources or examples on how to accomplish this?
推荐答案
我真的会推荐 http://www. antlr.org/,当然还有经典的《 Dragon Compilers》一书.
I would really recommend http://www.antlr.org/ and of course the classic Dragon Compilers book.
对于像JavaScript这样的简单语言,手动滚动递归下降解析器并不难,但是使用yacc或antlr之类的工具几乎总是更容易.
For an easy language like JavaScript it's not hard to hand roll a recursive descent parser, but it's almost always easier to use a tool like yacc or antlr.
我想回到您问题的基础,您真的想学习BNF式语法语法,并为目标选择一种语法.如果有的话,解析树应该会掉出来,成为该语法的实例"体现.
I think to step back to the basics of your question, you really want to study up on BNF-esque grammar syntax and pick a syntax for your target. If you have that, the parse tree should sort of fall out, being the 'instance' manifestation of that grammar.
此外,请勿尝试将解析树的创建转换为最终解决方案(例如生成代码或诸如此类).它似乎可行且更有效.但总是会有一段时间,您真的希望您拥有那棵原样"的解析树.
Also, don't try to turn the creation of your parse tree into your final solution (like generating code, or what-not). It might seem do-able and more effecient; but invariably there will come a time when you'll really wish you had that parse tree 'as is' laying around.
这篇关于如何将令牌流转换为解析树的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!