<?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 Ahmed Ryan on Medium]]></title>
        <description><![CDATA[Stories by Ahmed Ryan on Medium]]></description>
        <link>https://medium.com/@fraxhost?source=rss-a98861f9b927------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*tPEJKRHIJEXc0AMKPOMIgA.png</url>
            <title>Stories by Ahmed Ryan on Medium</title>
            <link>https://medium.com/@fraxhost?source=rss-a98861f9b927------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Sat, 28 Nov 2026 18:52:16 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@fraxhost/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[Left Recursion in Context-Free Grammar]]></title>
            <link>https://fraxhost.medium.com/left-recursion-in-context-free-grammar-960305ff28ce?source=rss-a98861f9b927------2</link>
            <guid isPermaLink="false">https://medium.com/p/960305ff28ce</guid>
            <category><![CDATA[context-free-grammar]]></category>
            <category><![CDATA[left-recursion]]></category>
            <dc:creator><![CDATA[Ahmed Ryan]]></dc:creator>
            <pubDate>Mon, 15 Dec 2025 08:03:43 GMT</pubDate>
            <atom:updated>2025-12-15T08:03:43.116Z</atom:updated>
            <content:encoded><![CDATA[<p>Left recursion occurs when a grammar rule has the form A → Aα. This creates an infinite loop for top-down parsers (like Recursive Descent) because they keep trying to expand A into A forever without consuming any input.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/319/0*mcG5zAVUgHznHyVi.png" /><figcaption>Left Recursion Rule</figcaption></figure><p>To fix this, we transform the recursion from Left (growing down-left) to Right (growing down-right).</p><h3>Immediate Left Recursion (The Standard Algorithm)</h3><p>This is the most common case. The form:</p><pre>A -&gt; Aα | β</pre><p>α: The part being repeated (recursive suffix)<br>β: The base case (non-recursive part)</p><h4>The Transformation</h4><p>We introduce a new helper variable (let’s call it A`) to handle the repetition on the right side.</p><pre>A  -&gt; βA`<br>A` -&gt; αA` | ε </pre><h3>Worked Examples</h3><h4>Example 1</h4><p>This example contains a single left-recursive option and a single non-recursive option.</p><pre>E -&gt; E + T | T where A = E, α = + T, β = T<br><br>E  -&gt; T E`<br>E` -&gt; + T E` | ε</pre><h4>Example 2</h4><p>This is an example where both the recursive part and base part consist of multiple symbols.</p><pre>S -&gt; S a b c | d e f where A = S, α = a b c, β = d e f<br><br>S  -&gt; d e f S`<br>S` -&gt; a b c S` | ε</pre><h4>Example 3</h4><p>This example contains single left-recursive options and multiple non-recursive options.</p><pre>P -&gt; P , id | id | int where A = P, α = , id, β1 = id, β2 = int<br><br>P -&gt; id P&#39; | int P&#39;<br>P&#39; -&gt; , id P&#39; | ε</pre><h4>Example 4</h4><p>This example contains multiple left-recursive options and multiple non-recursive options.</p><pre>S -&gt; S a | S b | c | d where A = S, α1 = a, α2 = b, β1 = c, β2 = d<br><br>S -&gt; c S&#39; | d S&#39;<br>S&#39; -&gt; a S&#39; | b S&#39; | ε</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=960305ff28ce" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Removal of Unit Production in CFG]]></title>
            <link>https://fraxhost.medium.com/removal-of-unit-production-in-cfg-29915309737c?source=rss-a98861f9b927------2</link>
            <guid isPermaLink="false">https://medium.com/p/29915309737c</guid>
            <category><![CDATA[context-free-grammar]]></category>
            <category><![CDATA[unit-production-removal]]></category>
            <category><![CDATA[grammar-simplification]]></category>
            <dc:creator><![CDATA[Ahmed Ryan]]></dc:creator>
            <pubDate>Fri, 12 Dec 2025 06:00:29 GMT</pubDate>
            <atom:updated>2025-12-12T06:00:29.271Z</atom:updated>
            <content:encoded><![CDATA[<p>A unit production is a rule of the form A → B, where a non-terminal leads directly to another single non-terminal. These are essentially “useless” steps that delay the parsing process. It is a standard cleaning step in compiler design, usually performed before converting a grammar to Chomsky Normal Form (CNF).</p><h3>The Logic: Bypass the Middleman</h3><p>If we have a chain like A → B → “apple”, the rule A → B is just a middleman. To remove it, we look at what B produces (“apple”) and let A produce it directly (A → “apple”).</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*N_5mt4Fq4EHGpHOxns_kzA.jpeg" /><figcaption>AI Generated Image of Unit Production Removal in CFG</figcaption></figure><h3>The Algorithm</h3><h4>1. Find Unit Pairs</h4><p>Identify all pairs (A, B) where A and B are non-terminals.</p><h4>2. Add New Rules</h4><p>For every pair (A, B), find all non-unit rules of B (e.g., B → terminals or B → Variables). Add these rules to A.</p><h4>3. Delete Unit Productions</h4><p>Remove all the original A → B rules from the grammar.</p><h3>Worked Example</h3><p>Original grammar:</p><pre>S -&gt; A | ab<br>A -&gt; B<br>B -&gt; C | d<br>C -&gt; e</pre><h4>Step 1: Identify Unit Chains</h4><p>We look at where each variable can go using only single steps.</p><p><strong>For S</strong></p><ul><li>S → A → B → C</li><li>Pairs: (S, A), (S, B), (S, C)</li></ul><p><strong>For A</strong></p><ul><li>A → B → C</li><li>Pairs: (A, B), (A, C)</li></ul><p><strong>For B</strong></p><ul><li>B → C</li><li>Pairs: (B, C)</li></ul><p><strong>For C</strong></p><ul><li>No unit productions</li></ul><h4>Step 2: Copy Non-Unit Rules</h4><p>Now, we copy the “final destinations” (non-unit rules) back to the start of the chains.</p><p><strong>Destinations</strong></p><ul><li>S → ab (keep original)</li><li>B → d</li><li>C → e</li></ul><p><strong>Updating S</strong></p><ul><li>S =&gt; B and B → d. Therefore, S → d.</li><li>S =&gt; C and C → e. Therefore, S → e.</li><li>S now produces: ab, d, e.</li></ul><p><strong>Updating A</strong></p><ul><li>A =&gt; B and B → d. Therefore, A → d.</li><li>A =&gt; C and C → e. Therefore, A → e.</li><li>A now produces: d, e.</li></ul><p><strong>Updating B</strong></p><ul><li>B =&gt; C and C → e. Therefore, B → e.</li><li>B now produces: d, e.</li></ul><h4><strong>Step 3: Delete Original Unit Rules</strong></h4><p>Remove S → A, A → B, and B → C.</p><h4>Final Grammar</h4><pre>S -&gt; ab | d | e<br>A -&gt; d | e<br>B -&gt; d | e<br>C -&gt; e</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=29915309737c" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Removal of Null Productions in Context-Free Grammar]]></title>
            <link>https://fraxhost.medium.com/removal-of-null-productions-in-context-free-grammar-6ca61325dc1f?source=rss-a98861f9b927------2</link>
            <guid isPermaLink="false">https://medium.com/p/6ca61325dc1f</guid>
            <category><![CDATA[null-production-removal]]></category>
            <category><![CDATA[context-free-grammar]]></category>
            <dc:creator><![CDATA[Ahmed Ryan]]></dc:creator>
            <pubDate>Fri, 12 Dec 2025 05:56:31 GMT</pubDate>
            <atom:updated>2025-12-12T05:56:31.684Z</atom:updated>
            <content:encoded><![CDATA[<p>To remove null productions (also called ε productions) from a CFG, we use an algorithm that identifies which variables can “disappear” and then updates the other rules to account for that possibility.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*vgZ1q8ZIIAG57gzvUGRVUg.jpeg" /><figcaption>AI Generated Image of Null Production Removal</figcaption></figure><p>A <strong>Null Production</strong> is any rule of the form A → ε.</p><h3>The Algorithm</h3><h4>1. Find Nullable Variables</h4><p>Identify all variables that can eventually turn into ε (either directly or via a chain)</p><h4>2. Create New Rules</h4><p>For every rule where a nullable variable appears on the Right-Hand Side (RHS), create a new version of that rule with the nullable variable removed.</p><h4>3. Cleanup</h4><p>Remove the original A → ε rules.</p><h3>Worked Example</h3><p>Original Grammar</p><pre>S -&gt; AB<br>A -&gt; aA | ε<br>B -&gt; bB | ε</pre><h4>Step 1: Find Nullable Variables</h4><ul><li>Direct Nulls: We see A → ε and B → ε. So, A and B are nullable.</li><li>Indirect Nulls: Let’s look at S → AB. Since A can vanish and B can vanish, S can effectively become ε. So, S is also nullable.</li></ul><h4>Step 2: Create New Rules (The Substitution)</h4><p>We look at every rule on the RHS. If we see a nullable variable, we must write the rule twice: once with the variable present, and once with it “erased”.</p><p><strong>Processing S → AB</strong></p><ul><li>If we keep both: S → AB</li><li>If A disappears: S → B</li><li>If B disappears: S → A</li><li>If both disappear: S → ε (We usually ignore this unless S is the start symbol and we specifically need to accept empty input)</li><li>New S Rules: S → AB | A | B</li></ul><p><strong>Processing A → aA</strong></p><ul><li>If we keep A: A → aA</li><li>If A disappears: A → a</li><li>New A Rules: A → aA | a</li></ul><p><strong>Processing B → bB</strong></p><ul><li>If we keep B: B → bB</li><li>If B disappears: B → b</li><li>New B Rules: B → bB | b</li></ul><h4>Step 3: Cleanup</h4><p>Delete the original ε rules (A → ε and B → ε)</p><h4>Final Grammar</h4><pre>S -&gt; AB | A | B<br>A -&gt; aA | a<br>B -&gt; bB | b</pre><p>Note: If the language must include the empty string, we usually add a special start rule S’ → S | ε, but for internal grammar simplification, we remove the rest.</p><h4>Tricky Case: Multiple Occurrences</h4><p>If we have a rule like S → aAaA and A is nullable, we must account for every combination of erasure.</p><ol><li>Original: aAaA</li><li>Erase first A: aaA</li><li>Erase second A: aAa</li><li>Erase both A’s: aa</li></ol><p>So the new rules become: S → aAaA | aaA | aAa | aa</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=6ca61325dc1f" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Removal of Useless Symbols in Context-Free Grammars]]></title>
            <link>https://fraxhost.medium.com/removal-of-useless-symbols-in-context-free-grammars-f24a3e635272?source=rss-a98861f9b927------2</link>
            <guid isPermaLink="false">https://medium.com/p/f24a3e635272</guid>
            <category><![CDATA[grammar-simplification]]></category>
            <category><![CDATA[context-free-grammar]]></category>
            <category><![CDATA[useless-symbols-removal]]></category>
            <dc:creator><![CDATA[Ahmed Ryan]]></dc:creator>
            <pubDate>Thu, 11 Dec 2025 00:45:12 GMT</pubDate>
            <atom:updated>2025-12-11T00:45:12.816Z</atom:updated>
            <content:encoded><![CDATA[<p>To find the reduced grammar, we must eliminate useless symbols from the given grammar. A symbol is useless if it is either:</p><ol><li><strong>Non-Generating</strong>: It can not eventually derive a string of terminals.</li><li><strong>Unreachable</strong>: It cannot be reached from the start symbol S.</li></ol><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*-_TPOgYfn7e19fm6_qaYgA.jpeg" /><figcaption>AI Generated Image of Useless Symbols Removal in CFG</figcaption></figure><p>Let’s try to remove the useless symbols from the following grammar.</p><pre>S -&gt; AC | B<br>A -&gt; a<br>C -&gt; c  | BC<br>E -&gt; aA | e</pre><h3>Step 1: Identify and Remove Non-Generating Symbols</h3><p>We look for variables that can produce a string of terminals (like a, c, e).</p><h4>Generating</h4><ul><li>A → a (Immediate)</li><li>C → c (Immediate)</li><li>E → e (Immediate)</li><li>S → AC (Since A and C are generating, S is generating)</li></ul><h4>Non-Generating</h4><ul><li>B: There are no rules defined for B (no B → …). Therefore, B is a “dead” variable.</li></ul><h4>Action</h4><ul><li>Remove S → B</li><li>Remove C → BC</li></ul><h4>Intermediate Grammar</h4><pre>S -&gt; AC<br>A -&gt; a<br>C -&gt; c<br>E -&gt; aA | e</pre><h3>Step 2: Identify and Remove Non-Generating Symbols</h3><p>We check which variables can be reached starting from S.</p><h4>Reachable</h4><ul><li>S (Start Symbol)</li><li>S → AC (Implies that A and C are reachable)</li></ul><h4>Unreachable</h4><ul><li>E: No rule on the right-hand side produces E. Even though E is valid in itself, the start symbol S can never trigger it.</li></ul><h4>Action</h4><p>Remove all rules for E.</p><ul><li>Remove → aA | e</li></ul><h3>Final Reduced Grammar</h3><p>After removing the useless symbols (B and E), we get:</p><pre>S -&gt; AC<br>A -&gt; a<br>C -&gt; c</pre><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f24a3e635272" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Ambiguity Analysis of Context-Free Grammar]]></title>
            <link>https://fraxhost.medium.com/ambiguity-analysis-of-context-free-grammar-f1586e0bb840?source=rss-a98861f9b927------2</link>
            <guid isPermaLink="false">https://medium.com/p/f1586e0bb840</guid>
            <category><![CDATA[ambiguity]]></category>
            <category><![CDATA[context-free-grammar]]></category>
            <dc:creator><![CDATA[Ahmed Ryan]]></dc:creator>
            <pubDate>Wed, 10 Dec 2025 23:06:53 GMT</pubDate>
            <atom:updated>2025-12-10T23:06:53.303Z</atom:updated>
            <content:encoded><![CDATA[<p>Ambiguity analysis is a critical concept in formal language theory and compiler design. If a grammar is ambiguous, a compiler cannot deterministically parse code — it won’t know if x = y + z means “add then assign” or something else entirely.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*RGDGdPJjUa9SOeRGUMbecQ.jpeg" /><figcaption>AI Generated Image of Ambiguity Analysis of Context-Free Grammar</figcaption></figure><h3>1. The Core Concept</h3><p>A Context-Free Grammar (CFG) is ambiguous if there is at least one string in the language that has:</p><ol><li>Two or more distinct Parse Trees.</li><li>Two or more distinct Leftmost Derivations.</li></ol><p><strong>Why does this happen</strong>? Ambiguity usually arises because the grammar fails to define two things:</p><h4>Precedence</h4><p>By definition, precedence determines which operator is applied first when different kinds of operators appear in the same expression.</p><p>Example: In most languages, * has higher precedence than +. So, a+b*c is grouped as a+(b*c).</p><h4>Associativity</h4><p>By definition, associativity tells us how operators of the same precedence group together. In a sequence of identical operators, do we group left-to-right or right-to-left?</p><p>Example: Is a-b-c parsed as (a-b)-c or a-(b-c)?</p><p><strong>Left-Associative Grammar</strong></p><pre>Expression -&gt; Expression - Term | Term<br>Term       -&gt; a | b | c </pre><p>The above grammar produces (a-b)-c</p><p><strong>Right-Associative Grammar</strong></p><pre>Expression -&gt; Term - Expression | Term<br>Term       -&gt; a | b | c</pre><p>The above grammar produces a-(b-c)</p><h3>2. Demonstration</h3><p>The “Flat” Grammar (Ambiguous):</p><pre>Expression -&gt; Expression Operator Expression | NUMBER<br>Operator   -&gt; / | -</pre><p>To prove ambiguity, we must find two valid structures for the string 6/3-1.</p><h4>Option A: Grouping Division First</h4><p>We start by choosing the subtraction rule first, forcing the division into the lower branch (left side)</p><pre>Expression =&gt; Expression Operator Expression<br>           =&gt; Expression Operator Expression Operator Expression (leftmost expression is converted to E Op E))<br>           =&gt; NUMBER Operator Expression Operator Expression<br>           =&gt; 6 Operator Expression Operator Expression<br>           =&gt; 6 / Expression Operator Expression<br>           =&gt; 6 / NUMBER Operator Expression<br>           =&gt; 6 / 3 Operator Expression<br>           =&gt; 6 / 3 - Expression<br>           =&gt; 6 / 3 - NUMBER<br>           =&gt; 6 / 3 - 1</pre><h4>Option B: Grouping Subtraction First</h4><p>We start by choosing the division rule first, forcing the subtraction into the lower branch (right side).</p><pre>Expression =&gt; Expression Operator Expression<br>           =&gt; NUMBER Operator Expression (leftmost expression is converted to Number)<br>           =&gt; 6 Operator Expression<br>           =&gt; 6 / Expression<br>           =&gt; 6 / Expression Operator Expression<br>           =&gt; 6 / NUMBER Operator Expression<br>           =&gt; 6 / 3 Operator Expression<br>           =&gt; 6 / 3 - Expression<br>           =&gt; 6 / 3 - NUMBER<br>           =&gt; 6 / 3 - 1</pre><h4>Verdict</h4><p>Since Derivation A (which expands the left side early) and Derivation B (which expands the right side early) are distinct sequences that produce the same string, the grammar is ambiguous.</p><h3>3. Classic Case: The Dangling Else</h3><p>This is the most famous ambiguity in computer science, found in C, Java, and Pascal.</p><p>The Grammar: S → if E then S | if E then S else S | …</p><p>The String: if condition1 then if condition2 then statement1 else statement2</p><p>The Ambiguity: Does the else belong to the first if or the second if?</p><ol><li>Inner binding: if C1 then { if C2 then S1 else S2 } (Standard Interpretation)</li><li>Outer binding: if C1 then { if C2 then S1 } else S2</li></ol><p>Most languages fix this by arbitrarily enforcing that an else matches the nearest unmatched if .</p><h3>4. How to Fix Ambiguity (Stratification)</h3><p>To fix the arithmetic ambiguity, we must stop using a single “flat” rule (Expression → Expression Operator Expression). Instead, we create layers (strata) of variables. Each layer enforces a precedence level. The lower the rule is in the list, the higher its precedence (because it gets parsed closer to the terminals).</p><h4>The Stratified Unambiguous Grammar</h4><ol><li><strong>Expression: </strong>Handles lowest precedence (+, -).</li><li><strong>Term: </strong>Handles medium precedence (*, /).</li><li><strong>Factor: </strong>Handles the highest precedence (parentheses, numbers).</li></ol><p>Here is the revised grammar.</p><pre>Expression -&gt; Expression - Term | Term<br>Term       -&gt; Term / Factor | Factor<br>Factor     -&gt; NUMBER | (Expression)</pre><p>Why this fixes 6/3-1 ?</p><ul><li>The parser must start at Expression.</li><li>Expression can only be split by - . It can not be split by / .</li><li>Therefore, the only valid parse is Expression → Expression - Term .</li><li>The division / is forced to happen inside the Term or Factor layers, “trapping” it beneath the subtraction.</li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=f1586e0bb840" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Derivations and Parse Trees]]></title>
            <link>https://fraxhost.medium.com/derivations-and-parse-trees-d9464dc55520?source=rss-a98861f9b927------2</link>
            <guid isPermaLink="false">https://medium.com/p/d9464dc55520</guid>
            <category><![CDATA[parse-tree]]></category>
            <category><![CDATA[right-most-derivation]]></category>
            <category><![CDATA[left-most-derivation]]></category>
            <dc:creator><![CDATA[Ahmed Ryan]]></dc:creator>
            <pubDate>Wed, 10 Dec 2025 00:33:32 GMT</pubDate>
            <atom:updated>2025-12-10T00:33:32.653Z</atom:updated>
            <content:encoded><![CDATA[<p>A derivation is just the step-by-step process a grammar uses to turn a starting symbol into a final sentence or expression. We can think of it like following a recipe — we start with the main ingredient and keep replacing parts according to the rules until we get the final dish.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*ZNj7VHwF6ovU8XE4-_7WdA.jpeg" /><figcaption>AI Generated Image of Parse Tree</figcaption></figure><h3>Leftmost vs Rightmost Derivation</h3><p>In the leftmost derivation, each time we make a replacement, we always replace the leftmost variable first. Whereas in the rightmost derivation, each time we make a replacement, we replace the rightmost variable first. They both lead to the same final expression, but the order in which the pieces get expanded differs.</p><p>Given the grammar:</p><ul><li>expression → binary</li><li>binary → expression operator expression</li><li>expression → literal</li><li>literal → NUMBER</li><li>operator → /</li><li>operator → -</li></ul><p>Let’s derive the string: 6/3-1 .</p><pre>                               expression<br>                                   |<br>                                 binary<br>                               /   |    \<br>                              /    |     \<br>                    expression  operator  expression<br>                        |          |          |<br>                      binary       -       literal<br>                     /   |   \                |<br>                    /    |    \             NUMBER<br>                  exp operator exp            |<br>                   |     |      |             1<br>                   |     /      |<br>                  literal    literal<br>                   |            |<br>                  NUMBER      NUMBER<br>                   |            |<br>                   6            3</pre><h4>Leftmost Derivation Example</h4><p>In a leftmost derivation, we traverse the tree in a depth-first, left-to-right order.</p><ol><li>We started at the Root ( expression )</li><li>We expanded the expression into binary</li><li>We went down the <strong>Left Branch</strong> ( expression → binary )</li><li>Expand the <strong>Left Branch </strong>again ( binary → expression operator expression )</li><li>Expand the <strong>Left Branch </strong>again ( expression → literal)</li><li>Expand the <strong>Left Branch </strong>again ( literal → NUMBER)</li><li>Expand the <strong>Left Branch </strong>again ( NUMBER → 6)</li><li>Resolve the next branch of step 4 ( operator → / )</li><li>Resolve the next branch of step 4 ( expression → literal )</li><li>Keep going like this …</li></ol><h4>Rightmost Derivation Example</h4><p>In a rightmost derivation, we construct the tree depth-first, right-to-left.</p><ol><li>We started at the Root ( expression )</li><li>We expanded the expression into binary</li><li>We immediately went down the <strong>Right Branch</strong> ( expression → literal → NUMBER → 1) and finished it.</li><li>Then we resolve the previous branch ( operator → — )</li><li>Only after the right side ( — 1 ) was fully grown, did we move to the <strong>Left Branch</strong> to build the division subtree ( 6 / 3 ).</li></ol><p>Too much mental work, right? It’s important to remember that <strong>leftmost and rightmost derivations always produce the same parse tree</strong>. Interestingly, the grammar we’ve been working with is <strong>ambiguous</strong>, meaning a single string can have more than one valid parse tree. However, <strong>ambiguity has nothing to do with whether we use a leftmost or rightmost derivation</strong>. We’ll cover <strong>ambiguity analysis</strong> in detail in another article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=d9464dc55520" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Context-Free Grammars]]></title>
            <link>https://fraxhost.medium.com/context-free-grammars-in-laymans-terms-2aa947c73c25?source=rss-a98861f9b927------2</link>
            <guid isPermaLink="false">https://medium.com/p/2aa947c73c25</guid>
            <category><![CDATA[context-free-grammar]]></category>
            <category><![CDATA[regular-grammar]]></category>
            <category><![CDATA[context-sensitive-grammar]]></category>
            <dc:creator><![CDATA[Ahmed Ryan]]></dc:creator>
            <pubDate>Tue, 09 Dec 2025 16:01:18 GMT</pubDate>
            <atom:updated>2025-12-09T22:39:57.572Z</atom:updated>
            <content:encoded><![CDATA[<p>Context-Free Grammars (CFGs) are the theoretical backbone of programming language syntax and a core concept in formal language theory.</p><figure><img alt="Context-Free Grammar (CFG) Structure" src="https://cdn-images-1.medium.com/max/1024/1*l_k_kMnecQlayHZZ5ZlVug.jpeg" /><figcaption>An AI Generated Image of CFG</figcaption></figure><h3>Parts of Context-Free Grammar</h3><p>CFG is just a formal way to describe how sentences in a language are built. Think of it like a set of instructions that tells us how to form valid strings or statements. It consists of four parts.</p><h4>Variables</h4><p>These are labels for things that still need to be expanded. We can consider them as placeholders or boxes that will eventually turn into real text. These are also known as Non-Terminals. Examples:</p><pre>A = some intermediate phrase<br>S = a full sentence<br>Expression = something like “a + b”</pre><h4>Terminals</h4><p>These are the real characters or tokens that appear in the final output. We can compare these to actual words or symbols. Once we reach terminals, we are done — no more expanding. Examples:</p><pre>+<br>if<br>id<br>numbers</pre><h4>Production Rules</h4><p>These are transformation instructions. We can keep applying these rules until we get a full sentence made only of terminals. A rule looks like A → some_thing. It means — Replace A with some_thing<em>.</em> Examples:</p><pre>S -&gt; A B<br>A -&gt; id<br>B -&gt; + A</pre><h4>Start Symbol</h4><p>This is the starting point of constructing a sentence. It is like saying “Start with S , and follow the rules until you get a full expression. In simple terms, it is equivalent to “Where to begin”.</p><h3>Why is it called “Context-Free”?</h3><p>Because every rule replaces just one variable at a time, no matter where it appears.</p><pre>A -&gt; id</pre><p>We can replace A with id anywhere, regardless of what comes before or after it.</p><h3>How is it different from Regular Grammar and Context-Sensitive Grammar?</h3><p>Regular grammar requires the Left Hand Side (LHS) to be exactly one non-terminal ( A ). The RHS can only be a terminal ( a ) or a terminal followed by one non-terminal ( aB ). In left-linear form, it can be one non-terminal followed by a terminal ( Ba ). Keep in mind that these forms can not be mixed within the same grammar.</p><pre>A → aB<br>A → a<br>A → ε (sometimes allowed)</pre><p>Context-sensitive grammars can have multiple symbols at the LHS, and the RHS must always be equal to or greater than the LHS. Structure of a rule:</p><pre>α A β → α γ β</pre><p>In the above example, A is replaced by γ only when A appears between α and β. The surrounding context matters here for applying the rule.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/9c27229690640ab75d7fc4131090eb18/href">https://medium.com/media/9c27229690640ab75d7fc4131090eb18/href</a></iframe><h4>Intuition</h4><p><strong>Regular Grammar → Patterns</strong></p><ul><li>No memory (finite automata)</li><li>Can’t count or match pairs deeply</li></ul><p><strong>Context-Free Grammar → Balanced Structures</strong></p><ul><li>Can match nested or paired constructs like parentheses</li></ul><p><strong>Context-Sensitive Grammar → Context Dependent Rules</strong></p><ul><li>Can enforce multiple equal counts or complex constraints.</li></ul><p>RG can be compared to the Domino chain — linear, no nesting. Meanwhile, CFG can be compared to LEGO blocks, where we can replace a block anywhere; it doesn’t matter what’s around it, and it supports nested structures. On the other hand, CSG is more like a jigsaw puzzle, where replacement is allowed only when the surrounding pieces match a specific pattern.</p><h4>Rule of Thumb</h4><p>CFG must have exactly one non-terminal on the left-hand side of the rule, whereas CSG can have multiple symbols and even terminals on the left-hand side of the rule.</p><iframe src="" width="0" height="0" frameborder="0" scrolling="no"><a href="https://medium.com/media/41844af1f345751615e909b01d5e779b/href">https://medium.com/media/41844af1f345751615e909b01d5e779b/href</a></iframe><h3>Summary</h3><p>A context-free grammar is a set of building rules that tell us how to turn abstract symbols into real words or expressions, starting from one initial symbol. It is used in programming language parsers, compilers, natural language processing, and the mathematics of formal languages.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=2aa947c73c25" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[What Really Happens When We Run GCC?]]></title>
            <link>https://fraxhost.medium.com/what-really-happens-when-we-run-gcc-fb9fefc66efd?source=rss-a98861f9b927------2</link>
            <guid isPermaLink="false">https://medium.com/p/fb9fefc66efd</guid>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Ahmed Ryan]]></dc:creator>
            <pubDate>Tue, 26 Aug 2025 05:00:27 GMT</pubDate>
            <atom:updated>2025-08-26T05:10:50.067Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/840/1*BR_2iDO-z3rIZAvREWvkhw.png" /><figcaption>Shell Code</figcaption></figure><p>Those of us who have taken a C Programming Course are surely familiar with the given shell code. For years, I thought that meant <em>“GCC is the compiler”</em>. Turns out, that’s only half the story. GCC actually stands for <strong>GNU Compiler Collection</strong> — it’s <em>not one compiler but a toolchain</em> that works together to turn our .c file into an executable.</p><p>And under the hood, a lot happens before we even see Hello, World! printed on your screen. Let’s dissect.</p><h4>Step 1: Preprocessing (cpp)</h4><p>This is where <strong>macros are expanded</strong> and #include files are literally pasted into our source.</p><pre>gcc -E filename.c -o filename.i</pre><p>If we open hello.i, we’ll see all of<em> </em>stdio.h<em> </em>dumped right into our code. That’s why the file is suddenly huge. Remember, when we #include &lt;stdio.h&gt;, we’re not including the actual printf function. Instead, we’re including <strong>declarations</strong> like int printf(const char *format, ...); .</p><h4>Step 2: Compilation (cc1)</h4><p>Now, GCC converts our preprocessed C into <strong>assembly</strong>.</p><pre>gcc -S filename.i -o filename.s</pre><p>This <strong>code </strong>is specific to our CPU architecture (e.g., x86–64). Upon opening, we’ll see human-readable instructions like mov, call, etc.</p><h4><strong>Step 3: Assembly (as)</strong></h4><p>The assembler takes that .s file and converts it into <strong>machine code</strong>, producing an object file.</p><pre>gcc -c filename.s -o filename.o</pre><p>This .o file is <strong>binary</strong> (chunks of 1s and 0s). It contains our main function, but it can’t run alone yet because it still needs external libraries (like printf from libc).</p><h4>Step 4: Linking (ld)</h4><p>Finally, the linker comes in to <strong>stitch everything together </strong>— our object file plus libraries like libc. The actual implementation of printf (machine code inside <strong>libc</strong>) lives in a precompiled binary library. In this stage, it <strong>pulls in the real function implementation</strong> from libc and glues it into our final executable.</p><pre>gcc filename.o -o filename</pre><p>Here comes our <strong>final executable binary</strong>.</p><h4>Final Executable Format</h4><p>The final executable varies based on Operating Systems.</p><ul><li>Linux — ELF (Executable and Linked Format)</li><li>Mac — Mach-O</li><li>Windows — PE (Portable Executable)</li></ul><p>To sum up, when we run gcc filename.c -o filename, we’re not just calling a compiler. We’re running a <strong>pipeline</strong> of tools: <strong><em>C source -&gt; Preprocessor -&gt; Compiler -&gt; Assembler -&gt; Linker -&gt; Executable</em></strong></p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=fb9fefc66efd" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Understanding Built-in Function and Standard Library in any Programming Language]]></title>
            <link>https://fraxhost.medium.com/understanding-built-in-function-and-standard-library-in-any-programming-language-24aca2ccad90?source=rss-a98861f9b927------2</link>
            <guid isPermaLink="false">https://medium.com/p/24aca2ccad90</guid>
            <category><![CDATA[programming-languages]]></category>
            <category><![CDATA[computer-science]]></category>
            <dc:creator><![CDATA[Ahmed Ryan]]></dc:creator>
            <pubDate>Mon, 25 Aug 2025 04:21:06 GMT</pubDate>
            <atom:updated>2025-08-25T04:23:30.692Z</atom:updated>
            <content:encoded><![CDATA[<figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*NqywJTGgMJt4vctyAccL5w.png" /></figure><h3>Built-in Function</h3><p>Built-in functions are part of the core library itself. They are always available without importing anything. We may think of these as basic tools that come with the language. Let’s see some examples in different languages.</p><h4><strong><em>Python</em></strong></h4><pre>len([1, 2, 3])     # returns length of a collection → 3<br>print(&quot;Hello&quot;)     # prints to console<br>type(42)           # returns the type → &lt;class &#39;int&#39;&gt;<br>id(&quot;x&quot;)            # returns unique memory identifier<br>max(1, 5, 2)       # returns maximum value → 5</pre><h4>JavaScript</h4><pre>parseInt(&quot;42&quot;)      // convert string to integer → 42<br>isNaN(&quot;abc&quot;)        // check if value is NaN → true<br>eval(&quot;2+3&quot;)         // evaluate code string → 5<br>Number(&quot;3.14&quot;)      // convert to number → 3.14<br>String(123)         // convert to string → &quot;123&quot;</pre><h4>C#</h4><pre>typeof(int)          // get type info at runtime<br>sizeof(int)          // get size of int in bytes (usually 4)<br>checked(2147483647 + 1) // force overflow checking<br>default(int)         // default value of int → 0</pre><h4>C</h4><pre>int x = sizeof(int);   // size of int in bytes (usually 4)<br>int y = 5 + 3;         // arithmetic built-in operators<br>int z = x &amp;&amp; y;        // logical AND</pre><h3>Standard Library</h3><p>Standard libraries are a collection of packages/modules that come bundled with the language installation. We can also think of these as advanced tools that we use when needed. We usually have to import those, but in languages like JS and C#, runtime/compiler preloads many standard objects (global scope or default assembly) which eliminates the necessity to import explicitly. Some examples should help I guess.</p><h4>Python</h4><pre>import math, os, json<br><br>math.sqrt(16)         # square root → 4.0<br>os.listdir(&quot;.&quot;)       # list files in current directory<br>json.loads(&#39;{&quot;a&quot;:1}&#39;) # parse JSON string → {&#39;a&#39;: 1}</pre><h4>JavaScript</h4><pre>Math.random()       // random float between 0 and 1<br>new Date().getFullYear() // current year<br>JSON.parse(&#39;{&quot;x&quot;:10}&#39;)   // parse JSON string → {x:10}</pre><h4>C#</h4><pre>System.Console.WriteLine(&quot;Hello&quot;); // print to console<br>System.Math.Sqrt(9);               // square root → 3<br>System.IO.File.ReadAllText(&quot;file.txt&quot;); // read file as string</pre><h4>C</h4><pre>#include &lt;stdio.h&gt;<br>#include &lt;stdlib.h&gt;<br>#include &lt;string.h&gt;<br><br>printf(&quot;Hi\n&quot;);              // print to console<br>char *p = malloc(10);        // allocate memory<br>free(p);                     // free allocated memory<br>strcpy(p, &quot;abc&quot;);            // copy string</pre><p>These two terms can feel fuzzy if we don’t separate them carefully. I hope we won’t have trouble anymore in differentiating between them.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=24aca2ccad90" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[MITRE ATT&CK]]></title>
            <link>https://fraxhost.medium.com/mitre-att-ck-96a24cfc09e2?source=rss-a98861f9b927------2</link>
            <guid isPermaLink="false">https://medium.com/p/96a24cfc09e2</guid>
            <category><![CDATA[cybersecurity]]></category>
            <category><![CDATA[mitre-attack-framework]]></category>
            <category><![CDATA[cyber-threat-intelligence]]></category>
            <dc:creator><![CDATA[Ahmed Ryan]]></dc:creator>
            <pubDate>Thu, 28 Nov 2024 15:09:09 GMT</pubDate>
            <atom:updated>2024-12-11T05:22:42.710Z</atom:updated>
            <content:encoded><![CDATA[<h4>Adversarial Tactics, Techniques, and Common Knowledge</h4><p>ATT&amp;CK is a framework that organizes adversary behavior into TTP (Tactics, Techniques, and Procedures) based on real world observations. Here, tactics refer to the objectives an adversary aims to achieve (e.g. initial access), techniques are the methods used to accomplish those objectives (e.g. spear phishing), and procedures are the detailed actions taken to implement a technique (e.g. crafting mail and sending malicious link to the target). Cybersecurity professionals use it for threat modeling, improving defensive capabilities, and develop secure strategies.</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*eYvg5ynI7MKRcgVVEM6QCw.png" /><figcaption>MITRE ATT&amp;CK FRAMEWORK</figcaption></figure><p>As of now, the framework has identified 14 Tactics. It is important to note that this framework is neither prescriptive nor sequential. Instead, it serves as a comprehensive taxonomy of tactics and techniques that attackers may employ in varying combinations, depending on their goals and the specific environment they aim to compromise. Attackers are not bound to follow the order of tactics as listed; they adapt their approach dynamically based on the context. Let’s explore these tactics briefly for better understanding.</p><ul><li><strong>Reconnaissance (Pre-Attack Planning)</strong><br>It is the first phase of any cyberattack and involves information gathering about a target (organization, individual, or system) to identify vulnerabilities that can be exploited. Attackers collect information about IP addresses, employee details, or software versions. It resembles the scouting phase in warfare.</li><li><strong>Resource Development (Preparing Tools and Assets)<br></strong>Attackers acquire, create, or compromise resources to support their operations in this phase. They prepare by setting up infrastructure (e.g. renting domains or compromising servers for phishing campaigns), creating or stealing accounts, developing custom malwares or buying prebuilt exploits, and designing payloads (e.g. malicious scripts, executables) and mechanisms (e.g. phishing links) for delivering attacks.</li><li><strong>Initial Access (Breaching the Perimeter)<br></strong>Attackers gain a foothold within a target’s network or environment in this phase. It is the visible entry point in the cyberattack lifecycle where adversaries attempt to bypass defenses and infiltrate systems. Access to systems is usually gained through spear-phishing campaigns, exploiting public-facing applications, or brute-forcing credentials.</li><li><strong>Executing (Carrying Out the Attack)<br></strong>Malwares or scripts are executed to disrupt operations, exfiltrate data, or achieve objectives.</li><li><strong>Persistence (Establishing a Foothold)<br></strong>Attackers establish persistence through backdoors, rogue accounts, or malicious services.</li><li><strong>Privilege Escalation (Seizing Command)<br></strong>Attackers gain higher-level access, such as admin privileges, to control critical systems.</li><li><strong>Defense Evasion (Avoid Detection)<br></strong>Attackers disable security tools, encrypt communications, or obfuscate malware to avoid detection.</li><li><strong>Credential Access (Stealing the Keys)<br></strong>Attackers harvest passwords, tokens, or cryptographic keys for further access.</li><li><strong>Discovery (Mapping the Territory)<br></strong>Attackers map the network and identify key systems, data, and resources.</li><li><strong>Lateral Movement (Expanding Control)<br></strong>Attackers use compromised credentials or tools to spread across systems and networks.</li><li><strong>Collection (Looting the Castle)<br></strong>Attackers collect sensitive files, database records, or proprietary information.</li><li><strong>Command and Control (Orchestrating the Attack)<br></strong>Attackers set up communication channels (C2 servers) to issue commands, monitor progress, and adapt strategies.</li><li><strong>Exfiltration (Escaping with the Loot)<br></strong>Data is extracted from the system using covert methods like encrypted traffic or cloud storage.</li><li><strong>Impact (Final Strike)<br></strong>Attackers disrupt operations, delete data, encrypt systems (ransomware), or otherwise cause damage.</li></ul><p>We will further discuss the techniques associated with each tactic in a different article.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=96a24cfc09e2" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>