{
    "slug": "abstract_syntax_tree",
    "term": "Abstract Syntax Tree (AST)",
    "category": "compiler",
    "difficulty": "advanced",
    "short": "Tree representation of code structure used by compilers and tools to analyse and transform programs.",
    "long": "An Abstract Syntax Tree (AST) represents source code as a hierarchical tree of syntactic constructs such as expressions, statements, and declarations. The 'abstract' aspect means non-essential syntax details (whitespace, comments, formatting, and often redundant parentheses) are removed, focusing only on semantic structure. ASTs are produced by parsers after tokenisation and are the foundation for compilers, static analysers, linters, and code transformation tools. In PHP, nikic/php-parser generates ASTs that power tools like PHPStan, Psalm, Rector, and PHP CS Fixer. Understanding ASTs enables building custom static analysis rules, automated refactoring (codemods), and code generation tools.",
    "aliases": [
        "AST",
        "syntax tree"
    ],
    "tags": [
        "compiler",
        "static-analysis",
        "php",
        "tooling"
    ],
    "misconception": "ASTs are only for compiler authors. In reality, everyday PHP tools like PHPStan, Rector, and PHP CS Fixer rely on ASTs — understanding them unlocks advanced automation.",
    "why_it_matters": "All modern code analysis and transformation tools operate on ASTs — understanding them allows you to build linters, refactoring tools, and automated code fixes instead of relying on fragile text manipulation.",
    "common_mistakes": [
        "Parsing PHP with regex — fails on real syntax like nested expressions, strings, and comments.",
        "Confusing AST with CST (Concrete Syntax Tree) — AST omits formatting details.",
        "Handling only a subset of node types — missing StaticCall or NullsafeMethodCall leads to incomplete analysis.",
        "Mutating nodes incorrectly during traversal instead of using proper NodeVisitor patterns."
    ],
    "when_to_use": [
        "Building static analysis tools or linters.",
        "Automating refactoring or codemods.",
        "Generating or transforming code programmatically.",
        "Understanding compiler or interpreter internals."
    ],
    "avoid_when": [
        "You only need simple text search without understanding syntax.",
        "Working with non-code structured data like JSON or XML (use native parsers instead)."
    ],
    "related": [
        "lexer_parser",
        "php_compilation_pipeline",
        "static_analysis",
        "transpilation"
    ],
    "prerequisites": [],
    "refs": [
        "https://github.com/nikic/PHP-Parser"
    ],
    "bad_code": "// ❌ Regex-based parsing — unreliable\npreg_match_all('/function\\s+(\\w+)\\s*\\(/', $code, $matches);\n// Breaks on closures, arrow functions, comments, strings, complex syntax",
    "good_code": "// ✅ Proper AST parsing using nikic/php-parser\nuse PhpParser\\ParserFactory;\nuse PhpParser\\Node;\nuse PhpParser\\NodeTraverser;\nuse PhpParser\\NodeVisitorAbstract;\n\n$parser = (new ParserFactory())->createForNewestSupportedVersion();\n$ast = $parser->parse($code);\n\n$traverser = new NodeTraverser();\n\n$traverser->addVisitor(new class extends NodeVisitorAbstract {\n    public function enterNode(Node $node): void {\n        if ($node instanceof Node\\Stmt\\Function_) {\n            echo 'Function: ' . $node->name->toString() . PHP_EOL;\n        }\n    }\n});\n\n$traverser->traverse($ast);",
    "example_note": "AST traversal allows structured inspection of code elements instead of unreliable string matching.",
    "quick_fix": "Never parse PHP with regex — use nikic/php-parser and operate on AST nodes.",
    "severity": "info",
    "effort": "high",
    "created": "2026-03-15",
    "updated": "2026-04-05",
    "citation": {
        "canonical_url": "https://codeclaritylab.com/glossary/abstract_syntax_tree",
        "html_url": "https://codeclaritylab.com/glossary/abstract_syntax_tree",
        "json_url": "https://codeclaritylab.com/glossary/abstract_syntax_tree.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": "[Abstract Syntax Tree (AST)](https://codeclaritylab.com/glossary/abstract_syntax_tree) (CodeClarityLab)",
                "footer_credit": "Source: CodeClarityLab Glossary — https://codeclaritylab.com/glossary/abstract_syntax_tree"
            }
        }
    }
}