Interpreter Pattern in C# – Real-Time Example (Rule / Expression Evaluation)

Interpreter Pattern in C# – Real-Time Example (Rule / Expression Evaluation)

What is Interpreter Pattern?

The Interpreter Pattern is a behavioral design pattern used to interpret and evaluate language grammar, expressions, or rules.

👉 Converts expressions into executable logic.

It is commonly used in:

  • Rule engines
  • Formula calculators
  • Query parsers
  • Expression evaluators

Why Use Interpreter Pattern?

  • Evaluate expressions dynamically
  • Build rule engines
  • Parse custom languages
  • Simplify grammar processing
  • Encapsulate evaluation logic

Real-Time Scenario

Calculator System:


10 + 5 - 3

👉 Expression needs interpretation and execution.


Real-Time Example – Simple Expression Interpreter


Step 1: Expression Interface


public interface IExpression
{
    int Interpret();
}

Step 2: Number Expression


public class NumberExpression : IExpression
{
    private readonly int _number;

    public NumberExpression(int number)
    {
        _number = number;
    }

    public int Interpret()
    {
        return _number;
    }
}

Step 3: Add Expression


public class AddExpression : IExpression
{
    private readonly IExpression _left;
    private readonly IExpression _right;

    public AddExpression(
        IExpression left,
        IExpression right)
    {
        _left = left;
        _right = right;
    }

    public int Interpret()
    {
        return _left.Interpret()
             + _right.Interpret();
    }
}

Step 4: Subtract Expression


public class SubtractExpression : IExpression
{
    private readonly IExpression _left;
    private readonly IExpression _right;

    public SubtractExpression(
        IExpression left,
        IExpression right)
    {
        _left = left;
        _right = right;
    }

    public int Interpret()
    {
        return _left.Interpret()
             - _right.Interpret();
    }
}

Usage Example


IExpression ten = new NumberExpression(10);
IExpression five = new NumberExpression(5);
IExpression three = new NumberExpression(3);

// 10 + 5
IExpression add =
    new AddExpression(ten, five);

// (10 + 5) - 3
IExpression expression =
    new SubtractExpression(add, three);

Console.WriteLine(expression.Interpret());

Output


12

Key Concept

Instead of:


eval("10 + 5 - 3");

We build:


Expression Tree

👉 Each rule/expression interpreted separately.


Diagram Understanding


Expression
   ↓
-------------------------
NumberExpression
AddExpression
SubtractExpression

👉 Expressions form tree structure.


Advantages

  • ✔ Easy to add new grammar rules
  • ✔ Flexible expression evaluation
  • ✔ Good for rule engines
  • ✔ Encapsulates parsing logic
  • ✔ Supports recursive structures

Disadvantages

  • ✖ Complexity increases quickly
  • ✖ Many classes/interfaces
  • ✖ Performance issues for complex grammars

When to Use

Use Interpreter Pattern when:

  • Simple language/parser required
  • Rule engine needed
  • Expressions must be evaluated dynamically
  • Grammar structure is simple and repetitive

Real Project Mapping (.NET + Angular)

Feature Usage
Formula calculator Interpreter
Rule engine Interpreter
Search filters Interpreter
Query parser Interpreter
Workflow conditions Interpreter

ASP.NET Core Real Example

LINQ Expression Trees


Expression<Func<User, bool>>

👉 Expressions interpreted dynamically by EF Core.


Advanced Example – Rule Engine


IF Salary > 50000
AND Experience > 5
THEN Eligible

👉 Business rules interpreted dynamically.


Interpreter vs Visitor

Interpreter Visitor
Evaluates expressions Adds operations
Focus on grammar/rules Focus on object operations
Builds expression trees Works on object structures

Pro Tip

Interpreter Pattern works well with:

  • Expression Trees
  • LINQ
  • Rule Engines
  • Workflow systems
  • DSLs (Domain Specific Languages)

Summary

Interpreter Pattern helps you:

  • Evaluate expressions dynamically
  • Build rule engines
  • Interpret custom grammar

👉 Perfect for:

  • Formula calculators
  • Query parsers
  • Business rules
  • Expression evaluation systems

Comments

Popular posts from this blog

Promises in Angular

Debouncing & Throttling in RxJS: Optimizing API Calls and User Interactions

Csharp Coding - Session