1

我正在学习 GoF 模式,维基百科文章中的解释器模式示例引起了我的注意:http ://en.wikipedia.org/wiki/Interpreter_pattern (我正在专门研究 Java 代码片段)。它看起来像一个简单表达式的抽象语法树!那么解释器模式是以 AST 的形式实现数据处理的吗?

4

1 回答 1

1

Structurally the GoF interpreter pattern ( and almost no other use of interpreter in CS ) is similar to an AST.

The interpreter pattern generally only has one action which evaluates the expression represented by the AST, whereas many AST implementations provide other means to traverse the tree. Frequently in OO AST implementations the traversal combines the visitor pattern and double dispatch. In LISP the AST is traversed using list operators. Normally an AST doesn't do anything, but the GoF interpreter pattern has some 'interpret' action which evaluates the tree.

The GoF pattern sort of combines and AST and an interpreter into the same thing, which is less flexible than the more common AST approaches, but sometimes is all you need.

'Expression Tree' is what it is more generally called, both before and after GoF recorded it with a poorly chosen name - the pattern is a self evaluating tree, there is no interpreter in the GoF interpreter pattern.

于 2014-10-24T23:12:31.450 回答