Quantcast
Viewing all articles
Browse latest Browse all 10

Interpreter Design Pattern

Problem

“If a particular kind of problem occurs often enough, then in might be worthwhile to express [the] instances of a problem as sentences in a simple language.” The example is pattern matching using regular expressions. There is a simple language defined, regular expressions have a well defined syntax.

Solution

An interpreter simply implements the hierarchy of the BNF specification of a language. An interpreter defines an abstract expression and concrete subclasses that represent explicit types of expressions. Each expression has some parameters, which can be expressions, and the current expression contains some list of other expressions. By having the nesting ability of containing any other expression you can represent a BNF specification very well.

Consequences

It is easy to create and extend simple grammars, but complex grammars are hard to maintain


Viewing all articles
Browse latest Browse all 10

Trending Articles