1

I am learning GoF patterns and an example of interpreter pattern from Wikipedia article caught my attention: http://en.wikipedia.org/wiki/Interpreter_pattern (I am specifically looking into Java code snippet). It looks like an Abstract Syntax Tree for a simple expression! So is Interpreter pattern about implementing data processing in form of ASTs?

Dave Schweisguth
  • 36,475
  • 10
  • 98
  • 121
ig12115
  • 43
  • 5
  • Looks like it to me. One more time: "let's reinvent new vocabulary to describe well-known ideas". Building interpreters has been around since the early 1950s. – Ira Baxter Oct 24 '14 at 21:28
  • @IraBaxter the entire purpose of the GoF book was to catalogue well-known ideas and give them new names – Pete Kirkham Oct 24 '14 at 23:21

1 Answers1

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.

Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • 1
    Great. Take well-known ideas, narrow them down to be less effective than the original, and then give the result a different name than the original. This is progress? – Ira Baxter Oct 25 '14 at 00:38
  • So basically the idea is to define a simple domain specific language? Any good example of the proper application of Interpreter Pattern? – ig12115 Oct 28 '14 at 22:23
  • I've used expression trees several times for evaluating expressions where a full-blown interpreter isn't required. They require a parser/fluent interface to construct them to become an external/internal DSL. I don't like calling them 'the interpreter pattern'. – Pete Kirkham Oct 29 '14 at 09:37