Interpreter Pattern in C#

The Interpreter Pattern defines a way to evaluate language grammar or expressions by building a tree of objects that interpret the rules. Each object represents a rule, symbol, or operation, and the whole system evaluates input by walking through these objects.

Real-Life Analogy: Language Translator

Imagine someone translating simple phrases like “hello” or “thank you” from English to Spanish. The translator knows the rules — how to break down and translate individual words or expressions.

If the phrase fits a pattern, it is interpreted correctly. If not, it is skipped or marked invalid. This is what the Interpreter Pattern does in code — it converts structured input into meaningful output based on rules.

Translator mapping words from one language to another
Like a translator interpreting each word in a sentence, the Interpreter Pattern processes input using a set of language rules.

Benefits of Interpreter Pattern

This pattern makes it easier to process expressions or languages that follow a known set of grammar rules. It allows you to add new rules or symbols without rewriting the parser.

It works well when your input is structured and rule-based — such as formulas, filters, or custom scripting.

  • Encapsulates grammar rules in easy-to-update classes
  • Makes expression trees easy to evaluate or extend
  • Good for domain-specific languages (DSLs)

When Should You Use It?

Use the Interpreter Pattern when your app needs to process structured input, like formulas, search filters, or commands. It's a strong fit when grammar is simple and rarely changes.

Consider it for:

  • Text parsing or simple programming languages
  • Building rule engines or interpreters
  • Filtering logic like search queries or alerts

When Not to Use It: Avoid it if the grammar is complex or changes frequently — it can become hard to maintain.

What to Implement

To implement this pattern, you need:

An expression interface, classes that represent grammar rules or symbols, and a method that interprets context.

  • Expression Interface: Declares the Interpret method
  • Terminal Expression: Represents basic grammar symbols
  • Non-Terminal Expression: Combines symbols using logic
  • Context: Stores the data to interpret

How It Works in C#


// Context
public class Context
{
    public string Input { get; set; }
    public string Output { get; set; }
}

// Expression Interface
public interface IExpression
{
    void Interpret(Context context);
}

// Terminal Expression
public class HelloExpression : IExpression
{
    public void Interpret(Context context)
    {
        if (context.Input == "hello")
            context.Output = "hola";
    }
}

public class ThanksExpression : IExpression
{
    public void Interpret(Context context)
    {
        if (context.Input == "thank you")
            context.Output = "gracias";
    }
}

Usage:


var context = new Context { Input = "hello" };

List<IExpression> expressions = new()
{
    new HelloExpression(),
    new ThanksExpression()
};

foreach (var expr in expressions)
    expr.Interpret(context);

Console.WriteLine(context.Output); // hola

Final Thoughts

The Interpreter Pattern helps you break down and process structured input like a mini language. It keeps your grammar rules clean and lets you add new phrases or symbols as needed.

If you're building something that needs rule-based logic, command parsing, or filtering — this pattern can simplify the entire process.