Harman Patil (Editor)

Interpreter pattern

Updated on
Edit
Like
Comment
Share on FacebookTweet on TwitterShare on LinkedInShare on Reddit
Interpreter pattern

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

  • Specialized database query languages such as SQL.
  • Specialized computer languages that are often used to describe communication protocols.
  • Most general-purpose computer languages actually incorporate several specialized languages.
  • 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.

    References

    Interpreter pattern Wikipedia


    Similar Topics