{
  "source_file": "0930",
  "source_platform": "Dev.to",
  "author": "Bala Priya C",
  "title_listed": "Abstract Syntax Tree (AST) - Explained in Plain English",
  "keywords": "",
  "domain": "数据处理",
  "content_type": "深度技术解析",
  "is_paid_flag": "否",
  "url": "https://dev.to/balapriya/abstract-syntax-tree-ast-explained-in-plain-english-1h38",
  "publish_date": "2021-12-06",
  "corpus_stage": "",
  "notes": "",
  "id": "37105ae415ae",
  "collected_at": "2026-10-02T14:01:22",
  "title": "Abstract Syntax Tree (AST) - Explained in Plain English - DEV Community",
  "body_markdown": "As a developer, the source code that you write is all so concise and elegant. And other developers can read and understand your code with ease. However, a compiler has to do much more to make sense of your code.\nIn this post, you’ll learn how compilers try to understand what your code does with a focus on what an\nAbstract Syntax Tree (AST)\nis, and its\nrelevance to static analysis\n.\nHow Does the Compiler Make Sense of Your Code?\nThe steps involved in the compiler’s processing of source code are illustrated below:\nLet’s expand on this a bit.\nThe source code is first split into smaller chunks called tokens through the process of\nlexical analysis\n. Lexical analysis is also called\ntokenization\n.\nThe tokens are then parsed into a tree called the syntax tree by the\nparser\n.\nAn\nAbstract Syntax Tree (AST)\nabstracts away certain details and retains just enough information to help the compiler understand the structure of the code.\nTherefore, an AST is a\ntree\ndata structure that best represents the\nsyntactic\nstructure of the\nsource code\n.\nDon’t worry if things aren’t clear just yet!\nOver the next few minutes, you’ll draw a relatable analogy. And understand how the compiler works very similarly to how you'd try to understand a sentence.\nLexical Analysis\nIn this section, you’ll learn about lexical analysis.\nSuppose you’re learning a new language—not a programming language though😄. And you’re given the task of inferring the meaning of a sentence in that language.\nSo how would you do it?\nAs a first step, you’ll try identifying the nouns, verbs, or more generally, words that matter. Lexical analysis is very similar to this step.\nGiven the source code, the compiler tries to first identify the different types of tokens that your code is composed of.\nA\ntoken\ncould be any valid entity in the programming language—a literal that takes a fixed value, a variable, an operator, or a function call.\nAs lexical analysis breaks down the source code into tokens, it is also called\ntokenization\n.\nSyntactic Analysis\nSo far, you've learned that\ntokenization\nleaves you with\ntokens\nor\nentities\n—just the way you’d identify entities in a sentence.\nLet's go back to the analogy again.\nAfter you’ve identified the nouns and verbs in the sentence, how’ll you go about inferring its meaning?\nWell, you’ll now try to\nparse the relationship\nbetween the nouns, verbs and the like—to see how they fit together—how they conform to the language’s grammar.\nThis leads to the step of\nsyntax analysis\nor\nparsing\n.\nAnd to perform syntactic analysis, there’s a parser that processes these tokens and parses them into a tree called the\nAbstract Syntax Tree (AST)\n.\nMore on AST Representation\nThe different entities and their relationships are often language-specific. For example, the syntactic structure of a sentence in German may be very different from its syntactic structure in Hindi.\nSimilarly, there’s no one common AST representation and the actual AST structure may depend on the specific language.\nIn general, the AST\nmodels\nthe relationship between tokens in the source code as a\ntree\ncomprising of\nnodes\nand the nodes containing\nchildren\n. And each node contains information about the type of token, and related data.\nFor example, if your\nnode\nrepresents a\nfunction call\n, the\narguments\nand the\nreturn values\nwill be the associated data.\nLet's draw the AST corresponding to the following equation:\n2\n+\n(\nz\n−\n1\n)\n2 + (z - 1)\n2\n+\n(\nz\n−\n1\n)\nIn the above AST representation, the nodes\n+\nand\n-\nare operators,\nz\nis a variable, and\n1\nand\n2\nare just literals.\nNotice how the parentheses are discarded in the AST; they're subsumed in the representation of\n(z - 1)\n:\nz\nand\n1\nare both children of the\n-\noperator node.\nWant to explore more on ASTs? The\nAST Explorer\nhelps in visualizing ASTs in several popular languages such as Go, Python, Java, and JavaScript.\nHere's a simple example in Python:\nmy_str\n=\n\"\ncode more!\n\"\nEnter fullscreen mode\nExit fullscreen mode\nHere, the type of node is\nVariableDeclaration\nas we declare the variable\nmy_str\n.\nRelevance of AST in Development\nSo where do AST show up in the development process?\nIn most languages, parsers that give you the AST will also give you the methods to traverse an AST.\nThis would enable you to visit different nodes of the AST to understand the functionality of each node, and additionally perform analysis.\nSo you can:\ndefine rules,\ntraverse the parse tree by visiting each node, and\ncheck for violation of rules.\nAnd this is where ASTs are relevant in static code analysis.\nStatic code analysis involves parsing of the source code into an intermediary representation—on which you can run analysis—without actually running the code. The intermediary representation is often the AST.\nThis analysis then returns potential security issues, bugs, and performance issues in your code, which you can fix almost immediately. For more information on static analysis, consider reading the following post.\nWhat Is Static Code Analysis?\nBala Priya C ・ Dec 6 '21\n#beginners\n#programming\n#codequality\n#codereview\nConclusion\nIn this tutorial, you’ve learned:\nhow lexical and syntactic analyses work,\nlexical analysis: identifies the tokens in the source code, and\nsyntactic analysis: parses the relationship between these tokens to see how they fit together.\nhow ASTs are tree representations of the source code, and\nhow ASTs can help in static analysis\nThank you for reading. Hope you found this post helpful!",
  "metadata": {
    "title": "Abstract Syntax Tree (AST) - Explained in Plain English - DEV Community",
    "description": "As a developer, the source code that you write is all so concise and elegant. And other developers... Tagged with programming, codequality, compilers, codereview.",
    "keywords": "programming, codequality, compilers, codereview, software, coding, development, engineering, inclusive, community",
    "author": null,
    "og:type": "article",
    "og:url": "https://dev.to/balapriya/abstract-syntax-tree-ast-explained-in-plain-english-1h38",
    "og:title": "Abstract Syntax Tree (AST) - Explained in Plain English",
    "og:description": "As a developer, the source code that you write is all so concise and elegant. And other developers...",
    "og:site_name": "DEV Community",
    "og:image": "https://media2.dev.to/dynamic/image/width=1000,height=500,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsgswlh2xoprz3lx55d0k.jpg",
    "twitter:site": "@thepracticaldev",
    "twitter:creator": "@",
    "twitter:title": "Abstract Syntax Tree (AST) - Explained in Plain English",
    "twitter:description": "As a developer, the source code that you write is all so concise and elegant. And other developers...",
    "twitter:card": "summary_large_image",
    "twitter:widgets:new-embed-design": "on",
    "twitter:image:src": "https://media2.dev.to/dynamic/image/width=1000,height=500,fit=cover,gravity=auto,format=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fsgswlh2xoprz3lx55d0k.jpg"
  },
  "paywall": {
    "listed_paid": false,
    "detected": false,
    "marker": "",
    "public_only": false,
    "policy": "付费/会员内容只保留页面公开可见部分，不绕过付费墙"
  },
  "crawl": {
    "success": true,
    "status_code": 200,
    "error": ""
  },
  "code_blocks": [
    {
      "file": "attachments/37105ae415ae/code/body-000.txt",
      "chars": 22,
      "language": "text"
    }
  ],
  "images": [
    {
      "status": "ok",
      "url": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhq40ro9228f1dmyfipty.jpg",
      "bytes": 32992,
      "reused": true,
      "local_path": "attachments/37105ae415ae/images/002.jpg",
      "src": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhq40ro9228f1dmyfipty.jpg",
      "alt": "Steps in the processing of source code",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://i.giphy.com/media/3ofSBivnP3i4u9SCdy/giphy.gif",
      "bytes": 1323667,
      "reused": true,
      "local_path": "attachments/37105ae415ae/images/003.gif",
      "src": "https://i.giphy.com/media/3ofSBivnP3i4u9SCdy/giphy.gif",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://i.giphy.com/media/lYpOXbTyaTF60/giphy.gif",
      "bytes": 387582,
      "reused": true,
      "local_path": "attachments/37105ae415ae/images/004.gif",
      "src": "https://i.giphy.com/media/lYpOXbTyaTF60/giphy.gif",
      "alt": "",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9w7abvsrlaa9sbr3nf35.png",
      "bytes": 65610,
      "reused": true,
      "local_path": "attachments/37105ae415ae/images/005.png",
      "src": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F9w7abvsrlaa9sbr3nf35.png",
      "alt": " ",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fejf0neetgwy9sxk9aumo.jpg",
      "bytes": 56692,
      "reused": true,
      "local_path": "attachments/37105ae415ae/images/006.jpg",
      "src": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fejf0neetgwy9sxk9aumo.jpg",
      "alt": " ",
      "downloaded": true
    },
    {
      "status": "ok",
      "url": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F587727%2F8087a463-9211-45b0-9bc9-964b95e195f7.jpg",
      "bytes": 30954,
      "reused": true,
      "local_path": "attachments/37105ae415ae/images/007.jpg",
      "src": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F587727%2F8087a463-9211-45b0-9bc9-964b95e195f7.jpg",
      "alt": "balapriya",
      "downloaded": true
    }
  ],
  "audio": [],
  "videos": [],
  "links": [
    {
      "href": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fhq40ro9228f1dmyfipty.jpg",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://i.giphy.com/media/3ofSBivnP3i4u9SCdy/giphy.gif",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://i.giphy.com/media/lYpOXbTyaTF60/giphy.gif",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://astexplorer.net/",
      "label": "AST Explorer",
      "type": "a"
    },
    {
      "href": "https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fejf0neetgwy9sxk9aumo.jpg",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://dev.to/balapriya",
      "label": "",
      "type": "a"
    },
    {
      "href": "https://dev.to/balapriya/static-code-analysis-a-beginners-guide-4h91",
      "label": "What Is Static Code Analysis? Bala Priya C ・ Dec 6 '21 #beginners #programming #codequality #codereview",
      "type": "a"
    }
  ],
  "repair_version": 2,
  "repaired_at": "2026-10-14T10:49:08Z",
  "source_url": "https://dev.to/balapriya/abstract-syntax-tree-ast-explained-in-plain-english-1h38",
  "papers": [],
  "actual_video_files": [],
  "body_chars": 5441,
  "expected_body_images": 6,
  "discovery_status": "ok",
  "delivery_status": "ok",
  "missing_reason": "",
  "raw_html": "attachments/37105ae415ae/pages/source.html",
  "local_html": "attachments/37105ae415ae/pages/article.html",
  "body_base_path": "attachments/37105ae415ae/pages/",
  "body_markdown_path": "attachments/37105ae415ae/pages/article.md"
}