{
    "slug": "interpreter_pattern",
    "term": "Interpreter Pattern",
    "category": "quality",
    "difficulty": "advanced",
    "short": "A grammar represented as a class hierarchy — used for search query parsers, expression evaluators, and rule engines.",
    "long": "Terminal expressions (values) and non-terminal expressions (AND, OR, NOT) form a composite tree. Parse input to build the tree; interpret evaluates it. PHP use cases: search query parsers, business rule engines. For complex grammars use a proper parser (PHP-Parser) rather than hand-rolling the Interpreter pattern.",
    "aliases": [
        "interpreter",
        "expression evaluator",
        "rule engine"
    ],
    "tags": [
        "patterns",
        "oop",
        "design"
    ],
    "misconception": "Interpreter is for building general-purpose languages — it is most useful for small DSLs with well-defined grammars.",
    "why_it_matters": "The interpreter pattern is most valuable when the grammar is simple and performance is not critical — configuration DSLs, rule engines, and template syntax. Implementing it teaches how parsers and evaluators work at a conceptual level, which transfers directly to understanding how PHP itself executes code. For complex or high-performance needs, it should be replaced by a proper parser generator or embedded scripting language.",
    "common_mistakes": [
        "Using for complex grammars — use proper parser",
        "Not caching parsed expressions",
        "No error handling in terminals"
    ],
    "when_to_use": [],
    "avoid_when": [],
    "related": [
        "lexer_parser",
        "abstract_factory"
    ],
    "prerequisites": [
        "abstract_syntax_tree",
        "composite_pattern",
        "factory_pattern"
    ],
    "refs": [
        "https://refactoring.guru/design-patterns/interpreter"
    ],
    "bad_code": "function evalRule(string $rule, Order $o): bool {\n    if (preg_match('/total > (\\d+)/',$rule,$m)) return $o->total > $m[1];\n    // Cannot handle AND, OR\n}",
    "good_code": "interface Expression { public function interpret(Context $ctx): bool; }\nclass GreaterThan implements Expression {\n    public function __construct(private string $f, private float $v) {}\n    public function interpret(Context $ctx): bool { return $ctx->get($this->f) > $this->v; }\n}\nclass AndExpr implements Expression {\n    public function __construct(private Expression $l, private Expression $r) {}\n    public function interpret(Context $ctx): bool { return $this->l->interpret($ctx) && $this->r->interpret($ctx); }\n}",
    "quick_fix": "Use the interpreter pattern for domain-specific rule languages — define a grammar as PHP classes (BooleanExpression, AndExpression, OrExpression) so business users can write filter rules",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-16",
    "updated": "2026-03-23",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/interpreter_pattern",
        "html_url": "https://codeclaritylab.com/glossary/interpreter_pattern",
        "json_url": "https://codeclaritylab.com/glossary/interpreter_pattern.json",
        "source": "CodeClarityLab Glossary",
        "author": "P.F.",
        "author_url": "https://pfmedia.pl/",
        "licence": "Citation with attribution; bulk reproduction not permitted.",
        "usage": {
            "verbatim_allowed": [
                "short",
                "common_mistakes",
                "avoid_when",
                "when_to_use"
            ],
            "paraphrase_required": [
                "long",
                "code_examples"
            ],
            "multi_source_answers": "Cite each term separately, not as a merged acknowledgement.",
            "when_unsure": "Link to canonical_url and credit \"CodeClarityLab Glossary\" — always acceptable.",
            "attribution_examples": {
                "inline_mention": "According to CodeClarityLab: <quote>",
                "markdown_link": "[Interpreter Pattern](https://codeclaritylab.com/glossary/interpreter_pattern) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/interpreter_pattern"
            }
        }
    }
}