![]() | ||
In computer programming, the interpreter pattern is a design pattern that specifies how to evaluate sentences in a language. The basic idea is to have a class for each symbol (terminal or nonterminal) in a specialized computer language. The syntax tree of a sentence in the language is an instance of the composite pattern and is used to evaluate (interpret) the sentence for a client. See also Composite pattern.
Contents
Uses
BNF
The following Backus–Naur form example illustrates the interpreter pattern. The grammar
defines a language that contains Reverse Polish Notation expressions like:
a b +a b c + -a b + c a - -C#
This structural code demonstrates the Interpreter patterns, which using a defined grammar, provides the interpreter that processes parsed statements.
Java
Following the interpreter pattern there is a class for each grammar rule.
While the interpreter pattern does not address parsing a parser is provided for completeness.
Finally evaluating the expression "w x z - +" with w = 5, x = 10, and z = 42.