<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Chirag Prajapati on Medium]]></title>
        <description><![CDATA[Stories by Chirag Prajapati on Medium]]></description>
        <link>https://medium.com/@22bt04116?source=rss-766b6156c0c4------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Chirag Prajapati on Medium</title>
            <link>https://medium.com/@22bt04116?source=rss-766b6156c0c4------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Tue, 01 Dec 2026 11:08:25 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@22bt04116/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[YACC in Compiler Construction for Domain-Specific Languages (DSLs)]]></title>
            <link>https://medium.com/@22bt04116/yacc-in-compiler-construction-for-domain-specific-languages-dsls-341447ef08fa?source=rss-766b6156c0c4------2</link>
            <guid isPermaLink="false">https://medium.com/p/341447ef08fa</guid>
            <category><![CDATA[compilers]]></category>
            <category><![CDATA[domain-specific-languages]]></category>
            <category><![CDATA[yacc]]></category>
            <dc:creator><![CDATA[Chirag Prajapati]]></dc:creator>
            <pubDate>Tue, 30 Sep 2025 06:47:05 GMT</pubDate>
            <atom:updated>2025-09-30T06:47:05.549Z</atom:updated>
            <content:encoded><![CDATA[<p><em>How YACC/Bison speeds up building parsers — and why that matters for query DSLs around blockchain smart contracts</em></p><p><strong>TL;DR:</strong> YACC (and its GNU descendant Bison) remain fast, pragmatic tools for taking a formal grammar and producing a working parser quickly. For DSLs — including query/inspection languages used in smart-contract tooling and analysis — a Flex + Bison workflow lets you prototype, iterate, and ship a robust parser (with error reporting, AST construction and semantic actions) far faster than hand-rolling everything. That said, there are tradeoffs (e.g., grammar ambiguity, integration with modern toolchains) and for some projects you may prefer newer generators (ANTLR, PEGs) — but YACC/Bison still shine when you want deterministic, compact LR parsing and tight control over generated C/C++ parsers.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*xJw3rRD62PS9vWgymJs8sQ.png" /></figure><h3>Why DSLs matter?</h3><p>Domain-Specific Languages let you expose only the concepts your domain needs, improving expressiveness and reducing user mistakes. In software engineering, DSLs are a well-studied technique to increase productivity and reliability by tailoring syntax and semantics to a particular class of problems. That general payoff is exactly why teams build query DSLs for search engines, static-analysis tools, and blockchain tooling: a focused query language (JSON DSL, SQL-like, or a bespoke pattern language) makes expressing complex queries far simpler than general purpose languages.</p><h3>YACC / Bison — what they are and why they’re useful</h3><p>YACC (Yet Another Compiler-Compiler) converts a grammar specification (BNF-like) and associated semantic actions into a parser subroutine; it was designed precisely to take the tedium out of writing parsers by hand. GNU Bison is a modern, widely used YACC-compatible implementation that generates deterministic LR/GLR parsers and integrates well with Flex (the lexical analyzer). Use cases include programming languages, config languages, and — importantly for this post — DSLs like query languages.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*CmjAdNq3iu8sHbIHke8y0Q.png" /></figure><h4>Practical strengths of Bison/YACC for DSLs</h4><ul><li>Rapid prototyping: write a concise grammar + actions and get a working parser.</li><li>Deterministic parsing (LALR / LR / GLR options): predictable parse tables and performance.</li><li>Tight control of semantic actions — build AST nodes, perform validations, or emit intermediate code inline.</li><li>Small runtime dependency (C/C++), good for embedding into low-level tooling or static analyzers.</li></ul><h3>Why DSLs for blockchain smart-contract ecosystems?</h3><p>Blockchain systems spawn many niche languages: contract languages (Solidity, Vyper), on-chain event schemas, and off-chain query and analysis languages (static-analysis patterns, audit query DSLs, SQL interfaces to on-chain data). Practically, there are two main use cases where a query DSL pops up:</p><ol><li><strong>Static analysis &amp; security tooling</strong> — tools like Securify expose a pattern/query language (a DSL) that lets auditors express security conditions and find contract patterns automatically. Implementations often parse domain patterns and check them against a contract model.</li><li><strong>On-chain → off-chain queries / user queries</strong> — developers and dApps commonly want SQL-like queries or structured query DSLs over on-chain event streams or indexed chain data. Because parsing on the chain is limited (gas, determinism), common designs push parsing and heavy logic off-chain (e.g., Chainlink nodes or indexing services), while the smart contract passes strings or compact query descriptors and receives results via oracles or external adapters.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/870/1*PAAcPb-6XlXpKgpNcJaKcg.png" /></figure><p>In both cases, a small, maintainable parser for the DSL (to convert text to AST, validate, and compile to checks or DB queries) is a key component — and Bison is a practical choice for that parser.</p><h3>Example: a tiny query DSL for smart-contract events (how Bison helps)</h3><p>Imagine a DSL that lets auditors express patterns over events emitted by contracts, e.g.: sql</p><p>MATCH Transfer(from:ADDR, to:ADDR, amount &gt; 1000) WHERE from != 0x0</p><p>You want a parser that turns that string into an AST node representing a Match with predicates (amount &gt; 1000, from != 0x0) so the rest of your tool can run pattern checks.</p><p>A minimal <strong>Bison</strong> grammar could look like:</p><p>%{ <br>#include “ast.h”<br>%}</p><p>%token IDENT ADDR NUMBER STRING<br>%left ‘&gt;’ ‘&lt;’ “==” “!=”</p><p>%%<br>program:<br> /* empty */<br> | program stmt<br> ;</p><p>stmt:<br> MATCH pattern opt_where { /* build Match AST node */ }<br> ;</p><p>pattern:<br> IDENT ‘(‘ arglist ‘)’ { /* construct pattern node */ }<br> ;</p><p>arglist:<br> /* empty */ { /* no args */ }<br> | arglist_nonempty<br> ;</p><p>arglist_nonempty:<br> arg<br> | arglist_nonempty ‘,’ arg<br>arg:<br> IDENT ‘:’ TYPE { /* field with type */ }<br> | IDENT COMP NUMBER { /* comparison predicate like amount &gt; 1000 */ }<br> ;</p><p>opt_where:<br> /* empty */ { /* no where */ }<br> | WHERE expr { /* attach predicate */ }<br> ;</p><p>expr:<br> expr ‘&amp;&amp;’ expr<br> | expr ‘||’ expr<br> | ‘(‘ expr ‘)’<br> | IDENT COMP literal<br> ;<br>%%</p><p>Couple this with a <strong>Flex</strong> scanner that recognizes IDENT, ADDR, numbers and operators, and you can generate lex.yy.c and parser.tab.c and link them into a tiny C/C++ binary that parses input strings into your AST. From there the AST can be compiled into a set of checks against contract bytecode, event logs, or translated into SQL for off-chain indexing systems. Tutorials and books on Flex+Bison walk through this exact flow.</p><h3>On-chain vs off-chain parsing and where Bison sits</h3><p><strong>Important architectural constraint:</strong> full parsing on-chain is usually infeasible (gas limits, determinism, code complexity). Two dominant architectures:</p><ul><li><strong>Off-chain parsing + on-chain verified input:</strong> parse and validate a DSL off-chain (using Bison), then send compact, prevalidated parameters or proofs on-chain.</li><li><strong>Oracle/adapter approach:</strong> the smart contract contains a function that accepts a query string; an oracle (e.g., Chainlink node) or external adapter performs the parsing/execution off-chain and returns results to the contract. This pattern lets developers keep expressive query DSLs while avoiding on-chain parsing costs.</li></ul><figure><img alt="" src="https://cdn-images-1.medium.com/max/941/1*NtIgn8PW6g20RrCRTFLZIQ.png" /></figure><p>Thus Bison is typically used <strong>off-chain</strong> in blockchain contexts — for indexers, auditors, or oracles — where you need a robust parser that integrates with the rest of your toolchain.</p><h3>Real world: DSLs used for smart-contract security (Securify / PQL)</h3><p>Securify, a widely cited static analyzer for Ethereum smart contracts, uses a domain-specific pattern language to express security checks and violations. Their system shows the power of DSLs: specify concise patterns (a DSL) and let the analysis engine check thousands of contracts automatically. A practical parser for such a DSL is an essential implementation piece — again the kind of task YACC/Bison was built to simplify.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/963/1*4vWLY1pJeSMkg1MpPeMmog.png" /></figure><h3>Pros, cons, and alternatives (practical guidance)</h3><p><strong>When to pick Bison/YACC</strong></p><ul><li>You need a small, fast, deterministic parser in C/C++ or want tight control over parser actions and memory layout.</li><li>Your grammar is LALR(1) or can be rewritten to be LALR(1) without excessive hackery.</li><li>You value a minimal runtime and predictable performance (indexers, embedded analyzers).</li></ul><p><strong>Limitations &amp; reasons people move to other tools</strong></p><ul><li>Modern ecosystems often prefer tools that generate Java/JS/C# parsers (ANTLR) or provide easier error recovery and more flexible grammar styles (PEG/LPeg). Some practitioners argue that flex/yacc are more low-level and harder to maintain for evolving DSLs. If you need IDE tooling, AST walkers in high-level languages, or smoother integration with JVM/JS ecosystems, evaluate ANTLR or parser combinators. (Critiques and comparisons are common in modern compiler engineering writeups.)</li></ul><h3>Practical checklist for using Bison to build a blockchain DSL parser</h3><ul><li><strong>Start with concrete examples</strong> — collect the queries you expect.</li><li><strong>Design a grammar small enough to be LALR(1)</strong> — Bison gives you diagnostics (bison -v) to find conflicts.</li><li><strong>Separate lexical concerns</strong> — use Flex for tokens (addresses, hex, numbers, strings).</li><li><strong>Emit a clear AST</strong> — semantic actions should build a small, well-documented AST shape that later phases consume.</li><li><strong>Keep heavy logic off-chain</strong> — use the parser in an offline service, or in an oracle adapter, not in on-chain bytecode.</li><li><strong>Write robust tests</strong> — fuzz the parser with malformed queries and validate error messages/positions.</li></ul><h3>Final takeaways</h3><ul><li>YACC/Bison are still pragmatic tools for rapidly delivering parsers for DSLs. They map directly from grammar to working parser and make AST construction and semantic actions straightforward.</li><li>In blockchain contexts the parser usually lives off-chain (in indexers, oracles, or analysis tools). DSLs power static analyzers (e.g., Securify’s patterns) and query systems that fetch or reason about on-chain data; a Bison-generated parser is a strong, low-overhead choice for those components.</li><li>Before you commit: weigh maintenance, language/runtime targets, and toolchain integration. If you need multi-language support or rich IDE tooling, consider alternatives (ANTLR, PEGs); if you need a compact, performant C/C++ parser, Bison remains a top pick.</li></ul><h3>References / Further reading</h3><ul><li>GNU Bison manual — <em>The Yacc-compatible parser generator</em> (official manual). <a href="https://www.gnu.org/software/bison/manual/">GNU</a></li><li>Johnson, S. C., <em>Yacc: Yet Another Compiler-Compiler</em> (original paper). <a href="https://epaperpress.com/lexandyacc/download/yacc.pdf">epaperpress.com</a></li><li>van Deursen, A., Klint, P., Visser, J., <em>Domain-Specific Languages</em> (survey). <a href="https://homepages.cwi.nl/~paulk/publications/Sigplan00.pdf">CWI Homepages</a></li><li>Tsankov, P. et al., <em>Securify: Practical Security Analysis of Smart Contracts</em> (paper describing DSL use in smart-contract analysis). <a href="https://arxiv.org/pdf/1806.01143">arXiv</a></li><li>Chainlink blog / docs on external adapters and how off-chain services can run SQL/queries for smart contracts (pattern for off-chain parsing/execution).</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=341447ef08fa" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>