我目前正在开发一种编程语言,大约9个月来,我一直在使用手写的lexer和解析器。我现在要做的是将其转换为ANTLR解析器。编译器解析器系统的结构大致如下所示:
currentParser
并可以执行reparse()
或skip(n tokens)
等操作的帮助程序Parser
的特定子类开始解析(使用parse(ParserManager pm, Token token)
方法)push
一个new Parser
,或者从解析器堆栈中弹出自己,这意味着堆栈中的下一个解析器将解析下一个令牌。ITypeList
这样的接口上,这些接口也作为单个参数传递给解析器。Parser
子类的一个例子是TypeParser
public TypeParser(ITyped typed) { ... }
或TypeListParser
public TypeListParser(ITypeList typeList) { ... }
这个简单的结构适用于每个Parser
实现。在ANTLR中有使用这些接口的方法吗?
发布于 2015-03-24 22:32:16
您的描述看起来像是Parser combinator模式的变体。
若要将这种设计转换为反To:
1. to create the AST as part of the grammar (actions), this is efficient, but makes your grammar heavily dependent on AST-Classes and Implementation language
2. to create the AST in a second pass with a parse tree visitor, this decouples parsing and AST construction but an intermediate parse tree is build, which costs some time.
https://stackoverflow.com/questions/29238671
复制