<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet href="/static/feed.xsl" type="text/xsl" ?>
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:quartz="https://quartz.jzhao.xyz/ns">
  <script xmlns="http://www.w3.org/1999/xhtml" src="/static/scripts/xslt-polyfill-214fd525.js"></script>
  <title>/wiki / compilerdesign</title>
  <subtitle>Recent notes in /wiki / compilerdesign on Animesh Mishra</subtitle>
  <link href="https://animishraa05.github.io/wiki/compilerdesign" />
  <link rel="alternate" type="text/html" href="https://animishraa05.github.io/wiki/compilerdesign" />
  <category term="wiki/compilerdesign" />
  <id>https://animishraa05.github.io/wiki/compilerdesign</id>
  <updated>2026-09-02T00:00:00.000Z</updated>
  <contributor>
    <name>Animesh Mishra</name>
    <email>animesh.mishra818@gmail.com</email>
  </contributor>
  <logo>https://animishraa05.github.io/icon.png</logo>
  <icon>https://animishraa05.github.io/icon.png</icon>
  <generator>Quartz v4.6.0 -- quartz.jzhao.xyz</generator>
  <rights type="html">&amp;amp;copy; 2026 Animesh Mishra</rights>
  
  <entry>
    <title>Compilerdesign -- Map of Content</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/.md" />
    <summary> Map of Content for Compilerdesign — 55 concepts. Start here to navigate compilerdesign.</summary>
    <published>2026-09-02T00:00:00.000Z</published>
    <updated>2026-09-02T00:00:00.000Z</updated>
    <publishedTime>Sep 02, 2026</publishedTime>
    <updatedTime>Sep 02, 2026</updatedTime>
    <category term="meta" label="meta" />
<category term="compilerdesign" label="compilerdesign" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;blockquote&gt;
&lt;p dir=&quot;auto&quot;&gt;Map of Content for &lt;strong&gt;Compilerdesign&lt;/strong&gt; — 55 concepts. Start here to navigate compilerdesign.&lt;/p&gt;
&lt;/blockquote&gt;
&lt;h2 id=&quot;core-concepts&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Concepts&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-concepts&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;a href=&quot;../../ambiguous-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;ambiguous-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Ambiguous Grammar&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../application-of-sdts&quot; class=&quot;internal alias&quot; data-slug=&quot;application-of-sdts&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Application of Syntax-Directed Translations&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../attributed-sdt&quot; class=&quot;internal alias&quot; data-slug=&quot;attributed-sdt&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;S-Attributed and L-Attributed SDTs&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../basic-blocks&quot; class=&quot;internal alias&quot; data-slug=&quot;basic-blocks&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Basic Blocks&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../bottom-up-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;bottom-up-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Bottom-Up Parsing&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../classification-of-context-free-grammars&quot; class=&quot;internal alias&quot; data-slug=&quot;classification-of-context-free-grammars&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Classification of Context-Free Grammars&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Generation&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../code-generator-design-issues&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generator-design-issues&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Issues in the Design of a Code Generator&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../common-subexpression-elimination&quot; class=&quot;internal alias&quot; data-slug=&quot;common-subexpression-elimination&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Common Subexpression Elimination&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../compiler-construction-tools&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler-construction-tools&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler Construction Tools&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../compiler-pass&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler-pass&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler Pass&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../compiler-vs-interpreter&quot; class=&quot;internal&quot; data-slug=&quot;compiler-vs-interpreter&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;compiler-vs-interpreter&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../constant-propagation&quot; class=&quot;internal alias&quot; data-slug=&quot;constant-propagation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Constant Propagation&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../context-free-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;context-free-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Context-Free Grammar&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../control-flow-graph&quot; class=&quot;internal alias&quot; data-slug=&quot;control-flow-graph&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Control Flow Graph&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../data-flow-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;data-flow-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Data Flow Analysis&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../error-handling-in-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;error-handling-in-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Error Handling in Compiler Design&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../first-and-follow-sets&quot; class=&quot;internal alias&quot; data-slug=&quot;first-and-follow-sets&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;FIRST and FOLLOW Sets&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../flex-lexical-analyzer-generator&quot; class=&quot;internal alias&quot; data-slug=&quot;flex-lexical-analyzer-generator&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Flex -- Fast Lexical Analyzer Generator&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../intermediate-code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;intermediate-code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Intermediate Code Generation&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../lexical-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;lexical-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Lexical Analysis&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../linker-and-loader&quot; class=&quot;internal alias&quot; data-slug=&quot;linker-and-loader&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Linker and Loader&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../liveliness-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;liveliness-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Liveliness Analysis&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../ll1-parsing-algorithm&quot; class=&quot;internal alias&quot; data-slug=&quot;ll1-parsing-algorithm&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LL(1) Parsing Algorithm&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../ll1-parsing-table&quot; class=&quot;internal alias&quot; data-slug=&quot;ll1-parsing-table&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LL(1) Parsing Table&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../loop-detection-in-tac&quot; class=&quot;internal alias&quot; data-slug=&quot;loop-detection-in-tac&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Detection of a Loop in Three-Address Code&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../lr0-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;lr0-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LR(0) Parser&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../lr-parsers&quot; class=&quot;internal alias&quot; data-slug=&quot;lr-parsers&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LR Parsers&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../object-code&quot; class=&quot;internal alias&quot; data-slug=&quot;object-code&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Object Code&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../operator-precedence-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;operator-precedence-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Operator Precedence Parser&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../optimization-techniques-compared&quot; class=&quot;internal&quot; data-slug=&quot;optimization-techniques-compared&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;optimization-techniques-compared&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../parser-introduction&quot; class=&quot;internal alias&quot; data-slug=&quot;parser-introduction&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Parser Introduction&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../parsing-techniques-compared&quot; class=&quot;internal&quot; data-slug=&quot;parsing-techniques-compared&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;parsing-techniques-compared&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../peephole-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;peephole-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Peephole Optimization&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../predictive-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;predictive-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Predictive Parser&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../programming-language-generations&quot; class=&quot;internal alias&quot; data-slug=&quot;programming-language-generations&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Programming Language Generations&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../recursive-descent-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;recursive-descent-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Recursive Descent Parser&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../runtime-environment&quot; class=&quot;internal alias&quot; data-slug=&quot;runtime-environment&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Runtime Environment&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../sdt-schemes&quot; class=&quot;internal alias&quot; data-slug=&quot;sdt-schemes&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax-Directed Translation Schemes&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../semantic-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;semantic-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Semantic Analysis&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../shift-reduce-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;shift-reduce-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Shift-Reduce Parser&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../single-pass-vs-multi-pass&quot; class=&quot;internal&quot; data-slug=&quot;single-pass-vs-multi-pass&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;single-pass-vs-multi-pass&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../static-and-dynamic-scoping&quot; class=&quot;internal alias&quot; data-slug=&quot;static-and-dynamic-scoping&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Static and Dynamic Scoping&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../storage-allocation-strategies&quot; class=&quot;internal alias&quot; data-slug=&quot;storage-allocation-strategies&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Storage Allocation Strategies&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../symbol-table-in-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;symbol-table-in-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Symbol Table in Compiler&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../syntax-directed-translation&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-directed-translation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax-Directed Translation&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../three-address-code&quot; class=&quot;internal alias&quot; data-slug=&quot;three-address-code&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Three-Address Code&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../token&quot; class=&quot;internal alias&quot; data-slug=&quot;token&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Token&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../top-down-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;top-down-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Top-Down Parsing&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../unambiguous-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;unambiguous-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Unambiguous Grammar&lt;/a&gt; — concept&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../working-of-lexical-analyzer&quot; class=&quot;internal alias&quot; data-slug=&quot;working-of-lexical-analyzer&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Working of Lexical Analyzer&lt;/a&gt; — concept&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;mechanisms--how-things-work&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Mechanisms &amp;#x26; How Things Work&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#mechanisms--how-things-work&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;&lt;em&gt;Pages that explain processes/flows.&lt;/em&gt;&lt;/p&gt;
&lt;h2 id=&quot;comparisons--tradeoffs&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Comparisons &amp;#x26; Tradeoffs&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#comparisons--tradeoffs&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;em&gt;(No synthesis yet — create via ingest or query —save)&lt;/em&gt;&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;suggested-reading-order&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Suggested Reading Order&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#suggested-reading-order&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ol dir=&quot;auto&quot;&gt;
&lt;li&gt;Browse Core Concepts above — start with most-linked page&lt;/li&gt;
&lt;li&gt;Follow Connections sections for dense graph traversal&lt;/li&gt;
&lt;/ol&gt;</content>
  </entry><entry>
    <title>ambiguous-grammar</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/ambiguous-grammar" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/ambiguous-grammar.md" />
    <summary>The Problem A grammar that allows more than one parse tree for the same input string means the compiler cannot determine the unique syntactic structure of the program.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A grammar that allows more than one parse tree for the same input string means the compiler cannot determine the unique syntactic structure of the program. This leads to unpredictable behavior — different parsers may interpret the same program differently.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A grammar is &lt;strong&gt;ambiguous&lt;/strong&gt; if there exists a string in its language that has more than one leftmost derivation (equivalently, more than one parse tree). Ambiguity is a property of the grammar, not necessarily of the language — many ambiguous grammars can be rewritten as unambiguous ones.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Ambiguity arises when a non-terminal can be expanded in multiple ways that lead to the same input. Classic examples: &lt;strong&gt;dangling else&lt;/strong&gt; (&lt;code class=&quot;notranslate&quot;&gt;if x if y ... else ...&lt;/code&gt; — which if does else belong to?), and &lt;strong&gt;expression associativity&lt;/strong&gt; (&lt;code class=&quot;notranslate&quot;&gt;E → E + E | id&lt;/code&gt; allows two trees for &lt;code class=&quot;notranslate&quot;&gt;a + b + c&lt;/code&gt;). The parser resolves ambiguity through precedence rules, associativity declarations, or grammar rewriting.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: ambiguous_grammar Pages: 1 --&gt;
&lt;svg width=&quot;196pt&quot; height=&quot;259pt&quot; viewBox=&quot;0.00 0.00 196.00 259.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 255.4)&quot;&gt;
&lt;title&gt;ambiguous_grammar&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-255.4 192.36,-255.4 192.36,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Input --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Input&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;140.54,-251.4 46.82,-251.4 46.82,-215.4 140.54,-215.4 140.54,-251.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;93.68&quot; y=&quot;-229.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Input: a + b * c&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Grammar --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Grammar&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;185.66,-178.4 1.71,-178.4 1.71,-142.4 185.66,-142.4 185.66,-178.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;93.68&quot; y=&quot;-156.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Grammar: E → E + E | E * E | id&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Input&amp;#45;&amp;gt;Grammar --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Input-&gt;Grammar&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M93.68,-215.21C93.68,-207.63 93.68,-198.5 93.68,-189.94&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;97.18,-189.94 93.68,-179.94 90.18,-189.94 97.18,-189.94&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Tree1 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Tree1&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;85.36,-94.4 0,-94.4 0,0 85.36,0 85.36,-94.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;42.68&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parse Tree 1&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;42.68&quot; y=&quot;-65.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;    +&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;42.68&quot; y=&quot;-50.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;   / \&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;42.68&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  a   *&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;42.68&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;     / \&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;42.68&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;    b   c&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;Tree1 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;Tree1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M85.71,-142.03C81.07,-131.9 74.92,-118.5 68.73,-105&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;72,-103.72 64.65,-96.09 65.63,-106.64 72,-103.72&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;99&quot; y=&quot;-115.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;a + (b * c)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Tree2 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Tree2&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;188.36,-94.4 103,-94.4 103,0 188.36,0 188.36,-94.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;145.68&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parse Tree 2&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;145.68&quot; y=&quot;-65.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;    *&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;145.68&quot; y=&quot;-50.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;   / \&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;145.68&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  +   c&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;145.68&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt; / \&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;145.68&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;a   b&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;Tree2 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;Tree2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M111.16,-141.97C115.75,-136.67 120.34,-130.6 123.68,-124.4 126.94,-118.36 129.77,-111.78 132.2,-105.1&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;135.39,-106.61 135.24,-96.02 128.75,-104.39 135.39,-106.61&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;151.04&quot; y=&quot;-115.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;(a + b) * c&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Definition:&lt;/strong&gt; More than one leftmost derivation for some string&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Not always a language property:&lt;/strong&gt; The same language can have both ambiguous and unambiguous grammars&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dangling else:&lt;/strong&gt; Classic ambiguity in languages with nested if-then-else&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Expression ambiguity:&lt;/strong&gt; Operator precedence and associativity are typically ambiguous in naive expression grammars&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parser resolution:&lt;/strong&gt; Yacc and Bison allow precedence and associativity declarations to resolve ambiguities&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Ambiguous_Grammar Pages: 1 --&gt;
&lt;svg width=&quot;205pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 205.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Ambiguous_Grammar&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 200.88,-165.63 200.88,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;92.39&quot; cy=&quot;-86.21&quot; rx=&quot;92.39&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;92.39&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Ambiguous Grammar&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;136.29&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;136.29&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M106.28,-104.37C111.5,-111.21 117.44,-118.97 122.64,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;95.42&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;68.43&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;68.43&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M85.97,-67.93C82.48,-57.98 78.18,-45.73 74.71,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;110.09&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../context-free-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;context-free-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Context-Free Grammar&lt;/a&gt; — ambiguity is a property of CFGs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../unambiguous-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;unambiguous-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Unambiguous Grammar&lt;/a&gt; — a grammar producing exactly one parse tree per string&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../top-down-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;top-down-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Top-Down Parsing&lt;/a&gt; — some ambiguous grammars cause non-determinism in LL parsers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../operator-precedence-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;operator-precedence-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Operator Precedence Parser&lt;/a&gt; — specifically designed to handle ambiguous expression grammars using precedence rules&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — detecting and resolving ambiguity is a core concern in parsing&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Inherent ambiguity:&lt;/strong&gt; Some languages are inherently ambiguous — every grammar for them is ambiguous&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ambiguity detection is undecidable:&lt;/strong&gt; There is no algorithm that can determine whether an arbitrary CFG is ambiguous&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Yacc/Bison resolution:&lt;/strong&gt; By default, yacc resolves shift-reduce conflicts in favor of shift — this may not always be what the user wants&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Disambiguating rules:&lt;/strong&gt; Associativity declarations (&lt;code class=&quot;notranslate&quot;&gt;%left&lt;/code&gt;, &lt;code class=&quot;notranslate&quot;&gt;%right&lt;/code&gt;) and precedence levels resolve ambiguity without grammar rewriting&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>application-of-sdts</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/application-of-sdts" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/application-of-sdts.md" />
    <summary>The Problem SDTs are a theoretical framework, but compilers need concrete translations: convert infix expressions to postfix, type-check assignments, emit three-address code, and set up symbol table entries.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;SDTs are a theoretical framework, but compilers need concrete translations: convert infix expressions to postfix, type-check assignments, emit three-address code, and set up symbol table entries. Each of these requires different semantic actions at different points in the grammar. Understanding SDT applications shows how the theory produces real compiler output.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Syntax-directed translations have several practical applications in compilers: &lt;strong&gt;infix-to-postfix conversion&lt;/strong&gt; (for expression evaluation), &lt;strong&gt;type checking&lt;/strong&gt; (verifying operand types against operator requirements), &lt;strong&gt;intermediate code generation&lt;/strong&gt; (emitting three-address code during parsing), &lt;strong&gt;symbol table management&lt;/strong&gt; (entering declarations, looking up identifiers), and &lt;strong&gt;grammar-based translation&lt;/strong&gt; (converting one language to another, like AST transformations).&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;For infix-to-postfix: each production &lt;code class=&quot;notranslate&quot;&gt;E → E + T&lt;/code&gt; has action &lt;code class=&quot;notranslate&quot;&gt;{ print(&#039;+&#039;); }&lt;/code&gt; placed at the point where the operator is recognized. For type checking: &lt;code class=&quot;notranslate&quot;&gt;E → E₁ + T&lt;/code&gt; has action &lt;code class=&quot;notranslate&quot;&gt;{ if (E₁.type != int || T.type != int) error(); E.type = int; }&lt;/code&gt;. For code generation: &lt;code class=&quot;notranslate&quot;&gt;E → id := E₁&lt;/code&gt; emits &lt;code class=&quot;notranslate&quot;&gt;gen(id.place, &#039;=&#039;, E₁.place)&lt;/code&gt;. Each application follows the same pattern: the action is attached to the production and executes when the parser reduces that production.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: sdt_apps Pages: 1 --&gt;
&lt;svg width=&quot;587pt&quot; height=&quot;195pt&quot; viewBox=&quot;0.00 0.00 587.00 195.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 191.2)&quot;&gt;
&lt;title&gt;sdt_apps&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-191.2 583,-191.2 583,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;g id=&quot;clust1&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_uses&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;8,-64.8 8,-142.4 571,-142.4 571,-64.8 8,-64.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;289.5&quot; y=&quot;-125.8&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;SDT Applications&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Postfix --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Postfix&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;106.34,-109.6 15.66,-109.6 15.66,-72.8 106.34,-72.8 106.34,-109.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61&quot; y=&quot;-94.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Infix → Postfix&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61&quot; y=&quot;-80.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;3 + 4 → 3 4 +&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Compiler --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Compiler&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;220.68,-36.8 151.32,-36.8 151.32,0 220.68,0 220.68,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;186&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Compiler&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;186&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Front-End&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Postfix&amp;#45;&amp;gt;Compiler --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Postfix-&gt;Compiler&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M92.54,-72.33C108.38,-63.36 127.77,-52.38 144.78,-42.75&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;146.17,-45.98 153.15,-38.01 142.72,-39.89 146.17,-45.98&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Types --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Types&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;248.03,-109.6 123.97,-109.6 123.97,-72.8 248.03,-72.8 248.03,-109.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;186&quot; y=&quot;-94.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Type Checking&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;186&quot; y=&quot;-80.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;verify operand types&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Types&amp;#45;&amp;gt;Compiler --&gt;
&lt;g id=&quot;edge7&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Types-&gt;Compiler&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M186,-72.33C186,-65.07 186,-56.48 186,-48.37&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;189.5,-48.53 186,-38.53 182.5,-48.53 189.5,-48.53&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- IR --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;IR&lt;/title&gt;
&lt;polygon fill=&quot;#e8d4ff&quot; stroke=&quot;black&quot; points=&quot;362.35,-109.6 265.65,-109.6 265.65,-72.8 362.35,-72.8 362.35,-109.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;314&quot; y=&quot;-94.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Three-Address&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;314&quot; y=&quot;-80.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Code Emission&lt;/text&gt;
&lt;/g&gt;
&lt;!-- IR&amp;#45;&amp;gt;Compiler --&gt;
&lt;g id=&quot;edge8&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;IR-&gt;Compiler&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M281.7,-72.33C265.48,-63.36 245.62,-52.38 228.21,-42.75&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;230.06,-39.77 219.61,-37.99 226.67,-45.9 230.06,-39.77&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Symbol --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Symbol&lt;/title&gt;
&lt;polygon fill=&quot;#fff3cd&quot; stroke=&quot;black&quot; points=&quot;469.68,-109.6 380.32,-109.6 380.32,-72.8 469.68,-72.8 469.68,-109.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;425&quot; y=&quot;-94.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Symbol Table&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;425&quot; y=&quot;-80.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Management&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Trans --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Trans&lt;/title&gt;
&lt;polygon fill=&quot;#ffd7be&quot; stroke=&quot;black&quot; points=&quot;562.68,-109.6 487.32,-109.6 487.32,-72.8 562.68,-72.8 562.68,-109.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;525&quot; y=&quot;-94.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Language&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;525&quot; y=&quot;-80.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Translation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Grammar --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Grammar&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;372.36,-187.2 255.64,-187.2 255.64,-150.4 372.36,-150.4 372.36,-187.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;314&quot; y=&quot;-172.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Grammar with&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;314&quot; y=&quot;-158&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Embedded Actions&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;Postfix --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;Postfix&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M255.48,-164.3C205.93,-160.61 139.21,-153.74 115,-142.4 102.93,-136.75 91.74,-127.39 82.72,-118.27&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;85.49,-116.11 76.11,-111.19 80.38,-120.89 85.49,-116.11&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;Types --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;Types&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M271.52,-149.99C266.55,-147.58 261.61,-145.03 257,-142.4 243.64,-134.8 229.68,-125.31 217.71,-116.63&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;219.93,-113.92 209.8,-110.79 215.77,-119.55 219.93,-113.92&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;IR --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;IR&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M314,-150.27C314,-141.64 314,-131.03 314,-121.24&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;317.5,-121.41 314,-111.41 310.5,-121.41 317.5,-121.41&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;Symbol --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;Symbol&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M358.05,-149.96C362.53,-147.62 366.93,-145.09 371,-142.4 381.52,-135.45 391.94,-126.41 400.78,-117.92&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;403.01,-120.64 407.65,-111.11 398.08,-115.67 403.01,-120.64&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;Trans --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;Trans&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M372.64,-162.9C412.41,-158.81 460.92,-152.07 479,-142.4 489.52,-136.78 498.9,-127.78 506.4,-118.95&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;509.11,-121.16 512.56,-111.14 503.62,-116.82 509.11,-121.16&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_sdt_apps Pages: 1 --&gt;
&lt;svg width=&quot;352pt&quot; height=&quot;206pt&quot; viewBox=&quot;0.00 0.00 352.00 206.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 202.21)&quot;&gt;
&lt;title&gt;semantic_sdt_apps&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-202.21 347.6,-202.21 347.6,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;154.82&quot; cy=&quot;-92.7&quot; rx=&quot;56.28&quot; ry=&quot;27.72&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;154.82&quot; y=&quot;-96.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Application&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;154.82&quot; y=&quot;-81&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;of SDTs&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE1&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;206.68&quot; cy=&quot;-30.16&quot; rx=&quot;53.77&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;206.68&quot; y=&quot;-26.86&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;SDT Schemes&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M176.21,-66.9C181.57,-60.44 187.2,-53.66 192.13,-47.7&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;202.68&quot; y=&quot;-49.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE2&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;111.06&quot; cy=&quot;-24.32&quot; rx=&quot;66.65&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;111.06&quot; y=&quot;-27.62&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Syntax-Directed&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;111.06&quot; y=&quot;-14.42&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Translation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M137.78,-66.07C134.03,-60.22 130.09,-54.06 126.42,-48.32&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;113.59&quot; y=&quot;-59.9&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT1 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT1&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;76.48&quot; cy=&quot;-114.05&quot; rx=&quot;70.55&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;76.48&quot; y=&quot;-117.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Intermediate&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;76.48&quot; y=&quot;-104.15&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Code Generation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT1 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M105.21,-106.22C105.09,-106.25 104.96,-106.29 104.84,-106.32&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;52.51&quot; y=&quot;-108.97&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;builds into -- SDTs emit IR&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT2 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;230.61&quot; cy=&quot;-122.16&quot; rx=&quot;43.74&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;230.61&quot; y=&quot;-125.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Semantic&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;230.61&quot; y=&quot;-112.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT2 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M199.35,-110.01C199.47,-110.06 199.6,-110.11 199.73,-110.16&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;271.57&quot; y=&quot;-112.78&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;builds into -- SDTs do type checking&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;150.99&quot; cy=&quot;-173.89&quot; rx=&quot;62.76&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;150.99&quot; y=&quot;-177.19&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Three-Address&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;150.99&quot; y=&quot;-163.99&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Code&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M153.5,-120.64C153.06,-129.91 152.58,-140.15 152.16,-149.16&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.56&quot; y=&quot;-137.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related -- TAC is common SDT output&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Infix to postfix:&lt;/strong&gt; Actions print operators when their operands are fully parsed — most common educational example&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Type checking:&lt;/strong&gt; Actions verify type compatibility and propagate type information through the parse tree&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Code emission:&lt;/strong&gt; SDTs emit three-address code instructions as actions during parsing&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Symbol table:&lt;/strong&gt; Actions enter declarations and look up identifiers during parsing&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Translation flexibility:&lt;/strong&gt; The same grammar can produce different outputs (code, types, errors) by changing the actions&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../sdt-schemes&quot; class=&quot;internal alias&quot; data-slug=&quot;sdt-schemes&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;SDT Schemes&lt;/a&gt; — translation schemes define how actions are embedded&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../syntax-directed-translation&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-directed-translation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax-Directed Translation&lt;/a&gt; — the theoretical foundation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../intermediate-code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;intermediate-code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Intermediate Code Generation&lt;/a&gt; — SDTs are the mechanism for emitting IR&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../three-address-code&quot; class=&quot;internal alias&quot; data-slug=&quot;three-address-code&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Three-Address Code&lt;/a&gt; — common target of SDT-based code generation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../semantic-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;semantic-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Semantic Analysis&lt;/a&gt; — type checking SDTs are part of semantic analysis&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Order dependency:&lt;/strong&gt; Action order matters — printing an operator before its operands gives prefix instead of postfix&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Side effects:&lt;/strong&gt; Actions with side effects (like entering symbol table entries) must execute exactly once per construct&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error recovery:&lt;/strong&gt; When the parser recovers from an error, previously executed actions may have created incomplete symbol table entries&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SDT vs separate pass:&lt;/strong&gt; The “action during parsing” model works for simple translation; complex optimizations need separate passes&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>attributed-sdt</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/attributed-sdt" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/attributed-sdt.md" />
    <summary>The Problem Semantic actions in syntax-directed translations need to pass information around the parse tree.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Semantic actions in syntax-directed translations need to pass information around the parse tree. Some information flows upward (a subexpression’s computed type), and some flows downward (the expected type context). Without a formal model of attribute flow, the evaluation order becomes unpredictable.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;&lt;strong&gt;S-attributed SDTs&lt;/strong&gt; use only &lt;strong&gt;synthesized attributes&lt;/strong&gt; — values computed from children and passed upward to the parent. &lt;strong&gt;L-attributed SDTs&lt;/strong&gt; allow both &lt;strong&gt;synthesized&lt;/strong&gt; and &lt;strong&gt;inherited attributes&lt;/strong&gt;, where inherited attributes flow from left-to-right and top-to-bottom. S-attributed SDTs can be evaluated bottom-up; L-attributed SDTs require top-down or left-to-right evaluation.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;In an S-attributed SDT, every action computes a value for the left-hand side non-terminal using only values from right-hand side non-terminals. In an L-attributed SDT, inherited attributes of a right-hand side symbol can depend only on: inherited attributes of the left-hand side, synthesized attributes of symbols to its left, or its own synthesized attributes.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: attributed_sdt Pages: 1 --&gt;
&lt;svg width=&quot;470pt&quot; height=&quot;173pt&quot; viewBox=&quot;0.00 0.00 470.00 173.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 168.8)&quot;&gt;
&lt;title&gt;attributed_sdt&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-168.8 466,-168.8 466,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;g id=&quot;clust1&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_s&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;8,-8 8,-156.8 213,-156.8 213,-8 8,-8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;110.5&quot; y=&quot;-140.2&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;S-Attributed (Synthesized Only)&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;clust2&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_l&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;221,-8 221,-156.8 454,-156.8 454,-8 221,-8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;337.5&quot; y=&quot;-140.2&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;L-Attributed (Synthesized + Inherited)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- S --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;S&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;174.69,-124 45.31,-124 45.31,-88 174.69,-88 174.69,-124&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;110&quot; y=&quot;-102.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E.val = T.val + E1.val&lt;/text&gt;
&lt;/g&gt;
&lt;!-- S1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;S1&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;113.85,-52 16.15,-52 16.15,-16 113.85,-16 113.85,-52&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;65&quot; y=&quot;-30.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;T.val = num.val&lt;/text&gt;
&lt;/g&gt;
&lt;!-- S&amp;#45;&amp;gt;S1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;S-&gt;S1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M98.88,-87.7C93.81,-79.81 87.7,-70.3 82.07,-61.55&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;85.17,-59.9 76.81,-53.38 79.28,-63.68 85.17,-59.9&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- S2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;S2&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;204.51,-52 131.49,-52 131.49,-16 204.51,-16 204.51,-52&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;168&quot; y=&quot;-30.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E1.val = ...&lt;/text&gt;
&lt;/g&gt;
&lt;!-- S&amp;#45;&amp;gt;S2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;S-&gt;S2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M124.34,-87.7C131.08,-79.56 139.26,-69.69 146.7,-60.7&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;149.18,-63.2 152.86,-53.27 143.79,-58.74 149.18,-63.2&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- L --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;L&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;385.86,-124 282.14,-124 282.14,-88 385.86,-88 385.86,-124&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;334&quot; y=&quot;-102.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;D.inh = inherited&lt;/text&gt;
&lt;/g&gt;
&lt;!-- L1 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;L1&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;325.52,-52 234.48,-52 234.48,-16 325.52,-16 325.52,-52&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;280&quot; y=&quot;-30.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;T.type = D.inh&lt;/text&gt;
&lt;/g&gt;
&lt;!-- L&amp;#45;&amp;gt;L1 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;L-&gt;L1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M320.65,-87.7C314.44,-79.64 306.92,-69.89 300.04,-60.98&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;303,-59.08 294.12,-53.3 297.46,-63.36 303,-59.08&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- L2 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;L2&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;440.85,-52 343.15,-52 343.15,-16 440.85,-16 440.85,-52&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;392&quot; y=&quot;-30.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;D1.inh = T.type&lt;/text&gt;
&lt;/g&gt;
&lt;!-- L&amp;#45;&amp;gt;L2 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;L-&gt;L2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M348.34,-87.7C355.08,-79.56 363.26,-69.69 370.7,-60.7&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;373.18,-63.2 376.86,-53.27 367.79,-58.74 373.18,-63.2&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;S-attributed:&lt;/strong&gt; Only synthesized attributes, bottom-up evaluation, works with any parser&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;L-attributed:&lt;/strong&gt; Synthesized + inherited attributes, left-to-right evaluation, works with top-down parsers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Synthesized attributes:&lt;/strong&gt; Flow upward (child → parent), computed from children&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Inherited attributes:&lt;/strong&gt; Flow downward and left-to-right (parent → child, sibling → sibling)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Evaluation compatibility:&lt;/strong&gt; S-attributed SDTs pair with LR parsing; L-attributed SDTs pair with LL parsing&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_S_Attributed_and_L_Attributed_SDTs Pages: 1 --&gt;
&lt;svg width=&quot;201pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 201.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_S_Attributed_and_L_Attributed_SDTs&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 197.07,-165.63 197.07,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;88.59&quot; cy=&quot;-86.21&quot; rx=&quot;88.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;88.59&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;S Attributed And L A&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;132.48&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;132.48&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M102.48,-104.37C107.7,-111.21 113.63,-118.97 118.83,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;91.61&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;64.63&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;64.63&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M82.17,-67.93C78.67,-57.98 74.37,-45.73 70.9,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;106.29&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../syntax-directed-translation&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-directed-translation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax-Directed Translation&lt;/a&gt; — SDT classification by attribute type&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../top-down-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;top-down-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Top-Down Parsing&lt;/a&gt; — L-attributed SDTs are naturally evaluated during top-down parsing&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../bottom-up-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;bottom-up-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Bottom-Up Parsing&lt;/a&gt; — S-attributed SDTs are naturally evaluated during bottom-up parsing&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — how attribute evaluation integrates with parsing strategies&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../semantic-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;semantic-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Semantic Analysis&lt;/a&gt; — attributes carry type information needed for semantic checking&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Circular dependencies:&lt;/strong&gt; Attribute dependency graphs must be acyclic — circular definitions are invalid&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;L-attributed ≠ all inherited:&lt;/strong&gt; Not all grammars with inherited attributes are L-attributed — the restriction on attribute dependencies is strict&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bottom-up evaluation of L-attributed:&lt;/strong&gt; Possible with explicit action placement in the grammar (annotation markers), but requires grammar transformation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Real compiler usage:&lt;/strong&gt; Most real compilers use a mix — type information from SDTs combined with separate semantic analysis passes&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>basic-blocks</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/basic-blocks" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/basic-blocks.md" />
    <summary>The Problem Three-address code is a flat sequence of instructions, but optimizations need to reason about program structure.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Three-address code is a flat sequence of instructions, but optimizations need to reason about program structure. A program has branches and joins — different paths of execution. Before analysis can begin, the compiler must partition TAC into straight-line segments where control enters at the top and leaves at the bottom, with no internal branches.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A basic block is a sequence of consecutive three-address code instructions with a single entry point (the first instruction) and a single exit point (the last instruction). No jumps enter or leave the block except at the entry and exit. Within a basic block, if one instruction executes, all execute. Basic blocks form the nodes of the control-flow graph.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Basic blocks are identified by finding &lt;strong&gt;leaders&lt;/strong&gt; — the first instruction of a block. Leaders are: the first instruction of the program, any instruction that is the target of a jump, and any instruction immediately following a jump. Starting from each leader, the block extends until another leader is reached (or the end). The block includes all instructions from the leader up to (but not including) the next leader.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: basic_blocks Pages: 1 --&gt;
&lt;svg width=&quot;224pt&quot; height=&quot;466pt&quot; viewBox=&quot;0.00 0.00 224.00 466.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 462.2)&quot;&gt;
&lt;title&gt;basic_blocks&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-462.2 220.37,-462.2 220.37,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- TAC --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;TAC&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;162.22,-458.2 53.15,-458.2 53.15,-335 162.22,-335 162.22,-458.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-443.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;TAC:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-429&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;1: t1 = a + b&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-414.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;2: t2 = c * d&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-400.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;3: if t1 &amp;#x3C; t2 goto L&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-385.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;4: t3 = t1 - t2&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-371.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;5: goto M&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-357&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;L: 6: t3 = t2 - t1&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-342.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;M: 7: result = t3&lt;/text&gt;
&lt;/g&gt;
&lt;!-- BB1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;BB1&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;157.37,-298 58,-298 58,-246.8 157.37,-246.8 157.37,-298&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-283.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Basic Block B1:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-268.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;1: t1 = a + b&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-254.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;2: t2 = c * d&lt;/text&gt;
&lt;/g&gt;
&lt;!-- TAC&amp;#45;&amp;gt;BB1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;TAC-&gt;BB1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M107.68,-334.86C107.68,-326.27 107.68,-317.66 107.68,-309.73&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;111.18,-309.81 107.68,-299.81 104.18,-309.81 111.18,-309.81&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- BB2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;BB2&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;162.22,-209.8 53.15,-209.8 53.15,-173 162.22,-173 162.22,-209.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-195&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Basic Block B2:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-180.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;3: if t1 &amp;#x3C; t2 goto L&lt;/text&gt;
&lt;/g&gt;
&lt;!-- BB1&amp;#45;&amp;gt;BB2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;BB1-&gt;BB2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M107.68,-246.4C107.68,-238.51 107.68,-229.71 107.68,-221.55&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;111.18,-221.74 107.68,-211.74 104.18,-221.74 111.18,-221.74&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- BB3 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;BB3&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;99.37,-125 0,-125 0,-73.8 99.37,-73.8 99.37,-125&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;49.68&quot; y=&quot;-110.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Basic Block B3:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;49.68&quot; y=&quot;-95.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;4: t3 = t1 - t2&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;49.68&quot; y=&quot;-81.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;5: goto M&lt;/text&gt;
&lt;/g&gt;
&lt;!-- BB2&amp;#45;&amp;gt;BB3 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;BB2-&gt;BB3&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M96.22,-172.62C89.28,-161.84 80.18,-147.73 71.88,-134.84&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;74.91,-133.08 66.55,-126.57 69.02,-136.87 74.91,-133.08&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;94.44&quot; y=&quot;-146&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;false&lt;/text&gt;
&lt;/g&gt;
&lt;!-- BB4 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;BB4&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;216.37,-117.8 117,-117.8 117,-81 216.37,-81 216.37,-117.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;166.68&quot; y=&quot;-103&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Basic Block B4:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;166.68&quot; y=&quot;-88.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;6: t3 = t2 - t1&lt;/text&gt;
&lt;/g&gt;
&lt;!-- BB2&amp;#45;&amp;gt;BB4 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;BB2-&gt;BB4&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M119.34,-172.62C127.74,-159.8 139.24,-142.26 148.8,-127.67&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;151.67,-129.69 154.22,-119.41 145.81,-125.85 151.67,-129.69&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;164.42&quot; y=&quot;-146&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;true (goto L)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- BB5 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;BB5&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;157.37,-36.8 58,-36.8 58,0 157.37,0 157.37,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Basic Block B5:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.68&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;7: result = t3&lt;/text&gt;
&lt;/g&gt;
&lt;!-- BB3&amp;#45;&amp;gt;BB5 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;BB3-&gt;BB5&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M68.04,-73.4C74.37,-64.77 81.5,-55.07 87.94,-46.29&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;90.6,-48.58 93.7,-38.45 84.96,-44.44 90.6,-48.58&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- BB4&amp;#45;&amp;gt;BB5 --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;BB4-&gt;BB5&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M153.59,-80.87C146,-70.71 136.3,-57.71 127.76,-46.29&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;130.64,-44.29 121.85,-38.37 125.03,-48.48 130.64,-44.29&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_bb Pages: 1 --&gt;
&lt;svg width=&quot;353pt&quot; height=&quot;206pt&quot; viewBox=&quot;0.00 0.00 353.00 206.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 202.21)&quot;&gt;
&lt;title&gt;semantic_bb&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-202.21 349.31,-202.21 349.31,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;163.58&quot; cy=&quot;-92.7&quot; rx=&quot;38.39&quot; ry=&quot;27.72&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;163.58&quot; y=&quot;-96.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Basic&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;163.58&quot; y=&quot;-81&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Blocks&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE1&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;215.44&quot; cy=&quot;-30.16&quot; rx=&quot;62.76&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;215.44&quot; y=&quot;-33.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Three-Address&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;215.44&quot; y=&quot;-20.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Code&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M183.49,-68.7C187.6,-63.74 191.93,-58.52 196.02,-53.58&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;267.53&quot; y=&quot;-63.84&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from -- TAC partitioned into blocks&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT1 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT1&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;119.83&quot; cy=&quot;-24.32&quot; rx=&quot;54.97&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;119.83&quot; y=&quot;-27.62&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Control Flow&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;119.83&quot; y=&quot;-14.42&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Graph&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT1 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M147.29,-67.24C143.21,-60.86 138.85,-54.05 134.83,-47.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.53&quot; y=&quot;-60.21&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;builds into -- blocks are CFG nodes&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT2 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;85.24&quot; cy=&quot;-114.05&quot; rx=&quot;46.33&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;85.24&quot; y=&quot;-117.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Data Flow&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;85.24&quot; y=&quot;-104.15&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT2 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M127.62,-102.5C127.3,-102.59 126.98,-102.68 126.66,-102.76&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;200.92&quot; y=&quot;-94.53&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;builds into -- DFA operates on blocks&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;239.37&quot; cy=&quot;-122.16&quot; rx=&quot;54.54&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;239.37&quot; y=&quot;-125.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;239.37&quot; y=&quot;-112.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Optimization&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M197.49,-105.88C197.64,-105.94 197.8,-106 197.96,-106.07&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;121.94&quot; y=&quot;-108.67&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related -- optimizations work per block&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;159.75&quot; cy=&quot;-173.89&quot; rx=&quot;70.55&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;159.75&quot; y=&quot;-177.19&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Intermediate&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;159.75&quot; y=&quot;-163.99&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Code Generation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M162.26,-120.64C161.83,-129.91 161.34,-140.15 160.92,-149.16&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;147.83&quot; y=&quot;-137.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Single entry, single exit:&lt;/strong&gt; Control enters at the top and leaves at the bottom&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Leader-based identification:&lt;/strong&gt; Partition TAC by finding leaders (first instruction, jump targets, post-jump instructions)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sequential execution:&lt;/strong&gt; If the first instruction executes, all instructions execute (no internal branches)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optimization unit:&lt;/strong&gt; Many optimizations (constant propagation, dead code elimination) operate within a single basic block&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CFG nodes:&lt;/strong&gt; Basic blocks are the vertices of the control-flow graph&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../three-address-code&quot; class=&quot;internal alias&quot; data-slug=&quot;three-address-code&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Three-Address Code&lt;/a&gt; — TAC is partitioned into basic blocks&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../control-flow-graph&quot; class=&quot;internal alias&quot; data-slug=&quot;control-flow-graph&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Control Flow Graph&lt;/a&gt; — basic blocks form the nodes of the CFG&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../data-flow-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;data-flow-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Data Flow Analysis&lt;/a&gt; — DFA uses GEN/KILL sets per basic block&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — many local optimizations operate within a single block&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../intermediate-code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;intermediate-code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Intermediate Code Generation&lt;/a&gt; — ICG produces the TAC that gets partitioned&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Empty blocks:&lt;/strong&gt; A leader may be immediately followed by another leader, creating an empty basic block — rare but possible&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Overlapping blocks:&lt;/strong&gt; Blocks cannot overlap — each instruction belongs to exactly one block&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Critical edges:&lt;/strong&gt; Edges from a block with multiple successors to a block with multiple predecessors are called critical edges — they complicate optimization&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Unreachable code:&lt;/strong&gt; Instructions after an unconditional jump (but before the next leader) are unreachable — dead code elimination can remove them&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>bottom-up-parsing</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/bottom-up-parsing" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/bottom-up-parsing.md" />
    <summary>The Problem Top-down parsers can only handle a restricted class of grammars (LL grammars).</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Top-down parsers can only handle a restricted class of grammars (LL grammars). Many common programming language constructs — especially operator precedence, left recursion, and certain ambiguous patterns — are not LL-parseable. A more powerful parsing strategy is needed.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Bottom-up parsing builds the parse tree from the leaves (tokens) upward to the root (start symbol). It reads tokens left-to-right and uses a stack to recognize production right-hand sides (handles). When a handle appears on top of the stack, the parser &lt;strong&gt;reduces&lt;/strong&gt; it to the corresponding non-terminal. LR parsers are the most common bottom-up family.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The parser has two operations: &lt;strong&gt;shift&lt;/strong&gt; (push the current token onto the stack) and &lt;strong&gt;reduce&lt;/strong&gt; (pop the top symbols matching a production’s RHS, push the LHS non-terminal). It uses a parsing table with ACTION and GOTO entries. The ACTION table specifies shift/reduce/accept based on state and current token. The GOTO table specifies the next state after a reduction.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: bottom_up Pages: 1 --&gt;
&lt;svg width=&quot;420pt&quot; height=&quot;186pt&quot; viewBox=&quot;0.00 0.00 420.00 186.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 182)&quot;&gt;
&lt;title&gt;bottom_up&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-182 416.11,-182 416.11,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Input --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Input&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;91.36,-85.8 0,-85.8 0,-49 91.36,-49 91.36,-85.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;45.68&quot; y=&quot;-71&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Token Stream&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;45.68&quot; y=&quot;-56.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;id + id $&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Parser&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;206.38,-85.8 128.36,-85.8 128.36,-49 206.38,-49 206.38,-85.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;167.37&quot; y=&quot;-71&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;LR Parser&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;167.37&quot; y=&quot;-56.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(bottom-up)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Input&amp;#45;&amp;gt;Parser --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Input-&gt;Parser&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M91.59,-67.4C99.71,-67.4 108.21,-67.4 116.46,-67.4&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;116.37,-70.9 126.37,-67.4 116.37,-63.9 116.37,-70.9&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Stack --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Stack&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;380.1,-178 326.1,-178 326.1,-54.8 380.1,-54.8 380.1,-178&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;353.1&quot; y=&quot;-163.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stack:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;353.1&quot; y=&quot;-148.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;id&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;353.1&quot; y=&quot;-134.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;T&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;353.1&quot; y=&quot;-120&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;353.1&quot; y=&quot;-105.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E +&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;353.1&quot; y=&quot;-91.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E + id&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;353.1&quot; y=&quot;-76.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E + T&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;353.1&quot; y=&quot;-62.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;Stack --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;Stack&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M206.44,-77.55C238.17,-86.02 283.13,-98.01 314.68,-106.42&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;313.62,-109.76 324.19,-108.96 315.43,-103 313.62,-109.76&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;250.23&quot; y=&quot;-97.83&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;shift/reduce&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Output --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Output&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;412.11,-36.8 294.08,-36.8 294.08,0 412.11,0 412.11,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;353.1&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Reverse Rightmost&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;353.1&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Derivation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;Output --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;Output&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M206.44,-57.25C228.51,-51.36 256.98,-43.77 282.89,-36.86&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;283.59,-40.29 292.35,-34.34 281.78,-33.53 283.59,-40.29&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Direction of tree building:&lt;/strong&gt; Leaves → root (bottom-up)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Derivation type:&lt;/strong&gt; Reverse of rightmost derivation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Operations:&lt;/strong&gt; Shift (push token) and Reduce (apply production)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LR family:&lt;/strong&gt; SLR, CLR (LR(1)), LALR — increasing power and complexity&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Handle:&lt;/strong&gt; The right-hand side of a production that is ready to be reduced&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Bottom_Up_Parsing Pages: 1 --&gt;
&lt;svg width=&quot;195pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 195.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Bottom_Up_Parsing&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 191.32,-165.63 191.32,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;82.83&quot; cy=&quot;-86.21&quot; rx=&quot;82.83&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;82.83&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Bottom Up Parsing&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;126.73&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;126.73&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M96.72,-104.37C101.95,-111.21 107.88,-118.97 113.08,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;85.86&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;58.88&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;58.88&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M76.41,-67.93C72.92,-57.98 68.62,-45.73 65.15,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;100.53&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../parser-introduction&quot; class=&quot;internal alias&quot; data-slug=&quot;parser-introduction&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Parser Introduction&lt;/a&gt; — bottom-up is a category of parsing&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../shift-reduce-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;shift-reduce-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Shift Reduce Parser&lt;/a&gt; — bottom-up parsers use shift-reduce as their fundamental mechanism&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../top-down-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;top-down-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Top-Down Parsing&lt;/a&gt; — bottom-up reduces; top-down predicts&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../lr-parsers&quot; class=&quot;internal alias&quot; data-slug=&quot;lr-parsers&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LR Parsers&lt;/a&gt; — SLR, CLR, and LALR are all bottom-up parsers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../context-free-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;context-free-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Context-Free Grammar&lt;/a&gt; — bottom-up parsers handle a wider class of CFGs (LR grammars)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Shift/Reduce conflicts:&lt;/strong&gt; Parser cannot decide whether to shift or reduce — common in ambiguous grammars&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reduce/Reduce conflicts:&lt;/strong&gt; Two different productions could reduce the same handle&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Table size:&lt;/strong&gt; Canonical LR(1) tables can be enormous — LALR merges states to reduce size at the cost of some power&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error recovery:&lt;/strong&gt; Detecting errors earlier in LR parsing vs LL is different — LR detects at reduce time&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>classification-of-context-free-grammars</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/classification-of-context-free-grammars" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/classification-of-context-free-grammars.md" />
    <summary>The Problem Not all context-free grammars are equally useful for parsing.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Not all context-free grammars are equally useful for parsing. Some grammars are ambiguous (producing multiple parse trees), some contain left recursion (breaking top-down parsers), and some require unbounded lookahead. A classification system helps parser designers choose or transform grammars for the target parsing strategy.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Context-free grammars are classified by their properties relevant to parsing: &lt;strong&gt;ambiguous vs unambiguous&lt;/strong&gt;, &lt;strong&gt;left-recursive vs non-left-recursive&lt;/strong&gt;, &lt;strong&gt;LL(k)&lt;/strong&gt; (parseable with k lookahead top-down), and &lt;strong&gt;LR(k)&lt;/strong&gt; (parseable with k lookahead bottom-up). Every LL grammar is also LR, but not vice versa. Grammars can often be transformed (left-recursion elimination, left-factoring) to fit a desired class.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A grammar is &lt;strong&gt;ambiguous&lt;/strong&gt; if some string has more than one leftmost derivation. A grammar is &lt;strong&gt;left-recursive&lt;/strong&gt; if a non-terminal derives a string starting with itself (direct: &lt;code class=&quot;notranslate&quot;&gt;A → Aα&lt;/code&gt;, or indirect: &lt;code class=&quot;notranslate&quot;&gt;A → Bβ, B → Aγ&lt;/code&gt;). A grammar is &lt;strong&gt;LL(k)&lt;/strong&gt; if for every non-terminal and every k-token lookahead, exactly one production can be chosen. A grammar is &lt;strong&gt;LR(k)&lt;/strong&gt; if handle identification can be done with k lookahead. The class hierarchy: &lt;code class=&quot;notranslate&quot;&gt;LL ⊂ LR ⊂ unambiguous CFG ⊂ CFG&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: cfg_classification Pages: 1 --&gt;
&lt;svg width=&quot;783pt&quot; height=&quot;99pt&quot; viewBox=&quot;0.00 0.00 783.00 99.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 94.8)&quot;&gt;
&lt;title&gt;cfg_classification&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-94.8 779.1,-94.8 779.1,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- CFG --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CFG&lt;/title&gt;
&lt;polygon fill=&quot;#fff3cd&quot; stroke=&quot;black&quot; points=&quot;230.71,-36.4 166.71,-36.4 166.71,-0.4 230.71,-0.4 230.71,-36.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;198.71&quot; y=&quot;-14.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;All CFGs&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Unamb --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Unamb&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;447.8,-63.4 357.76,-63.4 357.76,-27.4 447.8,-27.4 447.8,-63.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;402.78&quot; y=&quot;-41.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Unambiguous&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CFG&amp;#45;&amp;gt;Unamb --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CFG-&gt;Unamb&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M230.84,-22.56C261.54,-26.66 309.13,-33.02 346.31,-37.99&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;345.61,-41.43 355.98,-39.28 346.54,-34.49 345.61,-41.43&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;311.41&quot; y=&quot;-39.67&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;no ambiguity&lt;/text&gt;
&lt;/g&gt;
&lt;!-- LR --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;LR&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;617.95,-63.8 539.93,-63.8 539.93,-27 617.95,-27 617.95,-63.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;578.94&quot; y=&quot;-49&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;LR(k)&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;578.94&quot; y=&quot;-34.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(bottom-up)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Unamb&amp;#45;&amp;gt;LR --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Unamb-&gt;LR&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M448.18,-45.4C472.65,-45.4 503.1,-45.4 528.35,-45.4&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;528.03,-48.9 538.03,-45.4 528.03,-41.9 528.03,-48.9&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;493.87&quot; y=&quot;-48.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;deterministic&lt;/text&gt;
&lt;/g&gt;
&lt;!-- LL --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;LL&lt;/title&gt;
&lt;polygon fill=&quot;#e8d4ff&quot; stroke=&quot;black&quot; points=&quot;775.1,-63.8 701.75,-63.8 701.75,-27 775.1,-27 775.1,-63.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;738.43&quot; y=&quot;-49&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;LL(k)&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;738.43&quot; y=&quot;-34.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(top-down)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- LR&amp;#45;&amp;gt;LL --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;LR-&gt;LL&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M618.07,-45.4C639.71,-45.4 667,-45.4 690.02,-45.4&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;689.76,-48.9 699.76,-45.4 689.76,-41.9 689.76,-48.9&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;659.85&quot; y=&quot;-60.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;left-to-right&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;659.85&quot; y=&quot;-48.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;parseable&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Amb --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Amb&lt;/title&gt;
&lt;polygon fill=&quot;#ffe5cc&quot; stroke=&quot;black&quot; points=&quot;95.35,-36.8 0,-36.8 0,0 95.35,0 95.35,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;47.68&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Ambiguous&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;47.68&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(multiple trees)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Amb&amp;#45;&amp;gt;CFG --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Amb-&gt;CFG&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;1 5&quot; d=&quot;M95.83,-18.4C114.81,-18.4 136.53,-18.4 155.04,-18.4&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;154.86,-21.9 164.86,-18.4 154.86,-14.9 154.86,-21.9&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- NonDet --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;NonDet&lt;/title&gt;
&lt;polygon fill=&quot;#ffe5cc&quot; stroke=&quot;black&quot; points=&quot;265.06,-90.8 132.35,-90.8 132.35,-54 265.06,-54 265.06,-90.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;198.71&quot; y=&quot;-76&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Non-deterministic&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;198.71&quot; y=&quot;-61.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(shift/reduce conflicts)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- NonDet&amp;#45;&amp;gt;Unamb --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;NonDet-&gt;Unamb&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;1 5&quot; d=&quot;M265.32,-63.63C291.5,-60.14 321.32,-56.15 346.39,-52.8&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;346.59,-56.3 356.04,-51.51 345.67,-49.37 346.59,-56.3&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_cfg_class Pages: 1 --&gt;
&lt;svg width=&quot;272pt&quot; height=&quot;222pt&quot; viewBox=&quot;0.00 0.00 272.00 222.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 217.72)&quot;&gt;
&lt;title&gt;semantic_cfg_class&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-217.72 267.99,-217.72 267.99,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;135.9&quot; cy=&quot;-106.84&quot; rx=&quot;65.47&quot; ry=&quot;27.72&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;135.9&quot; y=&quot;-110.74&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;CFG&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;135.9&quot; y=&quot;-95.14&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Classification&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE1&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;207.29&quot; cy=&quot;-65.33&quot; rx=&quot;56.7&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;207.29&quot; y=&quot;-68.63&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Context-Free&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;207.29&quot; y=&quot;-55.43&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Grammar&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M174.94,-84.14C175.07,-84.07 175.2,-83.99 175.33,-83.92&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;156.63&quot; y=&quot;-75.93&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT1 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT1&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;135.73&quot; cy=&quot;-24.32&quot; rx=&quot;47.19&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;135.73&quot; y=&quot;-27.62&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Top-Down&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;135.73&quot; y=&quot;-14.42&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Parsing&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT1 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M135.84,-78.9C135.82,-69.18 135.8,-58.35 135.78,-48.91&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;135.81&quot; y=&quot;-55.81&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;LL grammars used here&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT2 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;65.15&quot; cy=&quot;-149.29&quot; rx=&quot;48.49&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;65.15&quot; y=&quot;-152.59&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Bottom-Up&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;65.15&quot; y=&quot;-139.39&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Parsing&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT2 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M97.63,-129.8C97.23,-130.04 96.84,-130.27 96.45,-130.5&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;48.52&quot; y=&quot;-122.05&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;LR grammars used here&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT3 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT3&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;207.88&quot; cy=&quot;-147.37&quot; rx=&quot;43.93&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;207.88&quot; y=&quot;-144.07&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;LR Parsers&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT3 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT3&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M175.68,-129.24C177.76,-130.41 179.82,-131.57 181.84,-132.71&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;199.02&quot; y=&quot;-122.88&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;137.33&quot; cy=&quot;-189.39&quot; rx=&quot;50.23&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;137.33&quot; y=&quot;-192.69&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Ambiguous&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;137.33&quot; y=&quot;-179.49&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Grammar&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M136.38,-134.79C136.55,-144.52 136.74,-155.35 136.91,-164.79&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;136.64&quot; y=&quot;-141.69&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;contrasts with&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;64.11&quot; cy=&quot;-66.22&quot; rx=&quot;61.88&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;64.11&quot; y=&quot;-69.52&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;FIRST and&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;64.11&quot; y=&quot;-56.32&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;FOLLOW Sets&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M96.23,-84.39C96.1,-84.32 95.97,-84.24 95.84,-84.17&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;109.79&quot; y=&quot;-76.18&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Ambiguous:&lt;/strong&gt; Multiple parse trees for the same string — problematic for deterministic parsing&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Left-recursive:&lt;/strong&gt; Direct or indirect recursion on the left — fatal for top-down parsers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LL(k):&lt;/strong&gt; Deterministic top-down parseable with k-token lookahead — requires left-factoring&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LR(k):&lt;/strong&gt; Deterministic bottom-up parseable with k-token lookahead — more powerful than LL&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Hierarchy:&lt;/strong&gt; Every LL grammar is LR, but LR grammars include non-LL languages (e.g., left-recursive expressions)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../context-free-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;context-free-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Context-Free Grammar&lt;/a&gt; — CFG is the formal foundation being classified&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../top-down-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;top-down-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Top-Down Parsing&lt;/a&gt; — requires non-left-recursive, LL(k) grammars&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../lr-parsers&quot; class=&quot;internal alias&quot; data-slug=&quot;lr-parsers&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LR Parsers&lt;/a&gt; — LR(k) grammars are the input class for LR parser generators&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../wiki/compilerdesign/ambiguous-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;wiki/compilerdesign/ambiguous-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Ambiguous Grammar&lt;/a&gt; — ambiguity is a key classification dimension&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../first-and-follow-sets&quot; class=&quot;internal alias&quot; data-slug=&quot;first-and-follow-sets&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;FIRST and FOLLOW Sets&lt;/a&gt; — used to determine if a grammar is LL(1)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;LL(1) ≠ LL(k):&lt;/strong&gt; A grammar may not be LL(1) but may be LL(2) — increasing lookahead increases power&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LR(0) &amp;#x3C; SLR &amp;#x3C; LALR &amp;#x3C; CLR:&lt;/strong&gt; Within LR family, each subclass handles a larger set of grammars&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Grammar transformation:&lt;/strong&gt; Left-recursive grammars can be mechanically transformed to non-left-recursive, but the resulting grammar may be harder to read&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Inherently ambiguous languages:&lt;/strong&gt; Some languages are inherently ambiguous — no unambiguous grammar exists for them&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>code-generation</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/code-generation" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/code-generation.md" />
    <summary>The Problem After analysis and optimization, the compiler holds an optimized intermediate representation.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;After analysis and optimization, the compiler holds an optimized intermediate representation. This IR must be translated into actual machine instructions (or assembly) that the target processor can execute — including instruction selection, register allocation, and addressing mode decisions.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Code generation is the final phase of a compiler. It takes the optimized intermediate representation and produces target machine code (or assembly). The generated code must be correct, efficient, and make effective use of the target architecture’s resources (registers, memory, instruction set).&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The code generator performs three key tasks: &lt;strong&gt;instruction selection&lt;/strong&gt; (mapping IR operations to target instructions), &lt;strong&gt;register allocation&lt;/strong&gt; (assigning variables to CPU registers), and &lt;strong&gt;instruction ordering&lt;/strong&gt; (scheduling instructions for efficiency). It produces relocatable object code or assembly that is passed to an assembler or linker.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: code_generation Pages: 1 --&gt;
&lt;svg width=&quot;271pt&quot; height=&quot;357pt&quot; viewBox=&quot;0.00 0.00 271.00 357.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 352.8)&quot;&gt;
&lt;title&gt;code_generation&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-352.8 267.35,-352.8 267.35,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;g id=&quot;clust1&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_tasks&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;114.35,-8 114.35,-231.2 255.35,-231.2 255.35,-8 114.35,-8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;184.85&quot; y=&quot;-214.6&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;Code Generator Tasks&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OptimizedIR --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OptimizedIR&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;141.36,-348.8 71.34,-348.8 71.34,-312 141.36,-312 141.36,-348.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;106.35&quot; y=&quot;-334&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Optimized&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;106.35&quot; y=&quot;-319.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;IR&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Gen --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Gen&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;141.37,-276 71.34,-276 71.34,-239.2 141.37,-239.2 141.37,-276&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;106.35&quot; y=&quot;-261.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;106.35&quot; y=&quot;-246.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Generator&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OptimizedIR&amp;#45;&amp;gt;Gen --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;OptimizedIR-&gt;Gen&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M106.35,-311.53C106.35,-304.27 106.35,-295.68 106.35,-287.57&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;109.85,-287.73 106.35,-277.73 102.85,-287.73 109.85,-287.73&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Target --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Target&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;106.7,-198.4 0,-198.4 0,-161.6 106.7,-161.6 106.7,-198.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;53.35&quot; y=&quot;-183.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Target Machine&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;53.35&quot; y=&quot;-169.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Code / Assembly&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Gen&amp;#45;&amp;gt;Target --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Gen-&gt;Target&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M94.06,-239.07C87.62,-229.88 79.58,-218.42 72.36,-208.11&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;75.24,-206.12 66.63,-199.94 69.5,-210.14 75.24,-206.12&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- ISel --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;ISel&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;196.03,-198.4 124.67,-198.4 124.67,-161.6 196.03,-161.6 196.03,-198.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;160.35&quot; y=&quot;-183.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Instruction&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;160.35&quot; y=&quot;-169.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Selection&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Gen&amp;#45;&amp;gt;ISel --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Gen-&gt;ISel&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M118.88,-239.07C125.51,-229.78 133.79,-218.18 141.21,-207.8&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;143.86,-210.1 146.83,-199.93 138.17,-206.04 143.86,-210.1&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- RegAlloc --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;RegAlloc&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;194.37,-125.6 126.34,-125.6 126.34,-88.8 194.37,-88.8 194.37,-125.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;160.35&quot; y=&quot;-110.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Register&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;160.35&quot; y=&quot;-96.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Allocation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- ISel&amp;#45;&amp;gt;RegAlloc --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;ISel-&gt;RegAlloc&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M160.35,-161.13C160.35,-153.87 160.35,-145.28 160.35,-137.17&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;163.85,-137.33 160.35,-127.33 156.85,-137.33 163.85,-137.33&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- ISched --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;ISched&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;198.04,-52.8 122.66,-52.8 122.66,-16 198.04,-16 198.04,-52.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;160.35&quot; y=&quot;-38&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Instruction&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;160.35&quot; y=&quot;-23.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Scheduling&lt;/text&gt;
&lt;/g&gt;
&lt;!-- RegAlloc&amp;#45;&amp;gt;ISched --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;RegAlloc-&gt;ISched&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M160.35,-88.33C160.35,-81.07 160.35,-72.48 160.35,-64.37&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;163.85,-64.53 160.35,-54.53 156.85,-64.53 163.85,-64.53&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Input:&lt;/strong&gt; Optimized intermediate representation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Output:&lt;/strong&gt; Target machine code (assembly or binary)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Three tasks:&lt;/strong&gt; Instruction selection, register allocation, instruction scheduling&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Target-dependent:&lt;/strong&gt; The code generator is specific to the target architecture&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Produces:&lt;/strong&gt; Object code that the linker/loader handles next&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Code_Generation Pages: 1 --&gt;
&lt;svg width=&quot;189pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 189.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Code_Generation&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 184.64,-165.63 184.64,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;76.15&quot; cy=&quot;-86.21&quot; rx=&quot;76.15&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;76.15&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Code Generation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;120.05&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;120.05&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M90.04,-104.37C95.26,-111.21 101.2,-118.97 106.4,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;79.18&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;52.19&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;52.19&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M69.73,-67.93C66.24,-57.98 61.94,-45.73 58.47,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;93.85&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — consumes optimized IR&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../code-generator-design-issues&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generator-design-issues&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Issues in Code Generator Design&lt;/a&gt; — design considerations for building a code generator&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../object-code&quot; class=&quot;internal alias&quot; data-slug=&quot;object-code&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Object Code&lt;/a&gt; — produces the object code output&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — code generation is phase 6, the final phase&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../linker-and-loader&quot; class=&quot;internal alias&quot; data-slug=&quot;linker-and-loader&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Linker and Loader&lt;/a&gt; — the code generator produces object files that the linker resolves&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Register spilling:&lt;/strong&gt; When there are more live variables than registers, some must be spilled to memory — frequent spilling destroys performance&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Strange instructions:&lt;/strong&gt; Some architectures have complex instructions (VLIW, SIMD) that require careful pattern matching during instruction selection&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;PIC vs absolute code:&lt;/strong&gt; Position-independent code requires different addressing strategies than absolute code&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>code-generator-design-issues</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/code-generator-design-issues" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/code-generator-design-issues.md" />
    <summary>The Problem Generating correct and efficient target code from intermediate representation is not straightforward.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Generating correct and efficient target code from intermediate representation is not straightforward. The code generator must make complex decisions — which instructions to use, which registers to allocate, how to handle memory — each affecting correctness, performance, and code size.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The design of a code generator involves several critical issues: &lt;strong&gt;instruction selection&lt;/strong&gt; (mapping IR to target instructions), &lt;strong&gt;register allocation&lt;/strong&gt; (assigning variables to limited CPU registers), &lt;strong&gt;instruction ordering&lt;/strong&gt; (scheduling for pipeline efficiency), &lt;strong&gt;addressing modes&lt;/strong&gt; (choosing how to access memory), and &lt;strong&gt;handling of special constructs&lt;/strong&gt; (function calls, aliasing, runtime checks).&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The code generator walks the IR and for each operation, selects one or more target instructions. It maintains a register descriptor (which variable is in which register) and an address descriptor (where each variable’s current value is located). When variables outnumber registers, some must be &lt;strong&gt;spilled&lt;/strong&gt; to memory. The generator must also handle calling conventions and runtime interface.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: code_gen_issues Pages: 1 --&gt;
&lt;svg width=&quot;544pt&quot; height=&quot;211pt&quot; viewBox=&quot;0.00 0.00 544.00 211.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 207.2)&quot;&gt;
&lt;title&gt;code_gen_issues&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-207.2 540,-207.2 540,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;g id=&quot;clust1&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_issues&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;64,-8 64,-85.6 528,-85.6 528,-8 64,-8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;296&quot; y=&quot;-69&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;Design Issues&lt;/text&gt;
&lt;/g&gt;
&lt;!-- IR --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;IR&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;275.01,-203.2 204.99,-203.2 204.99,-166.4 275.01,-166.4 275.01,-203.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240&quot; y=&quot;-188.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Optimized&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240&quot; y=&quot;-174&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;IR&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CodeGen --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CodeGen&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;275.01,-130.4 204.99,-130.4 204.99,-93.6 275.01,-93.6 275.01,-130.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240&quot; y=&quot;-115.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240&quot; y=&quot;-101.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Generator&lt;/text&gt;
&lt;/g&gt;
&lt;!-- IR&amp;#45;&amp;gt;CodeGen --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;IR-&gt;CodeGen&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M240,-165.93C240,-158.67 240,-150.08 240,-141.97&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;243.5,-142.13 240,-132.13 236.5,-142.13 243.5,-142.13&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Target --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Target&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;54,-52.8 0,-52.8 0,-16 54,-16 54,-52.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;27&quot; y=&quot;-38&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Target&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;27&quot; y=&quot;-23.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Code&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CodeGen&amp;#45;&amp;gt;Target --&gt;
&lt;g id=&quot;edge7&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CodeGen-&gt;Target&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M204.74,-109.44C159.31,-106.86 83.39,-100.42 60,-85.6 51.48,-80.2 44.69,-71.72 39.54,-63.26&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;42.62,-61.6 34.78,-54.48 36.47,-64.94 42.62,-61.6&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- I1 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;I1&lt;/title&gt;
&lt;polygon fill=&quot;#fff3cd&quot; stroke=&quot;black&quot; points=&quot;143.68,-52.8 72.32,-52.8 72.32,-16 143.68,-16 143.68,-52.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;108&quot; y=&quot;-38&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Instruction&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;108&quot; y=&quot;-23.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Selection&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CodeGen&amp;#45;&amp;gt;I1 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CodeGen-&gt;I1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M204.68,-105.34C188.09,-101.56 168.58,-95.41 153,-85.6 143.15,-79.4 134.16,-70.44 126.85,-61.8&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;129.79,-59.87 120.82,-54.23 124.31,-64.23 129.79,-59.87&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- I2 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;I2&lt;/title&gt;
&lt;polygon fill=&quot;#fff3cd&quot; stroke=&quot;black&quot; points=&quot;230.02,-52.8 161.98,-52.8 161.98,-16 230.02,-16 230.02,-52.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;196&quot; y=&quot;-38&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Register&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;196&quot; y=&quot;-23.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Allocation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CodeGen&amp;#45;&amp;gt;I2 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CodeGen-&gt;I2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M229.79,-93.47C224.5,-84.37 217.92,-73.05 211.97,-62.83&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;215.12,-61.29 207.06,-54.41 209.07,-64.81 215.12,-61.29&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- I3 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;I3&lt;/title&gt;
&lt;polygon fill=&quot;#fff3cd&quot; stroke=&quot;black&quot; points=&quot;319.68,-52.8 248.32,-52.8 248.32,-16 319.68,-16 319.68,-52.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;284&quot; y=&quot;-38&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Instruction&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;284&quot; y=&quot;-23.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Ordering&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CodeGen&amp;#45;&amp;gt;I3 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CodeGen-&gt;I3&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M250.21,-93.47C255.5,-84.37 262.08,-73.05 268.03,-62.83&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;270.93,-64.81 272.94,-54.41 264.88,-61.29 270.93,-64.81&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- I4 --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;I4&lt;/title&gt;
&lt;polygon fill=&quot;#fff3cd&quot; stroke=&quot;black&quot; points=&quot;414.02,-52.8 337.98,-52.8 337.98,-16 414.02,-16 414.02,-52.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;376&quot; y=&quot;-38&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Addressing&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;376&quot; y=&quot;-23.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Modes&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CodeGen&amp;#45;&amp;gt;I4 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CodeGen-&gt;I4&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M275.16,-105.52C292.3,-101.75 312.68,-95.56 329,-85.6 339.13,-79.41 348.49,-70.46 356.14,-61.82&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;358.76,-64.14 362.48,-54.22 353.39,-59.65 358.76,-64.14&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- I5 --&gt;
&lt;g id=&quot;node8&quot; class=&quot;node&quot;&gt;
&lt;title&gt;I5&lt;/title&gt;
&lt;polygon fill=&quot;#fff3cd&quot; stroke=&quot;black&quot; points=&quot;519.69,-52.8 432.31,-52.8 432.31,-16 519.69,-16 519.69,-52.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;476&quot; y=&quot;-38&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Function&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;476&quot; y=&quot;-23.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Call Handling&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CodeGen&amp;#45;&amp;gt;I5 --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CodeGen-&gt;I5&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M275.38,-108.66C321.04,-105.21 397.63,-97.79 423,-85.6 434.77,-79.94 445.66,-70.7 454.46,-61.68&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;457.01,-64.08 461.2,-54.34 451.86,-59.34 457.01,-64.08&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Instruction selection:&lt;/strong&gt; Pattern matching IR operations to target instructions (e.g., &lt;code class=&quot;notranslate&quot;&gt;x = y + 1&lt;/code&gt; → &lt;code class=&quot;notranslate&quot;&gt;INC&lt;/code&gt; vs &lt;code class=&quot;notranslate&quot;&gt;ADD&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Register allocation:&lt;/strong&gt; Graph coloring, linear scan — assigning variables to registers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Register spilling:&lt;/strong&gt; When registers are exhausted, some values must be stored in memory&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Addressing modes:&lt;/strong&gt; Direct, indirect, indexed, base+offset — different modes have different costs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Instruction scheduling:&lt;/strong&gt; Reordering instructions for pipeline efficiency without changing semantics&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Issues_in_the_Design_of_a_Code_Generator Pages: 1 --&gt;
&lt;svg width=&quot;204pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 204.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Issues_in_the_Design_of_a_Code_Generator&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 199.94,-165.63 199.94,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;91.45&quot; cy=&quot;-86.21&quot; rx=&quot;91.45&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;91.45&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Issues In The Design&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;135.35&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;135.35&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M105.34,-104.37C110.56,-111.21 116.49,-118.97 121.7,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;132.56&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;67.49&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;67.49&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M85.03,-67.93C81.54,-57.98 77.23,-45.73 73.76,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;109.15&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Generation&lt;/a&gt; — these are the design considerations for implementing a code generator&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../three-address-code&quot; class=&quot;internal alias&quot; data-slug=&quot;three-address-code&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Three-Address Code&lt;/a&gt; — the IR form that the code generator processes&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — optimization and code generation trade off (e.g., register allocation)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../object-code&quot; class=&quot;internal alias&quot; data-slug=&quot;object-code&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Object Code&lt;/a&gt; — the output the generator must produce correctly&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../runtime-environment&quot; class=&quot;internal alias&quot; data-slug=&quot;runtime-environment&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Runtime Environment&lt;/a&gt; — calling conventions, stack layout, and memory management affect code generation&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Graph coloring NP-complete:&lt;/strong&gt; Optimal register allocation is NP-complete — compilers use heuristics (linear scan, greedy)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Aliasing:&lt;/strong&gt; If two variables point to the same memory location, the code generator cannot freely reorder operations&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Peculiar instructions:&lt;/strong&gt; Some ISAs have complex instructions (string copy, CRC, SIMD) that require careful pattern matching&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Self-modifying code:&lt;/strong&gt; Rarely needed, but some dynamic code systems require the generator to produce position-independent code&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>code-optimization</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/code-optimization" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/code-optimization.md" />
    <summary>The Problem Naively translated code contains redundant computations, dead code, and suboptimal patterns that waste CPU cycles and memory.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Naively translated code contains redundant computations, dead code, and suboptimal patterns that waste CPU cycles and memory. Hand-optimizing assembly is impractical. The compiler must automatically improve code quality while preserving program semantics.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Code optimization is the fifth phase of a compiler (and sometimes spans multiple passes). It transforms the intermediate representation into functionally equivalent but more efficient code — faster execution, smaller size, or lower power consumption — by applying algebraic, control-flow, and data-flow transformations.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The optimizer applies a series of transformations. &lt;strong&gt;Machine-independent optimizations&lt;/strong&gt; work on IR: constant folding (evaluating constant expressions at compile time), dead code elimination, common subexpression elimination, loop hoisting, and strength reduction. &lt;strong&gt;Machine-dependent optimizations&lt;/strong&gt; work during code generation: register allocation, instruction scheduling, and peephole optimization.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: optimization Pages: 1 --&gt;
&lt;svg width=&quot;415pt&quot; height=&quot;299pt&quot; viewBox=&quot;0.00 0.00 415.00 299.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 295)&quot;&gt;
&lt;title&gt;optimization&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-295 410.68,-295 410.68,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;g id=&quot;clust1&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_types&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;94.68,-8 94.68,-100 398.68,-100 398.68,-8 94.68,-8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;246.68&quot; y=&quot;-83.4&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;Optimization Categories&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Before --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Before&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;224.69,-291 126.66,-291 126.66,-211 224.69,-211 224.69,-291&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;175.68&quot; y=&quot;-276.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Unoptimized IR&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;175.68&quot; y=&quot;-261.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;t1 = 2 * 3.14&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;175.68&quot; y=&quot;-247.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;t2 = t1 * r&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;175.68&quot; y=&quot;-233&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;nop&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;175.68&quot; y=&quot;-218.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;t3 = t1 * r&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Optimizer --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Optimizer&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;209.35,-174 142.01,-174 142.01,-138 209.35,-138 209.35,-174&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;175.68&quot; y=&quot;-152.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Optimizer&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Before&amp;#45;&amp;gt;Optimizer --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Before-&gt;Optimizer&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M175.68,-210.56C175.68,-202.24 175.68,-193.62 175.68,-185.78&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;179.18,-185.79 175.68,-175.79 172.18,-185.79 179.18,-185.79&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- After --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;After&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;85.35,-60 0,-60 0,-23.2 85.35,-23.2 85.35,-60&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;42.68&quot; y=&quot;-45.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Optimized IR&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;42.68&quot; y=&quot;-30.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;t1 = r * 6.28&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Optimizer&amp;#45;&amp;gt;After --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Optimizer-&gt;After&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M141.79,-149.25C121.81,-144.36 97.15,-135.56 79.98,-120 65.61,-106.98 56.21,-87.22 50.45,-70.93&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;53.89,-70.21 47.48,-61.77 47.23,-72.36 53.89,-70.21&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;125.83&quot; y=&quot;-111&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;preserves semantics&lt;/text&gt;
&lt;/g&gt;
&lt;!-- MI --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;MI&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;248.37,-67.2 102.98,-67.2 102.98,-16 248.37,-16 248.37,-67.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;175.68&quot; y=&quot;-52.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Machine-Independent&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;175.68&quot; y=&quot;-38&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Constant Folding, CSE,&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;175.68&quot; y=&quot;-23.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Loop Hoisting)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Optimizer&amp;#45;&amp;gt;MI --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Optimizer-&gt;MI&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M175.68,-137.93C175.68,-122.29 175.68,-98.64 175.68,-78.8&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;179.18,-78.86 175.68,-68.86 172.18,-78.86 179.18,-78.86&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- MD --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;MD&lt;/title&gt;
&lt;polygon fill=&quot;#e8d4ff&quot; stroke=&quot;black&quot; points=&quot;390.71,-67.2 266.64,-67.2 266.64,-16 390.71,-16 390.71,-67.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;328.68&quot; y=&quot;-52.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Machine-Dependent&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;328.68&quot; y=&quot;-38&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Register Allocation,&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;328.68&quot; y=&quot;-23.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Peephole)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Optimizer&amp;#45;&amp;gt;MD --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Optimizer-&gt;MD&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M203.05,-137.51C219.2,-126.99 239.88,-113.12 257.68,-100 268.43,-92.07 279.8,-83.14 290.22,-74.72&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;292.37,-77.49 297.91,-68.46 287.95,-72.06 292.37,-77.49&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Semantics-preserving:&lt;/strong&gt; Optimized code must produce the same output for every input&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Two categories:&lt;/strong&gt; Machine-independent (on IR) and machine-dependent (on target)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Common techniques:&lt;/strong&gt; Constant folding, dead code elimination, CSE, loop optimizations, strength reduction&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optimization levels:&lt;/strong&gt; Compilers offer multiple levels (O0, O1, O2, O3) trading compile time vs runtime performance&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Code_Optimization Pages: 1 --&gt;
&lt;svg width=&quot;194pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 194.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Code_Optimization&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 189.89,-165.63 189.89,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;81.4&quot; cy=&quot;-86.21&quot; rx=&quot;81.4&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81.4&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Code Optimization&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;125.3&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;125.3&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M95.29,-104.37C100.51,-111.21 106.45,-118.97 111.65,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;84.43&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;57.44&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;57.44&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M74.98,-67.93C71.49,-57.98 67.19,-45.73 63.72,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;99.1&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../intermediate-code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;intermediate-code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Intermediate Code Generation&lt;/a&gt; — optimizes the IR&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Generation&lt;/a&gt; — optimized IR is passed to the code generator&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../three-address-code&quot; class=&quot;internal alias&quot; data-slug=&quot;three-address-code&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Three-Address Code&lt;/a&gt; — optimizations are often expressed as TAC transformations&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../data-flow-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;data-flow-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Data Flow Analysis&lt;/a&gt; — many optimizations require data-flow analysis to determine safety&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — code optimization is phase 5&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Optimization can hide bugs:&lt;/strong&gt; Some optimizations exploit undefined behavior in languages like C, causing working debug builds to break in optimized builds&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Diminishing returns:&lt;/strong&gt; Higher optimization levels (O3 vs O2) often yield marginal gains with significantly longer compile times&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Code size vs speed:&lt;/strong&gt; Some optimizations (loop unrolling, function inlining) increase code size for speed — must be tuned per application&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>common-subexpression-elimination</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/common-subexpression-elimination" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/common-subexpression-elimination.md" />
    <summary>The Problem Programs repeatedly compute the same expression multiple times — especially in loops, repeated indexing calculations (a[i*cols+j]), and aliased computations.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Programs repeatedly compute the same expression multiple times — especially in loops, repeated indexing calculations (&lt;code class=&quot;notranslate&quot;&gt;a[i*cols+j]&lt;/code&gt;), and aliased computations. Each redundant computation wastes CPU cycles. The compiler must detect when two expressions compute the same value and reuse the earlier result.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Common Subexpression Elimination (CSE) is a compiler optimization that identifies expressions that have been computed before and whose operands haven’t changed since. It replaces the redundant computation with a reference to the previously computed value. CSE can be &lt;strong&gt;local&lt;/strong&gt; (within a single basic block) or &lt;strong&gt;global&lt;/strong&gt; (across basic blocks using available-expression analysis).&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Local CSE scans a basic block and builds a table of computed expressions. For each new expression &lt;code class=&quot;notranslate&quot;&gt;x = a op b&lt;/code&gt;, it checks if &lt;code class=&quot;notranslate&quot;&gt;a op b&lt;/code&gt; has already been computed with the same operands and no intervening assignments to &lt;code class=&quot;notranslate&quot;&gt;a&lt;/code&gt; or &lt;code class=&quot;notranslate&quot;&gt;b&lt;/code&gt;. If found, the new computation is replaced with &lt;code class=&quot;notranslate&quot;&gt;x = previous_temp&lt;/code&gt;. Global CSE uses available-expression data-flow analysis to propagate this information across basic blocks in the CFG.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: cse_example Pages: 1 --&gt;
&lt;svg width=&quot;146pt&quot; height=&quot;276pt&quot; viewBox=&quot;0.00 0.00 146.00 276.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 272)&quot;&gt;
&lt;title&gt;cse_example&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-272 142.42,-272 142.42,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Before --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Before&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;138.42,-268 0,-268 0,-188 138.42,-188 138.42,-268&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.21&quot; y=&quot;-253.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Before CSE:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.21&quot; y=&quot;-238.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;t1 = a * b&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.21&quot; y=&quot;-224.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;t2 = c + d&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.21&quot; y=&quot;-210&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;t3 = a * b  ← redundant&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.21&quot; y=&quot;-195.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;x  = t3 + t2&lt;/text&gt;
&lt;/g&gt;
&lt;!-- After --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;After&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;127.9,-152 10.52,-152 10.52,-72 127.9,-72 127.9,-152&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.21&quot; y=&quot;-137.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;After CSE:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.21&quot; y=&quot;-122.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;t1 = a * b&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.21&quot; y=&quot;-108.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;t2 = c + d&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.21&quot; y=&quot;-94&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;      ← t3 eliminated&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.21&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;x  = t1 + t2&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Before&amp;#45;&amp;gt;After --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Before-&gt;After&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M69.21,-187.77C69.21,-179.98 69.21,-171.69 69.21,-163.58&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;72.71,-163.82 69.21,-153.82 65.71,-163.82 72.71,-163.82&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Savings --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Savings&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;137.24,-36 1.18,-36 1.18,0 137.24,0 137.24,-36&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.21&quot; y=&quot;-14.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Saved: 1 multiplication&lt;/text&gt;
&lt;/g&gt;
&lt;!-- After&amp;#45;&amp;gt;Savings --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;After-&gt;Savings&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M69.21,-71.72C69.21,-63.74 69.21,-55.48 69.21,-47.94&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;72.71,-47.97 69.21,-37.97 65.71,-47.97 72.71,-47.97&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_cse Pages: 1 --&gt;
&lt;svg width=&quot;584pt&quot; height=&quot;206pt&quot; viewBox=&quot;0.00 0.00 584.00 206.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 202.21)&quot;&gt;
&lt;title&gt;semantic_cse&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-202.21 579.97,-202.21 579.97,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;258.33&quot; cy=&quot;-92.7&quot; rx=&quot;87.95&quot; ry=&quot;27.72&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;258.33&quot; y=&quot;-96.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Common Subexpr.&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;258.33&quot; y=&quot;-81&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Elimination&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE1&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;310.2&quot; cy=&quot;-30.16&quot; rx=&quot;54.54&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;310.2&quot; y=&quot;-33.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;310.2&quot; y=&quot;-20.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Optimization&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M280.93,-65.45C284.32,-61.37 287.78,-57.19 291.08,-53.21&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;267.5&quot; y=&quot;-51.23&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE2&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;214.58&quot; cy=&quot;-24.32&quot; rx=&quot;46.33&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;214.58&quot; y=&quot;-27.62&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Data Flow&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;214.58&quot; y=&quot;-14.42&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M240.79,-65.29C237.08,-59.49 233.2,-53.42 229.59,-47.79&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;152.4&quot; y=&quot;-59.24&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from -- needs available expr analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CON1 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CON1&lt;/title&gt;
&lt;ellipse fill=&quot;#ffe5cc&quot; stroke=&quot;black&quot; cx=&quot;179.99&quot; cy=&quot;-114.05&quot; rx=&quot;54.54&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;179.99&quot; y=&quot;-117.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Peephole&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;179.99&quot; y=&quot;-104.15&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Optimization&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;CON1 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--CON1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M191.22,-110.99C191.11,-111.02 191.01,-111.05 190.9,-111.08&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;95.53&quot; y=&quot;-113.73&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;contrasts with -- CSE is IR-level, not target-level&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CON2 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CON2&lt;/title&gt;
&lt;ellipse fill=&quot;#ffe5cc&quot; stroke=&quot;black&quot; cx=&quot;334.12&quot; cy=&quot;-122.16&quot; rx=&quot;53.26&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;334.12&quot; y=&quot;-125.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Constant&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;334.12&quot; y=&quot;-112.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Propagation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;CON2 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--CON2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M314.21,-114.42C314.3,-114.46 314.39,-114.49 314.48,-114.52&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;445.16&quot; y=&quot;-117.17&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;contrasts with -- CSE targets repeated expressions, not constants&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;254.5&quot; cy=&quot;-173.89&quot; rx=&quot;34.22&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;254.5&quot; y=&quot;-177.19&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Basic&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;254.5&quot; y=&quot;-163.99&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Blocks&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M257.02,-120.64C256.58,-129.91 256.1,-140.15 255.67,-149.16&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;176.06&quot; y=&quot;-137.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related -- local CSE works within a block&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Local CSE:&lt;/strong&gt; Works within a single basic block — simple and fast&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Global CSE:&lt;/strong&gt; Works across blocks using available-expression data-flow analysis&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Available expressions:&lt;/strong&gt; An expression &lt;code class=&quot;notranslate&quot;&gt;a op b&lt;/code&gt; is available at point p if it was computed earlier and operands haven’t changed&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Safety:&lt;/strong&gt; Always safe — replacing a computation with a reference to an identical computation preserves semantics&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Loop benefits:&lt;/strong&gt; Most impactful in loops where expressions are repeatedly computed with the same operands&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — CSE is a classic compiler optimization technique&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../data-flow-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;data-flow-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Data Flow Analysis&lt;/a&gt; — global CSE requires available-expression analysis&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../peephole-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;peephole-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Peephole Optimization&lt;/a&gt; — CSE works at the IR level, not the target instruction level&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../constant-propagation&quot; class=&quot;internal alias&quot; data-slug=&quot;constant-propagation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Constant Propagation&lt;/a&gt; — CSE targets repeated expression evaluation, not constant values&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../basic-blocks&quot; class=&quot;internal alias&quot; data-slug=&quot;basic-blocks&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Basic Blocks&lt;/a&gt; — local CSE operates within a single basic block&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Operand aliasing:&lt;/strong&gt; If &lt;code class=&quot;notranslate&quot;&gt;a&lt;/code&gt; and &lt;code class=&quot;notranslate&quot;&gt;b&lt;/code&gt; can be modified through pointers between the two computations, CSE cannot safely eliminate the redundant computation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cost trade-off:&lt;/strong&gt; CSE increases register pressure by keeping more values live — may slow down register allocation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Global CSE complexity:&lt;/strong&gt; Available-expression analysis is more complex than reaching-definitions analysis because expressions involve multiple variables&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Partial redundancy:&lt;/strong&gt; When an expression is available on some paths but not all — partial redundancy elimination (PRE) is a more sophisticated optimization&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>compiler-construction-tools</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/compiler-construction-tools" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/compiler-construction-tools.md" />
    <summary>The Problem Building a compiler from scratch requires implementing scanners, parsers, code generators, and optimizers — each involving complex algorithms (finite automata, LR parsing, graph coloring).</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Building a compiler from scratch requires implementing scanners, parsers, code generators, and optimizers — each involving complex algorithms (finite automata, LR parsing, graph coloring). Writing these manually for every new compiler is repetitive, error-prone, and time-consuming.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Compiler construction tools are specialized software that automate parts of compiler development. &lt;strong&gt;Lex/Flex&lt;/strong&gt; generates lexical analyzers from regular expression specifications. &lt;strong&gt;Yacc/Bison&lt;/strong&gt; generates parsers from grammar specifications. Other tools handle code generation, optimization, and symbol table management.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The developer writes high-level specifications: regular expressions for tokens (Lex) and context-free grammar rules with actions (Yacc). The tool generates source code (typically C) that implements the scanner or parser. The generated code is compiled and linked with the rest of the compiler. This approach reduces development time and eliminates manual implementation errors.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: compiler_tools Pages: 1 --&gt;
&lt;svg width=&quot;254pt&quot; height=&quot;335pt&quot; viewBox=&quot;0.00 0.00 254.00 335.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 331.2)&quot;&gt;
&lt;title&gt;compiler_tools&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-331.2 250.04,-331.2 250.04,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- LexSpec --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;LexSpec&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;88.7,-327.2 22.67,-327.2 22.67,-290.4 88.7,-290.4 88.7,-327.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;55.69&quot; y=&quot;-312.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Lex Spec&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;55.69&quot; y=&quot;-298&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(.l file)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Flex --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Flex&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;111.37,-254.4 0,-254.4 0,-217.6 111.37,-217.6 111.37,-254.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;55.69&quot; y=&quot;-239.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Flex&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;55.69&quot; y=&quot;-225.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Lexer Generator)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- LexSpec&amp;#45;&amp;gt;Flex --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;LexSpec-&gt;Flex&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M55.69,-289.93C55.69,-282.67 55.69,-274.08 55.69,-265.97&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;59.19,-266.13 55.69,-256.13 52.19,-266.13 59.19,-266.13&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- YaccSpec --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;YaccSpec&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;224.37,-327.2 151,-327.2 151,-290.4 224.37,-290.4 224.37,-327.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;187.69&quot; y=&quot;-312.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Yacc Spec&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;187.69&quot; y=&quot;-298&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(.y file)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Yacc --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Yacc&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;246.04,-254.4 129.34,-254.4 129.34,-217.6 246.04,-217.6 246.04,-254.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;187.69&quot; y=&quot;-239.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Yacc/Bison&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;187.69&quot; y=&quot;-225.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Parser Generator)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- YaccSpec&amp;#45;&amp;gt;Yacc --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;YaccSpec-&gt;Yacc&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M187.69,-289.93C187.69,-282.67 187.69,-274.08 187.69,-265.97&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;191.19,-266.13 187.69,-256.13 184.19,-266.13 191.19,-266.13&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- LexerC --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;LexerC&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;110.69,-181.6 44.68,-181.6 44.68,-144.8 110.69,-144.8 110.69,-181.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.69&quot; y=&quot;-166.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;lex.yy.c&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.69&quot; y=&quot;-152.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(C Lexer)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Flex&amp;#45;&amp;gt;LexerC --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Flex-&gt;LexerC&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M61.24,-217.13C63.55,-209.7 66.29,-200.88 68.86,-192.6&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;72.14,-193.85 71.76,-183.26 65.45,-191.77 72.14,-193.85&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- ParserC --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;ParserC&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;211.35,-181.6 140.02,-181.6 140.02,-144.8 211.35,-144.8 211.35,-181.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;175.69&quot; y=&quot;-166.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;y.tab.c&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;175.69&quot; y=&quot;-152.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(C Parser)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Yacc&amp;#45;&amp;gt;ParserC --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Yacc-&gt;ParserC&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M184.66,-217.13C183.43,-209.87 181.97,-201.28 180.6,-193.17&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;184.05,-192.58 178.93,-183.31 177.15,-193.75 184.05,-192.58&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- CCompiler --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CCompiler&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;158.69,-108.8 82.68,-108.8 82.68,-72.8 158.69,-72.8 158.69,-108.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;120.69&quot; y=&quot;-87.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;C Compiler&lt;/text&gt;
&lt;/g&gt;
&lt;!-- LexerC&amp;#45;&amp;gt;CCompiler --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;LexerC-&gt;CCompiler&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M88.54,-144.44C93.33,-136.59 99.07,-127.2 104.36,-118.53&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;107.27,-120.48 109.5,-110.12 101.3,-116.83 107.27,-120.48&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- ParserC&amp;#45;&amp;gt;CCompiler --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;ParserC-&gt;CCompiler&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M161.81,-144.44C155.48,-136.33 147.86,-126.58 140.91,-117.69&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;143.84,-115.75 134.92,-110.02 138.32,-120.06 143.84,-115.75&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- CompilerExe --&gt;
&lt;g id=&quot;node8&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CompilerExe&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;152.69,-36.8 88.68,-36.8 88.68,0 152.69,0 152.69,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;120.69&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Your&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;120.69&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Compiler&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CCompiler&amp;#45;&amp;gt;CompilerExe --&gt;
&lt;g id=&quot;edge7&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CCompiler-&gt;CompilerExe&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M120.69,-72.4C120.69,-65.18 120.69,-56.6 120.69,-48.47&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;124.19,-48.6 120.69,-38.6 117.19,-48.6 124.19,-48.6&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Lexer generators:&lt;/strong&gt; Flex, Lex — input: regex patterns → output: DFA-based lexer in C&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parser generators:&lt;/strong&gt; Yacc, Bison — input: CFG grammar → output: LALR(1) parser in C&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reduces manual work:&lt;/strong&gt; Automatically generates complex automata algorithms&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Separation of concerns:&lt;/strong&gt; Developer focuses on language specification, not implementation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integrated tools:&lt;/strong&gt; Lex and Yacc are designed to work together — tokens defined in Lex are used in Yacc&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Compiler_Construction_Tools Pages: 1 --&gt;
&lt;svg width=&quot;203pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 203.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Compiler_Construction_Tools&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 199.44,-165.63 199.44,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;90.96&quot; cy=&quot;-86.21&quot; rx=&quot;90.96&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;90.96&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Compiler Constructio&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;134.85&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;134.85&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M104.85,-104.37C110.07,-111.21 116,-118.97 121.21,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;93.98&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;67&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;67&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M84.54,-67.93C81.05,-57.98 76.74,-45.73 73.27,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;108.66&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../lexical-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;lexical-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Lexical Analysis&lt;/a&gt; — Flex generates lexers that perform lexical analysis&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — Yacc generates parsers for syntax analysis&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../flex-lexical-analyzer-generator&quot; class=&quot;internal alias&quot; data-slug=&quot;flex-lexical-analyzer-generator&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Flex&lt;/a&gt; — Fast Lexical Analyzer Generator, the modern version of Lex&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../lr-parsers&quot; class=&quot;internal alias&quot; data-slug=&quot;lr-parsers&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LALR Parser&lt;/a&gt; — Yacc generates LALR(1) parsers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler&lt;/a&gt; — these tools are used to build compilers&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Tool dependencies:&lt;/strong&gt; Generated code often requires libraries from the tool (e.g., yacc’s yyparse needs yylex from lex)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LALR limitations:&lt;/strong&gt; Yacc uses LALR(1) which cannot handle all grammars — ambiguous or LR(1)-only grammars need workarounds&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Modern alternatives:&lt;/strong&gt; ANTLR generates LL(*) parsers for multiple languages; LLVM provides code generation IR tools&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>compiler-pass</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/compiler-pass" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/compiler-pass.md" />
    <summary>The Problem Compiler phases produce intermediate data (tokens, parse trees, IR) that must be stored between phases.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Compiler phases produce intermediate data (tokens, parse trees, IR) that must be stored between phases. Reading and writing this intermediate data to memory or disk has a cost. The compiler designer must decide how many times to scan the program and how much intermediate data to materialize.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A pass is a complete scan of the input (source code or intermediate representation) by a compiler. A &lt;strong&gt;single-pass&lt;/strong&gt; compiler processes the source code once, combining multiple phases into one scan. A &lt;strong&gt;multi-pass&lt;/strong&gt; compiler makes multiple scans, materializing intermediate representations between passes.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;In a single-pass compiler, lexical analysis, syntax analysis, and code generation are interleaved. As the parser recognizes a construct, it emits code immediately. In a multi-pass compiler, each phase runs as a separate pass, writing its output to a file or memory structure that the next pass reads. Pascal uses a single pass; C uses multiple passes.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: compiler_pass Pages: 1 --&gt;
&lt;svg width=&quot;323pt&quot; height=&quot;393pt&quot; viewBox=&quot;0.00 0.00 323.00 393.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 388.8)&quot;&gt;
&lt;title&gt;compiler_pass&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-388.8 319,-388.8 319,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;g id=&quot;clust1&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_single&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;8,-153.6 8,-376.8 166,-376.8 166,-153.6 8,-153.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87&quot; y=&quot;-360.2&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;Single-Pass Compiler&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;clust2&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_multi&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;174,-8 174,-376.8 307,-376.8 307,-8 174,-8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240.5&quot; y=&quot;-360.2&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;Multi-Pass Compiler&lt;/text&gt;
&lt;/g&gt;
&lt;!-- S1 --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;S1&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;114.01,-344 59.99,-344 59.99,-307.2 114.01,-307.2 114.01,-344&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87&quot; y=&quot;-329.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Source&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87&quot; y=&quot;-314.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Code&lt;/text&gt;
&lt;/g&gt;
&lt;!-- SPass --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;SPass&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;158.37,-271.2 15.63,-271.2 15.63,-234.4 158.37,-234.4 158.37,-271.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87&quot; y=&quot;-256.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;One Pass&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87&quot; y=&quot;-242&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Lex + Syn + CodeGen)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- S1&amp;#45;&amp;gt;SPass --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;S1-&gt;SPass&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M87,-306.73C87,-299.47 87,-290.88 87,-282.77&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;90.5,-282.93 87,-272.93 83.5,-282.93 90.5,-282.93&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- SOut --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;SOut&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;114,-198.4 60,-198.4 60,-161.6 114,-161.6 114,-198.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87&quot; y=&quot;-183.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Target&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87&quot; y=&quot;-169.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Code&lt;/text&gt;
&lt;/g&gt;
&lt;!-- SPass&amp;#45;&amp;gt;SOut --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;SPass-&gt;SOut&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M87,-233.93C87,-226.67 87,-218.08 87,-209.97&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;90.5,-210.13 87,-200.13 83.5,-210.13 90.5,-210.13&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- M1 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;M1&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;267.01,-344 212.99,-344 212.99,-307.2 267.01,-307.2 267.01,-344&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240&quot; y=&quot;-329.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Source&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240&quot; y=&quot;-314.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Code&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Pass1 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Pass1&lt;/title&gt;
&lt;polygon fill=&quot;#e8d4ff&quot; stroke=&quot;black&quot; points=&quot;278.85,-271.2 201.15,-271.2 201.15,-234.4 278.85,-234.4 278.85,-271.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240&quot; y=&quot;-256.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Pass 1&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240&quot; y=&quot;-242&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Lex + Syn)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- M1&amp;#45;&amp;gt;Pass1 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;M1-&gt;Pass1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M240,-306.73C240,-299.47 240,-290.88 240,-282.77&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;243.5,-282.93 240,-272.93 236.5,-282.93 243.5,-282.93&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- IR1 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;IR1&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;267,-198 213,-198 213,-162 267,-162 267,-198&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240&quot; y=&quot;-176.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;IR File&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Pass1&amp;#45;&amp;gt;IR1 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Pass1-&gt;IR1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M240,-233.93C240,-226.56 240,-217.82 240,-209.6&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;243.5,-209.64 240,-199.64 236.5,-209.64 243.5,-209.64&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Pass2 --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Pass2&lt;/title&gt;
&lt;polygon fill=&quot;#e8d4ff&quot; stroke=&quot;black&quot; points=&quot;279.85,-125.6 200.15,-125.6 200.15,-88.8 279.85,-88.8 279.85,-125.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240&quot; y=&quot;-110.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Pass 2&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240&quot; y=&quot;-96.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Opt + Gen)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- IR1&amp;#45;&amp;gt;Pass2 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;IR1-&gt;Pass2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M240,-161.86C240,-154.41 240,-145.46 240,-137.04&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;243.5,-137.19 240,-127.19 236.5,-137.19 243.5,-137.19&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- MOut --&gt;
&lt;g id=&quot;node8&quot; class=&quot;node&quot;&gt;
&lt;title&gt;MOut&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;267,-52.8 213,-52.8 213,-16 267,-16 267,-52.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240&quot; y=&quot;-38&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Target&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240&quot; y=&quot;-23.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Code&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Pass2&amp;#45;&amp;gt;MOut --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Pass2-&gt;MOut&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M240,-88.33C240,-81.07 240,-72.48 240,-64.37&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;243.5,-64.53 240,-54.53 236.5,-64.53 243.5,-64.53&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Single-pass:&lt;/strong&gt; Faster compilation, less memory, tighter coupling of phases&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multi-pass:&lt;/strong&gt; Better code quality, modular compiler design, supports optimization&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Language constraints:&lt;/strong&gt; Some languages require multi-pass (forward references, C requires seeing struct definitions before use)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Intermediate files:&lt;/strong&gt; Multi-pass compilers read/write intermediate representation between passes&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Compiler_Pass Pages: 1 --&gt;
&lt;svg width=&quot;179pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 179.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Compiler_Pass&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 175.05,-165.63 175.05,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;66.56&quot; cy=&quot;-86.21&quot; rx=&quot;66.56&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;66.56&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Compiler Pass&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;110.46&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;110.46&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M80.21,-104.06C85.54,-111.04 91.65,-119.02 96.97,-125.98&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.55&quot; y=&quot;-119.22&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;42.6&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;42.6&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M60.14,-67.93C56.65,-57.98 52.34,-45.73 48.88,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;84.26&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — passes group phases into scans&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../single-pass-vs-multi-pass&quot; class=&quot;internal alias&quot; data-slug=&quot;single-pass-vs-multi-pass&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Single Pass vs Multi-Pass Compiler&lt;/a&gt; — synthesis comparing the two strategies&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler&lt;/a&gt; — the overall architecture of a compiler&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — optimization typically requires multiple passes for effective transformation&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Hybrid approaches:&lt;/strong&gt; Modern compilers like GCC and LLVM are multi-pass but use efficient in-memory IR, not files between passes&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pascal is single-pass:&lt;/strong&gt; Pascal was designed specifically to allow single-pass compilation — no forward references without explicit forward declaration&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Multi-pass enables optimization:&lt;/strong&gt; Dead code elimination, constant propagation, and loop transformations all require multiple passes to analyze&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>Compiler vs Interpreter</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/compiler-vs-interpreter" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/compiler-vs-interpreter.md" />
    <summary>What’s Being Compared Both compilers and interpreters translate high-level language to executable form, but they differ fundamentally in when and how translation happens.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;whats-being-compared&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;What’s Being Compared&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#whats-being-compared&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Both compilers and interpreters translate high-level language to executable form, but they differ fundamentally in when and how translation happens. This distinction shapes language design, tooling, debugging, and deployment.&lt;/p&gt;
&lt;h2 id=&quot;the-core-tension&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Core Tension&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-core-tension&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A compiler translates the entire source program before execution, producing a separate executable. An interpreter translates and executes the program line by line during execution. The trade-off is between runtime performance (compiler) and development flexibility (interpreter).&lt;/p&gt;
&lt;h2 id=&quot;comparison&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Comparison&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#comparison&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Dimension&lt;/th&gt;&lt;th&gt;&lt;a href=&quot;../../compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler&lt;/a&gt;&lt;/th&gt;&lt;th&gt;&lt;a href=&quot;../../compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Interpreter&lt;/a&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Translation&lt;/td&gt;&lt;td&gt;Entire program ahead of time&lt;/td&gt;&lt;td&gt;Line by line during execution&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Execution speed&lt;/td&gt;&lt;td&gt;Fast (pre-compiled)&lt;/td&gt;&lt;td&gt;Slow (translation overhead at runtime)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Output&lt;/td&gt;&lt;td&gt;Separate executable file&lt;/td&gt;&lt;td&gt;No separate file — runs immediately&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Debugging&lt;/td&gt;&lt;td&gt;Harder (disconnected from source)&lt;/td&gt;&lt;td&gt;Easier (direct source interaction)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Error detection&lt;/td&gt;&lt;td&gt;Before execution (compile time)&lt;/td&gt;&lt;td&gt;During execution (runtime)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Memory usage&lt;/td&gt;&lt;td&gt;Lower at runtime&lt;/td&gt;&lt;td&gt;Higher (translator stays in memory)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Portability&lt;/td&gt;&lt;td&gt;Recompile for each target&lt;/td&gt;&lt;td&gt;Run interpreter on each target&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Security&lt;/td&gt;&lt;td&gt;Source not distributed&lt;/td&gt;&lt;td&gt;Source (or bytecode) must be present&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;when-to-choose-compiler&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;When to Choose Compiler&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#when-to-choose-compiler&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;Performance-critical applications&lt;/li&gt;
&lt;li&gt;Production deployment (fast startup and execution)&lt;/li&gt;
&lt;li&gt;Applications distributed without source&lt;/li&gt;
&lt;li&gt;Languages with static typing and compile-time checks&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;when-to-choose-interpreter&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;When to Choose Interpreter&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#when-to-choose-interpreter&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;Rapid prototyping and development&lt;/li&gt;
&lt;li&gt;Scripting and glue code&lt;/li&gt;
&lt;li&gt;Interactive programming environments (REPLs)&lt;/li&gt;
&lt;li&gt;Educational settings&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-insight&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Insight&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-insight&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Modern languages blur the distinction — Java is compiled to bytecode (by javac) then interpreted/JIT-compiled by the JVM. Python compiles to bytecode (.pyc) before interpretation. The practical reality is a spectrum from pure compilation (C, Go) through JIT compilation (Java, C#) to pure interpretation (bash, early Python).&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: compiler_vs_interpreter Pages: 1 --&gt;
&lt;svg width=&quot;606pt&quot; height=&quot;50pt&quot; viewBox=&quot;0.00 0.00 606.00 50.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 45.6)&quot;&gt;
&lt;title&gt;compiler_vs_interpreter&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-45.6 602.44,-45.6 602.44,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- A --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;A&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;144.38,-41.6 0,-41.6 0,0 144.38,0 144.38,-41.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;72.19&quot; y=&quot;-25&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Compiler Vs Interpre&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;72.19&quot; y=&quot;-8.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Input&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;367.52,-41.6 223.14,-41.6 223.14,0 367.52,0 367.52,-41.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;295.33&quot; y=&quot;-25&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Compiler Vs Interpre&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;295.33&quot; y=&quot;-8.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Core Mechanism&lt;/text&gt;
&lt;/g&gt;
&lt;!-- A&amp;#45;&amp;gt;B --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;A-&gt;B&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M144.68,-20.8C165.96,-20.8 189.45,-20.8 211.39,-20.8&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;211.19,-24.3 221.19,-20.8 211.19,-17.3 211.19,-24.3&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;183.76&quot; y=&quot;-25&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;triggers&lt;/text&gt;
&lt;/g&gt;
&lt;!-- C --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;C&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;598.44,-41.6 454.06,-41.6 454.06,0 598.44,0 598.44,-41.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;526.25&quot; y=&quot;-25&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Compiler Vs Interpre&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;526.25&quot; y=&quot;-8.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Output&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B&amp;#45;&amp;gt;C --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B-&gt;C&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M367.82,-20.8C391.49,-20.8 418.03,-20.8 442.47,-20.8&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;442.4,-24.3 452.4,-20.8 442.4,-17.3 442.4,-24.3&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;410.79&quot; y=&quot;-25&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;produces&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_compiler_vs_interpreter Pages: 1 --&gt;
&lt;svg width=&quot;202pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 202.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_compiler_vs_interpreter&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 198.01,-165.63 198.01,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;89.52&quot; cy=&quot;-86.21&quot; rx=&quot;89.52&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;89.52&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Compiler Vs Interpre&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;133.42&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;133.42&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M103.41,-104.37C108.64,-111.21 114.57,-118.97 119.77,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;92.55&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;65.57&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;65.57&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M83.1,-67.93C79.61,-57.98 75.31,-45.73 71.84,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;107.23&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;a href=&quot;../../compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler&lt;/a&gt; — the compiled approach, left side of comparison&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — compilation involves multiple distinct phases&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../compiler-pass&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler-pass&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler Pass&lt;/a&gt; — how compilers organize translation into passes&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../intermediate-code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;intermediate-code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Intermediate Code Generation&lt;/a&gt; — bytecode is a form of IR used by both compilers and interpreters&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../runtime-environment&quot; class=&quot;internal alias&quot; data-slug=&quot;runtime-environment&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Runtime Environment&lt;/a&gt; — interpreters require a runtime environment while executing&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>compiler</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/compiler" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/compiler.md" />
    <summary>The Problem High-level programming languages (C, C++, Java, Python) are human-readable but machines only understand binary machine code.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;High-level programming languages (C, C++, Java, Python) are human-readable but machines only understand binary machine code. Without a compiler, every program would need to be written in assembly or machine code — a tedious, error-prone, and non-portable process.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A compiler is software that translates a program written in a high-level source language into an equivalent low-level target language (machine code or assembly). It acts as the bridge between human-readable code and machine-understandable instructions, automating translation, checking correctness, and reporting errors.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A compiler operates in multiple phases organized into two major parts: the &lt;strong&gt;analysis (front-end)&lt;/strong&gt; and &lt;strong&gt;synthesis (back-end)&lt;/strong&gt;. The front-end analyzes the source program and creates an intermediate representation. The back-end uses this representation to generate target code. The phases include lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and final code generation.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: compiler_flow Pages: 1 --&gt;
&lt;svg width=&quot;146pt&quot; height=&quot;384pt&quot; viewBox=&quot;0.00 0.00 146.00 384.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 380)&quot;&gt;
&lt;title&gt;compiler_flow&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-380 142.08,-380 142.08,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Source --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Source&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;138.08,-376 0,-376 0,-339.2 138.08,-339.2 138.08,-376&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.04&quot; y=&quot;-361.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Source Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.04&quot; y=&quot;-346.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(High-Level Language)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Frontend --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Frontend&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;103.39,-291.2 34.69,-291.2 34.69,-254.4 103.39,-254.4 103.39,-291.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.04&quot; y=&quot;-276.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Front End&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.04&quot; y=&quot;-262&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Analysis)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Source&amp;#45;&amp;gt;Frontend --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Source-&gt;Frontend&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M69.04,-339.04C69.04,-328.57 69.04,-315.02 69.04,-302.98&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;72.54,-303.13 69.04,-293.13 65.54,-303.13 72.54,-303.13&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;79.88&quot; y=&quot;-312.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;input&lt;/text&gt;
&lt;/g&gt;
&lt;!-- IR --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;IR&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;117.73,-206.4 20.35,-206.4 20.35,-169.6 117.73,-169.6 117.73,-206.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.04&quot; y=&quot;-191.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Intermediate&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.04&quot; y=&quot;-177.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Representation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Frontend&amp;#45;&amp;gt;IR --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Frontend-&gt;IR&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M69.04,-254.24C69.04,-243.77 69.04,-230.22 69.04,-218.18&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;72.54,-218.33 69.04,-208.33 65.54,-218.33 72.54,-218.33&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;89.61&quot; y=&quot;-227.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;produces&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Backend --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Backend&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;107.05,-121.6 31.03,-121.6 31.03,-84.8 107.05,-84.8 107.05,-121.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.04&quot; y=&quot;-106.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Back End&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.04&quot; y=&quot;-92.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Synthesis)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- IR&amp;#45;&amp;gt;Backend --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;IR-&gt;Backend&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M69.04,-169.44C69.04,-158.97 69.04,-145.42 69.04,-133.38&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;72.54,-133.53 69.04,-123.53 65.54,-133.53 72.54,-133.53&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;79.88&quot; y=&quot;-142.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;input&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Target --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Target&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;131.39,-36.8 6.69,-36.8 6.69,0 131.39,0 131.39,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.04&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Target Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.04&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Machine/Assembly)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Backend&amp;#45;&amp;gt;Target --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Backend-&gt;Target&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M69.04,-84.64C69.04,-74.17 69.04,-60.62 69.04,-48.58&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;72.54,-48.73 69.04,-38.73 65.54,-48.73 72.54,-48.73&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;89.61&quot; y=&quot;-57.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;produces&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_compiler Pages: 1 --&gt;
&lt;svg width=&quot;342pt&quot; height=&quot;209pt&quot; viewBox=&quot;0.00 0.00 342.00 209.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 205.07)&quot;&gt;
&lt;title&gt;semantic_compiler&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-205.07 338.33,-205.07 338.33,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;193.66&quot; cy=&quot;-100.51&quot; rx=&quot;45.03&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;193.66&quot; y=&quot;-96.61&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Compiler&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE1&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;265.05&quot; cy=&quot;-59.01&quot; rx=&quot;69.28&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;265.05&quot; y=&quot;-55.71&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Phases of Compiler&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M219.43,-85.53C224.86,-82.37 230.63,-79.02 236.17,-75.8&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;246.3&quot; y=&quot;-83.36&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE2&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;193.49&quot; cy=&quot;-18&quot; rx=&quot;58.31&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;193.49&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Lexical Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M193.62,-82.2C193.59,-68.53 193.56,-49.92 193.53,-36.26&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;175.07&quot; y=&quot;-61.93&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT1 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT1&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;122.91&quot; cy=&quot;-142.96&quot; rx=&quot;61.73&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;122.91&quot; y=&quot;-139.66&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Code Generation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT1 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M168.52,-115.6C162.64,-119.13 156.35,-122.9 150.37,-126.48&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;139.18&quot; y=&quot;-112.94&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CON1 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CON1&lt;/title&gt;
&lt;ellipse fill=&quot;#ffe5cc&quot; stroke=&quot;black&quot; cx=&quot;265.63&quot; cy=&quot;-141.05&quot; rx=&quot;41.29&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;265.63&quot; y=&quot;-137.75&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Interpreter&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;CON1 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--CON1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;1 5&quot; d=&quot;M220.05,-115.38C226.63,-119.08 233.67,-123.05 240.2,-126.73&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;257.64&quot; y=&quot;-112.95&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;contrasts with&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;195.09&quot; cy=&quot;-183.07&quot; rx=&quot;95.75&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;195.09&quot; y=&quot;-179.77&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Compiler Construction Tools&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M193.98,-118.83C194.21,-132.52 194.54,-151.13 194.78,-164.8&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;208.13&quot; y=&quot;-144.52&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;121.87&quot; cy=&quot;-59.9&quot; rx=&quot;121.87&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;121.87&quot; y=&quot;-56.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Programming Language Generations&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M167.34,-85.62C162.69,-82.99 157.82,-80.24 153.06,-77.54&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;146.44&quot; y=&quot;-84.28&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Two-part architecture:&lt;/strong&gt; Front-end (analysis) + Back-end (synthesis)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Language translation:&lt;/strong&gt; Source language → Target language (not execution)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error detection:&lt;/strong&gt; Reports lexical, syntactic, and semantic errors&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Portability:&lt;/strong&gt; Same source can be compiled for different target machines&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optimization:&lt;/strong&gt; Can improve code quality without changing semantics&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — the compiler’s internal pipeline is organized into distinct phases&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../lexical-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;lexical-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Lexical Analysis&lt;/a&gt; — first phase that breaks source into tokens&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — verifies grammatical structure during compilation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../compiler-vs-interpreter&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler-vs-interpreter&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler vs Interpreter&lt;/a&gt; — compiler translates then stops; interpreter executes as it translates&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../wiki/compilerdesign/compiler-construction-tools&quot; class=&quot;internal alias&quot; data-slug=&quot;wiki/compilerdesign/compiler-construction-tools&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler Construction Tools&lt;/a&gt; — tools like Lex and Yacc automate parts of compiler creation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../programming-language-generations&quot; class=&quot;internal alias&quot; data-slug=&quot;programming-language-generations&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Programming Language Generations&lt;/a&gt; — compilers are essential for higher-generation languages&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Compiler vs Cross-Compiler:&lt;/strong&gt; A compiler that runs on one platform but generates code for a different platform is a cross-compiler&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Just-In-Time Compilation:&lt;/strong&gt; Modern JVMs use JIT compilation — bytecode is compiled to native code at runtime, blurring the line between compiler and interpreter&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Incremental Compilation:&lt;/strong&gt; Not all compilers recompile everything — many (like javac) support incremental compilation for faster development cycles&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>constant-propagation</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/constant-propagation" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/constant-propagation.md" />
    <summary>The Problem Source code often contains expressions whose operands are all known at compile time — either literal constants or variables that can only hold one value.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Source code often contains expressions whose operands are all known at compile time — either literal constants or variables that can only hold one value. Evaluating these at runtime wastes cycles. The compiler should replace them with their computed values and propagate those values through subsequent uses.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Constant propagation replaces variables whose values are known at compile time with their constant values, and constant folding evaluates constant expressions at compile time. If &lt;code class=&quot;notranslate&quot;&gt;x = 5&lt;/code&gt; and later &lt;code class=&quot;notranslate&quot;&gt;y = x * 2&lt;/code&gt;, the compiler replaces this with &lt;code class=&quot;notranslate&quot;&gt;y = 10&lt;/code&gt;. This eliminates runtime computation and often enables further optimizations (dead code elimination, branch elimination).&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The compiler tracks which variables hold known constant values at each program point. It starts by identifying assignments of constants to variables (&lt;code class=&quot;notranslate&quot;&gt;x = 5&lt;/code&gt;). For each subsequent use of &lt;code class=&quot;notranslate&quot;&gt;x&lt;/code&gt;, if no intervening assignment has changed &lt;code class=&quot;notranslate&quot;&gt;x&lt;/code&gt;, it replaces &lt;code class=&quot;notranslate&quot;&gt;x&lt;/code&gt; with &lt;code class=&quot;notranslate&quot;&gt;5&lt;/code&gt;. When a constant expression is formed (e.g., &lt;code class=&quot;notranslate&quot;&gt;5 * 2&lt;/code&gt;), constant folding evaluates it at compile time (&lt;code class=&quot;notranslate&quot;&gt;10&lt;/code&gt;). This propagates forward — simplifications create more opportunities for propagation. Reaching-definitions analysis determines which assignments reach which uses.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: constant_prop Pages: 1 --&gt;
&lt;svg width=&quot;163pt&quot; height=&quot;350pt&quot; viewBox=&quot;0.00 0.00 163.00 350.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 345.6)&quot;&gt;
&lt;title&gt;constant_prop&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-345.6 159.41,-345.6 159.41,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Before --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Before&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;140.91,-341.6 14.5,-341.6 14.5,-261.6 140.91,-261.6 140.91,-341.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.71&quot; y=&quot;-326.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Before CP+CF:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.71&quot; y=&quot;-312.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  pi = 3.14&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.71&quot; y=&quot;-298&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  r  = 5&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.71&quot; y=&quot;-283.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  area = pi * r * r&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.71&quot; y=&quot;-269.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  if area &gt; 100 goto L&lt;/text&gt;
&lt;/g&gt;
&lt;!-- After --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;After&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;124.73,-225.6 30.68,-225.6 30.68,-145.6 124.73,-145.6 124.73,-225.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.71&quot; y=&quot;-210.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;After CP+CF:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.71&quot; y=&quot;-196.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  pi = 3.14&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.71&quot; y=&quot;-182&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  r  = 5&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.71&quot; y=&quot;-167.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  area = 78.5&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.71&quot; y=&quot;-153.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  if false goto L&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Before&amp;#45;&amp;gt;After --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Before-&gt;After&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M77.71,-261.37C77.71,-253.58 77.71,-245.29 77.71,-237.18&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;81.21,-237.42 77.71,-227.42 74.21,-237.42 81.21,-237.42&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Folded --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Folded&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;155.41,-109.6 0,-109.6 0,-72.8 155.41,-72.8 155.41,-109.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.71&quot; y=&quot;-94.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Further:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.71&quot; y=&quot;-80.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  (dead branch eliminated)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- After&amp;#45;&amp;gt;Folded --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;After-&gt;Folded&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M77.71,-145.15C77.71,-137.03 77.71,-128.64 77.71,-120.99&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;81.21,-121.24 77.71,-111.24 74.21,-121.24 81.21,-121.24&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Eliminated --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Eliminated&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;119.56,-36.8 35.85,-36.8 35.85,0 119.56,0 119.56,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.71&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Result:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.71&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  area = 78.5&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Folded&amp;#45;&amp;gt;Eliminated --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Folded-&gt;Eliminated&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M77.71,-72.33C77.71,-65.07 77.71,-56.48 77.71,-48.37&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;81.21,-48.53 77.71,-38.53 74.21,-48.53 81.21,-48.53&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_const_prop Pages: 1 --&gt;
&lt;svg width=&quot;583pt&quot; height=&quot;206pt&quot; viewBox=&quot;0.00 0.00 583.00 206.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 202.21)&quot;&gt;
&lt;title&gt;semantic_const_prop&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-202.21 579.01,-202.21 579.01,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;336.5&quot; cy=&quot;-92.7&quot; rx=&quot;60.89&quot; ry=&quot;27.72&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;336.5&quot; y=&quot;-96.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Constant&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;336.5&quot; y=&quot;-81&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Propagation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE1&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;388.36&quot; cy=&quot;-30.16&quot; rx=&quot;54.54&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;388.36&quot; y=&quot;-33.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;388.36&quot; y=&quot;-20.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Optimization&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M358.19,-66.54C361.86,-62.12 365.64,-57.56 369.23,-53.23&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;345.21&quot; y=&quot;-51.79&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE2&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;292.74&quot; cy=&quot;-24.32&quot; rx=&quot;46.33&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;292.74&quot; y=&quot;-27.62&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Data Flow&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;292.74&quot; y=&quot;-14.42&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M319.21,-65.68C315.38,-59.71 311.37,-53.43 307.65,-47.62&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;239.89&quot; y=&quot;-59.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from -- uses reaching definitions&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CON1 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CON1&lt;/title&gt;
&lt;ellipse fill=&quot;#ffe5cc&quot; stroke=&quot;black&quot; cx=&quot;258.16&quot; cy=&quot;-114.05&quot; rx=&quot;76.16&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;258.16&quot; y=&quot;-117.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Common Subexpr.&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;258.16&quot; y=&quot;-104.15&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Elimination&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;CON1 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--CON1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M283.81,-107.06C283.7,-107.09 283.59,-107.12 283.48,-107.15&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;141.82&quot; y=&quot;-109.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;contrasts with -- CP is about constant values, not repeated expressions&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;412.29&quot; cy=&quot;-122.16&quot; rx=&quot;54.54&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;412.29&quot; y=&quot;-125.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Peephole&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;412.29&quot; y=&quot;-112.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Optimization&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M383.2,-110.86C383.32,-110.9 383.45,-110.95 383.57,-111&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;479.2&quot; y=&quot;-113.63&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related -- peephole can do local constant folding&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;332.67&quot; cy=&quot;-173.89&quot; rx=&quot;34.22&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;332.67&quot; y=&quot;-177.19&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Basic&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;332.67&quot; y=&quot;-163.99&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Blocks&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M335.18,-120.64C334.74,-129.91 334.26,-140.15 333.84,-149.16&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;257.23&quot; y=&quot;-137.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related -- local CP works within a block&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Constant propagation:&lt;/strong&gt; Replacing variable uses with known constant values&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Constant folding:&lt;/strong&gt; Evaluating constant expressions at compile time (e.g., &lt;code class=&quot;notranslate&quot;&gt;2 * 3.14&lt;/code&gt; → &lt;code class=&quot;notranslate&quot;&gt;6.28&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cascading:&lt;/strong&gt; CP creates more constant expressions, CF evaluates them, creating more CP opportunities&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reaching definitions:&lt;/strong&gt; Used to determine which assignments reach which variable uses&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Conditional branches:&lt;/strong&gt; CP can simplify conditional expressions, enabling dead branch elimination&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — CP/CF is a fundamental optimization technique&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../data-flow-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;data-flow-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Data Flow Analysis&lt;/a&gt; — reaching-definitions analysis drives global CP&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../common-subexpression-elimination&quot; class=&quot;internal alias&quot; data-slug=&quot;common-subexpression-elimination&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Common Subexpression Elimination&lt;/a&gt; — CP simplifies constant expressions; CSE eliminates redundant computations&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../peephole-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;peephole-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Peephole Optimization&lt;/a&gt; — local constant folding can be done as a peephole optimization on target code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../basic-blocks&quot; class=&quot;internal alias&quot; data-slug=&quot;basic-blocks&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Basic Blocks&lt;/a&gt; — local CP works within a single block; global CP needs the CFG&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Over-approximation:&lt;/strong&gt; The analysis must be conservative — if a variable might have been modified (e.g., through a pointer), CP cannot assume its previous constant value&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Conditional constant propagation:&lt;/strong&gt; When a variable is constant on one branch but not another, the analysis must handle this precisely&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Sparse conditional constant propagation (SCCP):&lt;/strong&gt; A more powerful form that simultaneously tracks constants and reachability&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Not always beneficial:&lt;/strong&gt; Propagating a constant may increase code size (different constants propagated to different uses) without runtime benefit&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>context-free-grammar</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/context-free-grammar" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/context-free-grammar.md" />
    <summary>The Problem Natural language descriptions of syntax (“an if statement consists of the keyword if, followed by a parenthesized expression, followed by a statement”) are imprecise, ambiguous, and cannot be processed by software.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Natural language descriptions of syntax (“an if statement consists of the keyword if, followed by a parenthesized expression, followed by a statement”) are imprecise, ambiguous, and cannot be processed by software. The compiler needs a formal, precise, and machine-readable specification of the language’s syntax.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A context-free grammar (CFG) is a formal system for specifying the syntax of a programming language. It consists of a set of &lt;strong&gt;production rules&lt;/strong&gt; of the form A → α, where A is a non-terminal and α is a string of terminals and non-terminals. CFGs are the input specification for parser generators and form the foundation of syntax analysis.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A CFG has four components: a set of &lt;strong&gt;terminals&lt;/strong&gt; (tokens), a set of &lt;strong&gt;non-terminals&lt;/strong&gt; (syntactic variables), a &lt;strong&gt;start symbol&lt;/strong&gt; (top-level non-terminal), and &lt;strong&gt;productions&lt;/strong&gt; (rules). For example, &lt;code class=&quot;notranslate&quot;&gt;E → E + T | T&lt;/code&gt; and &lt;code class=&quot;notranslate&quot;&gt;T → id | num&lt;/code&gt;. The parser uses these rules to derive the source program through a sequence of replacements. The set of all strings derivable from the start symbol is the language defined by the grammar.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: cfg Pages: 1 --&gt;
&lt;svg width=&quot;592pt&quot; height=&quot;318pt&quot; viewBox=&quot;0.00 0.00 592.00 318.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 313.6)&quot;&gt;
&lt;title&gt;cfg&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-313.6 588,-313.6 588,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;g id=&quot;clust1&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_cfg&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;8,-195.2 8,-301.6 420,-301.6 420,-195.2 8,-195.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;214&quot; y=&quot;-285&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;CFG Components&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Term --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Term&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;84.34,-254.4 15.66,-254.4 15.66,-217.6 84.34,-217.6 84.34,-254.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;50&quot; y=&quot;-239.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Terminals&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;50&quot; y=&quot;-225.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(id, +, *, ;)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- NonTerm --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;NonTerm&lt;/title&gt;
&lt;polygon fill=&quot;#e8d4ff&quot; stroke=&quot;black&quot; points=&quot;197.34,-254.4 102.66,-254.4 102.66,-217.6 197.34,-217.6 197.34,-254.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;150&quot; y=&quot;-239.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Non-Terminals&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;150&quot; y=&quot;-225.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(E, T, F)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Prod --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Prod&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;294.68,-268.8 215.32,-268.8 215.32,-203.2 294.68,-203.2 294.68,-268.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;255&quot; y=&quot;-254&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Productions&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;255&quot; y=&quot;-239.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E → E + T&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;255&quot; y=&quot;-225.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E → T&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;255&quot; y=&quot;-210.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;T → id&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Start --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Start&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;411.68,-254 312.32,-254 312.32,-218 411.68,-218 411.68,-254&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;362&quot; y=&quot;-232.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Start Symbol: E&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Parser&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;564.35,-167.2 463.65,-167.2 463.65,-116 564.35,-116 564.35,-167.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;514&quot; y=&quot;-152.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parser&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;514&quot; y=&quot;-138&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(uses CFG to&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;514&quot; y=&quot;-123.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;derive program)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Derivation --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Derivation&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;550.68,-80 477.32,-80 477.32,0 550.68,0 550.68,-80&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;514&quot; y=&quot;-65.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Derivation:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;514&quot; y=&quot;-50.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E ⇒ E+T&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;514&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  ⇒ T+T&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;514&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  ⇒ id+T&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;514&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  ⇒ id+id&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;Derivation --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;Derivation&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M514,-115.72C514,-108.36 514,-100.01 514,-91.63&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;517.5,-91.78 514,-81.78 510.5,-91.78 517.5,-91.78&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Input --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Input&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;511.86,-254 430.14,-254 430.14,-218 511.86,-218 511.86,-254&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;471&quot; y=&quot;-232.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Input: id + id&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Input&amp;#45;&amp;gt;Parser --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Input-&gt;Parser&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M479.09,-217.62C484.32,-206.37 491.32,-191.33 497.68,-177.67&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;500.69,-179.49 501.74,-168.95 494.35,-176.54 500.69,-179.49&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- CFG --&gt;
&lt;g id=&quot;node8&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CFG&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;584,-254 530,-254 530,-218 584,-218 584,-254&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;557&quot; y=&quot;-232.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;CFG&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CFG&amp;#45;&amp;gt;Parser --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CFG-&gt;Parser&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M548.91,-217.62C543.68,-206.37 536.68,-191.33 530.32,-177.67&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;533.65,-176.54 526.26,-168.95 527.31,-179.49 533.65,-176.54&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Formal definition:&lt;/strong&gt; G = (V, Σ, R, S) — non-terminals, terminals, productions, start symbol&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;BNF notation:&lt;/strong&gt; Backus-Naur Form is the standard notation for writing CFGs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Derivation:&lt;/strong&gt; Replacing non-terminals with right-hand sides of productions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Parse tree:&lt;/strong&gt; Graphical representation of a derivation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Language:&lt;/strong&gt; Set of all strings derivable from the start symbol&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Context_Free_Grammar Pages: 1 --&gt;
&lt;svg width=&quot;212pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 212.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Context_Free_Grammar&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 207.57,-165.63 207.57,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;99.08&quot; cy=&quot;-86.21&quot; rx=&quot;99.08&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;99.08&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Context Free Grammar&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;142.98&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;142.98&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M112.97,-104.37C118.2,-111.21 124.13,-118.97 129.33,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;102.11&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;75.13&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;75.13&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M92.66,-67.93C89.17,-57.98 84.87,-45.73 81.4,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;116.78&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — the parser uses a CFG to validate program structure&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../wiki/compilerdesign/ambiguous-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;wiki/compilerdesign/ambiguous-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Ambiguous Grammar&lt;/a&gt; — a grammar that produces multiple parse trees for the same input&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../top-down-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;top-down-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Top-Down Parsing&lt;/a&gt; — top-down parsers follow leftmost derivations from a CFG&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../bottom-up-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;bottom-up-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Bottom-Up Parsing&lt;/a&gt; — bottom-up parsers compute reverse rightmost derivations from a CFG&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../first-and-follow-sets&quot; class=&quot;internal alias&quot; data-slug=&quot;first-and-follow-sets&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;FIRST and FOLLOW Sets&lt;/a&gt; — computed from CFG to guide parser table construction&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../context-free-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;context-free-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Context-Free Grammar&lt;/a&gt; — CFGs are categorized by production rule structure into regular, context-free, context-sensitive, and unrestricted&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Left recursion:&lt;/strong&gt; Top-down parsers enter infinite loops with left-recursive productions (A → Aα)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ambiguity:&lt;/strong&gt; A grammar may be ambiguous even though the language is not — the grammar must be rewritten&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Grammar transformations:&lt;/strong&gt; Left recursion elimination and left factoring are common transformations to make grammars parseable&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>control-flow-graph</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/control-flow-graph" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/control-flow-graph.md" />
    <summary>The Problem A basic block captures straight-line code, but programs have branches, loops, and conditional execution.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A basic block captures straight-line code, but programs have branches, loops, and conditional execution. The compiler needs a global view of how control flows between blocks to perform inter-block optimizations, data-flow analysis, and loop detection. Without a control-flow graph, each block is an island — no analysis can cross block boundaries.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A control-flow graph (CFG) is a directed graph where nodes are &lt;strong&gt;basic blocks&lt;/strong&gt; and edges represent potential control flow paths. There is a directed edge from block A to block B if control can pass from the last instruction of A to the first instruction of B (via jump, fall-through, or call). The CFG has a unique &lt;strong&gt;entry node&lt;/strong&gt; (start of the program) and typically one or more &lt;strong&gt;exit nodes&lt;/strong&gt; (program termination points).&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The CFG is constructed after basic block identification. For each block, the compiler examines its last instruction: if it ends with a conditional jump &lt;code class=&quot;notranslate&quot;&gt;if X goto L&lt;/code&gt;, edges go to the block starting with label &lt;code class=&quot;notranslate&quot;&gt;L&lt;/code&gt; and to the next block (fall-through). If it ends with an unconditional jump &lt;code class=&quot;notranslate&quot;&gt;goto L&lt;/code&gt;, a single edge goes to L’s block. If it ends with a return, it’s an exit node. The resulting graph is the framework for all global compiler analysis and optimization.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: cfg_example Pages: 1 --&gt;
&lt;svg width=&quot;149pt&quot; height=&quot;438pt&quot; viewBox=&quot;0.00 0.00 149.00 438.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 433.6)&quot;&gt;
&lt;title&gt;cfg_example&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-433.6 144.86,-433.6 144.86,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Entry --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Entry&lt;/title&gt;
&lt;polygon fill=&quot;#90ee90&quot; stroke=&quot;black&quot; points=&quot;96.01,-429.6 42.01,-429.6 42.01,-393.6 96.01,-393.6 96.01,-429.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.01&quot; y=&quot;-408&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Entry&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B1&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; points=&quot;96.01,-356.6 42.01,-356.6 42.01,-305.4 96.01,-305.4 96.01,-356.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.01&quot; y=&quot;-341.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B1:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.01&quot; y=&quot;-327.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;x = 1&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.01&quot; y=&quot;-313&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;y = 2&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Entry&amp;#45;&amp;gt;B1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Entry-&gt;B1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M69.01,-393.16C69.01,-385.82 69.01,-376.99 69.01,-368.35&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;72.51,-368.36 69.01,-358.36 65.51,-368.36 72.51,-368.36&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- B2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B2&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;117.04,-268.4 20.98,-268.4 20.98,-231.6 117.04,-231.6 117.04,-268.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.01&quot; y=&quot;-253.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B2:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.01&quot; y=&quot;-239.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;if cond goto B4&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B1&amp;#45;&amp;gt;B2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B1-&gt;B2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M69.01,-305C69.01,-297.11 69.01,-288.31 69.01,-280.15&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;72.51,-280.34 69.01,-270.34 65.51,-280.34 72.51,-280.34&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- B3 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B3&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;62.03,-183.6 0,-183.6 0,-146.8 62.03,-146.8 62.03,-183.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;31.01&quot; y=&quot;-168.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B3:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;31.01&quot; y=&quot;-154.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;x = x + 1&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B2&amp;#45;&amp;gt;B3 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B2-&gt;B3&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M43.11,-231.32C37.71,-226.28 32.76,-220.31 29.78,-213.6 27.3,-208.01 26.36,-201.67 26.28,-195.48&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;29.77,-195.73 26.9,-185.53 22.78,-195.29 29.77,-195.73&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;38.4&quot; y=&quot;-204.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;true&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B4 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B4&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;140.86,-183.6 81.17,-183.6 81.17,-146.8 140.86,-146.8 140.86,-183.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;111.01&quot; y=&quot;-168.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B4:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;111.01&quot; y=&quot;-154.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;y = x * 2&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B2&amp;#45;&amp;gt;B4 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B2-&gt;B4&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M77.92,-231.44C83.4,-220.65 90.53,-206.57 96.78,-194.26&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;99.89,-195.87 101.29,-185.37 93.64,-192.7 99.89,-195.87&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;103.09&quot; y=&quot;-204.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;false&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B3&amp;#45;&amp;gt;B2 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B3-&gt;B2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M39.19,-184.02C44.17,-194.87 50.63,-208.95 56.27,-221.24&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;52.97,-222.44 60.32,-230.06 59.33,-219.52 52.97,-222.44&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.74&quot; y=&quot;-204.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;loop&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B5 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B5&lt;/title&gt;
&lt;polygon fill=&quot;#ffcccc&quot; stroke=&quot;black&quot; points=&quot;139.35,-109.8 82.67,-109.8 82.67,-73 139.35,-73 139.35,-109.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;111.01&quot; y=&quot;-95&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B5:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;111.01&quot; y=&quot;-80.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;return y&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B4&amp;#45;&amp;gt;B5 --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B4-&gt;B5&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M111.01,-146.45C111.01,-138.89 111.01,-129.86 111.01,-121.38&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;114.51,-121.46 111.01,-111.46 107.51,-121.46 114.51,-121.46&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Exit --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Exit&lt;/title&gt;
&lt;polygon fill=&quot;#ffcccc&quot; stroke=&quot;black&quot; points=&quot;138.01,-36 84.01,-36 84.01,0 138.01,0 138.01,-36&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;111.01&quot; y=&quot;-14.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Exit&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B5&amp;#45;&amp;gt;Exit --&gt;
&lt;g id=&quot;edge7&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B5-&gt;Exit&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M111.01,-72.75C111.01,-65.23 111.01,-56.25 111.01,-47.82&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;114.51,-47.96 111.01,-37.96 107.51,-47.96 114.51,-47.96&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_cfg Pages: 1 --&gt;
&lt;svg width=&quot;395pt&quot; height=&quot;206pt&quot; viewBox=&quot;0.00 0.00 395.00 206.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 202.21)&quot;&gt;
&lt;title&gt;semantic_cfg&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-202.21 391.47,-202.21 391.47,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;197.18&quot; cy=&quot;-92.7&quot; rx=&quot;62.91&quot; ry=&quot;27.72&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;197.18&quot; y=&quot;-96.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Control Flow&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;197.18&quot; y=&quot;-81&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Graph&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE1&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;249.04&quot; cy=&quot;-30.16&quot; rx=&quot;48.47&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;249.04&quot; y=&quot;-26.86&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Basic Blocks&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M218.88,-66.54C224.21,-60.11 229.78,-53.39 234.67,-47.5&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;284.79&quot; y=&quot;-59.72&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from -- blocks are nodes&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE2&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;153.43&quot; cy=&quot;-24.32&quot; rx=&quot;62.76&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;153.43&quot; y=&quot;-27.62&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Three-Address&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;153.43&quot; y=&quot;-14.42&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Code&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M179.89,-65.68C176.16,-59.85 172.25,-53.74 168.61,-48.05&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;155.74&quot; y=&quot;-59.57&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT1 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT1&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;118.84&quot; cy=&quot;-114.05&quot; rx=&quot;46.33&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;118.84&quot; y=&quot;-117.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Data Flow&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;118.84&quot; y=&quot;-104.15&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT1 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M143.2,-107.41C143.09,-107.44 142.99,-107.47 142.88,-107.5&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;71.52&quot; y=&quot;-99.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;builds into -- DFA iterates over CFG&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT2 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;272.97&quot; cy=&quot;-122.16&quot; rx=&quot;44.17&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;272.97&quot; y=&quot;-125.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Loop&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;272.97&quot; y=&quot;-112.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Detection&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT2 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M244.75,-111.19C244.87,-111.24 244.99,-111.28 245.11,-111.33&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;316.2&quot; y=&quot;-113.96&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;builds into -- loops from CFG cycles&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;193.35&quot; cy=&quot;-173.89&quot; rx=&quot;54.54&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;193.35&quot; y=&quot;-177.19&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;193.35&quot; y=&quot;-163.99&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Optimization&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M195.86,-120.64C195.43,-129.91 194.94,-140.15 194.52,-149.16&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;130.42&quot; y=&quot;-137.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related -- optimizations use CFG&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Directed graph:&lt;/strong&gt; Nodes = basic blocks, Edges = control flow&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Unique entry:&lt;/strong&gt; Single entry node (start of the program)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Edges represent jumps:&lt;/strong&gt; Conditional, unconditional, and fall-through&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Cycle = loop:&lt;/strong&gt; Back edges in the CFG identify loops (using dominator analysis)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Framework for analysis:&lt;/strong&gt; All global data-flow analysis works by iterating over the CFG&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../basic-blocks&quot; class=&quot;internal alias&quot; data-slug=&quot;basic-blocks&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Basic Blocks&lt;/a&gt; — blocks are the nodes of the CFG&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../data-flow-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;data-flow-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Data Flow Analysis&lt;/a&gt; — DFA uses the CFG to propagate information across blocks&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../loop-detection-in-tac&quot; class=&quot;internal alias&quot; data-slug=&quot;loop-detection-in-tac&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Detection of a Loop in TAC&lt;/a&gt; — loops are identified by analyzing back edges in the CFG&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — many optimizations use the CFG to determine safe transformation scope&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../intermediate-code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;intermediate-code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Intermediate Code Generation&lt;/a&gt; — ICG produces the TAC that the CFG represents&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../code-generator-design-issues&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generator-design-issues&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Issues in Code Generator Design&lt;/a&gt; — code generators use CFG for instruction scheduling and register allocation&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Irreducible CFG:&lt;/strong&gt; When gotos create multiple-entry loops, the CFG is irreducible — some analyses cannot handle this&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dead code:&lt;/strong&gt; Blocks unreachable from the entry are dead code and can be removed&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Critical edges:&lt;/strong&gt; Edges from blocks with multiple successors to blocks with multiple predecessors — they complicate code motion optimizations&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CFG explosion:&lt;/strong&gt; For large programs, the CFG can have thousands of nodes — iterative analysis must be efficient&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>data-flow-analysis</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/data-flow-analysis" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/data-flow-analysis.md" />
    <summary>The Problem Many compiler optimizations require knowing how data moves through the program — which definitions reach which uses, which variables are live at which points, which expressions are available.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Many compiler optimizations require knowing how data moves through the program — which definitions reach which uses, which variables are live at which points, which expressions are available. Without data-flow analysis, the compiler cannot safely apply transformations because it doesn’t know if they preserve program semantics.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Data-flow analysis is a technique for collecting information about the possible values computed at various points in a program. It solves &lt;strong&gt;data-flow equations&lt;/strong&gt; over the control-flow graph to determine properties like &lt;strong&gt;reaching definitions&lt;/strong&gt; (which assignments reach a given point), &lt;strong&gt;live variables&lt;/strong&gt; (variables that will be used later), and &lt;strong&gt;available expressions&lt;/strong&gt; (expressions already computed).&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Data-flow analysis works on the CFG of basic blocks. For each basic block, the compiler computes &lt;strong&gt;GEN&lt;/strong&gt; (definitions/expressions generated within the block) and &lt;strong&gt;KILL&lt;/strong&gt; (definitions/expressions that become invalid). Data-flow equations propagate these sets across block boundaries until a fixed point is reached. The direction of propagation depends on the analysis type: forward (reaching definitions) or backward (live variables).&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: data_flow Pages: 1 --&gt;
&lt;svg width=&quot;342pt&quot; height=&quot;318pt&quot; viewBox=&quot;0.00 0.00 342.00 318.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 314.4)&quot;&gt;
&lt;title&gt;data_flow&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-314.4 337.7,-314.4 337.7,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;g id=&quot;clust1&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_cfg&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;8,-8 8,-302.4 196,-302.4 196,-8 8,-8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;102&quot; y=&quot;-285.8&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;CFG with Data-Flow Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B1 --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B1&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;136.02,-269.6 65.98,-269.6 65.98,-204 136.02,-204 136.02,-269.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101&quot; y=&quot;-254.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B1:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101&quot; y=&quot;-240.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;x = 5&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101&quot; y=&quot;-226&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;GEN: {d1}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101&quot; y=&quot;-211.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;KILL: {d3}&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B2 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B2&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;92.02,-160.8 21.98,-160.8 21.98,-95.2 92.02,-95.2 92.02,-160.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;57&quot; y=&quot;-146&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B2:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;57&quot; y=&quot;-131.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;y = x&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;57&quot; y=&quot;-117.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;GEN: {d2}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;57&quot; y=&quot;-102.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;KILL: {}&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B1&amp;#45;&amp;gt;B2 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B1-&gt;B2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M87.8,-203.76C83.62,-193.62 78.94,-182.25 74.51,-171.5&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;77.81,-170.33 70.77,-162.42 71.34,-173 77.81,-170.33&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- B3 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B3&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;180.02,-160.8 109.98,-160.8 109.98,-95.2 180.02,-95.2 180.02,-160.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;145&quot; y=&quot;-146&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B3:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;145&quot; y=&quot;-131.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;x = 10&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;145&quot; y=&quot;-117.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;GEN: {d3}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;145&quot; y=&quot;-102.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;KILL: {d1}&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B1&amp;#45;&amp;gt;B3 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B1-&gt;B3&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M114.2,-203.76C118.38,-193.62 123.06,-182.25 127.49,-171.5&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;130.66,-173 131.23,-162.42 124.19,-170.33 130.66,-173&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- B4 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B4&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;128,-52 74,-52 74,-16 128,-16 128,-52&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101&quot; y=&quot;-30.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B4&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B2&amp;#45;&amp;gt;B4 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B2-&gt;B4&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M72.39,-94.82C77.41,-84.32 82.94,-72.76 87.79,-62.62&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;90.92,-64.19 92.07,-53.66 84.6,-61.17 90.92,-64.19&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- B3&amp;#45;&amp;gt;B4 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B3-&gt;B4&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M129.61,-94.82C124.59,-84.32 119.06,-72.76 114.21,-62.62&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;117.4,-61.17 109.93,-53.66 111.08,-64.19 117.4,-61.17&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- InOut --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;InOut&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;333.7,-168 204.3,-168 204.3,-88 333.7,-88 333.7,-168&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;269&quot; y=&quot;-153.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Reaching Definitions:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;269&quot; y=&quot;-138.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;IN[B2] = {d1}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;269&quot; y=&quot;-124.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;OUT[B2] = {d1, d2}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;269&quot; y=&quot;-110&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;IN[B3] = {d1}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;269&quot; y=&quot;-95.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;OUT[B3] = {d3}&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CFG --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CFG&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;296,-254.8 242,-254.8 242,-218.8 296,-218.8 296,-254.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;269&quot; y=&quot;-233.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;CFG&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CFG&amp;#45;&amp;gt;InOut --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CFG-&gt;InOut&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M269,-218.64C269,-207.94 269,-193.56 269,-179.46&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;272.5,-179.8 269,-169.8 265.5,-179.8 272.5,-179.8&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Reaching definitions:&lt;/strong&gt; Which definition points can reach a program point&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Live variable analysis:&lt;/strong&gt; Which variables will be used before being redefined&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Available expressions:&lt;/strong&gt; Which expressions have already been computed&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Data-flow equations:&lt;/strong&gt; IN[B] = ∪OUT[predecessors]; OUT[B] = GEN[B] ∪ (IN[B] - KILL[B])&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fixed-point iteration:&lt;/strong&gt; Analysis iterates until the sets stabilize (no more changes)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Data_Flow_Analysis Pages: 1 --&gt;
&lt;svg width=&quot;196pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 196.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Data_Flow_Analysis&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 192.27,-165.63 192.27,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;83.78&quot; cy=&quot;-86.21&quot; rx=&quot;83.78&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;83.78&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Data Flow Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;127.68&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.68&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M97.67,-104.37C102.9,-111.21 108.83,-118.97 114.03,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;86.81&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;59.83&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.83&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M77.36,-67.93C73.87,-57.98 69.57,-45.73 66.1,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.48&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../three-address-code&quot; class=&quot;internal alias&quot; data-slug=&quot;three-address-code&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Three-Address Code&lt;/a&gt; — data-flow analysis operates on TAC basic blocks&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — optimizations like dead code elimination and constant propagation need data-flow info&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../loop-detection-in-tac&quot; class=&quot;internal alias&quot; data-slug=&quot;loop-detection-in-tac&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Detection of a Loop in TAC&lt;/a&gt; — loops require data-flow analysis for effective optimization&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../code-generator-design-issues&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generator-design-issues&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Issues in Code Generator Design&lt;/a&gt; — register allocation uses live variable analysis&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../intermediate-code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;intermediate-code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Intermediate Code Generation&lt;/a&gt; — IR in TAC form enables data-flow analysis&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Conservative approximation:&lt;/strong&gt; Data-flow analysis must be conservative (safe) — if it cannot determine a property, it assumes the worst case&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pointers and aliasing:&lt;/strong&gt; When variables can be accessed through pointers, tracking definitions becomes imprecise&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Control flow complexity:&lt;/strong&gt; Irreducible control flow (gotos, multiple entries) complicates data-flow analysis&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Array accesses:&lt;/strong&gt; &lt;code class=&quot;notranslate&quot;&gt;a[i]&lt;/code&gt; and &lt;code class=&quot;notranslate&quot;&gt;a[j]&lt;/code&gt; may or may not access the same location if i ≠ j — analysis must be conservative&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>error-handling-in-compiler</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/error-handling-in-compiler" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/error-handling-in-compiler.md" />
    <summary>The Problem Programs contain errors — typos, missing semicolons, type mismatches, undeclared variables.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Programs contain errors — typos, missing semicolons, type mismatches, undeclared variables. A compiler that stops at the first error forces users into a painful edit-compile-fix cycle, one error at a time. Without effective error handling, compilation is slow and frustrating.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Error handling in a compiler encompasses detecting errors, reporting them clearly, and recovering to find more errors in the same compilation. Errors are categorized as &lt;strong&gt;lexical&lt;/strong&gt; (invalid characters), &lt;strong&gt;syntax&lt;/strong&gt; (grammar violations), &lt;strong&gt;semantic&lt;/strong&gt; (type mismatches, undeclared identifiers), and &lt;strong&gt;logical&lt;/strong&gt; (runtime issues, detected by the programmer).&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Each compiler phase can detect errors relevant to its domain. The lexer flags illegal characters. The parser reports syntax violations and uses recovery strategies (panic mode, error productions) to continue parsing. The semantic analyzer reports type and scope errors. Error messages include the error type, location (line/column), and a descriptive message.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: error_handling Pages: 1 --&gt;
&lt;svg width=&quot;169pt&quot; height=&quot;496pt&quot; viewBox=&quot;0.00 0.00 169.00 496.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 492)&quot;&gt;
&lt;title&gt;error_handling&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-492 164.71,-492 164.71,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Source --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Source&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;144.04,-488 58,-488 58,-451.2 144.04,-451.2 144.04,-488&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.02&quot; y=&quot;-473.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Source Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.02&quot; y=&quot;-458.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;int x = ;;&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Lexer --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Lexer&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;149.69,-415.2 52.35,-415.2 52.35,-378.4 149.69,-378.4 149.69,-415.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.02&quot; y=&quot;-400.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Lexer&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.02&quot; y=&quot;-386&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Lexical Errors)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Source&amp;#45;&amp;gt;Lexer --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Source-&gt;Lexer&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M101.02,-450.73C101.02,-443.47 101.02,-434.88 101.02,-426.77&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;104.52,-426.93 101.02,-416.93 97.52,-426.93 104.52,-426.93&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Parser --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Parser&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;149.36,-342.4 52.68,-342.4 52.68,-305.6 149.36,-305.6 149.36,-342.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.02&quot; y=&quot;-327.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parser&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.02&quot; y=&quot;-313.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Syntax Errors)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Lexer&amp;#45;&amp;gt;Parser --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Lexer-&gt;Parser&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M101.02,-377.93C101.02,-370.67 101.02,-362.08 101.02,-353.97&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;104.52,-354.13 101.02,-344.13 97.52,-354.13 104.52,-354.13&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Semantic --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Semantic&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;116.04,-269.6 0,-269.6 0,-232.8 116.04,-232.8 116.04,-269.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;58.02&quot; y=&quot;-254.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Semantic Analyzer&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;58.02&quot; y=&quot;-240.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Semantic Errors)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;Semantic --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;Semantic&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M90.17,-305.13C85.45,-297.36 79.81,-288.08 74.59,-279.47&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;77.69,-277.84 69.51,-271.11 71.71,-281.47 77.69,-277.84&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- ErrorReport --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;ErrorReport&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;158.38,-196.8 43.67,-196.8 43.67,-145.6 158.38,-145.6 158.38,-196.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.02&quot; y=&quot;-182&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Error Report&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.02&quot; y=&quot;-167.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;line 1: syntax error&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.02&quot; y=&quot;-153.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  before &#039;;&#039;&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;ErrorReport --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;ErrorReport&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M111.23,-305.36C116.48,-295.25 122.34,-282.12 125.02,-269.6 128.45,-253.61 128.09,-248.87 125.02,-232.8 123.43,-224.47 120.72,-215.82 117.67,-207.76&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;120.96,-206.56 113.96,-198.61 114.48,-209.2 120.96,-206.56&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Semantic&amp;#45;&amp;gt;ErrorReport --&gt;
&lt;g id=&quot;edge7&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Semantic-&gt;ErrorReport&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M67.78,-232.5C71.91,-225.01 76.88,-216 81.7,-207.25&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;84.73,-208.99 86.5,-198.55 78.6,-205.61 84.73,-208.99&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Recovery --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Recovery&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;160.71,-109.6 41.34,-109.6 41.34,-72.8 160.71,-72.8 160.71,-109.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.02&quot; y=&quot;-94.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Error Recovery&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.02&quot; y=&quot;-80.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(skip to sync token)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- ErrorReport&amp;#45;&amp;gt;Recovery --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;ErrorReport-&gt;Recovery&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M101.02,-145.52C101.02,-137.81 101.02,-129.24 101.02,-121.26&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;104.52,-121.32 101.02,-111.32 97.52,-121.32 104.52,-121.32&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Continue --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Continue&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;140.7,-36.8 61.34,-36.8 61.34,0 140.7,0 140.7,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.02&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Continue&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.02&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Compilation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Recovery&amp;#45;&amp;gt;Continue --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Recovery-&gt;Continue&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M101.02,-72.33C101.02,-65.07 101.02,-56.48 101.02,-48.37&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;104.52,-48.53 101.02,-38.53 97.52,-48.53 104.52,-48.53&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Error categories:&lt;/strong&gt; Lexical, syntax, semantic, logical&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Recovery strategies:&lt;/strong&gt; Panic mode (skip to sync token), phrase-level (local fix), error productions (extend grammar), global correction (minimal change)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reporting format:&lt;/strong&gt; Error type + location + description&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error count:&lt;/strong&gt; Compilers typically report all errors found, not just the first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Warnings vs Errors:&lt;/strong&gt; Warnings don’t stop compilation; errors do&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Error_Handling_in_Compiler_Design Pages: 1 --&gt;
&lt;svg width=&quot;202pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 202.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Error_Handling_in_Compiler_Design&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 197.54,-165.63 197.54,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;89.05&quot; cy=&quot;-86.21&quot; rx=&quot;89.05&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;89.05&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Error Handling In Co&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;132.95&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;132.95&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M102.94,-104.37C108.17,-111.21 114.1,-118.97 119.3,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;92.08&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;65.1&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;65.1&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M82.63,-67.93C79.14,-57.98 74.84,-45.73 71.37,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;106.76&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../lexical-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;lexical-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Lexical Analysis&lt;/a&gt; — detects illegal character sequences&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — detects grammar violations&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../semantic-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;semantic-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Semantic Analysis&lt;/a&gt; — detects type and scope errors&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — error handling is distributed across all phases&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler&lt;/a&gt; — error handling is a key objective of compiler design&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Cascading errors:&lt;/strong&gt; One error can cause a cascade of spurious errors — if a semicolon is missing, the parser may report dozens of subsequent errors before recovering&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Recovery is heuristic:&lt;/strong&gt; No error recovery strategy works perfectly for all languages or all error types&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;IDE integration:&lt;/strong&gt; Modern development relies on incremental compilation and real-time error checking — different error reporting strategy than batch compilation&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>first-and-follow-sets</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/first-and-follow-sets" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/first-and-follow-sets.md" />
    <summary>The Problem Top-down predictive parsers need to decide which production to apply based on the current input token.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Top-down predictive parsers need to decide which production to apply based on the current input token. Without knowing which tokens can begin a non-terminal’s expansion (FIRST) and which tokens can follow a non-terminal (FOLLOW), the parser cannot make deterministic decisions.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;&lt;strong&gt;FIRST(α)&lt;/strong&gt; is the set of terminal symbols that can begin strings derived from α. &lt;strong&gt;FOLLOW(A)&lt;/strong&gt; is the set of terminal symbols that can appear immediately after A in some sentential form. These sets are computed from a context-free grammar and used to construct predictive parsing tables.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;FIRST sets are computed bottom-up: for each production A → X₁X₂…Xₙ, add FIRST(X₁) to FIRST(A). If X₁ can derive ε, add FIRST(X₂), and so on. FOLLOW sets are computed top-down: add $ (end marker) to FOLLOW(S). For each production A → αBβ, add FIRST(β) (minus ε) to FOLLOW(B). If β ⇒ ε, add FOLLOW(A) to FOLLOW(B).&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: first_follow Pages: 1 --&gt;
&lt;svg width=&quot;272pt&quot; height=&quot;335pt&quot; viewBox=&quot;0.00 0.00 272.00 335.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 331.2)&quot;&gt;
&lt;title&gt;first_follow&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-331.2 267.84,-331.2 267.84,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Grammar --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Grammar&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;170.85,-327.2 80.13,-327.2 80.13,-261.6 170.85,-261.6 170.85,-327.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;125.49&quot; y=&quot;-312.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Grammar&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;125.49&quot; y=&quot;-298&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E  → T E&#039;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;125.49&quot; y=&quot;-283.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E&#039; → + T E&#039; | ε&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;125.49&quot; y=&quot;-269.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;T  → id&lt;/text&gt;
&lt;/g&gt;
&lt;!-- FIRST --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;FIRST&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;110.99,-225.6 0,-225.6 0,-160 110.99,-160 110.99,-225.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;55.49&quot; y=&quot;-210.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FIRST Sets&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;55.49&quot; y=&quot;-196.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FIRST(E) = {id}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;55.49&quot; y=&quot;-182&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FIRST(E&#039;) = {+, ε}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;55.49&quot; y=&quot;-167.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FIRST(T) = {id}&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;FIRST --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;FIRST&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M102.96,-261.34C97.09,-252.99 90.68,-243.87 84.53,-235.11&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;87.47,-233.21 78.85,-227.04 81.74,-237.23 87.47,-233.21&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- FOLLOW --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;FOLLOW&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;263.84,-225.6 129.14,-225.6 129.14,-160 263.84,-160 263.84,-225.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;196.49&quot; y=&quot;-210.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FOLLOW Sets&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;196.49&quot; y=&quot;-196.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FOLLOW(E) = {$, )}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;196.49&quot; y=&quot;-182&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FOLLOW(E&#039;) = {$, )}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;196.49&quot; y=&quot;-167.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FOLLOW(T) = {+, $, )}&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;FOLLOW --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;FOLLOW&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M148.35,-261.34C154.3,-252.99 160.8,-243.87 167.05,-235.11&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;169.85,-237.21 172.8,-227.03 164.15,-233.15 169.85,-237.21&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- ParsingTable --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;ParsingTable&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;170.51,-124 80.47,-124 80.47,-87.2 170.51,-87.2 170.51,-124&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;125.49&quot; y=&quot;-109.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Predictive&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;125.49&quot; y=&quot;-94.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parsing Table&lt;/text&gt;
&lt;/g&gt;
&lt;!-- FIRST&amp;#45;&amp;gt;ParsingTable --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;FIRST-&gt;ParsingTable&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M81.96,-159.59C89.24,-150.73 97.04,-141.23 104,-132.76&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;106.55,-135.17 110.2,-125.22 101.15,-130.72 106.55,-135.17&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- FOLLOW&amp;#45;&amp;gt;ParsingTable --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;FOLLOW-&gt;ParsingTable&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M169.65,-159.59C162.27,-150.73 154.35,-141.23 147.29,-132.76&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;150.09,-130.65 141,-125.21 144.71,-135.13 150.09,-130.65&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Parser --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Parser&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;188.18,-51.2 62.81,-51.2 62.81,0 188.18,0 188.18,-51.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;125.49&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;LL(1) Parser&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;125.49&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(uses table for&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;125.49&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;deterministic choice)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- ParsingTable&amp;#45;&amp;gt;Parser --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;ParsingTable-&gt;Parser&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M125.49,-86.9C125.49,-79.81 125.49,-71.37 125.49,-63.08&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;128.99,-63.14 125.49,-53.14 121.99,-63.14 128.99,-63.14&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;FIRST(X):&lt;/strong&gt; Terminals that begin X, plus ε if X ⇒ ε&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;FOLLOW(A):&lt;/strong&gt; Terminals that can follow A in a derivation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Algorithm:&lt;/strong&gt; FIRST is computed iteratively; FOLLOW requires computing nullable non-terminals first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LL(1) condition:&lt;/strong&gt; No conflict in parsing table — for each A and each terminal a, at most one production applies&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_FIRST_and_FOLLOW_Sets Pages: 1 --&gt;
&lt;svg width=&quot;200pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 200.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_FIRST_and_FOLLOW_Sets&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 196.09,-165.63 196.09,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;87.61&quot; cy=&quot;-86.21&quot; rx=&quot;87.61&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87.61&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;First And Follow Set&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;131.5&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;131.5&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M101.5,-104.37C106.72,-111.21 112.65,-118.97 117.86,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;90.63&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;63.65&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;63.65&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M81.19,-67.93C77.7,-57.98 73.39,-45.73 69.92,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;105.31&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../context-free-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;context-free-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Context-Free Grammar&lt;/a&gt; — FIRST/FOLLOW are computed from production rules&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../top-down-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;top-down-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Top-Down Parsing&lt;/a&gt; — LL(1) parsers use FIRST/FOLLOW for predictive parsing tables&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — parsing uses FIRST/FOLLOW to guide decisions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../wiki/compilerdesign/ambiguous-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;wiki/compilerdesign/ambiguous-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Ambiguous Grammar&lt;/a&gt; — ambiguity can cause FIRST/FOLLOW conflicts in parsing tables&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Nullable non-terminals:&lt;/strong&gt; Non-terminals that derive ε complicate FIRST computation — ε propagates through chains&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Left recursion:&lt;/strong&gt; Left-recursive grammars cause infinite loops in FIRST computation — must be eliminated first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LL(1) conflicts:&lt;/strong&gt; FIRST/FIRST conflict (two productions start with same token) or FIRST/FOLLOW conflict (production can derive ε and the next token is in FOLLOW) make a grammar not LL(1)&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>flex-lexical-analyzer-generator</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/flex-lexical-analyzer-generator" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/flex-lexical-analyzer-generator.md" />
    <summary>The Problem Writing a lexical analyzer manually requires implementing a deterministic finite automaton (DFA) from regular expressions — handling character transitions, accepting states, maximal munch, and error handling.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Writing a lexical analyzer manually requires implementing a deterministic finite automaton (DFA) from regular expressions — handling character transitions, accepting states, maximal munch, and error handling. This is tedious, error-prone, and must be redone for each language.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Flex (Fast Lexical Analyzer Generator) is a tool that automatically generates a lexical analyzer in C from a specification file containing regular expression patterns and corresponding actions. It is the modern open-source replacement for the classic Lex tool.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The developer writes a &lt;code class=&quot;notranslate&quot;&gt;.l&lt;/code&gt; specification file with three sections: definitions (character classes, constants), rules (regex patterns → C code actions), and user code (helper functions). Flex converts this spec into a DFA implemented as a C source file (&lt;code class=&quot;notranslate&quot;&gt;lex.yy.c&lt;/code&gt;). The generated lexer reads input, matches the longest possible token, and executes the associated C action.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: flex_flow Pages: 1 --&gt;
&lt;svg width=&quot;148pt&quot; height=&quot;407pt&quot; viewBox=&quot;0.00 0.00 148.00 407.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 403.2)&quot;&gt;
&lt;title&gt;flex_flow&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-403.2 144.05,-403.2 144.05,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- FlexSpec --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;FlexSpec&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;140.05,-399.2 0,-399.2 0,-319.2 140.05,-319.2 140.05,-399.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.03&quot; y=&quot;-384.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Flex Spec File (.l)&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.03&quot; y=&quot;-370&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;%%&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.03&quot; y=&quot;-355.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;[0-9]+  { return NUM; }&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.03&quot; y=&quot;-341.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;[a-z]+  { return ID; }&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.03&quot; y=&quot;-326.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;.       { return yytext[0]; }&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Flex --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Flex&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;112.04,-283.2 28.02,-283.2 28.02,-232 112.04,-232 112.04,-283.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.03&quot; y=&quot;-268.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Flex&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.03&quot; y=&quot;-254&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Fast Lexical&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.03&quot; y=&quot;-239.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Analyzer)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- FlexSpec&amp;#45;&amp;gt;Flex --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;FlexSpec-&gt;Flex&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M70.03,-318.84C70.03,-310.96 70.03,-302.73 70.03,-294.96&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;73.53,-295.16 70.03,-285.16 66.53,-295.16 73.53,-295.16&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- LexerC --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;LexerC&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;122.05,-196 18.01,-196 18.01,-144.8 122.05,-144.8 122.05,-196&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.03&quot; y=&quot;-181.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;lex.yy.c&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.03&quot; y=&quot;-166.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Generated DFA&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.03&quot; y=&quot;-152.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;in C)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Flex&amp;#45;&amp;gt;LexerC --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Flex-&gt;LexerC&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M70.03,-231.96C70.03,-224.34 70.03,-215.79 70.03,-207.56&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;73.53,-207.8 70.03,-197.8 66.53,-207.8 73.53,-207.8&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- CComp --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CComp&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;108.03,-108.8 32.02,-108.8 32.02,-72.8 108.03,-72.8 108.03,-108.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.03&quot; y=&quot;-87.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;C Compiler&lt;/text&gt;
&lt;/g&gt;
&lt;!-- LexerC&amp;#45;&amp;gt;CComp --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;LexerC-&gt;CComp&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M70.03,-144.42C70.03,-136.77 70.03,-128.3 70.03,-120.42&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;73.53,-120.63 70.03,-110.63 66.53,-120.63 73.53,-120.63&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- LexerExe --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;LexerExe&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;107.71,-36.8 32.34,-36.8 32.34,0 107.71,0 107.71,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.03&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Lexer&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;70.03&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Executable&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CComp&amp;#45;&amp;gt;LexerExe --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CComp-&gt;LexerExe&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M70.03,-72.4C70.03,-65.18 70.03,-56.6 70.03,-48.47&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;73.53,-48.6 70.03,-38.6 66.53,-48.6 73.53,-48.6&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Input format:&lt;/strong&gt; Flex &lt;code class=&quot;notranslate&quot;&gt;.l&lt;/code&gt; specification with definitions, rules, and user code sections&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Output:&lt;/strong&gt; C source file implementing a DFA-based lexer&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Maximal munch:&lt;/strong&gt; Automatically matches the longest possible token&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pattern language:&lt;/strong&gt; Regular expressions with extensions (character classes, quantifiers)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Integration:&lt;/strong&gt; Designed to work with Yacc/Bison — tokens defined in Flex are used by the parser&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Flex____Fast_Lexical_Analyzer_Generator Pages: 1 --&gt;
&lt;svg width=&quot;195pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 195.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Flex____Fast_Lexical_Analyzer_Generator&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 190.83,-165.63 190.83,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;82.35&quot; cy=&quot;-86.21&quot; rx=&quot;82.35&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;82.35&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Flex    Fast Lexical&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;126.24&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;126.24&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M96.24,-104.37C101.46,-111.21 107.39,-118.97 112.59,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;85.37&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;58.39&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;58.39&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M75.93,-67.93C72.43,-57.98 68.13,-45.73 64.66,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;100.05&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../lexical-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;lexical-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Lexical Analysis&lt;/a&gt; — automates lexer generation from regex specifications&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../wiki/compilerdesign/compiler-construction-tools&quot; class=&quot;internal alias&quot; data-slug=&quot;wiki/compilerdesign/compiler-construction-tools&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler Construction Tools&lt;/a&gt; — Flex is one of the standard compiler tools&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../token&quot; class=&quot;internal alias&quot; data-slug=&quot;token&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Token&lt;/a&gt; — generated lexer produces token streams&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — Flex tokens feed into Yacc/Bison parsers&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Start conditions:&lt;/strong&gt; Flex supports start conditions for context-sensitive lexing (e.g., different rules inside comments vs regular code)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Performance:&lt;/strong&gt; Generated lexers are DFA-based, so they run in O(n) time relative to input length&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Portability:&lt;/strong&gt; &lt;code class=&quot;notranslate&quot;&gt;lex.yy.c&lt;/code&gt; is standard C, compilable on any system with a C compiler&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Flex vs Lex:&lt;/strong&gt; Flex is faster and generates more efficient code than the original Lex&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>intermediate-code-generation</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/intermediate-code-generation" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/intermediate-code-generation.md" />
    <summary>The Problem Generating target machine code directly from the annotated syntax tree is complex because different target machines have different architectures, register sets, and instruction sets.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Generating target machine code directly from the annotated syntax tree is complex because different target machines have different architectures, register sets, and instruction sets. A compiler would need a separate code generator for each target, duplicating most of the logic.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Intermediate code generation is the fourth phase of a compiler. It transforms the annotated syntax tree into a &lt;strong&gt;machine-independent intermediate representation (IR)&lt;/strong&gt;. This IR is easier to optimize than source code and easier to translate to multiple target architectures than direct code generation.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The intermediate code generator walks the annotated syntax tree and emits IR instructions. Common IR forms include &lt;strong&gt;three-address code (TAC)&lt;/strong&gt; — each instruction has at most three operands — and &lt;strong&gt;static single assignment (SSA)&lt;/strong&gt; form. The IR is designed to be high-level enough for optimization but low-level enough for code generation.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: icg Pages: 1 --&gt;
&lt;svg width=&quot;140pt&quot; height=&quot;292pt&quot; viewBox=&quot;0.00 0.00 140.00 292.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 287.6)&quot;&gt;
&lt;title&gt;icg&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-287.6 135.5,-287.6 135.5,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- AnnotatedTree --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;AnnotatedTree&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;104.04,-283.6 23.35,-283.6 23.35,-246.8 104.04,-246.8 104.04,-283.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;63.69&quot; y=&quot;-268.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Annotated&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;63.69&quot; y=&quot;-254.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Syntax Tree&lt;/text&gt;
&lt;/g&gt;
&lt;!-- IRGen --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;IRGen&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;121.06,-209.8 6.33,-209.8 6.33,-173 121.06,-173 121.06,-209.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;63.69&quot; y=&quot;-195&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Intermediate Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;63.69&quot; y=&quot;-180.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Generator&lt;/text&gt;
&lt;/g&gt;
&lt;!-- AnnotatedTree&amp;#45;&amp;gt;IRGen --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;AnnotatedTree-&gt;IRGen&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M63.69,-246.45C63.69,-238.89 63.69,-229.86 63.69,-221.38&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;67.19,-221.46 63.69,-211.46 60.19,-221.46 67.19,-221.46&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- IR --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;IR&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;127.39,-136 0,-136 0,-84.8 127.39,-84.8 127.39,-136&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;63.69&quot; y=&quot;-121.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Three-Address Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;63.69&quot; y=&quot;-106.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  t1 = id + 42&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;63.69&quot; y=&quot;-92.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  t2 = t1 * 2&lt;/text&gt;
&lt;/g&gt;
&lt;!-- IRGen&amp;#45;&amp;gt;IR --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;IRGen-&gt;IR&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M63.69,-172.87C63.69,-165.41 63.69,-156.42 63.69,-147.64&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;67.19,-147.89 63.69,-137.89 60.19,-147.89 67.19,-147.89&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Optimizer --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Optimizer&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;97.36,-36.8 30.02,-36.8 30.02,0 97.36,0 97.36,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;63.69&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;63.69&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Optimizer&lt;/text&gt;
&lt;/g&gt;
&lt;!-- IR&amp;#45;&amp;gt;Optimizer --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;IR-&gt;Optimizer&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M63.69,-84.33C63.69,-73.21 63.69,-60.06 63.69,-48.51&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;67.19,-48.72 63.69,-38.72 60.19,-48.72 67.19,-48.72&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.6&quot; y=&quot;-57.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;for optimization&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Machine-independent:&lt;/strong&gt; Same IR can target different architectures&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Common forms:&lt;/strong&gt; Three-address code, SSA form, bytecode&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Simplifies retargeting:&lt;/strong&gt; New target only needs a new code generator from IR&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Enables optimization:&lt;/strong&gt; IR is easier to analyze and transform than source or machine code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Decouples front-end from back-end:&lt;/strong&gt; Front-end produces IR; back-end consumes IR&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Intermediate_Code_Generation Pages: 1 --&gt;
&lt;svg width=&quot;208pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 208.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Intermediate_Code_Generation&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 203.77,-165.63 203.77,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;95.28&quot; cy=&quot;-86.21&quot; rx=&quot;95.28&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;95.28&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Intermediate Code Ge&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;139.18&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;139.18&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M109.17,-104.37C114.4,-111.21 120.33,-118.97 125.53,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;98.31&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;71.33&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;71.33&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M88.86,-67.93C85.37,-57.98 81.07,-45.73 77.6,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;112.98&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../semantic-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;semantic-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Semantic Analysis&lt;/a&gt; — consumes the annotated syntax tree&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — the IR is the input to optimization&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../three-address-code&quot; class=&quot;internal alias&quot; data-slug=&quot;three-address-code&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Three-Address Code&lt;/a&gt; — a common IR form where each instruction has ≤ 3 operands&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — intermediate code generation is phase 4&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Generation&lt;/a&gt; — the back-end takes optimized IR and produces target code&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;IR forms vary:&lt;/strong&gt; Some compilers use multiple IR forms at different levels of abstraction&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Addressing modes:&lt;/strong&gt; Machine-independent IR may not capture all target-specific addressing modes — the code generator handles this mapping&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;SSA vs TAC:&lt;/strong&gt; SSA form simplifies optimization but requires phi-nodes; TAC is simpler but requires extra data-flow analysis for optimizations&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>lexical-analysis</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/lexical-analysis" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/lexical-analysis.md" />
    <summary>The Problem Source code is a sequence of characters (letters, digits, symbols).</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Source code is a sequence of characters (letters, digits, symbols). Compiler phases need meaningful units (keywords, identifiers, operators, literals) — not raw characters. Processing character-by-character throughout compilation would be inefficient and would conflate low-level text processing with high-level grammar analysis.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Lexical analysis (scanning) is the first phase of a compiler. It reads the source program’s character stream and groups characters into meaningful sequences called &lt;strong&gt;tokens&lt;/strong&gt;. It discards whitespace and comments, and produces a stream of tokens that the parser consumes.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The lexer scans left-to-right, one character at a time. It uses patterns (typically specified as regular expressions) to recognize token types: keywords (&lt;code class=&quot;notranslate&quot;&gt;if&lt;/code&gt;, &lt;code class=&quot;notranslate&quot;&gt;while&lt;/code&gt;), identifiers (&lt;code class=&quot;notranslate&quot;&gt;count&lt;/code&gt;, &lt;code class=&quot;notranslate&quot;&gt;sum&lt;/code&gt;), operators (&lt;code class=&quot;notranslate&quot;&gt;+&lt;/code&gt;, &lt;code class=&quot;notranslate&quot;&gt;=&lt;/code&gt;), literals (&lt;code class=&quot;notranslate&quot;&gt;42&lt;/code&gt;, &lt;code class=&quot;notranslate&quot;&gt;&quot;hello&quot;&lt;/code&gt;), and delimiters (&lt;code class=&quot;notranslate&quot;&gt;;&lt;/code&gt;, &lt;code class=&quot;notranslate&quot;&gt;{&lt;/code&gt;). When a pattern matches, the lexer creates a token pair: &lt;code class=&quot;notranslate&quot;&gt;&amp;#x3C;token-class, attribute-value&gt;&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: lexical_analysis Pages: 1 --&gt;
&lt;svg width=&quot;713pt&quot; height=&quot;102pt&quot; viewBox=&quot;0.00 0.00 713.00 102.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 98.4)&quot;&gt;
&lt;title&gt;lexical_analysis&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-98.4 708.82,-98.4 708.82,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Source --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Source&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;86.04,-65.6 0,-65.6 0,-28.8 86.04,-28.8 86.04,-65.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;43.02&quot; y=&quot;-50.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Source Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;43.02&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;int sum = 42;&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Scanner --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Scanner&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;297.78,-65.2 197.07,-65.2 197.07,-29.2 297.78,-29.2 297.78,-65.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;247.42&quot; y=&quot;-43.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Lexer / Scanner&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Source&amp;#45;&amp;gt;Scanner --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Source-&gt;Scanner&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M86.45,-47.2C115.15,-47.2 153.51,-47.2 185.46,-47.2&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;185.4,-50.7 195.4,-47.2 185.4,-43.7 185.4,-50.7&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;141.56&quot; y=&quot;-50.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;character stream&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Tokens --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Tokens&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;502.33,-94.4 408.28,-94.4 408.28,0 502.33,0 502.33,-94.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;455.3&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Token Stream&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;455.3&quot; y=&quot;-65.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;&amp;#x3C;keyword, int&gt;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;455.3&quot; y=&quot;-50.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;&amp;#x3C;id, sum&gt;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;455.3&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;&amp;#x3C;assign, =&gt;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;455.3&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;&amp;#x3C;num, 42&gt;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;455.3&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;&amp;#x3C;delim, ;&gt;&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Scanner&amp;#45;&amp;gt;Tokens --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Scanner-&gt;Tokens&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M298.26,-47.2C327.91,-47.2 365.66,-47.2 396.6,-47.2&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;396.58,-50.7 406.58,-47.2 396.58,-43.7 396.58,-50.7&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;353.03&quot; y=&quot;-50.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;token generation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Parser&lt;/title&gt;
&lt;polygon fill=&quot;#e8d4ff&quot; stroke=&quot;black&quot; points=&quot;704.82,-65.6 596.13,-65.6 596.13,-28.8 704.82,-28.8 704.82,-65.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;650.48&quot; y=&quot;-50.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parser&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;650.48&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Syntax Analysis)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Tokens&amp;#45;&amp;gt;Parser --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Tokens-&gt;Parser&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M502.57,-47.2C527.09,-47.2 557.6,-47.2 584.46,-47.2&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;584.2,-50.7 594.2,-47.2 584.2,-43.7 584.2,-50.7&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;549.23&quot; y=&quot;-50.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;token stream&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Input:&lt;/strong&gt; Character stream (source code as text)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Output:&lt;/strong&gt; Token stream (sequence of &amp;#x3C;class, value&gt; pairs)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pattern specification:&lt;/strong&gt; Uses regular expressions and finite automata&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Separates concerns:&lt;/strong&gt; Simplifies the parser by handling low-level character processing&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Whitespace/comments:&lt;/strong&gt; Stripped during lexical analysis (not passed to parser)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Lexical_Analysis Pages: 1 --&gt;
&lt;svg width=&quot;184pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 184.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Lexical_Analysis&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 180.32,-165.63 180.32,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;71.83&quot; cy=&quot;-86.21&quot; rx=&quot;71.83&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;71.83&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Lexical Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;115.73&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;115.73&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M85.48,-104.06C90.81,-111.04 96.92,-119.02 102.24,-125.98&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;74.82&quot; y=&quot;-119.22&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;47.87&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;47.87&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M65.41,-67.93C61.92,-57.98 57.61,-45.73 54.15,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;89.53&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../token&quot; class=&quot;internal alias&quot; data-slug=&quot;token&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Token&lt;/a&gt; — the output unit of lexical analysis, a token is the atomic element&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — parser consumes the token stream from the lexer&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../flex-lexical-analyzer-generator&quot; class=&quot;internal alias&quot; data-slug=&quot;flex-lexical-analyzer-generator&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Flex&lt;/a&gt; — tool that automates lexer generation from regular expression specifications&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — lexical analysis is the first phase&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../error-handling-in-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;error-handling-in-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Error Handling in Compiler Design&lt;/a&gt; — lexer detects illegal character sequences&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Maximal munch:&lt;/strong&gt; When multiple token patterns match, the lexer picks the longest match (e.g., &lt;code class=&quot;notranslate&quot;&gt;==&lt;/code&gt; is one token, not &lt;code class=&quot;notranslate&quot;&gt;=&lt;/code&gt; followed by &lt;code class=&quot;notranslate&quot;&gt;=&lt;/code&gt; )&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lookahead:&lt;/strong&gt; Some languages require lookahead to disambiguate tokens (e.g., C’s &lt;code class=&quot;notranslate&quot;&gt;++x&lt;/code&gt; vs &lt;code class=&quot;notranslate&quot;&gt;+ +x&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Context-sensitive lexing:&lt;/strong&gt; In some languages, the same character sequence can be different token types depending on context (typedef names in C)&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>linker-and-loader</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/linker-and-loader" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/linker-and-loader.md" />
    <summary>The Problem A compiled object file contains unresolved references to functions and variables defined in other files or libraries.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A compiled object file contains unresolved references to functions and variables defined in other files or libraries. Additionally, the object file uses relative addresses that must be adjusted when the code is placed in memory. Without linking and loading, separately compiled modules cannot execute together.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The &lt;strong&gt;linker&lt;/strong&gt; combines multiple object files into a single executable by resolving symbol references (connecting definitions to uses) and performing relocation (adjusting addresses). The &lt;strong&gt;loader&lt;/strong&gt; reads the executable from disk into memory, performs final address fixups, and transfers control to the program’s entry point.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The linker takes object files and libraries as input. It performs &lt;strong&gt;symbol resolution&lt;/strong&gt; — matching each symbol reference to its definition across all input files. It then performs &lt;strong&gt;relocation&lt;/strong&gt; — assigning final memory addresses and modifying code/data to use these addresses. The output is an executable file. The loader reads this file, allocates memory, sets up the runtime environment, and jumps to the entry point.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: linker_loader Pages: 1 --&gt;
&lt;svg width=&quot;248pt&quot; height=&quot;365pt&quot; viewBox=&quot;0.00 0.00 248.00 365.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 360.8)&quot;&gt;
&lt;title&gt;linker_loader&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-360.8 244.01,-360.8 244.01,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Obj1 --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Obj1&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;68.01,-356.8 0,-356.8 0,-320 68.01,-320 68.01,-356.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;34.01&quot; y=&quot;-342&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;main.o&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;34.01&quot; y=&quot;-327.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(calls foo)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Linker --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Linker&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;183.69,-284 70.32,-284 70.32,-232.8 183.69,-232.8 183.69,-284&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.01&quot; y=&quot;-269.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Linker&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.01&quot; y=&quot;-254.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(symbol resolution&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.01&quot; y=&quot;-240.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;+ relocation)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Obj1&amp;#45;&amp;gt;Linker --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Obj1-&gt;Linker&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M55.11,-319.7C65.02,-311.39 77.14,-301.22 88.6,-291.62&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;90.6,-294.5 96.01,-285.39 86.1,-289.14 90.6,-294.5&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Obj2 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Obj2&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;168.36,-356.8 85.66,-356.8 85.66,-320 168.36,-320 168.36,-356.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.01&quot; y=&quot;-342&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;util.o&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.01&quot; y=&quot;-327.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(defines foo)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Obj2&amp;#45;&amp;gt;Linker --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Obj2-&gt;Linker&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M127.01,-319.7C127.01,-312.61 127.01,-304.17 127.01,-295.88&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;130.51,-295.94 127.01,-285.94 123.51,-295.94 130.51,-295.94&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- LibC --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;LibC&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;240.01,-356.8 186.01,-356.8 186.01,-320 240.01,-320 240.01,-356.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;213.01&quot; y=&quot;-342&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;libc.a&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;213.01&quot; y=&quot;-327.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(printf)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- LibC&amp;#45;&amp;gt;Linker --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;LibC-&gt;Linker&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M193.49,-319.7C184.42,-311.47 173.34,-301.42 162.84,-291.9&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;165.47,-289.56 155.72,-285.44 160.77,-294.75 165.47,-289.56&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Exec --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Exec&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;164.69,-196.8 89.32,-196.8 89.32,-160 164.69,-160 164.69,-196.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.01&quot; y=&quot;-182&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Executable&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.01&quot; y=&quot;-167.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(a.out)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Linker&amp;#45;&amp;gt;Exec --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Linker-&gt;Exec&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M127.01,-232.72C127.01,-225.01 127.01,-216.44 127.01,-208.46&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;130.51,-208.52 127.01,-198.52 123.51,-208.52 130.51,-208.52&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Loader --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Loader&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;183.02,-124 70.99,-124 70.99,-72.8 183.02,-72.8 183.02,-124&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.01&quot; y=&quot;-109.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Loader&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.01&quot; y=&quot;-94.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(load into memory&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.01&quot; y=&quot;-80.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;+ fix addresses)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Exec&amp;#45;&amp;gt;Loader --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Exec-&gt;Loader&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M127.01,-159.7C127.01,-152.61 127.01,-144.17 127.01,-135.88&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;130.51,-135.94 127.01,-125.94 123.51,-135.94 130.51,-135.94&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Memory --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Memory&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;158.01,-36.8 96,-36.8 96,0 158.01,0 158.01,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.01&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Running&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.01&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Program&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Loader&amp;#45;&amp;gt;Memory --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Loader-&gt;Memory&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M127.01,-72.72C127.01,-65.01 127.01,-56.44 127.01,-48.46&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;130.51,-48.52 127.01,-38.52 123.51,-48.52 130.51,-48.52&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Linker input:&lt;/strong&gt; Object files (.o, .obj) and libraries (.a, .lib, .so, .dll)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Linker output:&lt;/strong&gt; Executable file (or shared library)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Symbol resolution:&lt;/strong&gt; Matching symbol references to definitions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Relocation:&lt;/strong&gt; Adjusting addresses to match final memory layout&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Loader function:&lt;/strong&gt; Read executable, allocate memory, resolve dynamic links, start execution&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Linker_and_Loader Pages: 1 --&gt;
&lt;svg width=&quot;193pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 193.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Linker_and_Loader&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 189.42,-165.63 189.42,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;80.94&quot; cy=&quot;-86.21&quot; rx=&quot;80.94&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;80.94&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Linker And Loader&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;124.83&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;124.83&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M94.83,-104.37C100.05,-111.21 105.98,-118.97 111.19,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;83.96&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;56.98&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;56.98&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M74.52,-67.93C71.03,-57.98 66.72,-45.73 63.25,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;98.64&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../object-code&quot; class=&quot;internal alias&quot; data-slug=&quot;object-code&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Object Code&lt;/a&gt; — linkers consume object code produced by compilers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../runtime-environment&quot; class=&quot;internal alias&quot; data-slug=&quot;runtime-environment&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Runtime Environment&lt;/a&gt; — the loader sets up the initial runtime environment&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Generation&lt;/a&gt; — the code generator produces object files for the linker&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../storage-allocation-strategies&quot; class=&quot;internal alias&quot; data-slug=&quot;storage-allocation-strategies&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Storage Allocation Strategies&lt;/a&gt; — the loader allocates memory for static data&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Dynamic linking:&lt;/strong&gt; Shared libraries (.so, .dll) are linked at load time or runtime, not at compile time — saves memory but adds complexity&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Link order matters:&lt;/strong&gt; Some linkers resolve symbols left-to-right — wrong order causes “undefined reference” errors&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Circular dependencies:&lt;/strong&gt; Libraries that depend on each other can cause linking failures — requires careful library organization&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Loading time:&lt;/strong&gt; Dynamic linking adds startup overhead; static linking creates larger executables but faster startup&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>liveliness-analysis</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/liveliness-analysis" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/liveliness-analysis.md" />
    <summary>The Problem Not all variables hold useful values at all program points.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Not all variables hold useful values at all program points. A variable’s value after its last use is dead — it can be overwritten without affecting the program result. Register allocation needs to know which values are live at each point (need to stay in a register) and which are dead (register can be reused). Dead code elimination also needs liveness information to remove assignments to dead variables.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Liveliness analysis (or live variable analysis) is a backward data-flow analysis that determines, for each program point, which variables are &lt;strong&gt;live&lt;/strong&gt; — meaning their current value will be used before being reassigned. A variable is live at a point if there exists a path from that point to a use of the variable that does not redefine it. Variables that are not live are dead.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The analysis solves backward data-flow equations over the CFG. For each basic block, it computes &lt;code class=&quot;notranslate&quot;&gt;DEF[B]&lt;/code&gt; (variables assigned in B before any use in B) and &lt;code class=&quot;notranslate&quot;&gt;USE[B]&lt;/code&gt; (variables used in B before any assignment to them). The equations are: &lt;code class=&quot;notranslate&quot;&gt;LIVEOUT[B] = ∪ LIVEIN[successors]&lt;/code&gt;, and &lt;code class=&quot;notranslate&quot;&gt;LIVEIN[B] = USE[B] ∪ (LIVEOUT[B] - DEF[B])&lt;/code&gt;. The analysis iterates over all blocks until it reaches a fixed point. A variable is live at a point if it is in the LIVEIN set of any block containing that point.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: liveliness Pages: 1 --&gt;
&lt;svg width=&quot;463pt&quot; height=&quot;319pt&quot; viewBox=&quot;0.00 0.00 463.00 319.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 315.2)&quot;&gt;
&lt;title&gt;liveliness&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-315.2 458.55,-315.2 458.55,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;g id=&quot;clust1&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_cfg&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;8,-8 8,-303.2 193,-303.2 193,-8 8,-8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;100.5&quot; y=&quot;-286.6&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;CFG with Liveliness Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B1 --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B1&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;148.85,-270.4 51.15,-270.4 51.15,-161.6 148.85,-161.6 148.85,-270.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;100&quot; y=&quot;-255.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B1:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;100&quot; y=&quot;-241.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;x = 5&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;100&quot; y=&quot;-226.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;y = x&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;100&quot; y=&quot;-212.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;USE={}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;100&quot; y=&quot;-198&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;DEF={x,y}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;100&quot; y=&quot;-183.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;LIVEIN={}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;100&quot; y=&quot;-169.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;LIVEOUT={x,z}&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B2 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B2&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;127,-125.6 73,-125.6 73,-88.8 127,-88.8 127,-125.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;100&quot; y=&quot;-110.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B2:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;100&quot; y=&quot;-96.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;z = x&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B1&amp;#45;&amp;gt;B2 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B1-&gt;B2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M100,-161.29C100,-152.93 100,-144.59 100,-137.08&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;103.5,-137.23 100,-127.23 96.5,-137.23 103.5,-137.23&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- B3 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B3&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;127,-52.8 73,-52.8 73,-16 127,-16 127,-52.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;100&quot; y=&quot;-38&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B3:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;100&quot; y=&quot;-23.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;print z&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B2&amp;#45;&amp;gt;B3 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B2-&gt;B3&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M100,-88.33C100,-81.07 100,-72.48 100,-64.37&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;103.5,-64.53 100,-54.53 96.5,-64.53 103.5,-64.53&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Analysis --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Analysis&lt;/title&gt;
&lt;polygon fill=&quot;#ffd700&quot; stroke=&quot;black&quot; points=&quot;454.55,-256 201.45,-256 201.45,-176 454.55,-176 454.55,-256&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;328&quot; y=&quot;-241.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Backward Iteration:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;328&quot; y=&quot;-226.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B3: USE={z} → live: z&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;328&quot; y=&quot;-212.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B2: USE={x} → live: x&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;328&quot; y=&quot;-198&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B1: DEF={x,y} → x live out → OK&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;328&quot; y=&quot;-183.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;     DEF={x,y} → y not in LIVEOUT → y dead!&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Result --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Result&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;420.05,-125.6 235.95,-125.6 235.95,-88.8 420.05,-88.8 420.05,-125.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;328&quot; y=&quot;-110.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Result:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;328&quot; y=&quot;-96.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;y assignment can be eliminated&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Analysis&amp;#45;&amp;gt;Result --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Analysis-&gt;Result&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M328,-175.54C328,-162.92 328,-149.19 328,-137.44&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;331.5,-137.56 328,-127.56 324.5,-137.56 331.5,-137.56&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;figure data-rehype-pretty-code-figure=&quot;&quot;&gt;&lt;pre style=&quot;--shiki-light:#24292e;--shiki-dark:#e1e4e8;--shiki-light-bg:#fff;--shiki-dark-bg:#24292e&quot; tabindex=&quot;0&quot; data-language=&quot;dot&quot; data-theme=&quot;github-light github-dark&quot; class=&quot;notranslate&quot;&gt;&lt;span class=&quot;clipboard-button&quot; type=&quot;button&quot; aria-label=&quot;copy source&quot;&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 -8 24 24&quot; fill=&quot;currentColor&quot; stroke=&quot;none&quot; stroke-width=&quot;0&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; class=&quot;copy-icon&quot;&gt;&lt;use href=&quot;#github-copy&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 -8 24 24&quot; fill=&quot;currentColor&quot; stroke=&quot;none&quot; stroke-width=&quot;0&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot; class=&quot;check-icon&quot;&gt;&lt;use href=&quot;#github-check&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/span&gt;&lt;code data-language=&quot;dot&quot; data-theme=&quot;github-light github-dark&quot; style=&quot;display: grid;&quot; class=&quot;notranslate&quot;&gt;&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;graph semantic_liveliness {&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;  layout=neato&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;  node [shape=ellipse fontname=&quot;Helvetica&quot; fontsize=11 style=filled]&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;  edge [fontname=&quot;Helvetica&quot; fontsize=9]&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt; &lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;  THIS [label=&quot;Liveliness\nAnalysis&quot; fillcolor=&quot;#ffd700&quot; fontsize=13 style=&quot;filled,bold&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;  PRE1 [label=&quot;Data Flow\nAnalysis&quot; fillcolor=&quot;#cce5ff&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;  PRE2 [label=&quot;Control Flow\nGraph&quot; fillcolor=&quot;#cce5ff]&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;  OUT1 [label=&quot;Code\nOptimization&quot; fillcolor=&quot;#d4edda&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;  OUT2 [label=&quot;Code Generator\nDesign Issues&quot; fillcolor=&quot;#d4edda&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;  CON1 [label=&quot;Constant\nPropagation&quot; fillcolor=&quot;#ffe5cc]&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt; &lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;  THIS -- PRE1 [label=&quot;built from -- backward DFA&quot; style=dashed]&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;  THIS -- PRE2 [label=&quot;built from -- iterates over CFG&quot; style=dashed]&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;  THIS -- OUT1 [label=&quot;builds into -- dead code elimination&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;  THIS -- OUT2 [label=&quot;builds into -- register allocation&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;  THIS -- CON1 [label=&quot;contrasts with -- CP is forward, liveness is backward&quot;]&lt;/span&gt;&lt;/span&gt;
&lt;span data-line=&quot;&quot;&gt;&lt;span&gt;}&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/figure&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Backward analysis:&lt;/strong&gt; Information flows backward through the CFG (from exits toward entry)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;USE/DEF formulation:&lt;/strong&gt; USE[B] = variables used before any assignment in B; DEF[B] = variables assigned before any use in B&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fixed-point iteration:&lt;/strong&gt; Repeats until LIVEOUT sets stabilize across all blocks&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dead code elimination:&lt;/strong&gt; Variables not live at a point can have their assignment eliminated&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Register allocation:&lt;/strong&gt; Liveness determines which values must be in registers simultaneously&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../data-flow-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;data-flow-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Data Flow Analysis&lt;/a&gt; — liveliness is a canonical example of backward data-flow analysis&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../control-flow-graph&quot; class=&quot;internal alias&quot; data-slug=&quot;control-flow-graph&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Control Flow Graph&lt;/a&gt; — analysis iterates over the CFG’s basic blocks&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — dead code elimination uses liveness information&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../code-generator-design-issues&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generator-design-issues&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Issues in Code Generator Design&lt;/a&gt; — register allocation uses liveness to determine register pressure&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../constant-propagation&quot; class=&quot;internal alias&quot; data-slug=&quot;constant-propagation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Constant Propagation&lt;/a&gt; — CP is forward; liveliness is backward&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Mixed liveness:&lt;/strong&gt; A variable may be live at the beginning of a block but not at the end — different points in the same block have different liveness&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Precise vs conservative:&lt;/strong&gt; The analysis must be conservative — if it cannot determine liveness precisely, it assumes the variable is live&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Aliasing:&lt;/strong&gt; Through pointers, assignments to &lt;code class=&quot;notranslate&quot;&gt;*p&lt;/code&gt; may affect any variable — the analysis must be conservative&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Optimization interaction:&lt;/strong&gt; Dead code elimination from liveness analysis can enable further optimizations, and vice versa&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>ll1-parsing-algorithm</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/ll1-parsing-algorithm" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/ll1-parsing-algorithm.md" />
    <summary>The Problem An LL(1) parsing table is a static data structure.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;An LL(1) parsing table is a static data structure. It needs an algorithm that reads the table, manages the parse stack, consumes input tokens, and produces a leftmost derivation (or detects syntax errors). The algorithm must be generic — the same driver works for any LL(1) grammar, only the table changes.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The LL(1) parsing algorithm is a stack-based procedure that uses the parsing table to guide its decisions. It reads the top of stack and the current input token, consults the table to choose a production, and either matches terminals against input or pushes non-terminal right-hand sides onto the stack. It runs in linear time O(n).&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Initialize stack with &lt;code class=&quot;notranslate&quot;&gt;$&lt;/code&gt; and start symbol. Set input pointer to first token. Repeat: let &lt;code class=&quot;notranslate&quot;&gt;X&lt;/code&gt; = top of stack, &lt;code class=&quot;notranslate&quot;&gt;a&lt;/code&gt; = current input token. If &lt;code class=&quot;notranslate&quot;&gt;X == a == $&lt;/code&gt;, accept. If &lt;code class=&quot;notranslate&quot;&gt;X == a ≠ $&lt;/code&gt;, pop stack, advance input. If &lt;code class=&quot;notranslate&quot;&gt;X&lt;/code&gt; is a non-terminal, look up &lt;code class=&quot;notranslate&quot;&gt;M[X, a]&lt;/code&gt;. If entry contains &lt;code class=&quot;notranslate&quot;&gt;X → Y₁Y₂...Yₖ&lt;/code&gt;, pop &lt;code class=&quot;notranslate&quot;&gt;X&lt;/code&gt;, push &lt;code class=&quot;notranslate&quot;&gt;Yₖ...Y₂Y₁&lt;/code&gt; in reverse order. If entry is empty, report error. The output is the sequence of productions applied — the leftmost derivation.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: ll1_algorithm Pages: 1 --&gt;
&lt;svg width=&quot;353pt&quot; height=&quot;292pt&quot; viewBox=&quot;0.00 0.00 353.00 292.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 287.6)&quot;&gt;
&lt;title&gt;ll1_algorithm&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-287.6 348.94,-287.6 348.94,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Start --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Start&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;222.39,-283.6 123.67,-283.6 123.67,-232.4 222.39,-232.4 222.39,-283.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;173.03&quot; y=&quot;-268.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Initialize:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;173.03&quot; y=&quot;-254.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stack = [$ E]&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;173.03&quot; y=&quot;-240&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Input = id + id $&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Loop --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Loop&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;232.39,-195.4 113.67,-195.4 113.67,-158.6 232.39,-158.6 232.39,-195.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;173.03&quot; y=&quot;-180.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Main Loop&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;173.03&quot; y=&quot;-166.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;X = top, a = current&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Start&amp;#45;&amp;gt;Loop --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Start-&gt;Loop&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M173.03,-232C173.03,-224.11 173.03,-215.31 173.03,-207.15&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;176.53,-207.34 173.03,-197.34 169.53,-207.34 176.53,-207.34&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Match --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Match&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;88.06,-110.6 0,-110.6 0,-73.8 88.06,-73.8 88.06,-110.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;44.03&quot; y=&quot;-95.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;X == a&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;44.03&quot; y=&quot;-81.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;pop, advance&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Loop&amp;#45;&amp;gt;Match --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Loop-&gt;Match&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M113.41,-166.83C83.87,-160.94 52.63,-152.21 43.78,-140.6 39.85,-135.45 38.36,-128.98 38.18,-122.49&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;41.67,-122.82 38.92,-112.59 34.69,-122.3 41.67,-122.82&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;84.9&quot; y=&quot;-131.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;if X == a (terminal)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- TableLookup --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;TableLookup&lt;/title&gt;
&lt;polygon fill=&quot;#ffd700&quot; stroke=&quot;black&quot; points=&quot;228.22,-110.6 107.83,-110.6 107.83,-73.8 228.22,-73.8 228.22,-110.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;168.03&quot; y=&quot;-95.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;X is non-terminal&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;168.03&quot; y=&quot;-81.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;M[X,a] = production&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Loop&amp;#45;&amp;gt;TableLookup --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Loop-&gt;TableLookup&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M171.97,-158.44C171.34,-147.97 170.52,-134.42 169.79,-122.38&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;173.29,-122.3 169.2,-112.53 166.31,-122.72 173.29,-122.3&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;212.23&quot; y=&quot;-131.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;if X is non-terminal&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Accept --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Accept&lt;/title&gt;
&lt;polygon fill=&quot;#90ee90&quot; stroke=&quot;black&quot; points=&quot;325.39,-110.6 246.67,-110.6 246.67,-73.8 325.39,-73.8 325.39,-110.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;286.03&quot; y=&quot;-95.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;X == $ == a&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;286.03&quot; y=&quot;-81.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;ACCEPT&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Loop&amp;#45;&amp;gt;Accept --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Loop-&gt;Accept&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M229.2,-158.12C239.12,-153.4 248.87,-147.62 257.03,-140.6 263.47,-135.06 268.9,-127.73 273.27,-120.5&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;276.09,-122.64 277.82,-112.19 269.95,-119.27 276.09,-122.64&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;297.26&quot; y=&quot;-131.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;if X == a == $&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Match&amp;#45;&amp;gt;Loop --&gt;
&lt;g id=&quot;edge7&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Match-&gt;Loop&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M88.21,-108.63C100.94,-114.03 114.5,-120.74 126.03,-128.6 134.89,-134.64 143.48,-142.51 150.85,-150.09&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;147.9,-152.07 157.28,-157 153.03,-147.3 147.9,-152.07&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Push --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Push&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;316.05,-36.8 206,-36.8 206,0 316.05,0 316.05,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;261.03&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Pop X, push RHS&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;261.03&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;in reverse order&lt;/text&gt;
&lt;/g&gt;
&lt;!-- TableLookup&amp;#45;&amp;gt;Push --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;TableLookup-&gt;Push&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M191.02,-73.45C202.53,-64.56 216.65,-53.66 229.19,-43.98&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;231.01,-47 236.78,-38.12 226.73,-41.46 231.01,-47&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Push&amp;#45;&amp;gt;Loop --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Push-&gt;Loop&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M297.57,-37.14C311.66,-46.03 326.23,-58.26 334.03,-73.8 347.36,-100.36 350.74,-118.36 331.03,-140.6 319.16,-154 280.01,-162.82 243.66,-168.31&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;243.4,-164.81 233.99,-169.7 244.39,-171.74 243.4,-164.81&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Error --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Error&lt;/title&gt;
&lt;polygon fill=&quot;#ffcccc&quot; stroke=&quot;black&quot; points=&quot;327.37,-276.4 240.69,-276.4 240.69,-239.6 327.37,-239.6 327.37,-276.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;284.03&quot; y=&quot;-261.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;M[X,a] empty&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;284.03&quot; y=&quot;-247.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;ERROR&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_ll1_algo Pages: 1 --&gt;
&lt;svg width=&quot;263pt&quot; height=&quot;206pt&quot; viewBox=&quot;0.00 0.00 263.00 206.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 202.21)&quot;&gt;
&lt;title&gt;semantic_ll1_algo&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-202.21 258.52,-202.21 258.52,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;131.53&quot; cy=&quot;-92.7&quot; rx=&quot;66.49&quot; ry=&quot;27.72&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;131.53&quot; y=&quot;-96.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;LL(1) Parsing&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;131.53&quot; y=&quot;-81&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Algorithm&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE1&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;183.4&quot; cy=&quot;-30.16&quot; rx=&quot;58.01&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;183.4&quot; y=&quot;-33.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;LL(1) Parsing&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;183.4&quot; y=&quot;-20.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Table&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M153.53,-66.18C157.06,-61.92 160.69,-57.53 164.16,-53.36&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;140.34&quot; y=&quot;-51.67&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE2&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;87.78&quot; cy=&quot;-24.32&quot; rx=&quot;45.46&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87.78&quot; y=&quot;-27.62&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Predictive&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87.78&quot; y=&quot;-14.42&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Parser&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M114.24,-65.68C110.42,-59.71 106.4,-53.43 102.68,-47.62&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;89.96&quot; y=&quot;-59.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT1 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT1&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;53.19&quot; cy=&quot;-114.05&quot; rx=&quot;40.28&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;53.19&quot; y=&quot;-117.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Syntax&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;53.19&quot; y=&quot;-104.15&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT1 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M75.43,-107.99C75.33,-108.01 75.24,-108.04 75.14,-108.07&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;95.55&quot; y=&quot;-110.73&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;207.32&quot; cy=&quot;-122.16&quot; rx=&quot;47.19&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;207.32&quot; y=&quot;-125.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Top-Down&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;207.32&quot; y=&quot;-112.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Parsing&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M180.39,-111.69C180.5,-111.74 180.62,-111.78 180.73,-111.83&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;166.8&quot; y=&quot;-114.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;127.7&quot; cy=&quot;-173.89&quot; rx=&quot;49.79&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.7&quot; y=&quot;-177.19&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;LR Parsing&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.7&quot; y=&quot;-163.99&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Algorithm&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M130.21,-120.64C129.78,-129.91 129.29,-140.15 128.87,-149.16&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;64.77&quot; y=&quot;-137.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;contrasts with -- LR is bottom-up&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Linear time:&lt;/strong&gt; O(n) where n is input length — each token is processed once&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stack-based:&lt;/strong&gt; Explicit stack replaces recursion&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Leftmost derivation:&lt;/strong&gt; Output is the sequence of productions in a leftmost derivation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error detection:&lt;/strong&gt; Found when table entry is empty — immediate detection&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Generic driver:&lt;/strong&gt; The same algorithm works for any LL(1) grammar by swapping the table&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../ll1-parsing-table&quot; class=&quot;internal alias&quot; data-slug=&quot;ll1-parsing-table&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LL(1) Parsing Table&lt;/a&gt; — the algorithm consults this table&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../predictive-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;predictive-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Predictive Parser&lt;/a&gt; — this is the algorithm that drives the predictive parser&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — the LL(1) algorithm is a concrete syntax analysis method&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../top-down-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;top-down-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Top-Down Parsing&lt;/a&gt; — produces a leftmost derivation (top-down)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../lr-parsers&quot; class=&quot;internal alias&quot; data-slug=&quot;lr-parsers&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LR Parsing Algorithm&lt;/a&gt; — LR uses ACTION/GOTO tables and produces reverse rightmost derivations&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;ε-productions:&lt;/strong&gt; When &lt;code class=&quot;notranslate&quot;&gt;M[A, a]&lt;/code&gt; has &lt;code class=&quot;notranslate&quot;&gt;A → ε&lt;/code&gt;, the algorithm pops &lt;code class=&quot;notranslate&quot;&gt;A&lt;/code&gt; without consuming input — effectively skipping the non-terminal&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Synchronization:&lt;/strong&gt; For error recovery, the algorithm can skip tokens until it finds one in FOLLOW(A) — called panic-mode recovery&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Infinite loop:&lt;/strong&gt; If the grammar contains left recursion or cycles (&lt;code class=&quot;notranslate&quot;&gt;A → A&lt;/code&gt;), the algorithm may loop forever — the table should prevent this for LL(1) grammars&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Table size limitation:&lt;/strong&gt; Real languages may need large tables — but LL(1) tables are much smaller than LR(1) tables&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>ll1-parsing-table</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/ll1-parsing-table" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/ll1-parsing-table.md" />
    <summary>The Problem A predictive parser needs a decision mechanism: given a non-terminal on the stack and the current input token, which production should it apply? Manually encoding these decisions for each non-terminal is tedious.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A predictive parser needs a decision mechanism: given a non-terminal on the stack and the current input token, which production should it apply? Manually encoding these decisions for each non-terminal is tedious. An automated construction method from FIRST and FOLLOW sets produces a compact table that drives the parser.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The LL(1) parsing table is a two-dimensional array where rows are non-terminals and columns are terminals (plus &lt;code class=&quot;notranslate&quot;&gt;$&lt;/code&gt;). Each entry &lt;code class=&quot;notranslate&quot;&gt;M[A, a]&lt;/code&gt; contains the production &lt;code class=&quot;notranslate&quot;&gt;A → α&lt;/code&gt; to apply when non-terminal &lt;code class=&quot;notranslate&quot;&gt;A&lt;/code&gt; is on top of the stack and the current input token is &lt;code class=&quot;notranslate&quot;&gt;a&lt;/code&gt;. The table is constructed algorithmically: for each production &lt;code class=&quot;notranslate&quot;&gt;A → α&lt;/code&gt;, add the production to &lt;code class=&quot;notranslate&quot;&gt;M[A, t]&lt;/code&gt; for every terminal &lt;code class=&quot;notranslate&quot;&gt;t&lt;/code&gt; in &lt;code class=&quot;notranslate&quot;&gt;FIRST(α)&lt;/code&gt;, and if &lt;code class=&quot;notranslate&quot;&gt;α&lt;/code&gt; derives ε, add it for every &lt;code class=&quot;notranslate&quot;&gt;t&lt;/code&gt; in &lt;code class=&quot;notranslate&quot;&gt;FOLLOW(A)&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The construction algorithm: (1) For each production &lt;code class=&quot;notranslate&quot;&gt;A → α&lt;/code&gt;, for each terminal &lt;code class=&quot;notranslate&quot;&gt;t in FIRST(α)&lt;/code&gt;, add &lt;code class=&quot;notranslate&quot;&gt;A → α&lt;/code&gt; to &lt;code class=&quot;notranslate&quot;&gt;M[A, t]&lt;/code&gt;. (2) If &lt;code class=&quot;notranslate&quot;&gt;ε in FIRST(α)&lt;/code&gt;, then for each terminal &lt;code class=&quot;notranslate&quot;&gt;t in FOLLOW(A)&lt;/code&gt; (including &lt;code class=&quot;notranslate&quot;&gt;$&lt;/code&gt;), add &lt;code class=&quot;notranslate&quot;&gt;A → α&lt;/code&gt; to &lt;code class=&quot;notranslate&quot;&gt;M[A, t]&lt;/code&gt;. (3) If the grammar is LL(1), every table entry will have at most one production. Multiple entries indicate the grammar is not LL(1).&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: ll1_table Pages: 1 --&gt;
&lt;svg width=&quot;276pt&quot; height=&quot;392pt&quot; viewBox=&quot;0.00 0.00 276.00 392.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 388)&quot;&gt;
&lt;title&gt;ll1_table&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-388 271.51,-388 271.51,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Grammar --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Grammar&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;171.85,-384 81.13,-384 81.13,-318.4 171.85,-318.4 171.85,-384&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;126.49&quot; y=&quot;-369.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Grammar:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;126.49&quot; y=&quot;-354.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E  → T E&#039;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;126.49&quot; y=&quot;-340.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E&#039; → + T E&#039; | ε&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;126.49&quot; y=&quot;-326&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;T  → id&lt;/text&gt;
&lt;/g&gt;
&lt;!-- FIRST --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;FIRST&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;110.99,-281.4 0,-281.4 0,-215.8 110.99,-215.8 110.99,-281.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;55.49&quot; y=&quot;-266.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FIRST Sets:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;55.49&quot; y=&quot;-252.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FIRST(E)  = {id}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;55.49&quot; y=&quot;-237.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FIRST(E&#039;) = {+, ε}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;55.49&quot; y=&quot;-223.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FIRST(T)  = {id}&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;FIRST --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;FIRST&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M103.83,-318.09C97.75,-309.47 91.08,-300.03 84.7,-290.98&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;87.68,-289.14 79.05,-282.98 81.96,-293.17 87.68,-289.14&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- FOLLOW --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;FOLLOW&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;267.51,-281.4 129.48,-281.4 129.48,-215.8 267.51,-215.8 267.51,-281.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;198.49&quot; y=&quot;-266.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FOLLOW Sets:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;198.49&quot; y=&quot;-252.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FOLLOW(E)  = {$, )}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;198.49&quot; y=&quot;-237.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FOLLOW(E&#039;) = {$, )}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;198.49&quot; y=&quot;-223.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;FOLLOW(T)  = {+, $, )}&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;FOLLOW --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;FOLLOW&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M149.47,-318.09C155.64,-309.47 162.4,-300.03 168.88,-290.98&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;171.63,-293.15 174.6,-282.98 165.94,-289.07 171.63,-293.15&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Table --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Table&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;203.6,-178.8 49.38,-178.8 49.38,-84.8 203.6,-84.8 203.6,-178.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;126.49&quot; y=&quot;-164&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parsing Table M:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;126.49&quot; y=&quot;-135.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;     id       |   +    |   $   &lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;126.49&quot; y=&quot;-121.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E  | E→TE&#039;   |        |       &lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;126.49&quot; y=&quot;-106.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E&#039; |          | E&#039;→+TE&#039;| E&#039;→ε &lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;126.49&quot; y=&quot;-92.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;T  | T→id    |        |       &lt;/text&gt;
&lt;/g&gt;
&lt;!-- FIRST&amp;#45;&amp;gt;Table --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;FIRST-&gt;Table&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M75.46,-215.32C80.63,-206.95 86.36,-197.69 92.05,-188.5&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;94.86,-190.61 97.14,-180.26 88.9,-186.93 94.86,-190.61&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- FOLLOW&amp;#45;&amp;gt;Table --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;FOLLOW-&gt;Table&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M178.25,-215.32C173,-206.95 167.19,-197.69 161.43,-188.5&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;164.54,-186.87 156.26,-180.26 158.61,-190.59 164.54,-186.87&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Parser --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Parser&lt;/title&gt;
&lt;polygon fill=&quot;#ffd700&quot; stroke=&quot;black&quot; points=&quot;169.83,-36.8 83.16,-36.8 83.16,0 169.83,0 169.83,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;126.49&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Predictive&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;126.49&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parser Driver&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Table&amp;#45;&amp;gt;Parser --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Table-&gt;Parser&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M126.49,-84.52C126.49,-72.36 126.49,-59.58 126.49,-48.6&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;129.99,-48.6 126.49,-38.6 122.99,-48.6 129.99,-48.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;159.02&quot; y=&quot;-57.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;guides parsing&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_ll1_table Pages: 1 --&gt;
&lt;svg width=&quot;296pt&quot; height=&quot;206pt&quot; viewBox=&quot;0.00 0.00 296.00 206.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 202.21)&quot;&gt;
&lt;title&gt;semantic_ll1_table&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-202.21 291.78,-202.21 291.78,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;166.52&quot; cy=&quot;-92.7&quot; rx=&quot;66.49&quot; ry=&quot;27.72&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;166.52&quot; y=&quot;-96.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;LL(1) Parsing&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;166.52&quot; y=&quot;-81&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Table&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE1&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;218.38&quot; cy=&quot;-30.16&quot; rx=&quot;61.88&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;218.38&quot; y=&quot;-33.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;FIRST and&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;218.38&quot; y=&quot;-20.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;FOLLOW Sets&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M188.52,-66.18C192.05,-61.92 195.68,-57.53 199.15,-53.36&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;175.33&quot; y=&quot;-51.67&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE2&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;122.77&quot; cy=&quot;-24.32&quot; rx=&quot;56.7&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;122.77&quot; y=&quot;-27.62&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Context-Free&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;122.77&quot; y=&quot;-14.42&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Grammar&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M149.23,-65.68C145.5,-59.85 141.59,-53.74 137.95,-48.05&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;125.08&quot; y=&quot;-59.57&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT1 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT1&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;88.18&quot; cy=&quot;-114.05&quot; rx=&quot;58.01&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;88.18&quot; y=&quot;-117.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;LL(1) Parsing&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;88.18&quot; y=&quot;-104.15&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Algorithm&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT1 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M110.42,-107.99C110.32,-108.01 110.23,-108.04 110.13,-108.07&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;90.01&quot; y=&quot;-110.73&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT2 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;242.31&quot; cy=&quot;-122.16&quot; rx=&quot;45.46&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;242.31&quot; y=&quot;-125.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Predictive&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;242.31&quot; y=&quot;-112.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Parser&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT2 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M215.38,-111.69C215.49,-111.74 215.61,-111.78 215.72,-111.83&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;235.81&quot; y=&quot;-114.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CON1 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CON1&lt;/title&gt;
&lt;ellipse fill=&quot;#ffe5cc&quot; stroke=&quot;black&quot; cx=&quot;162.69&quot; cy=&quot;-173.89&quot; rx=&quot;49.79&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;162.69&quot; y=&quot;-177.19&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;LR Parsing&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;162.69&quot; y=&quot;-163.99&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Table&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;CON1 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--CON1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M165.2,-120.64C164.77,-129.91 164.28,-140.15 163.86,-149.16&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;82.27&quot; y=&quot;-137.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;contrasts with -- LR uses ACTION/GOTO&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;M[A, a] format:&lt;/strong&gt; Row = non-terminal, column = terminal (including &lt;code class=&quot;notranslate&quot;&gt;$&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Algorithmic construction:&lt;/strong&gt; Built from FIRST and FOLLOW sets, not manually&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;One entry per cell:&lt;/strong&gt; For an LL(1) grammar, each cell contains at most one production&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ε-productions:&lt;/strong&gt; In the table for FOLLOW-set terminals when FIRST contains ε&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error cells:&lt;/strong&gt; Empty cells represent syntax errors — used for error detection and recovery&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../first-and-follow-sets&quot; class=&quot;internal alias&quot; data-slug=&quot;first-and-follow-sets&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;FIRST and FOLLOW Sets&lt;/a&gt; — the table is constructed from these sets&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../context-free-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;context-free-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Context-Free Grammar&lt;/a&gt; — grammar transformations may be needed before construction&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../predictive-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;predictive-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Predictive Parser&lt;/a&gt; — the table is the core data structure&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../ll1-parsing-algorithm&quot; class=&quot;internal alias&quot; data-slug=&quot;ll1-parsing-algorithm&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LL(1) Parsing Algorithm&lt;/a&gt; — the algorithm that interprets the table&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../lr-parsers&quot; class=&quot;internal alias&quot; data-slug=&quot;lr-parsers&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LR Parsing Tables&lt;/a&gt; — LR tables have ACTION and GOTO parts for bottom-up parsing&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Multiple entries:&lt;/strong&gt; A cell with multiple productions means the grammar is not LL(1) — ambiguous or left-recursive&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Left recursion:&lt;/strong&gt; Left-recursive grammars produce multiple entries in the table — must eliminate left recursion first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Left factoring:&lt;/strong&gt; Common prefixes produce FIRST conflicts — solved by left-factoring the grammar (e.g., &lt;code class=&quot;notranslate&quot;&gt;A → αβ₁ | αβ₂&lt;/code&gt; becomes &lt;code class=&quot;notranslate&quot;&gt;A → αA&#039;, A&#039; → β₁ | β₂&lt;/code&gt;)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Table size:&lt;/strong&gt; Number of rows = count of non-terminals, columns = count of terminals — grows with grammar size&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>loop-detection-in-tac</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/loop-detection-in-tac" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/loop-detection-in-tac.md" />
    <summary>The Problem Loops are where programs spend most of their execution time.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Loops are where programs spend most of their execution time. To optimize effectively (loop invariant code motion, induction variable elimination), the compiler must first identify which instructions belong to loops and what kind of loops they are.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Loop detection in TAC identifies loop structures in the control-flow graph (CFG). A &lt;strong&gt;loop&lt;/strong&gt; in the CFG is a set of nodes (basic blocks) where: every node can reach the loop header, and the header dominates all nodes in the loop. The key concept is &lt;strong&gt;dominators&lt;/strong&gt; — node d dominates node n if every path from entry to n goes through d.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The compiler builds a control-flow graph from TAC instructions. It computes the &lt;strong&gt;dominator tree&lt;/strong&gt; (which nodes dominate which). A &lt;strong&gt;back edge&lt;/strong&gt; is identified when an edge from node n to node h has h dominating n. The loop consists of all nodes that can reach n without going through h. Natural loops have a single entry (the header) and are amenable to optimization.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: loop_detection Pages: 1 --&gt;
&lt;svg width=&quot;488pt&quot; height=&quot;387pt&quot; viewBox=&quot;0.00 0.00 488.00 387.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 382.6)&quot;&gt;
&lt;title&gt;loop_detection&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-382.6 484.04,-382.6 484.04,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;g id=&quot;clust1&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_cfg&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;8,-8 8,-370.6 322,-370.6 322,-8 8,-8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;165&quot; y=&quot;-354&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;Control-Flow Graph (TAC Basic Blocks)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Entry --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Entry&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;221.68,-337.8 156.32,-337.8 156.32,-301.8 221.68,-301.8 221.68,-337.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;189&quot; y=&quot;-316.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B1: Entry&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Header --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Header&lt;/title&gt;
&lt;polygon fill=&quot;#ffd700&quot; stroke=&quot;black&quot; points=&quot;242.37,-243.2 135.63,-243.2 135.63,-206.4 242.37,-206.4 242.37,-243.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;189&quot; y=&quot;-228.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B2: Loop Header&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;189&quot; y=&quot;-214&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(L1: t1 = i &amp;#x3C; n)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Entry&amp;#45;&amp;gt;Header --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Entry-&gt;Header&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M189,-301.31C189,-288.27 189,-270.17 189,-254.96&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;192.5,-255.04 189,-245.04 185.5,-255.04 192.5,-255.04&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Body1 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Body1&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;128.35,-136.8 15.65,-136.8 15.65,-100 128.35,-100 128.35,-136.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;72&quot; y=&quot;-122&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B3: Loop Body&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;72&quot; y=&quot;-107.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(sum = sum + a[i])&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Header&amp;#45;&amp;gt;Body1 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Header-&gt;Body1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;red&quot; d=&quot;M169.15,-206.09C150.24,-189.22 121.58,-163.64 100.28,-144.64&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;red&quot; stroke=&quot;red&quot; points=&quot;102.87,-142.25 93.07,-138.2 98.21,-147.47 102.87,-142.25&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;146.57&quot; y=&quot;-157.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;back edge&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Exit --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Exit&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;231.69,-136.4 146.31,-136.4 146.31,-100.4 231.69,-100.4 231.69,-136.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;189&quot; y=&quot;-114.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B5: Continue&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Header&amp;#45;&amp;gt;Exit --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Header-&gt;Exit&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M189,-206.09C189,-190.25 189,-166.73 189,-148.19&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;192.5,-148.36 189,-138.36 185.5,-148.36 192.5,-148.36&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;201.51&quot; y=&quot;-157.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;i &gt;= n&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Body2 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Body2&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;257.35,-52 180.65,-52 180.65,-16 257.35,-16 257.35,-52&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;219&quot; y=&quot;-30.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B4: i = i + 1&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Body1&amp;#45;&amp;gt;Body2 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Body1-&gt;Body2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;red&quot; d=&quot;M103.9,-99.52C125.6,-87.36 154.51,-71.15 177.93,-58.02&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;red&quot; stroke=&quot;red&quot; points=&quot;179.59,-61.1 186.6,-53.16 176.17,-55 179.59,-61.1&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;177.37&quot; y=&quot;-73&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;back edge&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Body2&amp;#45;&amp;gt;Header --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Body2-&gt;Header&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;red&quot; d=&quot;M227.88,-52.44C237.08,-72.83 249.08,-107.52 241,-136.8 234.93,-158.78 221.69,-180.79 210.08,-197.18&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;red&quot; stroke=&quot;red&quot; points=&quot;207.5,-194.78 204.4,-204.92 213.14,-198.93 207.5,-194.78&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;266.74&quot; y=&quot;-115.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;back edge&lt;/text&gt;
&lt;/g&gt;
&lt;!-- DomTree --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;DomTree&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;480.04,-264.8 329.96,-264.8 329.96,-184.8 480.04,-184.8 480.04,-264.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;405&quot; y=&quot;-250&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Dominator Tree:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;405&quot; y=&quot;-235.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B1 dominates B2&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;405&quot; y=&quot;-221.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;B2 dominates B3, B4, B5&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;405&quot; y=&quot;-206.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Back edge: B4 → B2&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;405&quot; y=&quot;-192.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Loop = {B2, B3, B4}&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CFG --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CFG&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;432,-337.8 378,-337.8 378,-301.8 432,-301.8 432,-337.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;405&quot; y=&quot;-316.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;CFG&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CFG&amp;#45;&amp;gt;DomTree --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CFG-&gt;DomTree&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M405,-301.31C405,-294.13 405,-285.42 405,-276.5&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;408.5,-276.7 405,-266.7 401.5,-276.7 408.5,-276.7&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Control-flow graph:&lt;/strong&gt; Nodes are basic blocks; edges are jumps&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dominator:&lt;/strong&gt; h dominates n if all paths from entry to n include h&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Back edge:&lt;/strong&gt; Edge from n to h where h dominates n&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Natural loop:&lt;/strong&gt; Header h + all nodes that can reach a back edge without passing through h&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Nested loops:&lt;/strong&gt; A loop inside another — inner loop is optimized first&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Detection_of_a_Loop_in_Three_Address_Code Pages: 1 --&gt;
&lt;svg width=&quot;203pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 203.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Detection_of_a_Loop_in_Three_Address_Code&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 198.99,-165.63 198.99,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;90.5&quot; cy=&quot;-86.21&quot; rx=&quot;90.5&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;90.5&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Detection Of A Loop &lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;134.4&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;134.4&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M104.39,-104.37C109.61,-111.21 115.54,-118.97 120.75,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;131.61&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;66.54&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;66.54&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M84.08,-67.93C80.59,-57.98 76.28,-45.73 72.81,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;108.2&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../three-address-code&quot; class=&quot;internal alias&quot; data-slug=&quot;three-address-code&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Three-Address Code&lt;/a&gt; — TAC provides the instruction sequence for CFG construction&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — loop detection enables loop optimizations&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../data-flow-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;data-flow-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Data Flow Analysis&lt;/a&gt; — data-flow analysis often computes loop information&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../intermediate-code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;intermediate-code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Intermediate Code Generation&lt;/a&gt; — TAC enables loop detection at the IR level&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../code-generator-design-issues&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generator-design-issues&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Issues in Code Generator Design&lt;/a&gt; — code generators must be aware of loop structure for register allocation&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Irreducible loops:&lt;/strong&gt; Multiple entry points (from goto) — cannot be identified as natural loops, require special handling&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Outer vs inner loops:&lt;/strong&gt; When loops are nested, the inner loop should be optimized first (maximizes benefit)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Infinite loops:&lt;/strong&gt; A loop with no exit edge — the compiler must detect this to avoid infinite optimization&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Loop-invariant code:&lt;/strong&gt; Instructions inside the loop that produce the same value every iteration — should be moved to the pre-header&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>lr-parsers</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/lr-parsers" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/lr-parsers.md" />
    <summary>The Problem Simple shift-reduce parsing needs conflict resolution rules.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Simple shift-reduce parsing needs conflict resolution rules. Without lookahead information, the parser frequently faces shift/reduce or reduce/reduce conflicts. Different languages require different amounts of lookahead and different levels of parsing power.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;LR parsers are bottom-up shift-reduce parsers that use a deterministic finite automaton with a state stack and a parsing table. Three main variants exist: &lt;strong&gt;SLR (Simple LR)&lt;/strong&gt; uses FOLLOW sets for conflict resolution, offering the simplest tables. &lt;strong&gt;CLR (Canonical LR)&lt;/strong&gt; or LR(1) uses full lookahead for maximum power. &lt;strong&gt;LALR (Look-Ahead LR)&lt;/strong&gt; merges CLR states for smaller tables while retaining most of CLR’s power.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;All three construct a set of LR items (productions with a dot position) and use them to build states. SLR uses LR(0) items and FOLLOW sets for reduce decisions. CLR uses LR(1) items with full lookahead information. LALR merges CLR states that have identical LR(0) cores but different lookaheads, producing smaller tables with fewer states but occasionally introducing reduce/reduce conflicts.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: lr_family Pages: 1 --&gt;
&lt;svg width=&quot;523pt&quot; height=&quot;205pt&quot; viewBox=&quot;0.00 0.00 523.00 205.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 200.8)&quot;&gt;
&lt;title&gt;lr_family&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-200.8 519.07,-200.8 519.07,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- SLR --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;SLR&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;149.39,-124 0,-124 0,-72.8 149.39,-72.8 149.39,-124&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;74.7&quot; y=&quot;-109.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;SLR (Simple LR)&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;74.7&quot; y=&quot;-94.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;LR(0) items + FOLLOW&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;74.7&quot; y=&quot;-80.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Small tables, least power&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Power --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Power&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;317.06,-36.8 196.34,-36.8 196.34,0 317.06,0 317.06,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;256.7&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parsing Power:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;256.7&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;SLR &amp;#x3C; LALR &amp;#x3C; CLR&lt;/text&gt;
&lt;/g&gt;
&lt;!-- SLR&amp;#45;&amp;gt;Power --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;SLR-&gt;Power&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M133.03,-72.4C156.1,-62.51 182.4,-51.24 204.79,-41.64&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;205.91,-44.97 213.72,-37.82 203.15,-38.54 205.91,-44.97&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- CLR --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CLR&lt;/title&gt;
&lt;polygon fill=&quot;#e8d4ff&quot; stroke=&quot;black&quot; points=&quot;515.07,-124 364.33,-124 364.33,-72.8 515.07,-72.8 515.07,-124&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;439.7&quot; y=&quot;-109.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;CLR (Canonical LR)&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;439.7&quot; y=&quot;-94.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;LR(1) items&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;439.7&quot; y=&quot;-80.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Large tables, most power&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CLR&amp;#45;&amp;gt;Power --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CLR-&gt;Power&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M381.04,-72.4C357.85,-62.51 331.4,-51.24 308.89,-41.64&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;310.47,-38.52 299.9,-37.82 307.73,-44.96 310.47,-38.52&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- TableSize --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;TableSize&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;500.06,-36.8 379.34,-36.8 379.34,0 500.06,0 500.06,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;439.7&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Table Size:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;439.7&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;CLR &gt; LALR &gt; SLR&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CLR&amp;#45;&amp;gt;TableSize --&gt;
&lt;g id=&quot;edge7&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CLR-&gt;TableSize&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M439.7,-72.72C439.7,-65.01 439.7,-56.44 439.7,-48.46&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;443.2,-48.52 439.7,-38.52 436.2,-48.52 443.2,-48.52&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- LALR --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;LALR&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;346.4,-124 167,-124 167,-72.8 346.4,-72.8 346.4,-124&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;256.7&quot; y=&quot;-109.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;LALR (Look-Ahead LR)&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;256.7&quot; y=&quot;-94.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Merged LR(1) items&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;256.7&quot; y=&quot;-80.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Medium tables, medium power&lt;/text&gt;
&lt;/g&gt;
&lt;!-- LALR&amp;#45;&amp;gt;Power --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;LALR-&gt;Power&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M256.7,-72.72C256.7,-65.01 256.7,-56.44 256.7,-48.46&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;260.2,-48.52 256.7,-38.52 253.2,-48.52 260.2,-48.52&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Grammar --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Grammar&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;299.71,-196.8 213.68,-196.8 213.68,-160 299.71,-160 299.71,-196.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;256.7&quot; y=&quot;-182&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Context-Free&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;256.7&quot; y=&quot;-167.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Grammar&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;SLR --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;SLR&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M214.94,-159.51C193.83,-150.45 167.64,-139.23 143.64,-128.95&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;145.11,-125.77 134.54,-125.05 142.36,-132.2 145.11,-125.77&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;CLR --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;CLR&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M298.68,-159.51C319.91,-150.45 346.24,-139.23 370.37,-128.95&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;371.7,-132.19 379.53,-125.05 368.96,-125.75 371.7,-132.19&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;LALR --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;LALR&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M256.7,-159.7C256.7,-152.61 256.7,-144.17 256.7,-135.88&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;260.2,-135.94 256.7,-125.94 253.2,-135.94 260.2,-135.94&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;SLR:&lt;/strong&gt; Simplest, smallest tables, least powerful — uses FOLLOW sets for reduce decisions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;CLR (LR(1)):&lt;/strong&gt; Most powerful, largest tables (potentially thousands of states) — uses full lookahead sets&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LALR:&lt;/strong&gt; Merges CLR states with same core — table size comparable to SLR, power nearly that of CLR&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Yacc/Bison:&lt;/strong&gt; Generate LALR(1) parsers — the practical sweet spot&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LR(0):&lt;/strong&gt; No lookahead, least powerful — not practical for real languages&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_LR_Parsers Pages: 1 --&gt;
&lt;svg width=&quot;178pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 178.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_LR_Parsers&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 174.12,-165.63 174.12,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;65.63&quot; cy=&quot;-86.21&quot; rx=&quot;50.29&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;65.63&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Lr Parsers&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;109.53&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;109.53&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M79.04,-103.75C84.39,-110.74 90.53,-118.78 95.9,-125.8&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;106.51&quot; y=&quot;-102.17&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;41.67&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;41.67&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M59.21,-67.93C55.72,-57.98 51.42,-45.73 47.95,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;83.33&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../shift-reduce-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;shift-reduce-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Shift Reduce Parser&lt;/a&gt; — LR parsers are shift-reduce parsers with state-based decision tables&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../bottom-up-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;bottom-up-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Bottom-Up Parsing&lt;/a&gt; — all LR parsers are bottom-up&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../first-and-follow-sets&quot; class=&quot;internal alias&quot; data-slug=&quot;first-and-follow-sets&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;FIRST and FOLLOW Sets&lt;/a&gt; — SLR uses FOLLOW sets for conflict resolution&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../wiki/compilerdesign/compiler-construction-tools&quot; class=&quot;internal alias&quot; data-slug=&quot;wiki/compilerdesign/compiler-construction-tools&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler Construction Tools&lt;/a&gt; — Yacc/Bison generate LALR parsers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — LR parsing is the most widely used syntax analysis method&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;State explosion:&lt;/strong&gt; CLR(1) can have thousands of states for real languages — LALR was invented to solve this&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LALR reduce/reduce conflicts:&lt;/strong&gt; Merging states can introduce reduce/reduce conflicts that didn’t exist in CLR — rare but possible&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Grammar class hierarchy:&lt;/strong&gt; Every SLR grammar is LALR, every LALR grammar is LR(1), but not vice versa&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Yacc uses LALR:&lt;/strong&gt; Most parser generators use LALR(1) — it handles nearly all programming language constructs&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>lr0-parser</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/lr0-parser" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/lr0-parser.md" />
    <summary>The Problem Shift-reduce parsing needs a systematic way to decide when to shift and when to reduce.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Shift-reduce parsing needs a systematic way to decide when to shift and when to reduce. An LR(0) parser introduces the concept of &lt;strong&gt;LR items&lt;/strong&gt; (productions with a dot position indicating how much has been seen) and uses them to build states for a deterministic finite automaton. This provides a formal foundation for all LR parsing variants.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;An LR(0) parser is the simplest LR parser variant. It uses &lt;strong&gt;LR(0) items&lt;/strong&gt; — grammar productions with a dot marking the current position — to build a finite automaton. The parser uses the automaton’s states and a parsing table derived from them to make shift/reduce decisions. It requires &lt;strong&gt;zero lookahead&lt;/strong&gt; tokens for reduce decisions, making it the least powerful but conceptually simplest LR parser.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The parser is built from LR(0) items. An item &lt;code class=&quot;notranslate&quot;&gt;A → α·β&lt;/code&gt; means we’ve seen &lt;code class=&quot;notranslate&quot;&gt;α&lt;/code&gt; and expect to see &lt;code class=&quot;notranslate&quot;&gt;β&lt;/code&gt;. The closure operation adds items for productions whose left-hand side follows the dot. The goto operation transitions on grammar symbols. The resulting collection of items (LR(0) states) forms the automaton. The ACTION table: shift based on terminal transitions, reduce when a state has a reduce item (a dot at the end), accept when reducing the augmented start symbol. If any state has both shift and reduce items (shift/reduce conflict) or multiple reduce items (reduce/reduce conflict), the grammar is not LR(0).&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: lr0 Pages: 1 --&gt;
&lt;svg width=&quot;453pt&quot; height=&quot;102pt&quot; viewBox=&quot;0.00 0.00 453.00 102.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 98.4)&quot;&gt;
&lt;title&gt;lr0&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-98.4 448.97,-98.4 448.97,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Items --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Items&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;82.01,-87.2 0,-87.2 0,-7.2 82.01,-7.2 82.01,-87.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;41.01&quot; y=&quot;-72.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;LR(0) Items:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;41.01&quot; y=&quot;-58&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E&#039; → ·E&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;41.01&quot; y=&quot;-43.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E  → ·T&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;41.01&quot; y=&quot;-29.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E  → ·E + T&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;41.01&quot; y=&quot;-14.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;T  → ·id&lt;/text&gt;
&lt;/g&gt;
&lt;!-- States --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;States&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;267.66,-94.4 118.01,-94.4 118.01,0 267.66,0 267.66,-94.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;192.84&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;LR(0) States:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;192.84&quot; y=&quot;-65.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;I₀: E&#039;→·E, E→·T, T→·id&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;192.84&quot; y=&quot;-50.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;I₁: E&#039;→E·, E→E·+T&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;192.84&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;I₂: T→id·&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;192.84&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;I₃: E→E+·T&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;192.84&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;I₄: T→id·, E→E+T·&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Items&amp;#45;&amp;gt;States --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Items-&gt;States&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M82.16,-47.2C89.73,-47.2 97.9,-47.2 106.26,-47.2&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;106.22,-50.7 116.22,-47.2 106.22,-43.7 106.22,-50.7&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Table --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Table&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;444.97,-94.4 303.66,-94.4 303.66,0 444.97,0 444.97,-94.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;374.32&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parsing Table:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;374.32&quot; y=&quot;-65.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;State | id | +   | $  | E | T&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;374.32&quot; y=&quot;-50.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  0   | s2 |     |    | 1 | 3&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;374.32&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  1   |    | s4  | acc|   |&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;374.32&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  2   |    | r(T→id)| r |  |&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;374.32&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  3   |    | r(E→T)| r  |  |&lt;/text&gt;
&lt;/g&gt;
&lt;!-- States&amp;#45;&amp;gt;Table --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;States-&gt;Table&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M268.13,-47.2C275.97,-47.2 283.98,-47.2 291.89,-47.2&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;291.88,-50.7 301.88,-47.2 291.88,-43.7 291.88,-50.7&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_lr0 Pages: 1 --&gt;
&lt;svg width=&quot;379pt&quot; height=&quot;200pt&quot; viewBox=&quot;0.00 0.00 379.00 200.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 195.89)&quot;&gt;
&lt;title&gt;semantic_lr0&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-195.89 375.21,-195.89 375.21,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;176.42&quot; cy=&quot;-92.7&quot; rx=&quot;59.86&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;176.42&quot; y=&quot;-88.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;LR(0) Parser&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE1&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;228.28&quot; cy=&quot;-30.16&quot; rx=&quot;56.28&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;228.28&quot; y=&quot;-33.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Shift-Reduce&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;228.28&quot; y=&quot;-20.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Parser&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M191.14,-74.95C196.77,-68.16 203.27,-60.31 209.25,-53.11&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;181.69&quot; y=&quot;-66.73&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE2&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;132.66&quot; cy=&quot;-24.32&quot; rx=&quot;48.49&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;132.66&quot; y=&quot;-27.62&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Bottom-Up&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;132.66&quot; y=&quot;-14.42&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Parsing&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M164.92,-74.74C159.65,-66.51 153.34,-56.64 147.7,-47.83&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;137.81&quot; y=&quot;-63.99&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT1 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT1&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;98.08&quot; cy=&quot;-114.05&quot; rx=&quot;45.07&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;98.08&quot; y=&quot;-110.75&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;SLR Parser&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT1 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M131.76,-104.87C131.62,-104.91 131.48,-104.95 131.34,-104.98&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;65.78&quot; y=&quot;-107.63&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;builds into -- SLR adds FOLLOW&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CON1 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CON1&lt;/title&gt;
&lt;ellipse fill=&quot;#ffe5cc&quot; stroke=&quot;black&quot; cx=&quot;252.21&quot; cy=&quot;-122.16&quot; rx=&quot;58.01&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;252.21&quot; y=&quot;-125.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;LL(1) Parsing&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;252.21&quot; y=&quot;-112.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Algorithm&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;CON1 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--CON1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M213.42,-107.09C213.57,-107.15 213.73,-107.2 213.88,-107.26&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;292.43&quot; y=&quot;-99.07&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;contrasts with -- bottom-up vs top-down&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;172.59&quot; cy=&quot;-173.89&quot; rx=&quot;43.93&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;172.59&quot; y=&quot;-170.59&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;LR Parsers&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M175.55,-111.13C174.92,-124.41 174.08,-142.27 173.45,-155.54&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;109.97&quot; y=&quot;-136.03&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related -- progenitor of LR family&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;LR(0) items:&lt;/strong&gt; Productions with a dot position marking parsing progress&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Zero lookahead:&lt;/strong&gt; Reduce decisions are made without any lookahead information&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LR(0) automaton:&lt;/strong&gt; States are sets of LR(0) items; transitions on grammar symbols&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Least powerful LR:&lt;/strong&gt; Most grammars are not LR(0) — shift/reduce conflicts are common&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Foundation:&lt;/strong&gt; All LR variants (SLR, LALR, CLR) build on the LR(0) item concept&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../shift-reduce-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;shift-reduce-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Shift Reduce Parser&lt;/a&gt; — LR(0) uses shift/reduce operations with formal state tracking&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../bottom-up-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;bottom-up-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Bottom-Up Parsing&lt;/a&gt; — LR(0) is a bottom-up parsing method&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../lr-parsers&quot; class=&quot;internal alias&quot; data-slug=&quot;lr-parsers&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LR Parsers&lt;/a&gt; — SLR, LALR, and CLR extend LR(0) with lookahead&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../predictive-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;predictive-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Predictive Parser&lt;/a&gt; — LR(0) is bottom-up; predictive is top-down&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../lr-parsers&quot; class=&quot;internal alias&quot; data-slug=&quot;lr-parsers&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;SLR Parser&lt;/a&gt; — SLR adds FOLLOW-based lookahead to LR(0) for conflict resolution&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Shift/reduce conflicts:&lt;/strong&gt; Very common in LR(0) — most real grammars need at least SLR&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Reduce/reduce conflicts:&lt;/strong&gt; Two different productions can be reduced in the same state — ambiguous grammar or design issue&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LR(0) ⊂ SLR ⊂ LALR ⊂ CLR:&lt;/strong&gt; Every LR(0) grammar is SLR, but most practical grammars need LALR or CLR&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;State explosion:&lt;/strong&gt; Even LR(0) can produce many states for real grammars — though far fewer than CLR(1)&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>object-code</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/object-code" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/object-code.md" />
    <summary>The Problem A compiler produces code for a single source file, but real programs consist of many source files compiled separately.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A compiler produces code for a single source file, but real programs consist of many source files compiled separately. The raw machine code from a single compilation cannot execute on its own — it needs to be combined with other modules, have its addresses resolved, and be loaded into memory.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Object code is the output of the code generation phase before linking. It contains machine instructions (in binary or assembly form) along with metadata: relocation information (which addresses need adjustment when combined), symbol tables (exported and imported symbols), and debugging information. The linker resolves references and produces a final executable.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The compiler produces a relocatable object file (&lt;code class=&quot;notranslate&quot;&gt;.o&lt;/code&gt; on Unix, &lt;code class=&quot;notranslate&quot;&gt;.obj&lt;/code&gt; on Windows) for each source file. This file has sections for code (&lt;code class=&quot;notranslate&quot;&gt;.text&lt;/code&gt;), initialized data (&lt;code class=&quot;notranslate&quot;&gt;.data&lt;/code&gt;), uninitialized data (&lt;code class=&quot;notranslate&quot;&gt;.bss&lt;/code&gt;), and symbol tables. Addresses in the code are relative or symbolic — the linker fills in actual addresses when it creates the executable.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: object_code Pages: 1 --&gt;
&lt;svg width=&quot;507pt&quot; height=&quot;128pt&quot; viewBox=&quot;0.00 0.00 507.00 128.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 124.2)&quot;&gt;
&lt;title&gt;object_code&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-124.2 503.42,-124.2 503.42,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- SourceA --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;SourceA&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;54,-104.6 0,-104.6 0,-68.6 54,-68.6 54,-104.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;27&quot; y=&quot;-83&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;main.c&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Compiler --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Compiler&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;154.01,-77.6 90,-77.6 90,-41.6 154.01,-41.6 154.01,-77.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;122.01&quot; y=&quot;-56&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Compiler&lt;/text&gt;
&lt;/g&gt;
&lt;!-- SourceA&amp;#45;&amp;gt;Compiler --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;SourceA-&gt;Compiler&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M54.4,-78.94C61.93,-76.75 70.34,-74.31 78.57,-71.92&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;79.51,-75.29 88.14,-69.14 77.56,-68.57 79.51,-75.29&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- SourceB --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;SourceB&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;54,-50.6 0,-50.6 0,-14.6 54,-14.6 54,-50.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;27&quot; y=&quot;-29&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;util.c&lt;/text&gt;
&lt;/g&gt;
&lt;!-- SourceB&amp;#45;&amp;gt;Compiler --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;SourceB-&gt;Compiler&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M54.4,-40.26C61.93,-42.45 70.34,-44.89 78.57,-47.28&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;77.56,-50.63 88.14,-50.06 79.51,-43.91 77.56,-50.63&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- ObjA --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;ObjA&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;298.05,-120.2 190.01,-120.2 190.01,-69 298.05,-69 298.05,-120.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;244.03&quot; y=&quot;-105.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;main.o&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;244.03&quot; y=&quot;-91&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(.text, .data, .bss,&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;244.03&quot; y=&quot;-76.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;relocation table)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Compiler&amp;#45;&amp;gt;ObjA --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Compiler-&gt;ObjA&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M154.23,-68.7C161.85,-70.93 170.28,-73.38 178.82,-75.88&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;177.62,-79.17 188.2,-78.61 179.58,-72.45 177.62,-79.17&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- ObjB --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;ObjB&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;298.05,-51.2 190.01,-51.2 190.01,0 298.05,0 298.05,-51.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;244.03&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;util.o&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;244.03&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(.text, .data, .bss,&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;244.03&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;relocation table)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Compiler&amp;#45;&amp;gt;ObjB --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Compiler-&gt;ObjB&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M154.23,-50.76C161.85,-48.6 170.28,-46.21 178.82,-43.79&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;179.53,-47.23 188.2,-41.13 177.62,-40.49 179.53,-47.23&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Linker --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Linker&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;388.05,-77.6 334.05,-77.6 334.05,-41.6 388.05,-41.6 388.05,-77.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;361.05&quot; y=&quot;-56&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Linker&lt;/text&gt;
&lt;/g&gt;
&lt;!-- ObjA&amp;#45;&amp;gt;Linker --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;ObjA-&gt;Linker&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M298.5,-78.33C306.7,-75.83 315.01,-73.31 322.76,-70.95&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;323.63,-74.34 332.18,-68.08 321.59,-67.64 323.63,-74.34&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- ObjB&amp;#45;&amp;gt;Linker --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;ObjB-&gt;Linker&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M298.5,-41.41C306.7,-43.83 315.01,-46.29 322.76,-48.58&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;321.59,-51.88 332.18,-51.36 323.58,-45.17 321.59,-51.88&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Exec --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Exec&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;499.42,-78 424.05,-78 424.05,-41.2 499.42,-41.2 499.42,-78&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;461.74&quot; y=&quot;-63.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Executable&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;461.74&quot; y=&quot;-48.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(a.out)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Linker&amp;#45;&amp;gt;Exec --&gt;
&lt;g id=&quot;edge7&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Linker-&gt;Exec&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M388.5,-59.6C395.87,-59.6 404.12,-59.6 412.33,-59.6&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;412.32,-63.1 422.32,-59.6 412.32,-56.1 412.32,-63.1&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Not directly executable:&lt;/strong&gt; Needs linking before execution&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contains metadata:&lt;/strong&gt; Relocation info, symbol tables, debug info&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Relocatable:&lt;/strong&gt; Addresses are relative/symbolic, not absolute&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Section structure:&lt;/strong&gt; Code (.text), initialized data (.data), uninitialized data (.bss)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Input to linker:&lt;/strong&gt; The linker combines multiple object files into an executable&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Object_Code Pages: 1 --&gt;
&lt;svg width=&quot;178pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 178.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Object_Code&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 174.12,-165.63 174.12,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;65.63&quot; cy=&quot;-86.21&quot; rx=&quot;58.44&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;65.63&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Object Code&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;109.53&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;109.53&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M79.28,-104.06C84.62,-111.04 90.72,-119.02 96.04,-125.98&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;106.7&quot; y=&quot;-102.42&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;41.67&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;41.67&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M59.21,-67.93C55.72,-57.98 51.42,-45.73 47.95,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;83.33&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Generation&lt;/a&gt; — the code generator produces object code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../linker-and-loader&quot; class=&quot;internal alias&quot; data-slug=&quot;linker-and-loader&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Linker and Loader&lt;/a&gt; — the linker processes object code into executables&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — object code is the final output of compilation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../compiler-pass&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler-pass&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler Pass&lt;/a&gt; — object code output depends on whether the compiler uses single or multiple passes&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Position-independent code (PIC):&lt;/strong&gt; Shared libraries use PIC where all addresses are relative to the program counter — requires different object code structure&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Link-time optimization (LTO):&lt;/strong&gt; Modern compilers can defer optimization to link time, keeping IR in object files&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Debug info formats:&lt;/strong&gt; DWARF (Unix) or CodeView (Windows) — these can significantly increase object file size&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>operator-precedence-parser</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/operator-precedence-parser" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/operator-precedence-parser.md" />
    <summary>The Problem Expression parsing is the most common parsing task, yet full LR parsing is overkill for expression grammars.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Expression parsing is the most common parsing task, yet full LR parsing is overkill for expression grammars. Expressions follow regular patterns: operators have precedence levels and associativity. A simpler, faster parser can handle expressions efficiently.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;An operator precedence parser is a bottom-up parser designed for &lt;strong&gt;operator grammars&lt;/strong&gt; (grammars with no production right-hand side having two adjacent non-terminals). It uses precedence relations (⋖, =, ⋗) between operators to determine handle boundaries — without needing a full LR parsing table. It is simple, fast, and ideal for expressions.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The parser builds a precedence table from the grammar. For each pair of operators, the table says whether the first operator has lower, equal, or higher precedence than the second. The parser uses a stack and compares the precedence of the operator on the stack top with the incoming operator. If the incoming operator has higher precedence, it shifts. If lower, it reduces.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: op_precedence Pages: 1 --&gt;
&lt;svg width=&quot;242pt&quot; height=&quot;391pt&quot; viewBox=&quot;0.00 0.00 242.00 391.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 386.6)&quot;&gt;
&lt;title&gt;op_precedence&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-386.6 237.89,-386.6 237.89,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Expr --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Expr&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;102.39,-360.6 0,-360.6 0,-324.6 102.39,-324.6 102.39,-360.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;51.2&quot; y=&quot;-339&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Input: id + id * id&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Parser&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;154.22,-254.6 74.17,-254.6 74.17,-203.4 154.22,-203.4 154.22,-254.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;114.2&quot; y=&quot;-239.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Operator&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;114.2&quot; y=&quot;-225.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Precedence&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;114.2&quot; y=&quot;-211&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parser&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Expr&amp;#45;&amp;gt;Parser --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Expr-&gt;Parser&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M61.04,-324.16C70,-308.29 83.49,-284.4 94.63,-264.65&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;97.6,-266.51 99.47,-256.09 91.51,-263.07 97.6,-266.51&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Table --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Table&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;233.89,-382.6 120.5,-382.6 120.5,-302.6 233.89,-302.6 233.89,-382.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;177.2&quot; y=&quot;-367.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Precedence Table&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;177.2&quot; y=&quot;-353.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  +   *   id&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;177.2&quot; y=&quot;-339&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;+ ⋗ ⋖ ⋖&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;177.2&quot; y=&quot;-324.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;* ⋗ ⋗ ⋖&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;177.2&quot; y=&quot;-310.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;id ⋗ ⋗ ⋗&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Table&amp;#45;&amp;gt;Parser --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Table-&gt;Parser&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M154.98,-302.25C148.13,-290.12 140.64,-276.85 133.97,-265.02&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;137.06,-263.38 129.09,-256.39 130.96,-266.82 137.06,-263.38&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;159.93&quot; y=&quot;-275.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;guiding&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Stack --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Stack&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;92.7,-166.4 27.69,-166.4 27.69,0 92.7,0 92.7,-166.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;60.2&quot; y=&quot;-151.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stack:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;60.2&quot; y=&quot;-137.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;id&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;60.2&quot; y=&quot;-122.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;F&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;60.2&quot; y=&quot;-108.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;F +&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;60.2&quot; y=&quot;-94&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;F + id&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;60.2&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;F + F&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;60.2&quot; y=&quot;-65.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;F + F *&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;60.2&quot; y=&quot;-50.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;F + F * id&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;60.2&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;F + F * F&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;60.2&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;F + F&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;60.2&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;F&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;Stack --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;Stack&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M104.79,-202.95C101.96,-195.41 98.67,-186.65 95.18,-177.37&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;98.5,-176.26 91.71,-168.13 91.95,-178.72 98.5,-176.26&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Result --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Result&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;227.55,-101.2 110.84,-101.2 110.84,-65.2 227.55,-65.2 227.55,-101.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;169.2&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parsed Expression&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;Result --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;Result&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M123.78,-202.95C133.52,-177.46 148.59,-138.06 158.67,-111.72&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;161.81,-113.31 162.11,-102.72 155.27,-110.81 161.81,-113.31&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Operator grammar:&lt;/strong&gt; No adjacent non-terminals in RHS — the key restriction&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Precedence relations:&lt;/strong&gt; Three relations — ⋖ (less), = (equal), ⋗ (greater)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Handle identification:&lt;/strong&gt; Handles are bounded by ⋖ on the left and ⋗ on the right&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No parsing table:&lt;/strong&gt; Uses a small precedence matrix instead of an LR table&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Limitation:&lt;/strong&gt; Cannot handle unary operators well (require separate treatment)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Operator_Precedence_Parser Pages: 1 --&gt;
&lt;svg width=&quot;208pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 208.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Operator_Precedence_Parser&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 203.77,-165.63 203.77,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;95.28&quot; cy=&quot;-86.21&quot; rx=&quot;95.28&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;95.28&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Operator Precedence &lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;139.18&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;139.18&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M109.17,-104.37C114.4,-111.21 120.33,-118.97 125.53,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;98.31&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;71.33&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;71.33&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M88.86,-67.93C85.37,-57.98 81.07,-45.73 77.6,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;112.98&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../bottom-up-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;bottom-up-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Bottom-Up Parsing&lt;/a&gt; — operator precedence is a bottom-up technique&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../context-free-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;context-free-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Context-Free Grammar&lt;/a&gt; — requires an operator grammar (no adjacent non-terminals)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../lr-parsers&quot; class=&quot;internal alias&quot; data-slug=&quot;lr-parsers&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LR Parsers&lt;/a&gt; — simpler than LR but less powerful&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../shift-reduce-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;shift-reduce-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Shift Reduce Parser&lt;/a&gt; — uses shift/reduce operations but with precedence-based decisions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../wiki/compilerdesign/ambiguous-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;wiki/compilerdesign/ambiguous-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Ambiguous Grammar&lt;/a&gt; — works well with operator grammars that are ambiguous, using precedence to disambiguate&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Unary operators:&lt;/strong&gt; require special handling since they break the binary operator table model&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Non-operator grammars:&lt;/strong&gt; If the grammar has adjacent non-terminals, operator precedence parsing cannot handle it&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Limited scope:&lt;/strong&gt; Best for expressions, not suitable for full programming language syntax&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Precedence table size:&lt;/strong&gt; Grows with the number of operators — for large languages, the table becomes unwieldy&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>Code Optimization Techniques Compared -- Peephole, CSE, Constant Propagation, Liveliness</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/optimization-techniques-compared" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/optimization-techniques-compared.md" />
    <summary>What’s Being Compared Compiler code optimization encompasses a variety of techniques operating at different stages and scopes.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;whats-being-compared&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;What’s Being Compared&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#whats-being-compared&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Compiler code optimization encompasses a variety of techniques operating at different stages and scopes. Peephole optimization, common subexpression elimination (CSE), constant propagation (with folding), and liveliness (live variable) analysis represent four fundamental approaches that differ in scope, level, and mechanism.&lt;/p&gt;
&lt;h2 id=&quot;the-core-tension&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Core Tension&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-core-tension&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Optimization is a trade-off between analysis cost and improvement. Local techniques (peephole) are cheap but miss global opportunities. Global techniques (CSE, CP) capture more but require expensive data-flow analysis. The techniques compound — constant propagation creates CSE opportunities, CSE reduces register pressure, and liveliness analysis enables further dead code elimination.&lt;/p&gt;
&lt;h2 id=&quot;comparison&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Comparison&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#comparison&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;





























































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Dimension&lt;/th&gt;&lt;th&gt;&lt;a href=&quot;../../peephole-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;peephole-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Peephole&lt;/a&gt;&lt;/th&gt;&lt;th&gt;&lt;a href=&quot;../../common-subexpression-elimination&quot; class=&quot;internal alias&quot; data-slug=&quot;common-subexpression-elimination&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;CSE&lt;/a&gt;&lt;/th&gt;&lt;th&gt;&lt;a href=&quot;../../constant-propagation&quot; class=&quot;internal alias&quot; data-slug=&quot;constant-propagation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Constant Propagation&lt;/a&gt;&lt;/th&gt;&lt;th&gt;&lt;a href=&quot;../../liveliness-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;liveliness-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Liveliness Analysis&lt;/a&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Scope&lt;/td&gt;&lt;td&gt;Local (2-5 instructions)&lt;/td&gt;&lt;td&gt;Local + Global&lt;/td&gt;&lt;td&gt;Local + Global&lt;/td&gt;&lt;td&gt;Global (whole CFG)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Level&lt;/td&gt;&lt;td&gt;Target instructions&lt;/td&gt;&lt;td&gt;Intermediate representation (TAC)&lt;/td&gt;&lt;td&gt;Intermediate representation (TAC)&lt;/td&gt;&lt;td&gt;Intermediate representation (TAC)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Mechanism&lt;/td&gt;&lt;td&gt;Pattern matching &amp;#x26; replacement&lt;/td&gt;&lt;td&gt;Available-expression DFA&lt;/td&gt;&lt;td&gt;Reaching-definitions DFA&lt;/td&gt;&lt;td&gt;Backward DFA (USE/DEF)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;What it removes&lt;/td&gt;&lt;td&gt;Redundant loads/stores, no-ops&lt;/td&gt;&lt;td&gt;Redundant recomputation&lt;/td&gt;&lt;td&gt;Runtime constant evaluation&lt;/td&gt;&lt;td&gt;Dead variable assignments&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Analysis cost&lt;/td&gt;&lt;td&gt;Very low (linear scan)&lt;/td&gt;&lt;td&gt;Medium (DFA iteration)&lt;/td&gt;&lt;td&gt;Medium (DFA iteration)&lt;/td&gt;&lt;td&gt;Medium (backward DFA iteration)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Dependency&lt;/td&gt;&lt;td&gt;Requires preceding CSE/CP&lt;/td&gt;&lt;td&gt;Benefits from CP&lt;/td&gt;&lt;td&gt;Independent&lt;/td&gt;&lt;td&gt;Used by CSE and DCE&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Safety&lt;/td&gt;&lt;td&gt;Must preserve flags/conditions&lt;/td&gt;&lt;td&gt;Always safe (same operands)&lt;/td&gt;&lt;td&gt;Conservative (must be sure)&lt;/td&gt;&lt;td&gt;Conservative (assume live)&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;when-to-choose-each&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;When to Choose Each&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#when-to-choose-each&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;&lt;strong&gt;Peephole:&lt;/strong&gt; Run last, after code generation. Best for cleaning up the final target instruction stream — removing redundant loads/stores and no-ops introduced by naive code generation.&lt;/p&gt;
&lt;p dir=&quot;auto&quot;&gt;&lt;strong&gt;CSE:&lt;/strong&gt; Use when expressions repeat (especially in loops). Essential for array address calculations (&lt;code class=&quot;notranslate&quot;&gt;a[i*cols+j]&lt;/code&gt;) and repeated field accesses.&lt;/p&gt;
&lt;p dir=&quot;auto&quot;&gt;&lt;strong&gt;Constant Propagation + Folding:&lt;/strong&gt; Use always — the simplest and safest optimization. Creates cascading simplification opportunities for other optimizations.&lt;/p&gt;
&lt;p dir=&quot;auto&quot;&gt;&lt;strong&gt;Liveliness Analysis:&lt;/strong&gt; Essential prerequisite for dead code elimination and register allocation. Run before register allocation to determine which values need registers.&lt;/p&gt;
&lt;h2 id=&quot;the-insight&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Insight&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-insight&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;These four techniques form a pipeline: constant propagation simplifies expressions (creating redundancies) → CSE eliminates the redundancies → code generator produces naive target code → peephole cleans it up — while liveliness analysis provides liveness information for both CSE (available expressions) and dead code elimination after each step. Individually each saves a few percent; together they can double performance.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: optimization_techniques_compared Pages: 1 --&gt;
&lt;svg width=&quot;616pt&quot; height=&quot;50pt&quot; viewBox=&quot;0.00 0.00 616.00 50.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 45.6)&quot;&gt;
&lt;title&gt;optimization_techniques_compared&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-45.6 611.81,-45.6 611.81,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- A --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;A&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;147.5,-41.6 0,-41.6 0,0 147.5,0 147.5,-41.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;73.75&quot; y=&quot;-25&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Optimization Techniq&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;73.75&quot; y=&quot;-8.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Input&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;373.77,-41.6 226.27,-41.6 226.27,0 373.77,0 373.77,-41.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;300.02&quot; y=&quot;-25&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Optimization Techniq&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;300.02&quot; y=&quot;-8.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Core Mechanism&lt;/text&gt;
&lt;/g&gt;
&lt;!-- A&amp;#45;&amp;gt;B --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;A-&gt;B&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M147.88,-20.8C169.21,-20.8 192.7,-20.8 214.67,-20.8&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;214.48,-24.3 224.48,-20.8 214.48,-17.3 214.48,-24.3&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;186.89&quot; y=&quot;-25&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;triggers&lt;/text&gt;
&lt;/g&gt;
&lt;!-- C --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;C&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;607.81,-41.6 460.31,-41.6 460.31,0 607.81,0 607.81,-41.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;534.06&quot; y=&quot;-25&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Optimization Techniq&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;534.06&quot; y=&quot;-8.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Output&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B&amp;#45;&amp;gt;C --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B-&gt;C&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M374.12,-20.8C397.75,-20.8 424.13,-20.8 448.51,-20.8&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;448.42,-24.3 458.42,-20.8 448.42,-17.3 448.42,-24.3&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;417.04&quot; y=&quot;-25&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;produces&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_optimization_techniques_compared Pages: 1 --&gt;
&lt;svg width=&quot;204pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 204.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_optimization_techniques_compared&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 199.93,-165.63 199.93,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;91.45&quot; cy=&quot;-86.21&quot; rx=&quot;91.45&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;91.45&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Optimization Techniq&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;135.34&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;135.34&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M105.33,-104.37C110.56,-111.21 116.49,-118.97 121.69,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;132.56&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;67.49&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;67.49&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M85.03,-67.93C81.53,-57.98 77.23,-45.73 73.76,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;109.15&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;a href=&quot;../../peephole-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;peephole-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Peephole Optimization&lt;/a&gt; — local target-level optimization&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../common-subexpression-elimination&quot; class=&quot;internal alias&quot; data-slug=&quot;common-subexpression-elimination&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Common Subexpression Elimination&lt;/a&gt; — global IR-level redundancy removal&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../constant-propagation&quot; class=&quot;internal alias&quot; data-slug=&quot;constant-propagation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Constant Propagation&lt;/a&gt; — compile-time constant evaluation and propagation&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../liveliness-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;liveliness-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Liveliness Analysis&lt;/a&gt; — backward data-flow analysis for live variable detection&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — the parent concept encompassing all techniques&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../data-flow-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;data-flow-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Data Flow Analysis&lt;/a&gt; — the analysis framework enabling CSE, CP, and liveliness&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../code-generator-design-issues&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generator-design-issues&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Issues in Code Generator Design&lt;/a&gt; — register allocation depends on liveness&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>parser-introduction</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/parser-introduction" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/parser-introduction.md" />
    <summary>The Problem The token stream from lexical analysis is flat — it has no structure information.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The token stream from lexical analysis is flat — it has no structure information. The compiler needs to reconstruct the hierarchical structure of the program (which statements are inside which blocks, how expressions are grouped) to enable semantic analysis and code generation.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A parser is the component of a compiler that performs syntax analysis. It reads the token stream, checks it against the language’s grammar, and produces a &lt;strong&gt;parse tree&lt;/strong&gt; or syntax tree that represents the grammatical structure. Parsers are broadly classified as &lt;strong&gt;top-down&lt;/strong&gt; or &lt;strong&gt;bottom-up&lt;/strong&gt;, each with different capabilities and trade-offs.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The parser implements a parsing algorithm using a context-free grammar as its specification. It reads tokens left-to-right and builds a parse tree. For each token, it decides which grammar production to apply. Top-down parsers predict productions based on lookahead; bottom-up parsers shift tokens onto a stack and reduce when a production’s right-hand side is recognized.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: parser_intro Pages: 1 --&gt;
&lt;svg width=&quot;307pt&quot; height=&quot;100pt&quot; viewBox=&quot;0.00 0.00 307.00 100.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 95.8)&quot;&gt;
&lt;title&gt;parser_intro&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-95.8 303.48,-95.8 303.48,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Tokens --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Tokens&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;60.67,-91.8 5.99,-91.8 5.99,-55 60.67,-55 60.67,-91.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;33.33&quot; y=&quot;-77&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Token&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;33.33&quot; y=&quot;-62.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stream&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Parser&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;181.13,-63.4 127.13,-63.4 127.13,-27.4 181.13,-27.4 181.13,-63.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;154.13&quot; y=&quot;-41.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parser&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Tokens&amp;#45;&amp;gt;Parser --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Tokens-&gt;Parser&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M60.99,-67.12C77.16,-63.31 98.02,-58.39 115.81,-54.2&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;116.57,-57.61 125.5,-51.91 114.96,-50.8 116.57,-57.61&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- ParseTree --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;ParseTree&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;299.48,-91.8 245.48,-91.8 245.48,-55 299.48,-55 299.48,-91.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;272.48&quot; y=&quot;-77&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parse&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;272.48&quot; y=&quot;-62.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Tree&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;ParseTree --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;ParseTree&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M181.54,-51.75C197.16,-55.51 217.18,-60.33 234.37,-64.47&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;233.17,-67.78 243.71,-66.72 234.81,-60.97 233.17,-67.78&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;213.31&quot; y=&quot;-65.73&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;valid&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Error --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Error&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;299.48,-36.8 245.48,-36.8 245.48,0 299.48,0 299.48,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;272.48&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Syntax&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;272.48&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Error&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;Error --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;Error&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M181.54,-39.27C197.16,-35.65 217.18,-31 234.37,-27.01&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;234.76,-30.51 243.71,-24.84 233.18,-23.7 234.76,-30.51&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;213.31&quot; y=&quot;-38.11&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;invalid&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Grammar --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Grammar&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;66.67,-36.8 0,-36.8 0,0 66.67,0 66.67,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;33.33&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;CFG&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;33.33&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Grammar&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;Parser --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;Parser&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M67.11,-25.85C82.25,-29.29 100.31,-33.39 115.96,-36.95&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;114.85,-40.29 125.37,-39.09 116.4,-33.46 114.85,-40.29&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;96.9&quot; y=&quot;-38.11&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;guide&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Input:&lt;/strong&gt; Token stream from the lexer&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Output:&lt;/strong&gt; Parse tree (or error)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Two main types:&lt;/strong&gt; Top-down (LL) and bottom-up (LR)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Grammar-driven:&lt;/strong&gt; The parser follows the productions of a CFG&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error handling:&lt;/strong&gt; Parser errors are syntax errors — the first and most common type programmers encounter&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Parser_Introduction Pages: 1 --&gt;
&lt;svg width=&quot;196pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 196.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Parser_Introduction&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 191.8,-165.63 191.8,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;83.32&quot; cy=&quot;-86.21&quot; rx=&quot;83.32&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;83.32&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Parser Introduction&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;127.21&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;127.21&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M97.21,-104.37C102.43,-111.21 108.36,-118.97 113.56,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;86.34&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;59.36&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.36&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M76.9,-67.93C73.4,-57.98 69.1,-45.73 65.63,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.02&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../lexical-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;lexical-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Lexical Analysis&lt;/a&gt; — consumes the token stream&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../context-free-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;context-free-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Context-Free Grammar&lt;/a&gt; — uses the CFG for structural decisions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../top-down-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;top-down-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Top-Down Parsing&lt;/a&gt; — subclass of parsing starting from the start symbol&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../bottom-up-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;bottom-up-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Bottom-Up Parsing&lt;/a&gt; — subclass of parsing starting from the input&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../semantic-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;semantic-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Semantic Analysis&lt;/a&gt; — passes the parse tree for semantic checks&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../shift-reduce-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;shift-reduce-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Shift Reduce Parser&lt;/a&gt; — the most common bottom-up parsing technique&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Lookahead:&lt;/strong&gt; More lookahead gives more power (LL(1) vs LL(k)) but increases table size&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Grammar class determines parser:&lt;/strong&gt; LR grammars are more powerful than LL grammars — LR parsers can handle more language constructs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Left recursion:&lt;/strong&gt; Top-down parsers cannot handle left recursion without entering infinite loops&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>Parsing Techniques Compared -- LL(1), LR(0), SLR, CLR, LALR</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/parsing-techniques-compared" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/parsing-techniques-compared.md" />
    <summary>What’s Being Compared Compiler design offers multiple parsing strategies — top-down (LL) and bottom-up (LR) with several variants.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;whats-being-compared&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;What’s Being Compared&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#whats-being-compared&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Compiler design offers multiple parsing strategies — top-down (LL) and bottom-up (LR) with several variants. Each has different power, table size, and implementation complexity. Understanding the spectrum from LL(1) through LALR helps compiler designers choose the right parsing technique for their language and constraints.&lt;/p&gt;
&lt;h2 id=&quot;the-core-tension&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Core Tension&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-core-tension&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Parsing power and table size are inversely related across the LR family, and a fundamental trade-off exists between human-readability (LL grammars require grammar transformations) and language coverage (LR handles more constructs natively). The CLR(1) parser is the most powerful but produces impractically large tables — LALR exists precisely to trade a tiny amount of power for dramatically smaller tables.&lt;/p&gt;
&lt;h2 id=&quot;comparison&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Comparison&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#comparison&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;





































































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Dimension&lt;/th&gt;&lt;th&gt;&lt;a href=&quot;../../ll1-parsing-table&quot; class=&quot;internal alias&quot; data-slug=&quot;ll1-parsing-table&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LL(1)&lt;/a&gt;&lt;/th&gt;&lt;th&gt;&lt;a href=&quot;../../lr0-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;lr0-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LR(0)&lt;/a&gt;&lt;/th&gt;&lt;th&gt;LR: &lt;a href=&quot;../../lr-parsers&quot; class=&quot;internal alias&quot; data-slug=&quot;lr-parsers&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;SLR&lt;/a&gt;&lt;/th&gt;&lt;th&gt;LR: &lt;a href=&quot;../../lr-parsers&quot; class=&quot;internal alias&quot; data-slug=&quot;lr-parsers&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;CLR (LR(1))&lt;/a&gt;&lt;/th&gt;&lt;th&gt;LR: &lt;a href=&quot;../../lr-parsers&quot; class=&quot;internal alias&quot; data-slug=&quot;lr-parsers&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LALR&lt;/a&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Strategy&lt;/td&gt;&lt;td&gt;Top-down, predictive&lt;/td&gt;&lt;td&gt;Bottom-up, shift-reduce&lt;/td&gt;&lt;td&gt;Bottom-up + FOLLOW&lt;/td&gt;&lt;td&gt;Bottom-up + full lookahead&lt;/td&gt;&lt;td&gt;Bottom-up + merged lookahead&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Lookahead&lt;/td&gt;&lt;td&gt;1 token&lt;/td&gt;&lt;td&gt;0 tokens&lt;/td&gt;&lt;td&gt;FOLLOW sets&lt;/td&gt;&lt;td&gt;1 token (precise)&lt;/td&gt;&lt;td&gt;1 token (approximate)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Table size&lt;/td&gt;&lt;td&gt;Small (rows = non-terminals)&lt;/td&gt;&lt;td&gt;Medium (states = LR(0) items)&lt;/td&gt;&lt;td&gt;Medium (SLR states)&lt;/td&gt;&lt;td&gt;Very large (thousands of states)&lt;/td&gt;&lt;td&gt;Medium (merged states)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Parsing power&lt;/td&gt;&lt;td&gt;Least&lt;/td&gt;&lt;td&gt;Less&lt;/td&gt;&lt;td&gt;Medium&lt;/td&gt;&lt;td&gt;Most&lt;/td&gt;&lt;td&gt;Near-CLR&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Grammar requirement&lt;/td&gt;&lt;td&gt;No left recursion, left-factored&lt;/td&gt;&lt;td&gt;LR(0) grammars&lt;/td&gt;&lt;td&gt;SLR grammars&lt;/td&gt;&lt;td&gt;LR(1) grammars&lt;/td&gt;&lt;td&gt;LALR(1) grammars&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Error detection&lt;/td&gt;&lt;td&gt;Immediate (empty table cell)&lt;/td&gt;&lt;td&gt;At state with no action&lt;/td&gt;&lt;td&gt;At state with no action&lt;/td&gt;&lt;td&gt;Most precise&lt;/td&gt;&lt;td&gt;Near CLR&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Conflicts&lt;/td&gt;&lt;td&gt;FIRST/FOLLOW conflicts&lt;/td&gt;&lt;td&gt;Shift/reduce, reduce/reduce&lt;/td&gt;&lt;td&gt;Reduced vs LR(0)&lt;/td&gt;&lt;td&gt;Minimal&lt;/td&gt;&lt;td&gt;Possible reduce/reduce from merging&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;when-to-choose-each&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;When to Choose Each&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#when-to-choose-each&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;&lt;strong&gt;LL(1) / Predictive:&lt;/strong&gt; When you are hand-writing a parser (recursive descent) or when the grammar is naturally LL(1). Used in many production compilers (GCC, Clang use hand-written recursive descent).&lt;/p&gt;
&lt;p dir=&quot;auto&quot;&gt;&lt;strong&gt;LR(0):&lt;/strong&gt; Rarely chosen in practice — almost never sufficient for real languages. Useful only as a pedagogical stepping stone to understanding SLR and LALR.&lt;/p&gt;
&lt;p dir=&quot;auto&quot;&gt;&lt;strong&gt;SLR:&lt;/strong&gt; When the grammar is simple enough that FOLLOW sets provide sufficient conflict resolution. Easier to debug than LALR because the relationship between conflicts and grammar rules is clearer.&lt;/p&gt;
&lt;p dir=&quot;auto&quot;&gt;&lt;strong&gt;CLR / LR(1):&lt;/strong&gt; When maximum parsing power is needed and table size is not a constraint. Research and educational use mainly — impractical for production compilers due to state explosion.&lt;/p&gt;
&lt;p dir=&quot;auto&quot;&gt;&lt;strong&gt;LALR:&lt;/strong&gt; The practical sweet spot — the default for parser generators like Yacc and Bison. Handles nearly all programming language constructs with table sizes comparable to SLR.&lt;/p&gt;
&lt;h2 id=&quot;the-insight&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Insight&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-insight&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The LR family shows a beautiful engineering trade-off: by adding just enough lookahead information (merge LR(1) states with same core), LALR achieves CLR-like power with SLR-like table sizes. Meanwhile, LL(1) represents an entirely different approach — predicting rather than reducing — which is simpler to implement by hand but requires more grammar transformation. Most modern compilers use either hand-written recursive descent (LL) or generated LALR parsers, with the middle variants (LR(0), SLR, CLR) serving mainly as conceptual building blocks.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: parsing_techniques_compared Pages: 1 --&gt;
&lt;svg width=&quot;635pt&quot; height=&quot;50pt&quot; viewBox=&quot;0.00 0.00 635.00 50.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 45.6)&quot;&gt;
&lt;title&gt;parsing_techniques_compared&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-45.6 630.52,-45.6 630.52,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- A --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;A&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;153.74,-41.6 0,-41.6 0,0 153.74,0 153.74,-41.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;76.87&quot; y=&quot;-25&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Parsing Techniques C&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;76.87&quot; y=&quot;-8.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Input&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;386.24,-41.6 232.5,-41.6 232.5,0 386.24,0 386.24,-41.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;309.37&quot; y=&quot;-25&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Parsing Techniques C&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;309.37&quot; y=&quot;-8.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Core Mechanism&lt;/text&gt;
&lt;/g&gt;
&lt;!-- A&amp;#45;&amp;gt;B --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;A-&gt;B&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M153.98,-20.8C175.42,-20.8 198.91,-20.8 220.96,-20.8&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;220.82,-24.3 230.82,-20.8 220.82,-17.3 220.82,-24.3&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;193.12&quot; y=&quot;-25&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;triggers&lt;/text&gt;
&lt;/g&gt;
&lt;!-- C --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;C&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;626.52,-41.6 472.78,-41.6 472.78,0 626.52,0 626.52,-41.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;549.65&quot; y=&quot;-25&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Parsing Techniques C&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;549.65&quot; y=&quot;-8.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Output&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B&amp;#45;&amp;gt;C --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B-&gt;C&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M386.42,-20.8C410.17,-20.8 436.56,-20.8 461.04,-20.8&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;460.99,-24.3 470.99,-20.8 460.99,-17.3 460.99,-24.3&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;429.51&quot; y=&quot;-25&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;produces&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_parsing_techniques_compared Pages: 1 --&gt;
&lt;svg width=&quot;208pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 208.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_parsing_techniques_compared&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 203.77,-165.63 203.77,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;95.28&quot; cy=&quot;-86.21&quot; rx=&quot;95.28&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;95.28&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Parsing Techniques C&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;139.17&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;139.17&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M109.17,-104.37C114.39,-111.21 120.32,-118.97 125.53,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;98.31&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;71.32&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;71.32&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M88.86,-67.93C85.37,-57.98 81.06,-45.73 77.59,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;112.98&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;a href=&quot;../../ll1-parsing-table&quot; class=&quot;internal alias&quot; data-slug=&quot;ll1-parsing-table&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LL(1) Parsing Table&lt;/a&gt; — top-down predictive parsing approach&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../lr0-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;lr0-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LR(0) Parser&lt;/a&gt; — simplest LR variant, zero lookahead&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../lr-parsers&quot; class=&quot;internal alias&quot; data-slug=&quot;lr-parsers&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LR Parsers (SLR, CLR, LALR)&lt;/a&gt; — the three main bottom-up LR variants&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../predictive-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;predictive-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Predictive Parser&lt;/a&gt; — LL(1) table-driven implementation&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../recursive-descent-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;recursive-descent-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Recursive Descent Parser&lt;/a&gt; — hand-written top-down approach&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../top-down-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;top-down-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Top-Down Parsing&lt;/a&gt; — the LL family category&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../bottom-up-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;bottom-up-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Bottom-Up Parsing&lt;/a&gt; — the LR family category&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>peephole-optimization</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/peephole-optimization" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/peephole-optimization.md" />
    <summary>The Problem Code generation produces naive, literal translations of IR instructions.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Code generation produces naive, literal translations of IR instructions. These often contain redundant loads/stores, dead stores, and inefficient instruction sequences that a broader global optimizer missed or that only appear after register allocation. A simple, fast post-processing pass can clean up these local inefficiencies.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Peephole optimization is a simple machine-dependent optimization technique that examines a small sliding window (the “peephole”) of consecutive target instructions and replaces inefficient patterns with better ones. Common patterns include: redundant load/store elimination, constant folding, strength reduction, dead code elimination, and algebraic simplifications like &lt;code class=&quot;notranslate&quot;&gt;x = x + 0 → nop&lt;/code&gt;.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The peephole optimizer scans the instruction stream with a fixed-size window (typically 2-5 instructions). For each window position, it checks against a set of pattern templates. When a pattern matches, it replaces the matched instructions with the optimized replacement. The window is then repositioned to check for cascading opportunities. Common patterns: &lt;code class=&quot;notranslate&quot;&gt;ST R1, M; LD M, R1&lt;/code&gt; → &lt;code class=&quot;notranslate&quot;&gt;ST R1, M&lt;/code&gt; (remove redundant load), &lt;code class=&quot;notranslate&quot;&gt;ADD #0&lt;/code&gt; → &lt;code class=&quot;notranslate&quot;&gt;nop&lt;/code&gt; (remove no-op addition), &lt;code class=&quot;notranslate&quot;&gt;MUL #2&lt;/code&gt; → &lt;code class=&quot;notranslate&quot;&gt;ADD same&lt;/code&gt; (strength reduction).&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: peephole Pages: 1 --&gt;
&lt;svg width=&quot;296pt&quot; height=&quot;289pt&quot; viewBox=&quot;0.00 0.00 296.00 289.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 285)&quot;&gt;
&lt;title&gt;peephole&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-285 291.72,-285 291.72,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Before --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Before&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;108.74,-281 0,-281 0,-186.6 108.74,-186.6 108.74,-281&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;54.37&quot; y=&quot;-266.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Before Peephole:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;54.37&quot; y=&quot;-251.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;LD R1, x&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;54.37&quot; y=&quot;-237.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;ST y, R1&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;54.37&quot; y=&quot;-223&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;LD R1, y&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;54.37&quot; y=&quot;-208.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;ADD R1, #0&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;54.37&quot; y=&quot;-194.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;ST z, R1&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Window --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Window&lt;/title&gt;
&lt;polygon fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; points=&quot;186.73,-138.6 74,-138.6 74,-102.6 186.73,-102.6 186.73,-138.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;130.37&quot; y=&quot;-117&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Peephole Window&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Before&amp;#45;&amp;gt;Window --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Before-&gt;Window&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M86.16,-186.29C95.03,-173.3 104.37,-159.64 112.16,-148.24&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;114.96,-150.35 117.71,-140.12 109.18,-146.4 114.96,-150.35&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- After --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;After&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;179.73,-65.6 81.01,-65.6 81.01,0 179.73,0 179.73,-65.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;130.37&quot; y=&quot;-50.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;After Peephole:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;130.37&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;LD R1, x&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;130.37&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;ST y, R1&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;130.37&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;ST z, R1&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Window&amp;#45;&amp;gt;After --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Window-&gt;After&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M130.37,-102.24C130.37,-95.02 130.37,-86.28 130.37,-77.51&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;133.87,-77.58 130.37,-67.58 126.87,-77.58 133.87,-77.58&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Patterns --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Patterns&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;287.72,-266.6 127.01,-266.6 127.01,-201 287.72,-201 287.72,-266.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;207.37&quot; y=&quot;-251.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Patterns:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;207.37&quot; y=&quot;-237.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;1. ST y,R; LD R,y → del LD&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;207.37&quot; y=&quot;-223&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;2. ADD #0 → nop&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;207.37&quot; y=&quot;-208.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;3. ST x,R; LD R,x → del LD&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Patterns&amp;#45;&amp;gt;Window --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Patterns-&gt;Window&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M185.1,-200.64C173.55,-183.96 159.61,-163.83 148.69,-148.07&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;151.77,-146.35 143.2,-140.13 146.01,-150.34 151.77,-146.35&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;175.59&quot; y=&quot;-159.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;match&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_peephole Pages: 1 --&gt;
&lt;svg width=&quot;405pt&quot; height=&quot;206pt&quot; viewBox=&quot;0.00 0.00 405.00 206.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 202.21)&quot;&gt;
&lt;title&gt;semantic_peephole&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-202.21 400.64,-202.21 400.64,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;176.26&quot; cy=&quot;-92.7&quot; rx=&quot;62.4&quot; ry=&quot;27.72&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;176.26&quot; y=&quot;-96.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Peephole&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;176.26&quot; y=&quot;-81&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Optimization&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE1&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;228.12&quot; cy=&quot;-30.16&quot; rx=&quot;54.54&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;228.12&quot; y=&quot;-33.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;228.12&quot; y=&quot;-20.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Optimization&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M197.95,-66.54C201.62,-62.12 205.4,-57.56 208.99,-53.23&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;184.97&quot; y=&quot;-51.79&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE2&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;132.5&quot; cy=&quot;-24.32&quot; rx=&quot;49.8&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;132.5&quot; y=&quot;-27.62&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;132.5&quot; y=&quot;-14.42&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Generation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M158.97,-65.68C155.14,-59.71 151.13,-53.43 147.41,-47.62&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;85.89&quot; y=&quot;-59.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;applied after -- in code generation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CON1 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CON1&lt;/title&gt;
&lt;ellipse fill=&quot;#ffe5cc&quot; stroke=&quot;black&quot; cx=&quot;97.92&quot; cy=&quot;-114.05&quot; rx=&quot;96.48&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.92&quot; y=&quot;-117.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Common Subexpression&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.92&quot; y=&quot;-104.15&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Elimination&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;CON1 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--CON1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M122.71,-107.29C122.6,-107.32 122.49,-107.35 122.38,-107.38&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.27&quot; y=&quot;-110.04&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;contrasts with -- local vs global&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CON2 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CON2&lt;/title&gt;
&lt;ellipse fill=&quot;#ffe5cc&quot; stroke=&quot;black&quot; cx=&quot;252.05&quot; cy=&quot;-122.16&quot; rx=&quot;53.26&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;252.05&quot; y=&quot;-125.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Constant&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;252.05&quot; y=&quot;-112.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Propagation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;CON2 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--CON2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M223.4,-111.02C223.52,-111.07 223.64,-111.12 223.76,-111.16&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;310.11&quot; y=&quot;-113.79&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;contrasts with -- post-generation vs IR-level&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;172.43&quot; cy=&quot;-173.89&quot; rx=&quot;67.09&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;172.43&quot; y=&quot;-177.19&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Code Generator&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;172.43&quot; y=&quot;-163.99&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Design Issues&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M174.94,-120.64C174.5,-129.91 174.02,-140.15 173.6,-149.16&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;160.51&quot; y=&quot;-137.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Local scope:&lt;/strong&gt; Examines only a small window of instructions (typically 2-5)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Pattern-based:&lt;/strong&gt; Defined by before/after template pairs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Machine-dependent:&lt;/strong&gt; Patterns are specific to the target instruction set&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Post-generation:&lt;/strong&gt; Applied after code generation or during the final phase&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Redundant instruction elimination:&lt;/strong&gt; The most common peephole improvement&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — peephole is a type of machine-dependent optimization&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Generation&lt;/a&gt; — peephole optimizes the generated target code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../common-subexpression-elimination&quot; class=&quot;internal alias&quot; data-slug=&quot;common-subexpression-elimination&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Common Subexpression Elimination&lt;/a&gt; — CSE is global/IR-level; peephole is local/target-level&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../constant-propagation&quot; class=&quot;internal alias&quot; data-slug=&quot;constant-propagation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Constant Propagation&lt;/a&gt; — CP works on IR; peephole works on target instructions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../code-generator-design-issues&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generator-design-issues&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Issues in Code Generator Design&lt;/a&gt; — instruction selection affects peephole opportunities&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Cascading effect:&lt;/strong&gt; One peephole optimization can create an opportunity for another — the optimizer must iterate until no more patterns match&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Oversized window:&lt;/strong&gt; A larger window catches more patterns but costs more to match — most implementations keep it small&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Architecture-specific:&lt;/strong&gt; A peephole optimization on x86 may not apply to ARM — patterns must be defined per target&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Safety:&lt;/strong&gt; Must preserve program semantics — pattern matching must be conservative about flags and condition codes&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>phases-of-compiler</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/phases-of-compiler" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/phases-of-compiler.md" />
    <summary>The Problem Translation from high-level source to low-level target code is too complex for a single monolithic step.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Translation from high-level source to low-level target code is too complex for a single monolithic step. Without a phased approach, error handling, optimization, and code generation would be entangled, making compilers impossible to build, maintain, or reason about.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The compilation process is divided into a sequence of phases, each performing a specific transformation. The six standard phases are: lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code optimization, and code generation. These phases are grouped into the front-end (analysis) and back-end (synthesis).&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The source program passes through each phase sequentially. The output of one phase becomes the input to the next. &lt;strong&gt;Lexical analysis&lt;/strong&gt; produces tokens. &lt;strong&gt;Syntax analysis&lt;/strong&gt; produces a parse tree. &lt;strong&gt;Semantic analysis&lt;/strong&gt; checks type consistency and augments the syntax tree. &lt;strong&gt;Intermediate code generation&lt;/strong&gt; produces a machine-independent IR. &lt;strong&gt;Code optimization&lt;/strong&gt; improves the IR. &lt;strong&gt;Code generation&lt;/strong&gt; produces the target machine code.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: compiler_phases Pages: 1 --&gt;
&lt;svg width=&quot;127pt&quot; height=&quot;548pt&quot; viewBox=&quot;0.00 0.00 127.00 548.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 544)&quot;&gt;
&lt;title&gt;compiler_phases&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-544 122.73,-544 122.73,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Source --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Source&lt;/title&gt;
&lt;polygon fill=&quot;#fff3cd&quot; stroke=&quot;black&quot; points=&quot;99.47,-540 19.26,-540 19.26,-504 99.47,-504 99.47,-540&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.36&quot; y=&quot;-518.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Source Code&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Phase1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Phase1&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;112.61,-468 6.12,-468 6.12,-432 112.61,-432 112.61,-468&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.36&quot; y=&quot;-453.3&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;1. Lexical Analysis&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.36&quot; y=&quot;-440.1&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;(Scanner)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Source&amp;#45;&amp;gt;Phase1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Source-&gt;Phase1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M59.36,-503.7C59.36,-496.41 59.36,-487.73 59.36,-479.54&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;62.86,-479.62 59.36,-469.62 55.86,-479.62 62.86,-479.62&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Phase2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Phase2&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;112.3,-396 6.43,-396 6.43,-360 112.3,-360 112.3,-396&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.36&quot; y=&quot;-381.3&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;2. Syntax Analysis&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.36&quot; y=&quot;-368.1&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;(Parser)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Phase1&amp;#45;&amp;gt;Phase2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Phase1-&gt;Phase2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M59.36,-431.7C59.36,-424.41 59.36,-415.73 59.36,-407.54&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;62.86,-407.62 59.36,-397.62 55.86,-407.62 62.86,-407.62&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Phase3 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Phase3&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;118.41,-324 0.31,-324 0.31,-288 118.41,-288 118.41,-324&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.36&quot; y=&quot;-302.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;3. Semantic Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Phase2&amp;#45;&amp;gt;Phase3 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Phase2-&gt;Phase3&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M59.36,-359.7C59.36,-352.41 59.36,-343.73 59.36,-335.54&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;62.86,-335.62 59.36,-325.62 55.86,-335.62 62.86,-335.62&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Phase4 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Phase4&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;118.73,-252 0,-252 0,-216 118.73,-216 118.73,-252&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.36&quot; y=&quot;-237.3&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;4. Intermediate Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.36&quot; y=&quot;-224.1&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Generation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Phase3&amp;#45;&amp;gt;Phase4 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Phase3-&gt;Phase4&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M59.36,-287.7C59.36,-280.41 59.36,-271.73 59.36,-263.54&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;62.86,-263.62 59.36,-253.62 55.86,-263.62 62.86,-263.62&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Phase5 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Phase5&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;97.93,-180 20.8,-180 20.8,-144 97.93,-144 97.93,-180&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.36&quot; y=&quot;-165.3&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;5. Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.36&quot; y=&quot;-152.1&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Optimization&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Phase4&amp;#45;&amp;gt;Phase5 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Phase4-&gt;Phase5&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M59.36,-215.7C59.36,-208.41 59.36,-199.73 59.36,-191.54&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;62.86,-191.62 59.36,-181.62 55.86,-191.62 62.86,-191.62&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Phase6 --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Phase6&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;94.58,-108 24.15,-108 24.15,-72 94.58,-72 94.58,-108&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.36&quot; y=&quot;-93.3&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;6. Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.36&quot; y=&quot;-80.1&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Generation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Phase5&amp;#45;&amp;gt;Phase6 --&gt;
&lt;g id=&quot;edge6&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Phase5-&gt;Phase6&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M59.36,-143.7C59.36,-136.41 59.36,-127.73 59.36,-119.54&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;62.86,-119.62 59.36,-109.62 55.86,-119.62 62.86,-119.62&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Target --&gt;
&lt;g id=&quot;node8&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Target&lt;/title&gt;
&lt;polygon fill=&quot;#fff3cd&quot; stroke=&quot;black&quot; points=&quot;97.94,-36 20.79,-36 20.79,0 97.94,0 97.94,-36&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;59.36&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Target Code&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Phase6&amp;#45;&amp;gt;Target --&gt;
&lt;g id=&quot;edge7&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Phase6-&gt;Target&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M59.36,-71.7C59.36,-64.41 59.36,-55.73 59.36,-47.54&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;62.86,-47.62 59.36,-37.62 55.86,-47.62 62.86,-47.62&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Phases are independent:&lt;/strong&gt; Each phase has a well-defined input and output&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Front-end:&lt;/strong&gt; Phases 1-3 (source-language dependent)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Back-end:&lt;/strong&gt; Phases 4-6 (target-machine dependent)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Symbol table:&lt;/strong&gt; All phases interact with a shared symbol table&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error handling:&lt;/strong&gt; Each phase can detect and report errors&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Phases_of_a_Compiler Pages: 1 --&gt;
&lt;svg width=&quot;208pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 208.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Phases_of_a_Compiler&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 204.23,-165.63 204.23,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;95.75&quot; cy=&quot;-86.21&quot; rx=&quot;95.75&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;95.75&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Phases Of A Compiler&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;139.64&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;139.64&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M109.63,-104.37C114.86,-111.21 120.79,-118.97 125.99,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;98.77&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;71.79&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;71.79&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M89.33,-67.93C85.83,-57.98 81.53,-45.73 78.06,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;113.45&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../lexical-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;lexical-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Lexical Analysis&lt;/a&gt; — phase 1, scans source code into tokens&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — phase 2, builds parse tree from tokens&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../semantic-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;semantic-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Semantic Analysis&lt;/a&gt; — phase 3, enforces type rules and scoping&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../compiler-pass&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler-pass&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler Pass&lt;/a&gt; — phases can be grouped into passes (single or multi-pass)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../intermediate-code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;intermediate-code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Intermediate Code Generation&lt;/a&gt; — phase 4, generates machine-independent IR&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — phase 5, improves IR quality&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Generation&lt;/a&gt; — phase 6, produces target code&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Phases vs Passes:&lt;/strong&gt; A single pass can combine multiple phases (e.g., lexical and syntax analysis often interleave)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Phase ordering:&lt;/strong&gt; Code optimization can span multiple passes or even be optional for simple compilers&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Symbol table access:&lt;/strong&gt; All phases read/write the symbol table — it is not a phase but a supporting data structure used throughout&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>predictive-parser</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/predictive-parser" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/predictive-parser.md" />
    <summary>The Problem Recursive descent parsers require writing code for each non-terminal. For large grammars, this becomes labor-intensive.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Recursive descent parsers require writing code for each non-terminal. For large grammars, this becomes labor-intensive. The parsing logic — choosing which production to apply based on lookahead — can be mechanized: given a grammar, a parsing table can be constructed automatically, and a generic driver can use the table to parse any LL(1) grammar.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A predictive parser is a table-driven top-down parser that uses a &lt;strong&gt;parsing table&lt;/strong&gt; (rows = non-terminals, columns = terminals) to decide which production to apply. It maintains an explicit stack of grammar symbols, eliminating the need for recursive function calls. The parser is driven by a simple algorithm: look up the top-of-stack non-terminal and current input token in the table to determine the next action.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The predictive parser has an input buffer, a stack (initialized with &lt;code class=&quot;notranslate&quot;&gt;$&lt;/code&gt; and the start symbol), and a parsing table &lt;code class=&quot;notranslate&quot;&gt;M[A, a]&lt;/code&gt;. At each step, it examines &lt;code class=&quot;notranslate&quot;&gt;X&lt;/code&gt; (top of stack) and &lt;code class=&quot;notranslate&quot;&gt;a&lt;/code&gt; (current input token). If &lt;code class=&quot;notranslate&quot;&gt;X&lt;/code&gt; is a terminal matching &lt;code class=&quot;notranslate&quot;&gt;a&lt;/code&gt;, it pops and advances. If &lt;code class=&quot;notranslate&quot;&gt;X&lt;/code&gt; is a non-terminal, it looks up &lt;code class=&quot;notranslate&quot;&gt;M[X, a]&lt;/code&gt; and replaces &lt;code class=&quot;notranslate&quot;&gt;X&lt;/code&gt; with the production’s right-hand side (pushed in reverse order). If the table entry is empty, a syntax error is reported.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: predictive_parser Pages: 1 --&gt;
&lt;svg width=&quot;395pt&quot; height=&quot;269pt&quot; viewBox=&quot;0.00 0.00 395.00 269.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 264.6)&quot;&gt;
&lt;title&gt;predictive_parser&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-264.6 390.92,-264.6 390.92,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Stack --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Stack&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;88.09,-260.6 34.09,-260.6 34.09,-166.2 88.09,-166.2 88.09,-260.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.09&quot; y=&quot;-245.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stack&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.09&quot; y=&quot;-231.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;$ E&#039;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.09&quot; y=&quot;-217&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;$ E&#039; T&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.09&quot; y=&quot;-202.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;$ E&#039; id&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.09&quot; y=&quot;-188.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;$ E&#039;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.09&quot; y=&quot;-173.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;$&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Driver --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Driver&lt;/title&gt;
&lt;polygon fill=&quot;#ffd700&quot; stroke=&quot;black&quot; points=&quot;276.55,-119.8 189.87,-119.8 189.87,-83 276.55,-83 276.55,-119.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;233.21&quot; y=&quot;-105&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Predictive&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;233.21&quot; y=&quot;-90.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parser Driver&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Stack&amp;#45;&amp;gt;Driver --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Stack-&gt;Driver&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M88.3,-196.15C116.6,-177.52 161.94,-147.67 194.16,-126.45&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;196.01,-129.43 202.44,-121 192.16,-123.58 196.01,-129.43&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Table --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Table&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;122.19,-148.4 0,-148.4 0,-54.4 122.19,-54.4 122.19,-148.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.09&quot; y=&quot;-133.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parsing Table M&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.09&quot; y=&quot;-105.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  id    +     $&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.09&quot; y=&quot;-90.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E  E→TE&#039;  -    -&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.09&quot; y=&quot;-76.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E&#039;  -   E&#039;→+TE&#039; E&#039;→ε&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.09&quot; y=&quot;-62&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;T  T→id  -    -&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Table&amp;#45;&amp;gt;Driver --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Table-&gt;Driver&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M122.55,-101.4C140.73,-101.4 160.54,-101.4 178.26,-101.4&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;178.06,-104.9 188.06,-101.4 178.06,-97.9 178.06,-104.9&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;156.03&quot; y=&quot;-104.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;consult&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Input --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Input&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;100.11,-36.8 22.08,-36.8 22.08,0 100.11,0 100.11,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.09&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Input Buffer&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;61.09&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;id + id $&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Input&amp;#45;&amp;gt;Driver --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Input-&gt;Driver&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M100.37,-35.26C107.65,-38.56 115.17,-42.04 122.19,-45.4 143.33,-55.52 166.48,-67.21 186.07,-77.29&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;184.42,-80.37 194.91,-81.85 187.63,-74.15 184.42,-80.37&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Output --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Output&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;386.92,-119.8 313.55,-119.8 313.55,-83 386.92,-83 386.92,-119.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;350.23&quot; y=&quot;-105&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Production&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;350.23&quot; y=&quot;-90.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;sequence&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Driver&amp;#45;&amp;gt;Output --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Driver-&gt;Output&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M277.04,-101.4C285.12,-101.4 293.61,-101.4 301.81,-101.4&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;301.64,-104.9 311.64,-101.4 301.64,-97.9 301.64,-104.9&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_predictive Pages: 1 --&gt;
&lt;svg width=&quot;336pt&quot; height=&quot;206pt&quot; viewBox=&quot;0.00 0.00 336.00 206.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 202.21)&quot;&gt;
&lt;title&gt;semantic_predictive&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-202.21 331.93,-202.21 331.93,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;136.34&quot; cy=&quot;-92.7&quot; rx=&quot;51.67&quot; ry=&quot;27.72&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;136.34&quot; y=&quot;-96.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Predictive&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;136.34&quot; y=&quot;-81&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Parser&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE1&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;188.21&quot; cy=&quot;-30.16&quot; rx=&quot;47.19&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;188.21&quot; y=&quot;-33.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Top-Down&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;188.21&quot; y=&quot;-20.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Parsing&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M157.44,-67.26C161.48,-62.39 165.68,-57.33 169.63,-52.56&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;145.03&quot; y=&quot;-51.81&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE2&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;92.59&quot; cy=&quot;-24.32&quot; rx=&quot;61.88&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;92.59&quot; y=&quot;-27.62&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;FIRST and&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;92.59&quot; y=&quot;-14.42&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;FOLLOW Sets&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M119.3,-66.07C115.56,-60.22 111.62,-54.06 107.94,-48.32&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;95.12&quot; y=&quot;-59.9&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT1 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT1&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;58.01&quot; cy=&quot;-114.05&quot; rx=&quot;58.01&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;58.01&quot; y=&quot;-117.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;LL(1) Parsing&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;58.01&quot; y=&quot;-104.15&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Algorithm&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT1 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M89.87,-105.37C89.74,-105.4 89.61,-105.44 89.47,-105.47&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;73.91&quot; y=&quot;-108.12&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;enables&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CON1 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CON1&lt;/title&gt;
&lt;ellipse fill=&quot;#ffe5cc&quot; stroke=&quot;black&quot; cx=&quot;212.13&quot; cy=&quot;-122.16&quot; rx=&quot;45.89&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;212.13&quot; y=&quot;-125.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Recursive&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;212.13&quot; y=&quot;-112.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Descent&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;CON1 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--CON1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M178.67,-109.15C178.81,-109.21 178.94,-109.26 179.08,-109.31&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;253.4&quot; y=&quot;-111.93&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;contrasts with -- table vs hand-written&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;132.51&quot; cy=&quot;-173.89&quot; rx=&quot;58.01&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;132.51&quot; y=&quot;-177.19&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;LL(1) Parsing&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;132.51&quot; y=&quot;-163.99&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Table&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M135.03,-120.64C134.59,-129.91 134.11,-140.15 133.68,-149.16&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;148.11&quot; y=&quot;-137.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Table-driven:&lt;/strong&gt; A single algorithm works for any LL(1) grammar by swapping the parsing table&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Non-recursive:&lt;/strong&gt; Uses an explicit stack instead of function call recursion&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LL(1):&lt;/strong&gt; Requires exactly one token of lookahead for deterministic decisions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Grammar requirements:&lt;/strong&gt; No left recursion, no common prefixes (must be left-factored)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Efficient:&lt;/strong&gt; Linear time O(n) where n is input length&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../top-down-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;top-down-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Top-Down Parsing&lt;/a&gt; — predictive parsing is a specific top-down approach&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../first-and-follow-sets&quot; class=&quot;internal alias&quot; data-slug=&quot;first-and-follow-sets&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;FIRST and FOLLOW Sets&lt;/a&gt; — used to construct the parsing table&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../ll1-parsing-algorithm&quot; class=&quot;internal alias&quot; data-slug=&quot;ll1-parsing-algorithm&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LL(1) Parsing Algorithm&lt;/a&gt; — the algorithm that drives the table&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../recursive-descent-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;recursive-descent-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Recursive Descent Parser&lt;/a&gt; — table-driven vs hand-written approach&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../ll1-parsing-table&quot; class=&quot;internal alias&quot; data-slug=&quot;ll1-parsing-table&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LL(1) Parsing Table&lt;/a&gt; — the data structure that the algorithm uses&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Multiple entries:&lt;/strong&gt; If the table has multiple entries for the same cell, the grammar is not LL(1)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;ε-productions:&lt;/strong&gt; Handled by using FOLLOW sets — when a non-terminal can derive ε, the parser matches its FOLLOW set&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error detection:&lt;/strong&gt; Errors are detected when the table entry is empty — error recovery routines can use the stack to skip tokens&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LL(1) limitation:&lt;/strong&gt; Not all grammars are LL(1) — operator precedence and certain if-then-else constructs require more lookahead&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>programming-language-generations</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/programming-language-generations" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/programming-language-generations.md" />
    <summary>The Problem Programming languages evolved from machine-specific binary to human-readable abstractions.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Programming languages evolved from machine-specific binary to human-readable abstractions. Understanding this evolution reveals why compilers exist, what problems different translation strategies solve, and how language design shapes compiler architecture.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Programming languages are classified into five generations based on their level of abstraction from machine code. &lt;strong&gt;1GL&lt;/strong&gt; is machine code (binary). &lt;strong&gt;2GL&lt;/strong&gt; is assembly language (mnemonics for machine instructions). &lt;strong&gt;3GL&lt;/strong&gt; is high-level languages (C, C++, Java, Python) — compiled or interpreted. &lt;strong&gt;4GL&lt;/strong&gt; is domain-specific languages (SQL, report generators). &lt;strong&gt;5GL&lt;/strong&gt; is constraint/logic-based languages (Prolog).&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Each generation raises the level of abstraction, making code more human-readable and portable but requiring more complex translation. 1GL needs no translation (runs directly). 2GL needs an assembler. 3GL needs a compiler or interpreter. 4GL and 5GL often use both compilation and interpretation, sometimes with just-in-time compilation for performance.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: lang_generations Pages: 1 --&gt;
&lt;svg width=&quot;141pt&quot; height=&quot;336pt&quot; viewBox=&quot;0.00 0.00 141.00 336.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 332)&quot;&gt;
&lt;title&gt;lang_generations&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-332 136.71,-332 136.71,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Gen1 --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Gen1&lt;/title&gt;
&lt;polygon fill=&quot;#fff3cd&quot; stroke=&quot;black&quot; points=&quot;127.72,-328 4.99,-328 4.99,-291.2 127.72,-291.2 127.72,-328&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;66.36&quot; y=&quot;-313.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;1GL: Machine Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;66.36&quot; y=&quot;-298.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(101010)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Gen2 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Gen2&lt;/title&gt;
&lt;polygon fill=&quot;#ffe5cc&quot; stroke=&quot;black&quot; points=&quot;115.04,-255.2 17.67,-255.2 17.67,-218.4 115.04,-218.4 115.04,-255.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;66.36&quot; y=&quot;-240.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;2GL: Assembly&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;66.36&quot; y=&quot;-226&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(MOV AX, BX)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Gen1&amp;#45;&amp;gt;Gen2 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Gen1-&gt;Gen2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M66.36,-290.73C66.36,-283.47 66.36,-274.88 66.36,-266.77&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;69.86,-266.93 66.36,-256.93 62.86,-266.93 69.86,-266.93&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Gen3 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Gen3&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;120.71,-182.4 12.01,-182.4 12.01,-145.6 120.71,-145.6 120.71,-182.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;66.36&quot; y=&quot;-167.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;3GL: High-Level&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;66.36&quot; y=&quot;-153.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(C, Java, Python)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Gen2&amp;#45;&amp;gt;Gen3 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Gen2-&gt;Gen3&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M66.36,-217.93C66.36,-210.67 66.36,-202.08 66.36,-193.97&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;69.86,-194.13 66.36,-184.13 62.86,-194.13 69.86,-194.13&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Gen4 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Gen4&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;132.71,-109.6 0,-109.6 0,-72.8 132.71,-72.8 132.71,-109.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;66.36&quot; y=&quot;-94.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;4GL: Domain-Specific&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;66.36&quot; y=&quot;-80.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(SQL, Report Gen)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Gen3&amp;#45;&amp;gt;Gen4 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Gen3-&gt;Gen4&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M66.36,-145.13C66.36,-137.87 66.36,-129.28 66.36,-121.17&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;69.86,-121.33 66.36,-111.33 62.86,-121.33 69.86,-121.33&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Gen5 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Gen5&lt;/title&gt;
&lt;polygon fill=&quot;#e8d4ff&quot; stroke=&quot;black&quot; points=&quot;132.39,-36.8 0.33,-36.8 0.33,0 132.39,0 132.39,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;66.36&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;5GL: Constraint/Logic&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;66.36&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Prolog)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Gen4&amp;#45;&amp;gt;Gen5 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Gen4-&gt;Gen5&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M66.36,-72.33C66.36,-65.07 66.36,-56.48 66.36,-48.37&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;69.86,-48.53 66.36,-38.53 62.86,-48.53 69.86,-48.53&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;1GL:&lt;/strong&gt; Binary machine code, platform-specific, no translation needed&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;2GL:&lt;/strong&gt; Assembly language, one-to-one with machine instructions, needs assembler&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;3GL:&lt;/strong&gt; High-level, portable, needs compiler or interpreter&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;4GL:&lt;/strong&gt; Domain-specific, declarative, often uses sophisticated interpreters&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;5GL:&lt;/strong&gt; Constraint/logic based, specify what not how&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Programming_Language_Generations Pages: 1 --&gt;
&lt;svg width=&quot;215pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 215.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Programming_Language_Generations&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 211.43,-165.63 211.43,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;102.94&quot; cy=&quot;-86.21&quot; rx=&quot;102.94&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;102.94&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Programming Language&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;146.84&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;146.84&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M116.83,-104.37C122.06,-111.21 127.99,-118.97 133.19,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;105.97&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;78.99&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;78.99&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M96.52,-67.93C93.03,-57.98 88.73,-45.73 85.26,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;120.65&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler&lt;/a&gt; — compilers translate 3GL and above to lower-level code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../compiler-vs-interpreter&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler-vs-interpreter&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler vs Interpreter&lt;/a&gt; — 3GL+ languages use either or both translation strategies&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — the abstraction gap between generations determines compiler complexity&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../programming-language-generations&quot; class=&quot;internal alias&quot; data-slug=&quot;programming-language-generations&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Programming Language Generations&lt;/a&gt; — the evolution that made compilers necessary&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Classification is rough:&lt;/strong&gt; Some languages span generations (Python is 3GL with some 4GL characteristics)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;JIT blurs the line:&lt;/strong&gt; Java is compiled to bytecode (3GL→intermediate) then JIT-compiled to native at runtime&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Modern trend:&lt;/strong&gt; Most new languages compile to an intermediate representation (bytecode, WASM) rather than directly to machine code&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>recursive-descent-parser</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/recursive-descent-parser" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/recursive-descent-parser.md" />
    <summary>The Problem Parser generators (Yacc, Bison) are powerful but introduce a build-time dependency and generate code that can be hard to debug.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Parser generators (Yacc, Bison) are powerful but introduce a build-time dependency and generate code that can be hard to debug. For many languages — especially simple ones, domain-specific languages, or educational compilers — a hand-written parser is simpler to maintain, debug, and integrate with the rest of the compiler.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A recursive descent parser is a top-down parser where each non-terminal in the grammar becomes a function in the implementation language. These functions call each other recursively, consuming tokens from the input as they match productions. The parser’s call stack mirrors the parse tree structure, and each function is responsible for recognizing one grammar construct.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;For each non-terminal &lt;code class=&quot;notranslate&quot;&gt;A&lt;/code&gt;, a function &lt;code class=&quot;notranslate&quot;&gt;parseA()&lt;/code&gt; is written. The function examines the current token (lookahead) and uses it to decide which production of &lt;code class=&quot;notranslate&quot;&gt;A&lt;/code&gt; to apply. It then calls the functions for the non-terminals in the chosen production’s right-hand side and consumes the expected terminals. For alternatives (&lt;code class=&quot;notranslate&quot;&gt;A → α | β&lt;/code&gt;), the parser uses lookahead to choose the correct branch. Backtracking is possible but rarely used in practice — predictive recursive descent avoids it entirely.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: recursive_descent Pages: 1 --&gt;
&lt;svg width=&quot;198pt&quot; height=&quot;378pt&quot; viewBox=&quot;0.00 0.00 198.00 378.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 374.4)&quot;&gt;
&lt;title&gt;recursive_descent&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-374.4 194.22,-374.4 194.22,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Grammar --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Grammar&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;90.72,-370.4 0,-370.4 0,-304.8 90.72,-304.8 90.72,-370.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;45.36&quot; y=&quot;-355.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Grammar:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;45.36&quot; y=&quot;-341.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E → T E&#039;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;45.36&quot; y=&quot;-326.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E&#039; → + T E&#039; | ε&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;45.36&quot; y=&quot;-312.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;T → id&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CallStack --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CallStack&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;168.04,-268.8 26.68,-268.8 26.68,-160 168.04,-160 168.04,-268.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.36&quot; y=&quot;-254&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Call Stack:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.36&quot; y=&quot;-239.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;parseE()&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.36&quot; y=&quot;-225.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  parseT() → match(id)&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.36&quot; y=&quot;-210.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  parseE&#039;()&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.36&quot; y=&quot;-196.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;    match(&#039;+&#039;)&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.36&quot; y=&quot;-182&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;    parseT() → match(id)&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.36&quot; y=&quot;-167.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;    parseE&#039;()&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;CallStack --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;CallStack&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M59.16,-304.44C62.45,-296.76 66.08,-288.3 69.76,-279.73&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;72.92,-281.24 73.65,-270.67 66.49,-278.48 72.92,-281.24&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Match --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Match&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;154.72,-124 40,-124 40,-72.8 154.72,-72.8 154.72,-124&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.36&quot; y=&quot;-109.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Token matched: id&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.36&quot; y=&quot;-94.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Token matched: +&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.36&quot; y=&quot;-80.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Token matched: id&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CallStack&amp;#45;&amp;gt;Match --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CallStack-&gt;Match&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M97.36,-159.74C97.36,-151.63 97.36,-143.43 97.36,-135.8&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;100.86,-135.88 97.36,-125.88 93.86,-135.88 100.86,-135.88&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Input --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Input&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;190.22,-355.6 108.5,-355.6 108.5,-319.6 190.22,-319.6 190.22,-355.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;149.36&quot; y=&quot;-334&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Input: id + id&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Input&amp;#45;&amp;gt;CallStack --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Input-&gt;CallStack&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M141.91,-319.23C137.32,-308.55 131.13,-294.11 124.82,-279.41&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;128.21,-278.43 121.05,-270.62 121.78,-281.19 128.21,-278.43&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Result --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Result&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;140.7,-36.8 54.02,-36.8 54.02,0 140.7,0 140.7,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.36&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parse tree&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;97.36&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;built implicitly&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Match&amp;#45;&amp;gt;Result --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Match-&gt;Result&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M97.36,-72.72C97.36,-65.01 97.36,-56.44 97.36,-48.46&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;100.86,-48.52 97.36,-38.52 93.86,-48.52 100.86,-48.52&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_rd Pages: 1 --&gt;
&lt;svg width=&quot;425pt&quot; height=&quot;206pt&quot; viewBox=&quot;0.00 0.00 425.00 206.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 202.21)&quot;&gt;
&lt;title&gt;semantic_rd&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-202.21 421.4,-202.21 421.4,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;188.86&quot; cy=&quot;-92.7&quot; rx=&quot;74.66&quot; ry=&quot;27.72&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;188.86&quot; y=&quot;-96.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Recursive&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;188.86&quot; y=&quot;-81&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Descent Parser&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE1&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;240.73&quot; cy=&quot;-30.16&quot; rx=&quot;47.19&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240.73&quot; y=&quot;-33.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Top-Down&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;240.73&quot; y=&quot;-20.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Parsing&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M211.16,-65.81C214.78,-61.44 218.5,-56.96 222.03,-52.7&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;198.09&quot; y=&quot;-61.96&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE2&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;145.11&quot; cy=&quot;-24.32&quot; rx=&quot;56.7&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;145.11&quot; y=&quot;-27.62&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Context-Free&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;145.11&quot; y=&quot;-14.42&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Grammar&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M171.32,-65.29C167.71,-59.64 163.93,-53.73 160.4,-48.22&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;147.35&quot; y=&quot;-59.45&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT1 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT1&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;110.53&quot; cy=&quot;-114.05&quot; rx=&quot;45.46&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;110.53&quot; y=&quot;-117.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Predictive&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;110.53&quot; y=&quot;-104.15&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Parser&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT1 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M128.3,-109.2C128.14,-109.25 127.98,-109.29 127.82,-109.34&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;64.03&quot; y=&quot;-111.97&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;builds into -- table-driven variant&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CON1 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CON1&lt;/title&gt;
&lt;ellipse fill=&quot;#ffe5cc&quot; stroke=&quot;black&quot; cx=&quot;264.65&quot; cy=&quot;-122.16&quot; rx=&quot;58.01&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;264.65&quot; y=&quot;-125.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;LL(1) Parsing&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;264.65&quot; y=&quot;-112.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Table&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;CON1 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--CON1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M240.67,-112.84C240.78,-112.88 240.88,-112.92 240.99,-112.96&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;329.11&quot; y=&quot;-115.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;contrasts with -- hand-written vs table-driven&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;185.03&quot; cy=&quot;-173.89&quot; rx=&quot;51.96&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;185.03&quot; y=&quot;-177.19&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Parser&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;185.03&quot; y=&quot;-163.99&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Introduction&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M187.55,-120.64C187.11,-129.91 186.63,-140.15 186.2,-149.16&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;173.11&quot; y=&quot;-137.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;One function per non-terminal:&lt;/strong&gt; The grammar structure directly maps to code structure&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Lookahead-based choice:&lt;/strong&gt; The function uses the current token to choose which production to apply&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;No backtracking (predictive):&lt;/strong&gt; Efficient recursive descent avoids backtracking by using sufficient lookahead&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Call stack = parse tree:&lt;/strong&gt; The recursion depth matches the nesting depth of the input&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Common in production compilers:&lt;/strong&gt; GCC, Clang, and many production compilers use hand-written recursive descent parsers&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../top-down-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;top-down-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Top-Down Parsing&lt;/a&gt; — recursive descent is the most concrete implementation of top-down parsing&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../context-free-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;context-free-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Context-Free Grammar&lt;/a&gt; — the grammar is coded as mutually recursive functions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Contrasts with:&lt;/strong&gt; &lt;a href=&quot;../../predictive-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;predictive-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Predictive Parser&lt;/a&gt; — recursive descent is hand-written; predictive uses a parsing table&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../parser-introduction&quot; class=&quot;internal alias&quot; data-slug=&quot;parser-introduction&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Parser Introduction&lt;/a&gt; — recursive descent is a type of top-down parser&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../first-and-follow-sets&quot; class=&quot;internal alias&quot; data-slug=&quot;first-and-follow-sets&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;FIRST and FOLLOW Sets&lt;/a&gt; — used to guide lookahead decisions in the hand-written functions&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Left recursion:&lt;/strong&gt; Recursive descent parsers loop infinitely on left-recursive grammars — must eliminate left recursion first&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Backtracking overhead:&lt;/strong&gt; Naive backtracking recursive descent can have exponential worst-case time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error reporting:&lt;/strong&gt; Hand-written parsers can produce better error messages than generated parsers, but require careful coding&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Grammar changes:&lt;/strong&gt; Changing the grammar requires rewriting the corresponding functions — parser generators handle this automatically&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>runtime-environment</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/runtime-environment" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/runtime-environment.md" />
    <summary>The Problem Compiled code must execute in a structured environment that manages memory, handles function calls, and supports the language’s features.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Compiled code must execute in a structured environment that manages memory, handles function calls, and supports the language’s features. Without a runtime environment, the compiled binary would have no mechanism for dynamic memory allocation, function call/return, or exception handling.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The runtime environment is the execution context in which the compiled program runs. It includes the &lt;strong&gt;call stack&lt;/strong&gt; (active function calls), &lt;strong&gt;heap&lt;/strong&gt; (dynamic memory), &lt;strong&gt;activation records&lt;/strong&gt; (function state), and the &lt;strong&gt;runtime system&lt;/strong&gt; (library code for language features like garbage collection, exception handling, and I/O).&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;When a function is called, the runtime pushes an &lt;strong&gt;activation record&lt;/strong&gt; (stack frame) onto the call stack. This record contains the return address, local variables, parameters, and saved registers. When the function returns, the record is popped. The heap grows dynamically as memory is allocated. The runtime system provides services that the compiled code calls — memory allocators, exception handlers, type-checking code.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: runtime_env Pages: 1 --&gt;
&lt;svg width=&quot;723pt&quot; height=&quot;174pt&quot; viewBox=&quot;0.00 0.00 723.00 174.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 170.4)&quot;&gt;
&lt;title&gt;runtime_env&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-170.4 718.6,-170.4 718.6,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;g id=&quot;clust1&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_memory&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;281.51,-26.4 281.51,-158.4 706.6,-158.4 706.6,-26.4 281.51,-26.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;494.06&quot; y=&quot;-141.8&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;Runtime Memory Layout&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Stack --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Stack&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;376.19,-70.8 289.51,-70.8 289.51,-34 376.19,-34 376.19,-70.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;332.85&quot; y=&quot;-56&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stack&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;332.85&quot; y=&quot;-41.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(func frames)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Heap --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Heap&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;562.24,-70.8 465.55,-70.8 465.55,-34 562.24,-34 562.24,-70.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;513.9&quot; y=&quot;-56&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Heap&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;513.9&quot; y=&quot;-41.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(dynamic alloc)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Stack&amp;#45;&amp;gt;Heap --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Stack-&gt;Heap&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;1 5&quot; d=&quot;M376.28,-52.4C399.42,-52.4 428.42,-52.4 453.75,-52.4&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;453.58,-55.9 463.58,-52.4 453.58,-48.9 453.58,-55.9&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;420.87&quot; y=&quot;-55.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;grows down&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Data --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Data&lt;/title&gt;
&lt;polygon fill=&quot;#e8d4ff&quot; stroke=&quot;black&quot; points=&quot;698.6,-70.8 599.24,-70.8 599.24,-34 698.6,-34 698.6,-70.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;648.92&quot; y=&quot;-56&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Static Data&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;648.92&quot; y=&quot;-41.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(globals/statics)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Heap&amp;#45;&amp;gt;Data --&gt;
&lt;!-- Code --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Code&lt;/title&gt;
&lt;polygon fill=&quot;#fff3cd&quot; stroke=&quot;black&quot; points=&quot;375.19,-125.8 290.51,-125.8 290.51,-89 375.19,-89 375.19,-125.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;332.85&quot; y=&quot;-111&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;332.85&quot; y=&quot;-96.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(instructions)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Runtime --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Runtime&lt;/title&gt;
&lt;polygon fill=&quot;#ffd700&quot; stroke=&quot;black&quot; points=&quot;252.51,-36.8 123.81,-36.8 123.81,0 252.51,0 252.51,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;188.16&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Runtime System&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;188.16&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(GC, exceptions, I/O)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Runtime&amp;#45;&amp;gt;Stack --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Runtime-&gt;Stack&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M253,-33.61C261.46,-35.63 270.07,-37.68 278.34,-39.65&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;277.35,-43.01 287.89,-41.93 278.98,-36.2 277.35,-43.01&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Runtime&amp;#45;&amp;gt;Heap --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Runtime-&gt;Heap&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M252.98,-18.3C289.22,-18.87 335.37,-20.63 376.19,-25.4 401.92,-28.4 430.03,-33.61 454.04,-38.66&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;453.12,-42.05 463.63,-40.72 454.59,-35.2 453.12,-42.05&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Program --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Program&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;66.69,-36.8 0,-36.8 0,0 66.69,0 66.69,-36.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;33.34&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Compiled&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;33.34&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Program&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Program&amp;#45;&amp;gt;Runtime --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Program-&gt;Runtime&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M67.13,-18.4C80.4,-18.4 96.3,-18.4 112.13,-18.4&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;112.09,-21.9 122.09,-18.4 112.09,-14.9 112.09,-21.9&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;95.25&quot; y=&quot;-21.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;uses&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Call stack:&lt;/strong&gt; Manages function activation records in LIFO order&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Heap:&lt;/strong&gt; Dynamic memory for objects that outlive the creating function&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Activation record:&lt;/strong&gt; Stores return address, parameters, local variables, temporaries&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Static data:&lt;/strong&gt; Global variables and static local variables allocated at load time&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Runtime system:&lt;/strong&gt; Library of services (memory management, I/O, error handling)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Runtime_Environment Pages: 1 --&gt;
&lt;svg width=&quot;206pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 206.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Runtime_Environment&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 201.84,-165.63 201.84,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;93.36&quot; cy=&quot;-86.21&quot; rx=&quot;93.36&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;93.36&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Runtime Environment&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;137.25&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;137.25&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M107.25,-104.37C112.47,-111.21 118.4,-118.97 123.61,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;96.38&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;69.4&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.4&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M86.94,-67.93C83.45,-57.98 79.14,-45.73 75.67,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;111.06&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../storage-allocation-strategies&quot; class=&quot;internal alias&quot; data-slug=&quot;storage-allocation-strategies&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Storage Allocation Strategies&lt;/a&gt; — runtime uses stack, heap, and static allocation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../linker-and-loader&quot; class=&quot;internal alias&quot; data-slug=&quot;linker-and-loader&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Linker and Loader&lt;/a&gt; — the loader sets up the initial runtime environment&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../static-and-dynamic-scoping&quot; class=&quot;internal alias&quot; data-slug=&quot;static-and-dynamic-scoping&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Static and Dynamic Scoping&lt;/a&gt; — scoping rules affect how runtime resolves variable references&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Generation&lt;/a&gt; — generated code must follow runtime calling conventions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler&lt;/a&gt; — the runtime environment is where compiled programs execute&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Stack overflow:&lt;/strong&gt; Infinite recursion or very deep call chains exhaust the stack&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Garbage collection:&lt;/strong&gt; Managed languages (Java, C#) include GC in the runtime — the compiler must generate GC-friendly code&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Setjmp/Longjmp:&lt;/strong&gt; Non-local jumps bypass normal stack frame unwinding — compilers must handle this carefully&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Trampolines and thunks:&lt;/strong&gt; Dynamic dispatch (virtual functions) requires runtime support from the environment&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>sdt-schemes</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/sdt-schemes" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/sdt-schemes.md" />
    <summary>The Problem Syntax-directed definitions specify semantic rules as equations attached to productions, but they don’t prescribe when during parsing the rules execute.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Syntax-directed definitions specify semantic rules as equations attached to productions, but they don’t prescribe when during parsing the rules execute. For side-effecting actions (like emitting code or printing), the compiler needs an execution order. A translation scheme embeds actions at specific positions within productions to control when they fire.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A syntax-directed translation scheme (SDT scheme) is a context-free grammar with semantic actions embedded at specific positions in the right-hand side of productions. Unlike SDDs (which declaratively specify attributes), SDT schemes prescribe an &lt;strong&gt;evaluation order&lt;/strong&gt; — actions execute when the parser reaches their position during parsing. Actions can be placed before, between, or after the grammar symbols.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;In an SDT scheme, actions are enclosed in braces &lt;code class=&quot;notranslate&quot;&gt;{ }&lt;/code&gt; and placed within the production right-hand side. During top-down parsing, actions execute in left-to-right order as the parser expands non-terminals. During bottom-up parsing, actions placed at the end execute at reduce time; actions in the middle require splitting the production (creating a marker non-terminal) to ensure proper execution order.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: sdt_schemes Pages: 1 --&gt;
&lt;svg width=&quot;170pt&quot; height=&quot;305pt&quot; viewBox=&quot;0.00 0.00 170.00 305.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 300.8)&quot;&gt;
&lt;title&gt;sdt_schemes&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-300.8 166.26,-300.8 166.26,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Grammar --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Grammar&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;162.26,-296.8 0,-296.8 0,-216.8 162.26,-216.8 162.26,-296.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81.13&quot; y=&quot;-282&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;SDT Scheme:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81.13&quot; y=&quot;-267.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E  → T  { print(&#039;T seen&#039;) }  E&#039;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81.13&quot; y=&quot;-253.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E&#039; → + T { print(&#039;+&#039;) } E&#039;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81.13&quot; y=&quot;-238.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E&#039; → ε&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81.13&quot; y=&quot;-224.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;T  → num { print(num.val) }&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parse --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Parse&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;161.65,-180.8 0.61,-180.8 0.61,-72 161.65,-72 161.65,-180.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81.13&quot; y=&quot;-166&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parsing &#039;3 + 4&#039;:&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81.13&quot; y=&quot;-151.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;E → T {print} E&#039;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81.13&quot; y=&quot;-137.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  → num {print val} {print} E&#039;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81.13&quot; y=&quot;-122.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  → prints: 3&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81.13&quot; y=&quot;-108.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  → + T {print} E&#039;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81.13&quot; y=&quot;-94&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  → num {print val} {print}&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81.13&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  → prints: +4&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Grammar&amp;#45;&amp;gt;Parse --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Grammar-&gt;Parse&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M81.13,-216.58C81.13,-208.87 81.13,-200.6 81.13,-192.3&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;84.63,-192.55 81.13,-182.55 77.63,-192.55 84.63,-192.55&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Output --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Output&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;123.99,-36 38.27,-36 38.27,0 123.99,0 123.99,-36&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81.13&quot; y=&quot;-14.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Output: 3 + 4&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parse&amp;#45;&amp;gt;Output --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parse-&gt;Output&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M81.13,-71.58C81.13,-63.29 81.13,-55.02 81.13,-47.58&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;84.63,-47.84 81.13,-37.84 77.63,-47.84 84.63,-47.84&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_sdt_schemes Pages: 1 --&gt;
&lt;svg width=&quot;353pt&quot; height=&quot;206pt&quot; viewBox=&quot;0.00 0.00 353.00 206.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 202.21)&quot;&gt;
&lt;title&gt;semantic_sdt_schemes&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-202.21 348.95,-202.21 348.95,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;198.6&quot; cy=&quot;-92.7&quot; rx=&quot;66.08&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;198.6&quot; y=&quot;-88.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;SDT Schemes&lt;/text&gt;
&lt;/g&gt;
&lt;!-- PRE1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;PRE1&lt;/title&gt;
&lt;ellipse fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; cx=&quot;250.46&quot; cy=&quot;-30.16&quot; rx=&quot;66.65&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;250.46&quot; y=&quot;-33.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Syntax-Directed&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;250.46&quot; y=&quot;-20.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Translation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;PRE1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--PRE1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M213.32,-74.95C218.84,-68.3 225.18,-60.65 231.05,-53.57&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;203.68&quot; y=&quot;-66.96&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;built from&lt;/text&gt;
&lt;/g&gt;
&lt;!-- OUT1 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;OUT1&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;154.85&quot; cy=&quot;-24.32&quot; rx=&quot;49.37&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;154.85&quot; y=&quot;-27.62&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Application&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;154.85&quot; y=&quot;-14.42&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;of SDTs&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;OUT1 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--OUT1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M187.11,-74.74C181.84,-66.51 175.53,-56.64 169.89,-47.83&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;158.23&quot; y=&quot;-63.99&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CON1 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CON1&lt;/title&gt;
&lt;ellipse fill=&quot;#ffe5cc&quot; stroke=&quot;black&quot; cx=&quot;120.26&quot; cy=&quot;-114.05&quot; rx=&quot;66.66&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;120.26&quot; y=&quot;-117.35&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;S-Attributed&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;120.26&quot; y=&quot;-104.15&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;and L-Attributed&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;CON1 --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--CON1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M151.22,-105.61C151.1,-105.65 150.97,-105.68 150.84,-105.72&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;75.52&quot; y=&quot;-108.36&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;contrasts with -- SDD vs SDT scheme&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;274.39&quot; cy=&quot;-122.16&quot; rx=&quot;70.55&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;274.39&quot; y=&quot;-125.46&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Intermediate&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;274.39&quot; y=&quot;-112.26&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Code Generation&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M236.94,-107.6C237.09,-107.66 237.23,-107.72 237.38,-107.78&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;250.92&quot; y=&quot;-110.39&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;194.77&quot; cy=&quot;-173.89&quot; rx=&quot;43.74&quot; ry=&quot;24.32&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;194.77&quot; y=&quot;-177.19&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Semantic&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;194.77&quot; y=&quot;-163.99&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge5&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M197.73,-111.13C197.2,-122.35 196.52,-136.85 195.94,-149.08&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;189.96&quot; y=&quot;-122&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;9.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Actions embedded in productions:&lt;/strong&gt; Semantic actions are placed within the RHS, not just at the end&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Execution order prescribed:&lt;/strong&gt; Actions fire when the parser reaches their position&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Top-down execution:&lt;/strong&gt; Left-to-right, in order of recursive descent expansion&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Bottom-up execution:&lt;/strong&gt; End-of-production actions at reduce time; middle actions need marker productions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Infix notation translation:&lt;/strong&gt; SDT schemes naturally produce prefix, postfix, or infix output&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../syntax-directed-translation&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-directed-translation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax-Directed Translation&lt;/a&gt; — SDT schemes are the operational form of SDDs&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../application-of-sdts&quot; class=&quot;internal alias&quot; data-slug=&quot;application-of-sdts&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Application of SDTs&lt;/a&gt; — SDT schemes are used for practical translation tasks&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../attributed-sdt&quot; class=&quot;internal alias&quot; data-slug=&quot;attributed-sdt&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;S-Attributed and L-Attributed SDTs&lt;/a&gt; — classification of attributes in SDDs vs action placement in schemes&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../intermediate-code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;intermediate-code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Intermediate Code Generation&lt;/a&gt; — SDT schemes often emit intermediate code as actions&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../semantic-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;semantic-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Semantic Analysis&lt;/a&gt; — SDT schemes execute semantic checks during parsing&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Bottom-up middle actions:&lt;/strong&gt; Actions in the middle of a production must be hoisted by creating a marker non-terminal — increases grammar size&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Side effect ordering:&lt;/strong&gt; When multiple actions have side effects, the order must be carefully designed&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Action dependencies:&lt;/strong&gt; An action may reference values from symbols before and after it — placement matters&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;LL vs LR compatibility:&lt;/strong&gt; SDT schemes are natural for LL parsing (left-to-right execution) but require care with bottom-up parsers&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>semantic-analysis</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/semantic-analysis" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/semantic-analysis.md" />
    <summary>The Problem A program can be syntactically correct but semantically meaningless or unsafe.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A program can be syntactically correct but semantically meaningless or unsafe. For example, adding a string to an integer or using an undeclared variable passes syntax analysis but makes no sense. The compiler needs to catch these logical and type errors.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Semantic analysis is the third phase of a compiler. It checks the source program for semantic consistency — type compatibility, variable declaration before use, function call argument matching, and scope rules. It augments the syntax tree with type information and performs type checking.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The semantic analyzer traverses the syntax tree (or parse tree) and verifies semantic rules. It checks that every identifier is declared, that types are compatible in expressions and assignments, that function calls match their signatures, and that control flow constructs are well-formed. It uses the &lt;strong&gt;symbol table&lt;/strong&gt; extensively to resolve identifiers and their attributes.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_analysis Pages: 1 --&gt;
&lt;svg width=&quot;152pt&quot; height=&quot;331pt&quot; viewBox=&quot;0.00 0.00 152.00 331.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 327.4)&quot;&gt;
&lt;title&gt;semantic_analysis&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-327.4 147.64,-327.4 147.64,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- ParseTree --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;ParseTree&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;116.02,-323.4 40.67,-323.4 40.67,-257.8 116.02,-257.8 116.02,-323.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;78.35&quot; y=&quot;-308.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parse Tree&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;78.35&quot; y=&quot;-294.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;    =&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;78.35&quot; y=&quot;-279.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;   / \&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;78.35&quot; y=&quot;-265.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  id  42&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Semantic --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Semantic&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;111.36,-220.8 45.34,-220.8 45.34,-184 111.36,-184 111.36,-220.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;78.35&quot; y=&quot;-206&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Semantic&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;78.35&quot; y=&quot;-191.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Analyzer&lt;/text&gt;
&lt;/g&gt;
&lt;!-- ParseTree&amp;#45;&amp;gt;Semantic --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;ParseTree-&gt;Semantic&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M78.35,-257.5C78.35,-249.36 78.35,-240.67 78.35,-232.68&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;81.85,-232.77 78.35,-222.77 74.85,-232.77 81.85,-232.77&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Augmented --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Augmented&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;80.69,-136 0,-136 0,-99.2 80.69,-99.2 80.69,-136&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;40.35&quot; y=&quot;-121.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Annotated&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;40.35&quot; y=&quot;-106.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Syntax Tree&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Semantic&amp;#45;&amp;gt;Augmented --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Semantic-&gt;Augmented&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M56.66,-183.82C51.66,-178.63 46.94,-172.56 43.98,-166 41.46,-160.41 40.1,-154.06 39.43,-147.88&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;42.93,-147.76 38.96,-137.93 35.94,-148.09 42.93,-147.76&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;73.16&quot; y=&quot;-157&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;with type info&lt;/text&gt;
&lt;/g&gt;
&lt;!-- SymbolTable --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;SymbolTable&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;123.03,-51.2 33.66,-51.2 33.66,0 123.03,0 123.03,-51.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;78.35&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Symbol Table&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;78.35&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;count: int&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;78.35&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;sum: float&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Augmented&amp;#45;&amp;gt;SymbolTable --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Augmented-&gt;SymbolTable&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M47.85,-98.82C52.32,-88.25 58.13,-74.48 63.48,-61.8&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;66.58,-63.47 67.25,-52.89 60.13,-60.74 66.58,-63.47&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;77.14&quot; y=&quot;-72.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;updates&lt;/text&gt;
&lt;/g&gt;
&lt;!-- SymbolTable&amp;#45;&amp;gt;Semantic --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;SymbolTable-&gt;Semantic&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M92.84,-51.61C95.47,-57.23 97.86,-63.28 99.35,-69.2 133.62,-205.32 107.79,-148.28 102.35,-166 101.51,-168.73 100.38,-171.44 99.06,-174.08&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;96.19,-172.06 94.09,-182.44 102.21,-175.64 96.19,-172.06&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;128.91&quot; y=&quot;-114.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;lookup&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Input:&lt;/strong&gt; Parse tree / syntax tree&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Output:&lt;/strong&gt; Annotated syntax tree with type information&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Type checking:&lt;/strong&gt; Ensures operands have compatible types&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scope resolution:&lt;/strong&gt; Maps identifier usages to their declarations&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;L-value/R-value checking:&lt;/strong&gt; Ensures the left side of assignment is an l-value&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Semantic_Analysis Pages: 1 --&gt;
&lt;svg width=&quot;193pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 193.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Semantic_Analysis&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 189.4,-165.63 189.4,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;80.92&quot; cy=&quot;-86.21&quot; rx=&quot;80.92&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;80.92&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Semantic Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;124.81&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;124.81&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M94.81,-104.37C100.03,-111.21 105.96,-118.97 111.16,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;83.94&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;56.96&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;56.96&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M74.5,-67.93C71,-57.98 66.7,-45.73 63.23,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;98.62&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — consumes the parse tree&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../wiki/compilerdesign/symbol-table-in-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;wiki/compilerdesign/symbol-table-in-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Symbol Table&lt;/a&gt; — uses symbol table for identifier resolution and type info&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../intermediate-code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;intermediate-code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Intermediate Code Generation&lt;/a&gt; — the annotated tree feeds IR generation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../static-and-dynamic-scoping&quot; class=&quot;internal alias&quot; data-slug=&quot;static-and-dynamic-scoping&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Static and Dynamic Scoping&lt;/a&gt; — scoping rules are enforced during semantic analysis&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — semantic analysis is phase 3&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Type coercion:&lt;/strong&gt; Languages like C automatically convert int to float — the analyzer must insert implicit type conversion nodes&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Duck typing:&lt;/strong&gt; Dynamically typed languages defer type checking to runtime — semantic analysis in their compilers is lighter&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Function overloading:&lt;/strong&gt; The semantic analyzer must resolve which overloaded function is being called based on argument types&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>shift-reduce-parser</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/shift-reduce-parser" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/shift-reduce-parser.md" />
    <summary>The Problem Building a parse tree bottom-up requires an algorithm to decide when a group of tokens forms a complete grammatical unit (a handle) and should be replaced by a non-terminal.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Building a parse tree bottom-up requires an algorithm to decide when a group of tokens forms a complete grammatical unit (a handle) and should be replaced by a non-terminal. The parser needs a systematic way to stack input and identify production right-hand sides.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A shift-reduce parser is the basic framework for bottom-up parsing. It uses a stack to hold grammar symbols and an input buffer. The parser repeatedly &lt;strong&gt;shifts&lt;/strong&gt; input tokens onto the stack until it recognizes a &lt;strong&gt;handle&lt;/strong&gt; (a production right-hand side) at the top of the stack, then &lt;strong&gt;reduces&lt;/strong&gt; by popping the handle and pushing the corresponding non-terminal.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The parser has four possible actions: &lt;strong&gt;shift&lt;/strong&gt; (push next input token onto stack), &lt;strong&gt;reduce&lt;/strong&gt; (pop handle, push non-terminal), &lt;strong&gt;accept&lt;/strong&gt; (parsing successful), or &lt;strong&gt;error&lt;/strong&gt; (syntax error). The parser continues shifting and reducing until it reduces the entire input to the start symbol. Conflicts arise when the parser can both shift and reduce (shift/reduce conflict) or reduce by two different productions (reduce/reduce conflict).&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: shift_reduce Pages: 1 --&gt;
&lt;svg width=&quot;1066pt&quot; height=&quot;172pt&quot; viewBox=&quot;0.00 0.00 1066.00 172.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 168)&quot;&gt;
&lt;title&gt;shift_reduce&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-168 1062.02,-168 1062.02,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;g id=&quot;clust1&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_parse&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;8,-64 8,-156 925,-156 925,-64 8,-64&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;466.5&quot; y=&quot;-139.4&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;Parsing id + id&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Step1 --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Step1&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;145.69,-123.2 16.31,-123.2 16.31,-72 145.69,-72 145.69,-123.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81&quot; y=&quot;-108.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stack: | id |&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81&quot; y=&quot;-94&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Input: + id $&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;81&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Action: reduce T → id&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Step2 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Step2&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;292.02,-123.2 163.98,-123.2 163.98,-72 292.02,-72 292.02,-123.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;228&quot; y=&quot;-108.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stack: | T |&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;228&quot; y=&quot;-94&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Input: + id $&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;228&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Action: reduce E → T&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Step3 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Step3&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;389.52,-123.2 310.48,-123.2 310.48,-72 389.52,-72 389.52,-123.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;350&quot; y=&quot;-108.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stack: | E |&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;350&quot; y=&quot;-94&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Input: + id $&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;350&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Action: shift&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Step4 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Step4&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;490.96,-123.2 407.04,-123.2 407.04,-72 490.96,-72 490.96,-123.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;449&quot; y=&quot;-108.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stack: | E + |&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;449&quot; y=&quot;-94&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Input: id $&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;449&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Action: shift&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Step5 --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Step5&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;638.69,-123.2 509.31,-123.2 509.31,-72 638.69,-72 638.69,-123.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;574&quot; y=&quot;-108.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stack: | E + id |&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;574&quot; y=&quot;-94&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Input: $&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;574&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Action: reduce T → id&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Step6 --&gt;
&lt;g id=&quot;node6&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Step6&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;806.86,-123.2 657.14,-123.2 657.14,-72 806.86,-72 806.86,-123.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;732&quot; y=&quot;-108.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stack: | E + T |&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;732&quot; y=&quot;-94&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Input: $&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;732&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Action: reduce E → E + T&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Step7 --&gt;
&lt;g id=&quot;node7&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Step7&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;916.69,-123.2 825.31,-123.2 825.31,-72 916.69,-72 916.69,-123.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;871&quot; y=&quot;-108.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stack: | E |&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;871&quot; y=&quot;-94&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Input: $&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;871&quot; y=&quot;-79.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Action: accept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser --&gt;
&lt;g id=&quot;node8&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Parser&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;1019.68,-116 934.32,-116 934.32,-79.2 1019.68,-79.2 1019.68,-116&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;977&quot; y=&quot;-101.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Shift-Reduce&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;977&quot; y=&quot;-86.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parser&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Stack --&gt;
&lt;g id=&quot;node9&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Stack&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;962,-36 908,-36 908,0 962,0 962,-36&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;935&quot; y=&quot;-14.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stack&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;Stack --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;Stack&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M967.47,-78.99C962.22,-69.3 955.6,-57.06 949.71,-46.17&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;952.91,-44.73 945.07,-37.6 946.75,-48.06 952.91,-44.73&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Input --&gt;
&lt;g id=&quot;node10&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Input&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;1058.02,-36 979.98,-36 979.98,0 1058.02,0 1058.02,-36&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;1019&quot; y=&quot;-14.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Input Buffer&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;Input --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;Input&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M986.53,-78.99C991.78,-69.3 998.4,-57.06 1004.29,-46.17&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;1007.25,-48.06 1008.93,-37.6 1001.09,-44.73 1007.25,-48.06&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Stack-based:&lt;/strong&gt; Grammar symbols are pushed/popped from a stack&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Four actions:&lt;/strong&gt; Shift, Reduce, Accept, Error&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Handle identification:&lt;/strong&gt; The parser must identify handles correctly&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Conflicts:&lt;/strong&gt; Shift/reduce and reduce/reduce conflicts require resolution rules&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Foundation:&lt;/strong&gt; LR parsers (SLR, CLR, LALR) are shift-reduce parsers with decision tables&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Shift_Reduce_Parser Pages: 1 --&gt;
&lt;svg width=&quot;200pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 200.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Shift_Reduce_Parser&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 196.11,-165.63 196.11,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;87.62&quot; cy=&quot;-86.21&quot; rx=&quot;87.62&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87.62&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Shift Reduce Parser&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;131.52&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;131.52&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M101.51,-104.37C106.73,-111.21 112.67,-118.97 117.87,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;90.65&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;63.66&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;63.66&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M81.2,-67.93C77.71,-57.98 73.41,-45.73 69.94,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;105.32&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../bottom-up-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;bottom-up-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Bottom-Up Parsing&lt;/a&gt; — shift-reduce is the mechanism for bottom-up parsing&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../lr-parsers&quot; class=&quot;internal alias&quot; data-slug=&quot;lr-parsers&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;LR Parsers&lt;/a&gt; — SLR, CLR, and LALR extend shift-reduce with state-based decision tables&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../operator-precedence-parser&quot; class=&quot;internal alias&quot; data-slug=&quot;operator-precedence-parser&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Operator Precedence Parser&lt;/a&gt; — a simpler shift-reduce variant using operator precedence relations&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../syntax-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;syntax-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Syntax Analysis&lt;/a&gt; — shift-reduce is a key parsing approach&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../wiki/compilerdesign/ambiguous-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;wiki/compilerdesign/ambiguous-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Ambiguous Grammar&lt;/a&gt; — ambiguity causes shift/reduce and reduce/reduce conflicts&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Handle identification:&lt;/strong&gt; The handle is always at the top of the stack — never buried — in viable prefix parsing&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Conflict resolution in Yacc:&lt;/strong&gt; Yacc resolves shift/reduce conflicts in favor of shift, reduce/reduce in favor of the first production listed&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Default reductions:&lt;/strong&gt; In ambiguous situations, the parser may make a default choice that doesn’t match the language designer’s intent&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>Single-Pass vs Multi-Pass Compiler</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/single-pass-vs-multi-pass" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/single-pass-vs-multi-pass.md" />
    <summary>What’s Being Compared A fundamental compiler design decision: how many complete scans (passes) over the source code should the compiler make? Single-pass compilers trade code quality for speed and simplicity; multi-pass compilers trade compilation time for optimization and flexibility.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;whats-being-compared&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;What’s Being Compared&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#whats-being-compared&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A fundamental compiler design decision: how many complete scans (passes) over the source code should the compiler make? Single-pass compilers trade code quality for speed and simplicity; multi-pass compilers trade compilation time for optimization and flexibility.&lt;/p&gt;
&lt;h2 id=&quot;the-core-tension&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Core Tension&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-core-tension&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Single-pass compilers must generate code on the fly, which means they cannot perform global optimizations or handle forward references without restrictions. Multi-pass compilers can analyze the entire program before generating code, enabling sophisticated optimization, but require more memory and compile time.&lt;/p&gt;
&lt;h2 id=&quot;comparison&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Comparison&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#comparison&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;


















































&lt;table&gt;&lt;thead&gt;&lt;tr&gt;&lt;th&gt;Dimension&lt;/th&gt;&lt;th&gt;&lt;a href=&quot;../../compiler-pass&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler-pass&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Single-Pass Compiler&lt;/a&gt;&lt;/th&gt;&lt;th&gt;&lt;a href=&quot;../../compiler-pass&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler-pass&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Multi-Pass Compiler&lt;/a&gt;&lt;/th&gt;&lt;/tr&gt;&lt;/thead&gt;&lt;tbody&gt;&lt;tr&gt;&lt;td&gt;Architecture&lt;/td&gt;&lt;td&gt;One scan, phases interleaved&lt;/td&gt;&lt;td&gt;Multiple scans, separate phases&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Compilation speed&lt;/td&gt;&lt;td&gt;Fast&lt;/td&gt;&lt;td&gt;Slower (more I/O)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Code quality&lt;/td&gt;&lt;td&gt;Lower (local decisions)&lt;/td&gt;&lt;td&gt;Higher (global analysis)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Memory usage&lt;/td&gt;&lt;td&gt;Lower (no IR storage)&lt;/td&gt;&lt;td&gt;Higher (IR between passes)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Language constraints&lt;/td&gt;&lt;td&gt;No forward refs without restriction&lt;/td&gt;&lt;td&gt;Forward refs handled naturally&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Optimization&lt;/td&gt;&lt;td&gt;Minimal&lt;/td&gt;&lt;td&gt;Full suite possible&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Error recovery&lt;/td&gt;&lt;td&gt;Limited&lt;/td&gt;&lt;td&gt;Better (more context)&lt;/td&gt;&lt;/tr&gt;&lt;tr&gt;&lt;td&gt;Classic example&lt;/td&gt;&lt;td&gt;Pascal&lt;/td&gt;&lt;td&gt;C, C++&lt;/td&gt;&lt;/tr&gt;&lt;/tbody&gt;&lt;/table&gt;
&lt;h2 id=&quot;when-to-choose-single-pass&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;When to Choose Single-Pass&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#when-to-choose-single-pass&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;Language designed for single-pass (Pascal)&lt;/li&gt;
&lt;li&gt;Simple, restricted language constructs&lt;/li&gt;
&lt;li&gt;Memory-constrained environments (embedded systems)&lt;/li&gt;
&lt;li&gt;Rapid prototyping compilers&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;when-to-choose-multi-pass&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;When to Choose Multi-Pass&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#when-to-choose-multi-pass&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;Complex language with forward references (C/C++)&lt;/li&gt;
&lt;li&gt;Optimization is important (production compilers)&lt;/li&gt;
&lt;li&gt;Multiple target architectures (same front-end, different back-ends)&lt;/li&gt;
&lt;li&gt;Modular compiler development (separate teams can work on different passes)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;the-insight&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Insight&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-insight&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The single-pass vs multi-pass distinction is not binary — modern compilers like LLVM use a multi-pass architecture but keep all IR in memory rather than writing intermediate files, getting the best of both worlds: fast inter-pass communication and powerful optimization.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: single_pass_vs_multi_pass Pages: 1 --&gt;
&lt;svg width=&quot;604pt&quot; height=&quot;50pt&quot; viewBox=&quot;0.00 0.00 604.00 50.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 45.6)&quot;&gt;
&lt;title&gt;single_pass_vs_multi_pass&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-45.6 600.12,-45.6 600.12,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- A --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;A&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;143.61,-41.6 0,-41.6 0,0 143.61,0 143.61,-41.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;71.8&quot; y=&quot;-25&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Single Pass Vs Multi&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;71.8&quot; y=&quot;-8.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Input&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;B&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;365.98,-41.6 222.37,-41.6 222.37,0 365.98,0 365.98,-41.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;294.18&quot; y=&quot;-25&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Single Pass Vs Multi&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;294.18&quot; y=&quot;-8.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Core Mechanism&lt;/text&gt;
&lt;/g&gt;
&lt;!-- A&amp;#45;&amp;gt;B --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;A-&gt;B&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M144.05,-20.8C165.36,-20.8 188.9,-20.8 210.86,-20.8&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;210.66,-24.3 220.66,-20.8 210.66,-17.3 210.66,-24.3&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;182.99&quot; y=&quot;-25&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;triggers&lt;/text&gt;
&lt;/g&gt;
&lt;!-- C --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;C&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;596.12,-41.6 452.52,-41.6 452.52,0 596.12,0 596.12,-41.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;524.32&quot; y=&quot;-25&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Single Pass Vs Multi&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;524.32&quot; y=&quot;-8.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;14.00&quot;&gt;Output&lt;/text&gt;
&lt;/g&gt;
&lt;!-- B&amp;#45;&amp;gt;C --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;B-&gt;C&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M366.42,-20.8C390.01,-20.8 416.46,-20.8 440.82,-20.8&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;440.71,-24.3 450.71,-20.8 440.71,-17.3 440.71,-24.3&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;409.25&quot; y=&quot;-25&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;produces&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_single_pass_vs_multi_pass Pages: 1 --&gt;
&lt;svg width=&quot;202pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 202.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_single_pass_vs_multi_pass&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 197.54,-165.63 197.54,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;89.05&quot; cy=&quot;-86.21&quot; rx=&quot;89.05&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;89.05&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Single Pass Vs Multi&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;132.95&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;132.95&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M102.94,-104.37C108.16,-111.21 114.09,-118.97 119.3,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;92.08&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;65.09&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;65.09&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M82.63,-67.93C79.14,-57.98 74.83,-45.73 71.36,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;106.75&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;a href=&quot;../../compiler-pass&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler-pass&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler Pass&lt;/a&gt; — the core concept being compared&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — how phases are grouped into passes&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../code-optimization&quot; class=&quot;internal alias&quot; data-slug=&quot;code-optimization&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Optimization&lt;/a&gt; — multi-pass enables sophisticated optimization&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Compiler&lt;/a&gt; — the overall compiler architecture&lt;/li&gt;
&lt;li&gt;&lt;a href=&quot;../../object-code&quot; class=&quot;internal alias&quot; data-slug=&quot;object-code&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Object Code&lt;/a&gt; — how passes affect final code quality&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>static-and-dynamic-scoping</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/static-and-dynamic-scoping" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/static-and-dynamic-scoping.md" />
    <summary>The Problem When a variable name is used in a program, the compiler needs to determine which declaration it refers to.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;When a variable name is used in a program, the compiler needs to determine which declaration it refers to. Different scoping rules yield different bindings for the same code, potentially changing program behavior. The compiler must implement the correct scoping strategy.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;&lt;strong&gt;Static (lexical) scoping&lt;/strong&gt; resolves variable references based on the program’s textual structure at compile time — an inner scope sees bindings from enclosing scopes. &lt;strong&gt;Dynamic scoping&lt;/strong&gt; resolves references based on the call stack at runtime — a function sees variables from the function that called it. Most compiled languages (C, Java) use static scoping; some scripting languages (older Lisp, bash) use dynamic scoping.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;In static scoping, the compiler determines the binding of each variable by examining the nesting structure of the program. A variable in an inner scope refers to the nearest declaration in an enclosing scope. In dynamic scoping, the binding is determined at runtime by walking the call stack — a variable refers to the most recent declaration in the current call chain.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: scoping Pages: 1 --&gt;
&lt;svg width=&quot;352pt&quot; height=&quot;188pt&quot; viewBox=&quot;0.00 0.00 352.00 188.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 184)&quot;&gt;
&lt;title&gt;scoping&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-184 348,-184 348,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;g id=&quot;clust1&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_static&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;8,-8 8,-172 174,-172 174,-8 8,-8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;91&quot; y=&quot;-155.4&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;Static Scoping&lt;/text&gt;
&lt;/g&gt;
&lt;g id=&quot;clust2&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_dynamic&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;182,-8 182,-172 336,-172 336,-8 182,-8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;259&quot; y=&quot;-155.4&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;Dynamic Scoping&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Code1 --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Code1&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;159.86,-139.2 22.14,-139.2 22.14,-88 159.86,-88 159.86,-139.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;91&quot; y=&quot;-124.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;int x = 1;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;91&quot; y=&quot;-110&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;void f() { print(x); }&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;91&quot; y=&quot;-95.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;void g() { int x = 2; f(); }&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Result1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Result1&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;166.02,-52 15.98,-52 15.98,-16 166.02,-16 166.02,-52&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;91&quot; y=&quot;-30.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;f() prints: 1 (uses outer x)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Code1&amp;#45;&amp;gt;Result1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Code1-&gt;Result1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M91,-87.62C91,-79.97 91,-71.5 91,-63.62&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;94.5,-63.83 91,-53.83 87.5,-63.83 94.5,-63.83&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Code2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Code2&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;327.86,-139.2 190.14,-139.2 190.14,-88 327.86,-88 327.86,-139.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;259&quot; y=&quot;-124.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;int x = 1;&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;259&quot; y=&quot;-110&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;void f() { print(x); }&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;259&quot; y=&quot;-95.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;void g() { int x = 2; f(); }&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Result2 --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Result2&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;327.83,-52 190.17,-52 190.17,-16 327.83,-16 327.83,-52&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;259&quot; y=&quot;-30.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;f() prints: 2 (uses g&#039;s x)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Code2&amp;#45;&amp;gt;Result2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Code2-&gt;Result2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M259,-87.62C259,-79.97 259,-71.5 259,-63.62&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;262.5,-63.83 259,-53.83 255.5,-63.83 262.5,-63.83&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Static scoping:&lt;/strong&gt; Binding determined at compile time, based on program text&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamic scoping:&lt;/strong&gt; Binding determined at runtime, based on call stack&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Static advantage:&lt;/strong&gt; Type checking at compile time, faster variable access, more reliable&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamic advantage:&lt;/strong&gt; More flexible, easier to implement (interpreter-friendly)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scope chains:&lt;/strong&gt; Static scoping uses lexical nesting (block structure); dynamic scoping uses stack frames&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Static_and_Dynamic_Scoping Pages: 1 --&gt;
&lt;svg width=&quot;206pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 206.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Static_and_Dynamic_Scoping&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 202.32,-165.63 202.32,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;93.83&quot; cy=&quot;-86.21&quot; rx=&quot;93.83&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;93.83&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Static And Dynamic S&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;137.73&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;137.73&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M107.72,-104.37C112.95,-111.21 118.88,-118.97 124.08,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;96.86&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;69.88&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;69.88&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M87.41,-67.93C83.92,-57.98 79.62,-45.73 76.15,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;111.53&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../semantic-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;semantic-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Semantic Analysis&lt;/a&gt; — scoping rules are enforced during semantic analysis&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../wiki/compilerdesign/symbol-table-in-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;wiki/compilerdesign/symbol-table-in-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Symbol Table&lt;/a&gt; — symbol table lookup implements scope resolution&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../runtime-environment&quot; class=&quot;internal alias&quot; data-slug=&quot;runtime-environment&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Runtime Environment&lt;/a&gt; — dynamic scoping requires runtime support for scope chain traversal&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — scope is determined during analysis phase&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../storage-allocation-strategies&quot; class=&quot;internal alias&quot; data-slug=&quot;storage-allocation-strategies&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Storage Allocation Strategies&lt;/a&gt; — scoping determines when variables are allocated/deallocated&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Static scoping with dynamic features:&lt;/strong&gt; Closures and first-class functions mix static scoping with runtime binding — variables captured in a closure are determined statically&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Dynamic scoping issues:&lt;/strong&gt; Makes programs harder to reason about — a function’s behavior depends on who calls it&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scope holes:&lt;/strong&gt; In some languages, a variable declared in a scope shadows outer declarations — the outer variable becomes inaccessible in the inner scope&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>storage-allocation-strategies</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/storage-allocation-strategies" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/storage-allocation-strategies.md" />
    <summary>The Problem A compiled program needs memory to store variables, data structures, and intermediate values.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A compiled program needs memory to store variables, data structures, and intermediate values. Different kinds of data have different lifetimes — global variables live for the entire program, local variables live only during a function call, and dynamically allocated data can outlive the creating function.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Three storage allocation strategies match data lifetimes: &lt;strong&gt;static allocation&lt;/strong&gt; (compile-time fixed addresses, entire program lifetime), &lt;strong&gt;stack allocation&lt;/strong&gt; (LIFO, function-local data), and &lt;strong&gt;heap allocation&lt;/strong&gt; (dynamic, programmer-controlled lifetime). The compiler decides which strategy to use based on the data’s scope and lifetime.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;&lt;strong&gt;Static allocation:&lt;/strong&gt; Globals and static variables get fixed addresses determined at compile time, stored in the data segment. &lt;strong&gt;Stack allocation:&lt;/strong&gt; Local variables and parameters are allocated on the call stack within activation records — they are automatically deallocated when the function returns. &lt;strong&gt;Heap allocation:&lt;/strong&gt; Memory is explicitly allocated (malloc/new) and freed (free/delete) by the program, managed by the runtime’s memory allocator.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: storage_allocation Pages: 1 --&gt;
&lt;svg width=&quot;495pt&quot; height=&quot;282pt&quot; viewBox=&quot;0.00 0.00 495.00 282.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 278.4)&quot;&gt;
&lt;title&gt;storage_allocation&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-278.4 491,-278.4 491,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;g id=&quot;clust1&quot; class=&quot;cluster&quot;&gt;
&lt;title&gt;cluster_types&lt;/title&gt;
&lt;polygon fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; points=&quot;8,-8 8,-114.4 479,-114.4 479,-8 8,-8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;243.5&quot; y=&quot;-97.8&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;Storage Allocation Strategies&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Static --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Static&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;158.36,-81.6 15.64,-81.6 15.64,-16 158.36,-16 158.36,-81.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87&quot; y=&quot;-66.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Static Allocation&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87&quot; y=&quot;-52.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Globals, statics&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87&quot; y=&quot;-38&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Lifetime: entire program&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;87&quot; y=&quot;-23.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Address: fixed&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Stack --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Stack&lt;/title&gt;
&lt;polygon fill=&quot;#d4edda&quot; stroke=&quot;black&quot; points=&quot;319.36,-81.6 176.64,-81.6 176.64,-16 319.36,-16 319.36,-81.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;248&quot; y=&quot;-66.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Stack Allocation&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;248&quot; y=&quot;-52.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Local variables, params&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;248&quot; y=&quot;-38&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Lifetime: function call&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;248&quot; y=&quot;-23.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Address: relative to SP&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Heap --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Heap&lt;/title&gt;
&lt;polygon fill=&quot;#e8d4ff&quot; stroke=&quot;black&quot; points=&quot;471.03,-81.6 336.97,-81.6 336.97,-16 471.03,-16 471.03,-81.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;404&quot; y=&quot;-66.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Heap Allocation&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;404&quot; y=&quot;-52.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Dynamic data, objects&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;404&quot; y=&quot;-38&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Lifetime: explicit&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;404&quot; y=&quot;-23.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Address: at runtime&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Usage --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Usage&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;335.37,-158.4 160.63,-158.4 160.63,-122.4 335.37,-122.4 335.37,-158.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;248&quot; y=&quot;-136.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Compiler decides per variable&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Usage&amp;#45;&amp;gt;Static --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Usage-&gt;Static&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M184.65,-122.04C178.9,-119.74 173.27,-117.2 168,-114.4 154.83,-107.41 141.7,-98.18 129.96,-88.88&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;132.42,-86.37 122.46,-82.76 127.99,-91.79 132.42,-86.37&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Usage&amp;#45;&amp;gt;Stack --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Usage-&gt;Stack&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M248,-122.12C248,-113.85 248,-103.53 248,-93.29&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;251.5,-93.41 248,-83.41 244.5,-93.41 251.5,-93.41&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Usage&amp;#45;&amp;gt;Heap --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Usage-&gt;Heap&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M311.86,-122.04C317.44,-119.75 322.9,-117.21 328,-114.4 340.45,-107.54 352.72,-98.44 363.66,-89.24&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;365.73,-92.08 370.98,-82.88 361.13,-86.8 365.73,-92.08&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Code --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Code&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;326.2,-274.4 169.8,-274.4 169.8,-194.4 326.2,-194.4 326.2,-274.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;248&quot; y=&quot;-259.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;int g; // static&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;248&quot; y=&quot;-245.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;void f() {&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;248&quot; y=&quot;-230.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  int x; // stack&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;248&quot; y=&quot;-216.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  int* p = malloc(4); // heap&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;248&quot; y=&quot;-202&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;}&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Code&amp;#45;&amp;gt;Usage --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Code-&gt;Usage&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M248,-194.12C248,-186.14 248,-177.88 248,-170.34&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;251.5,-170.37 248,-160.37 244.5,-170.37 251.5,-170.37&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Static:&lt;/strong&gt; Fixed address, fast access, no overhead, entire program lifetime&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stack:&lt;/strong&gt; LIFO, automatic allocation/deallocation, function-scoped&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Heap:&lt;/strong&gt; Flexible, dynamic sizes, explicit management, risk of leaks/fragmentation&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Compiler must decide:&lt;/strong&gt; For each variable, the compiler selects the appropriate strategy&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Recursive functions:&lt;/strong&gt; Require stack allocation (or heap) — static allocation cannot support recursion&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Storage_Allocation_Strategies Pages: 1 --&gt;
&lt;svg width=&quot;201pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 201.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Storage_Allocation_Strategies&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 197.07,-165.63 197.07,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;88.59&quot; cy=&quot;-86.21&quot; rx=&quot;88.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;88.59&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Storage Allocation S&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;132.48&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;132.48&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M102.48,-104.37C107.7,-111.21 113.63,-118.97 118.83,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;91.61&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;64.63&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;64.63&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M82.17,-67.93C78.67,-57.98 74.37,-45.73 70.9,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;106.29&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../runtime-environment&quot; class=&quot;internal alias&quot; data-slug=&quot;runtime-environment&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Runtime Environment&lt;/a&gt; — storage allocation is a key function of the runtime&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../static-and-dynamic-scoping&quot; class=&quot;internal alias&quot; data-slug=&quot;static-and-dynamic-scoping&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Static and Dynamic Scoping&lt;/a&gt; — scoping determines variable lifetimes, which guides allocation strategy&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Generation&lt;/a&gt; — the code generator emits instructions for each allocation type&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../wiki/compilerdesign/symbol-table-in-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;wiki/compilerdesign/symbol-table-in-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Symbol Table&lt;/a&gt; — the symbol table stores the storage class and allocation information&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../linker-and-loader&quot; class=&quot;internal alias&quot; data-slug=&quot;linker-and-loader&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Linker and Loader&lt;/a&gt; — loader sets up static data and stack pointer&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Dangling pointers:&lt;/strong&gt; Heap-allocated memory freed while still referenced — the compiler can’t always detect this&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Memory leaks:&lt;/strong&gt; Heap memory not freed — managed languages use GC to prevent this&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Recursion requires stack:&lt;/strong&gt; Without dynamic allocation (stack or heap), recursive functions cannot work because each call needs separate local variables&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Fragmentation:&lt;/strong&gt; Heap allocation can fragment memory, causing allocation failures even when enough total free memory exists&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>symbol-table-in-compiler</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/symbol-table-in-compiler" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/symbol-table-in-compiler.md" />
    <summary>The Problem A compiler needs to store and retrieve information about identifiers (variables, functions, types) as it processes the source program.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A compiler needs to store and retrieve information about identifiers (variables, functions, types) as it processes the source program. Without a symbol table, the compiler would have no way to enforce scope rules, check type consistency, or generate correct memory references.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A symbol table is a data structure maintained by the compiler that stores information about each identifier in the source program: its name, type, scope, memory location, and other attributes. All phases of the compiler interact with the symbol table — the lexer inserts new identifiers, the semantic analyzer looks up types, and the code generator retrieves memory addresses.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;When the lexer encounters an identifier, it checks the symbol table. If the identifier is new (first occurrence), it’s inserted with tentative attributes. During semantic analysis, the table is updated with type information, scope information, and memory offsets. The code generator reads the table to determine variable addresses. The table is typically implemented as a hash table for O(1) lookup.&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: symbol_table Pages: 1 --&gt;
&lt;svg width=&quot;331pt&quot; height=&quot;238pt&quot; viewBox=&quot;0.00 0.00 331.00 238.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 234)&quot;&gt;
&lt;title&gt;symbol_table&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-234 326.79,-234 326.79,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Lexer --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Lexer&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;83.68,-230 5.67,-230 5.67,-193.2 83.68,-193.2 83.68,-230&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;44.67&quot; y=&quot;-215.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Lexer&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;44.67&quot; y=&quot;-200.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(inserts ids)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- SymTable --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;SymTable&lt;/title&gt;
&lt;polygon fill=&quot;#ffd700&quot; stroke=&quot;black&quot; points=&quot;322.79,-158.4 125.35,-158.4 125.35,-92.8 322.79,-92.8 322.79,-158.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;224.07&quot; y=&quot;-143.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Symbol Table&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;224.07&quot; y=&quot;-129.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  count: int, scope=2, addr=0x100&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;224.07&quot; y=&quot;-114.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  sum: float, scope=1, addr=0x104&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;224.07&quot; y=&quot;-100.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  main: func, →&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Lexer&amp;#45;&amp;gt;SymTable --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Lexer-&gt;SymTable&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M84.17,-192.94C101.86,-184.36 123.52,-173.86 144.43,-163.73&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;145.77,-166.96 153.25,-159.45 142.72,-160.66 145.77,-166.96&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Parser --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Parser&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;89.35,-175 0,-175 0,-138.2 89.35,-138.2 89.35,-175&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;44.67&quot; y=&quot;-160.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parser&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;44.67&quot; y=&quot;-145.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(scope mgmt)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;SymTable --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;SymTable&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M89.52,-148.94C97.16,-147.6 105.39,-146.16 113.89,-144.68&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;114.45,-148.13 123.7,-142.96 113.25,-141.24 114.45,-148.13&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- Semantic --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Semantic&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;77.68,-120.2 11.66,-120.2 11.66,-69 77.68,-69 77.68,-120.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;44.67&quot; y=&quot;-105.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Semantic&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;44.67&quot; y=&quot;-91&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Analyzer&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;44.67&quot; y=&quot;-76.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(types)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Semantic&amp;#45;&amp;gt;SymTable --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Semantic-&gt;SymTable&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M77.8,-100.21C88.41,-102.07 100.87,-104.25 113.96,-106.53&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;113.13,-109.94 123.58,-108.21 114.33,-103.05 113.13,-109.94&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- CodeGen --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;CodeGen&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;84.35,-51.2 5,-51.2 5,0 84.35,0 84.35,-51.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;44.67&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Code&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;44.67&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Generator&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;44.67&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(addresses)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- CodeGen&amp;#45;&amp;gt;SymTable --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;CodeGen-&gt;SymTable&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M84.6,-47.55C105.04,-59.07 130.69,-73.52 154.34,-86.86&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;152.58,-89.88 163.01,-91.75 156.02,-83.79 152.58,-89.88&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;All-phase interaction:&lt;/strong&gt; Every compiler phase reads from or writes to the symbol table&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Scope management:&lt;/strong&gt; Symbols are organized by scope — entering a scope pushes a new layer, exiting pops it&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Typical operations:&lt;/strong&gt; insert(name), lookup(name), delete(name), update(attributes)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Implementation:&lt;/strong&gt; Hash table for primary access, often with linked lists for scope chains&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Stored information:&lt;/strong&gt; Name, type, scope level, size, memory offset, line number&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Symbol_Table_in_Compiler Pages: 1 --&gt;
&lt;svg width=&quot;211pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 211.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Symbol_Table_in_Compiler&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 206.63,-165.63 206.63,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;98.14&quot; cy=&quot;-86.21&quot; rx=&quot;98.14&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;98.14&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Symbol Table In Comp&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;142.04&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;142.04&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M112.03,-104.37C117.25,-111.21 123.19,-118.97 128.39,-125.77&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;101.17&quot; y=&quot;-119.27&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;74.18&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;74.18&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M91.72,-67.93C88.23,-57.98 83.93,-45.73 80.46,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;115.84&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../lexical-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;lexical-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Lexical Analysis&lt;/a&gt; — lexer inserts identifiers into the symbol table&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../semantic-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;semantic-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Semantic Analysis&lt;/a&gt; — checks and updates type information in the symbol table&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../code-generation&quot; class=&quot;internal alias&quot; data-slug=&quot;code-generation&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Code Generation&lt;/a&gt; — retrieves memory addresses and type info for code emission&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../static-and-dynamic-scoping&quot; class=&quot;internal alias&quot; data-slug=&quot;static-and-dynamic-scoping&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Static and Dynamic Scoping&lt;/a&gt; — scoping strategy determines symbol table organization&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../phases-of-compiler&quot; class=&quot;internal alias&quot; data-slug=&quot;phases-of-compiler&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Phases of a Compiler&lt;/a&gt; — symbol table is used across all phases&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Nested scopes:&lt;/strong&gt; Same name can refer to different variables in different scopes — lookup must search from innermost to outermost&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Forward references:&lt;/strong&gt; In languages allowing forward references (C), the symbol entry may be created before its full type is known&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Overloaded functions:&lt;/strong&gt; C++/Java allow multiple functions with the same name but different parameters — the symbol table must store multiple entries with different signatures&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry><entry>
    <title>syntax-analysis</title>
    <link href="https://animishraa05.github.io/wiki/compilerdesign/syntax-analysis" />
    <link rel="alternate" type="text/markdown" href="https://animishraa05.github.io/wiki/compilerdesign/syntax-analysis.md" />
    <summary>The Problem A token stream is still flat and lacks structure.</summary>
    <published>2026-05-13T00:00:00.000Z</published>
    <updated>2026-05-13T00:00:00.000Z</updated>
    <publishedTime>May 13, 2026</publishedTime>
    <updatedTime>May 13, 2026</updatedTime>
    <category term="dev" label="dev" />
<category term="compiler-design" label="compiler-design" />
    <author>
      <name>Animesh Mishra</name>
      <email>animesh.mishra818@gmail.com</email>
    </author>
    <content type="html">&lt;h2 id=&quot;the-problem&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;The Problem&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#the-problem&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;A token stream is still flat and lacks structure. The compiler needs to verify whether the sequence of tokens forms a valid program according to the language’s grammar — checking for missing semicolons, unbalanced parentheses, incorrect statement ordering, and structural violations.&lt;/p&gt;
&lt;h2 id=&quot;core-idea&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Core Idea&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#core-idea&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;Syntax analysis (parsing) is the second phase of a compiler. It takes the token stream from the lexer and determines whether it conforms to the grammar rules of the programming language. The output is typically a &lt;strong&gt;parse tree&lt;/strong&gt; (or syntax tree) that represents the grammatical structure of the program.&lt;/p&gt;
&lt;h2 id=&quot;how-it-works&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;How It Works&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#how-it-works&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;p dir=&quot;auto&quot;&gt;The parser reads tokens and applies grammar rules to build a derivation of the program. It uses a &lt;strong&gt;context-free grammar (CFG)&lt;/strong&gt; as its specification. Two major parsing strategies exist: &lt;strong&gt;top-down&lt;/strong&gt; (builds the tree from the start symbol, expanding non-terminals) and &lt;strong&gt;bottom-up&lt;/strong&gt; (builds the tree from the tokens, reducing to the start symbol).&lt;/p&gt;
&lt;h2 id=&quot;visual-explanation&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Visual Explanation&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#visual-explanation&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: syntax_analysis Pages: 1 --&gt;
&lt;svg width=&quot;295pt&quot; height=&quot;232pt&quot; viewBox=&quot;0.00 0.00 295.00 232.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 228.2)&quot;&gt;
&lt;title&gt;syntax_analysis&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-228.2 290.87,-228.2 290.87,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- Tokens --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Tokens&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;200.22,-224.2 75.13,-224.2 75.13,-187.4 200.22,-187.4 200.22,-224.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;137.68&quot; y=&quot;-209.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Token Stream&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;137.68&quot; y=&quot;-195&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;&amp;#x3C;id&gt; &amp;#x3C;=&gt; &amp;#x3C;num&gt; &amp;#x3C;;&gt;&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Parser&lt;/title&gt;
&lt;polygon fill=&quot;#cce5ff&quot; stroke=&quot;black&quot; points=&quot;192.03,-150.4 83.33,-150.4 83.33,-113.6 192.03,-113.6 192.03,-150.4&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;137.68&quot; y=&quot;-135.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parser&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;137.68&quot; y=&quot;-121.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;(Syntax Analysis)&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Tokens&amp;#45;&amp;gt;Parser --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Tokens-&gt;Parser&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M137.68,-187.05C137.68,-179.49 137.68,-170.46 137.68,-161.98&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;141.18,-162.06 137.68,-152.06 134.18,-162.06 141.18,-162.06&quot;&gt;&lt;/polygon&gt;
&lt;/g&gt;
&lt;!-- ParseTree --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;ParseTree&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;75.36,-65.6 0,-65.6 0,0 75.36,0 75.36,-65.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;37.68&quot; y=&quot;-50.8&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Parse Tree&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;37.68&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;    =&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;37.68&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;   / \&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;37.68&quot; y=&quot;-7.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;  id  42&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;ParseTree --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;ParseTree&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M97.08,-113.2C88.54,-108.29 80.03,-102.41 73.1,-95.6 67.21,-89.82 61.99,-82.86 57.48,-75.73&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;60.62,-74.16 52.55,-67.29 54.57,-77.69 60.62,-74.16&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;103.39&quot; y=&quot;-86.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;valid program&lt;/text&gt;
&lt;/g&gt;
&lt;!-- SymbolTable --&gt;
&lt;g id=&quot;node4&quot; class=&quot;node&quot;&gt;
&lt;title&gt;SymbolTable&lt;/title&gt;
&lt;polygon fill=&quot;#f0f4ff&quot; stroke=&quot;black&quot; points=&quot;182.36,-50.8 93,-50.8 93,-14.8 182.36,-14.8 182.36,-50.8&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;137.68&quot; y=&quot;-29.2&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Symbol Table&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;SymbolTable --&gt;
&lt;g id=&quot;edge3&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;SymbolTable&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; stroke-dasharray=&quot;5 2&quot; d=&quot;M137.68,-113.17C137.68,-98.97 137.68,-78.82 137.68,-62.37&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;141.18,-62.7 137.68,-52.7 134.18,-62.7 141.18,-62.7&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;171.31&quot; y=&quot;-86.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;adds identifiers&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Error --&gt;
&lt;g id=&quot;node5&quot; class=&quot;node&quot;&gt;
&lt;title&gt;Error&lt;/title&gt;
&lt;polygon fill=&quot;#ffcccc&quot; stroke=&quot;black&quot; points=&quot;283.02,-51.2 200.33,-51.2 200.33,-14.4 283.02,-14.4 283.02,-51.2&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;241.68&quot; y=&quot;-36.4&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Syntax Error&lt;/text&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;241.68&quot; y=&quot;-22&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;12.00&quot;&gt;Reporting&lt;/text&gt;
&lt;/g&gt;
&lt;!-- Parser&amp;#45;&amp;gt;Error --&gt;
&lt;g id=&quot;edge4&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;Parser-&gt;Error&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M183.97,-113.14C192.84,-108.34 201.57,-102.51 208.68,-95.6 218.26,-86.28 225.7,-73.58 231.06,-62.11&quot;&gt;&lt;/path&gt;
&lt;polygon fill=&quot;black&quot; stroke=&quot;black&quot; points=&quot;234.23,-63.6 234.98,-53.04 227.8,-60.83 234.23,-63.6&quot;&gt;&lt;/polygon&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;252.68&quot; y=&quot;-86.6&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;10.00&quot;&gt;invalid program&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;key-properties&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Key Properties&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#key-properties&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Input:&lt;/strong&gt; Token stream from lexical analysis&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Output:&lt;/strong&gt; Parse tree / syntax tree (or error)&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Grammar-based:&lt;/strong&gt; Uses a context-free grammar to define valid syntax&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error recovery:&lt;/strong&gt; Can report errors and continue parsing to find more errors&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Two strategies:&lt;/strong&gt; Top-down (recursive descent, LL) vs bottom-up (LR, LALR)&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;semantic-network&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Semantic Network&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#semantic-network&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;div class=&quot;graphviz-container&quot; style=&quot;display: flex; justify-content: center; margin: 1.5rem 0; overflow-x: auto;&quot; dir=&quot;auto&quot;&gt;&lt;!--?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot; standalone=&quot;no&quot;?--&gt;

&lt;!-- Generated by graphviz version 15.1.0 (0)
 --&gt;
&lt;!-- Title: semantic_Syntax_Analysis Pages: 1 --&gt;
&lt;svg width=&quot;184pt&quot; height=&quot;170pt&quot; viewBox=&quot;0.00 0.00 184.00 170.00&quot; xmlns=&quot;http://www.w3.org/2000/svg&quot; xmlns:xlink=&quot;http://www.w3.org/1999/xlink&quot;&gt;
&lt;g id=&quot;graph0&quot; class=&quot;graph&quot; transform=&quot;scale(1 1) rotate(0) translate(4 165.63)&quot;&gt;
&lt;title&gt;semantic_Syntax_Analysis&lt;/title&gt;
&lt;polygon fill=&quot;white&quot; stroke=&quot;none&quot; points=&quot;-4,4 -4,-165.63 179.84,-165.63 179.84,4 -4,4&quot;&gt;&lt;/polygon&gt;
&lt;!-- THIS --&gt;
&lt;g id=&quot;node1&quot; class=&quot;node&quot;&gt;
&lt;title&gt;THIS&lt;/title&gt;
&lt;ellipse fill=&quot;#ffd700&quot; stroke=&quot;black&quot; stroke-width=&quot;2&quot; cx=&quot;71.35&quot; cy=&quot;-86.21&quot; rx=&quot;71.35&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;71.35&quot; y=&quot;-82.31&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;13.00&quot;&gt;Syntax Analysis&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL1 --&gt;
&lt;g id=&quot;node2&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL1&lt;/title&gt;
&lt;ellipse fill=&quot;#f0f0f0&quot; stroke=&quot;black&quot; cx=&quot;115.25&quot; cy=&quot;-143.63&quot; rx=&quot;60.59&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;115.25&quot; y=&quot;-140.33&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Related Concept&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL1 --&gt;
&lt;g id=&quot;edge1&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL1&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M85,-104.06C90.34,-111.04 96.44,-119.02 101.76,-125.98&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;74.34&quot; y=&quot;-119.22&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;related&lt;/text&gt;
&lt;/g&gt;
&lt;!-- REL2 --&gt;
&lt;g id=&quot;node3&quot; class=&quot;node&quot;&gt;
&lt;title&gt;REL2&lt;/title&gt;
&lt;ellipse fill=&quot;#d4edda&quot; stroke=&quot;black&quot; cx=&quot;47.39&quot; cy=&quot;-18&quot; rx=&quot;41.67&quot; ry=&quot;18&quot;&gt;&lt;/ellipse&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;47.39&quot; y=&quot;-14.7&quot; font-family=&quot;Helvetica,sans-Serif&quot; font-size=&quot;11.00&quot;&gt;Builds Into&lt;/text&gt;
&lt;/g&gt;
&lt;!-- THIS&amp;#45;&amp;#45;REL2 --&gt;
&lt;g id=&quot;edge2&quot; class=&quot;edge&quot;&gt;
&lt;title&gt;THIS--REL2&lt;/title&gt;
&lt;path fill=&quot;none&quot; stroke=&quot;black&quot; d=&quot;M64.93,-67.93C61.44,-57.98 57.14,-45.73 53.67,-35.86&quot;&gt;&lt;/path&gt;
&lt;text xml:space=&quot;preserve&quot; text-anchor=&quot;middle&quot; x=&quot;89.05&quot; y=&quot;-39.29&quot; font-family=&quot;Times,serif&quot; font-size=&quot;14.00&quot;&gt;builds into&lt;/text&gt;
&lt;/g&gt;
&lt;/g&gt;
&lt;/svg&gt;
&lt;/div&gt;
&lt;h2 id=&quot;connections&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Connections&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#connections&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../lexical-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;lexical-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Lexical Analysis&lt;/a&gt; — consumes the token stream produced by the lexer&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Built from:&lt;/strong&gt; &lt;a href=&quot;../../context-free-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;context-free-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Context-Free Grammar&lt;/a&gt; — the grammar is the specification the parser checks against&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Builds into:&lt;/strong&gt; &lt;a href=&quot;../../semantic-analysis&quot; class=&quot;internal alias&quot; data-slug=&quot;semantic-analysis&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Semantic Analysis&lt;/a&gt; — the parse tree is input for semantic checks&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../top-down-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;top-down-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Top-Down Parsing&lt;/a&gt; — builds parse tree from root to leaves&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../bottom-up-parsing&quot; class=&quot;internal alias&quot; data-slug=&quot;bottom-up-parsing&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Bottom-Up Parsing&lt;/a&gt; — builds parse tree from leaves to root&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Related:&lt;/strong&gt; &lt;a href=&quot;../../wiki/compilerdesign/ambiguous-grammar&quot; class=&quot;internal alias&quot; data-slug=&quot;wiki/compilerdesign/ambiguous-grammar&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;Ambiguous Grammar&lt;/a&gt; — a grammar that allows multiple parse trees for the same input&lt;/li&gt;
&lt;/ul&gt;
&lt;h2 id=&quot;edge-cases--gotchas&quot; dir=&quot;auto&quot;&gt;&lt;span class=&quot;highlight-span&quot;&gt;Edge Cases &amp;#x26; Gotchas&lt;/span&gt;&lt;a data-role=&quot;anchor&quot; data-no-popover href=&quot;#edge-cases--gotchas&quot; class=&quot;internal&quot;&gt;&lt;span class=&quot;indicator-hook&quot;&gt;&lt;/span&gt;&lt;svg xmlns=&quot;http://www.w3.org/2000/svg&quot; width=&quot;16&quot; height=&quot;16&quot; viewBox=&quot;0 0 24 24&quot; fill=&quot;none&quot; stroke=&quot;currentColor&quot; stroke-width=&quot;2&quot; stroke-linecap=&quot;round&quot; stroke-linejoin=&quot;round&quot;&gt;&lt;use href=&quot;#github-anchor&quot;&gt;&lt;/use&gt;&lt;/svg&gt;&lt;/a&gt;&lt;/h2&gt;
&lt;ul dir=&quot;auto&quot;&gt;
&lt;li&gt;&lt;strong&gt;Left recursion:&lt;/strong&gt; Top-down parsers cannot handle left-recursive grammars — must be eliminated&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Ambiguity:&lt;/strong&gt; An ambiguous grammar can produce two different parse trees for the same program&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Error recovery strategies:&lt;/strong&gt; Panic mode (skip tokens until sync token found), phrase-level recovery, error productions&lt;/li&gt;
&lt;/ul&gt;</content>
  </entry>
</feed>