Interpreter

From CSSEMediaWiki
(Difference between revisions)
Jump to: navigation, search
Line 1: Line 1:
 
The interpreter pattern is specific implimentation of the [[Composite|composite pattern]] generally used for language parsing. In the interpreter pattern a class is produced for each symbol in the language. A statement can then be broken down into a syntax tree of these classes which can in turn be used to interpret the statement.  
 
The interpreter pattern is specific implimentation of the [[Composite|composite pattern]] generally used for language parsing. In the interpreter pattern a class is produced for each symbol in the language. A statement can then be broken down into a syntax tree of these classes which can in turn be used to interpret the statement.  
  
 +
==Use When==
 +
You should use Interpreter when there is a language that you need to interpret and when the statements of the language can be represented as abstract syntax trees. It works best when:
 +
* the grammar of the language is simple. Otherwise, the class hierarchy for the grammar becomes large and complex.
 +
* efficiency is not essential.
  
== See also ==  
+
==Structure==
* [[Composite]]
+
 
 +
[[Image:Interpreter.jpg]]
 +
 
 +
==Participants==
 +
===Abstract Expression===
 +
 
 +
===Terminal Expression===
 +
 
 +
===Nonterminal Expression===
 +
 
 +
===Context===
 +
 
 +
===Client===
 +
 
 +
== Related Patterns ==  
 +
* [[Composite]]: The abstract syntax tree uses a [[Composite]] pattern.
 +
* [[Flyweight]]: This pattern can be used to share the terminal symbols.
 +
* [[Iterator]]: This pattern can be used to traverse the Interpreter structure.
 +
* [[Visitor]]: This pattern can be used to collect the behavior for all abstract syntax tree nodes in one class.
  
 
[[Category:Design Patterns]]
 
[[Category:Design Patterns]]

Revision as of 00:48, 25 July 2009

The interpreter pattern is specific implimentation of the composite pattern generally used for language parsing. In the interpreter pattern a class is produced for each symbol in the language. A statement can then be broken down into a syntax tree of these classes which can in turn be used to interpret the statement.

Contents

Use When

You should use Interpreter when there is a language that you need to interpret and when the statements of the language can be represented as abstract syntax trees. It works best when:

  • the grammar of the language is simple. Otherwise, the class hierarchy for the grammar becomes large and complex.
  • efficiency is not essential.

Structure

Interpreter.jpg

Participants

Abstract Expression

Terminal Expression

Nonterminal Expression

Context

Client

Related Patterns

  • Composite: The abstract syntax tree uses a Composite pattern.
  • Flyweight: This pattern can be used to share the terminal symbols.
  • Iterator: This pattern can be used to traverse the Interpreter structure.
  • Visitor: This pattern can be used to collect the behavior for all abstract syntax tree nodes in one class.


Personal tools