Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions app/sem5/cd/[chapter]/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ import { SlrSolvedProblemContent } from "../content/ch10-slr-solved-problem";
import { Ch11Content } from "../content/chapter11";
import { LalrSolvedProblemContent } from "../content/ch11-lalr-solved-problem";
import { Ch12Content } from "../content/chapter12";
import { Ch13Content } from "../content/chapter13";
import { Ch14Content } from "../content/chapter14";
import { ExprEvalExampleContent } from "../content/ch14-expr-eval-example";
import { Ch15Content } from "../content/chapter15";
import { Ch16Content } from "../content/chapter16";
import { SdtPostfixTraceContent } from "../content/ch16-sdt-postfix-trace";
import { Ch17Content } from "../content/chapter17";
import { Ch18Content } from "../content/chapter18";
import { Ch19Content } from "../content/chapter19";
import { ScopeManagementContentExport } from "../content/ch19-scope-management";
import { Ch20Content } from "../content/chapter20";
import { ParameterPassingContentExport } from "../content/ch20-parameter-passing";

import { ArrowBigLeft, ArrowBigRight } from "lucide-react";
import { chapters, SubTopic } from "../constants";

Expand Down Expand Up @@ -67,6 +80,19 @@ const chapterComponents: Record<string, React.ComponentType> = {
ch11: Ch11Content,
"ch11-lalr-solved-problem": LalrSolvedProblemContent,
ch12: Ch12Content,
ch13: Ch13Content,
ch14: Ch14Content,
"ch14-expr-eval-example": ExprEvalExampleContent,
ch15: Ch15Content,
ch16: Ch16Content,
"ch16-sdt-postfix-trace": SdtPostfixTraceContent,
ch17: Ch17Content,
ch18: Ch18Content,
ch19: Ch19Content,
"ch19-scope-management": ScopeManagementContentExport,
ch20: Ch20Content,
"ch20-parameter-passing": ParameterPassingContentExport,

};

type ChapterProps = {
Expand Down
33 changes: 33 additions & 0 deletions app/sem5/cd/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,37 @@ export const chapters: Chapter[] = [
],
},
{ id: "ch12", title: "Advanced Parsing & Ambiguity" },
{ id: "ch13", title: "Introduction to Semantic Analysis" },
{
id: "ch14",
title: "Attribute Grammars",
subTopics: [
{ id: "ch14-expr-eval-example", title: "Worked Example: Expression Evaluation", isPage: true },
],
},
{ id: "ch15", title: "Syntax Directed Definitions (SDD)" },
{
id: "ch16",
title: "Syntax Directed Translation (SDT)",
subTopics: [
{ id: "ch16-sdt-postfix-trace", title: "SDT Trace: Infix to Postfix", isPage: true },
],
},
{ id: "ch17", title: "S-Attributed & L-Attributed Definitions" },
{ id: "ch18", title: "Evaluation Order & Dependency Graphs" },
{
id: "ch19",
title: "Symbol Table",
subTopics: [
{ id: "ch19-scope-management", title: "Scope Management in Detail", isPage: true },
],
},
{
id: "ch20",
title: "Runtime Environment & Activation Records",
subTopics: [
{ id: "ch20-parameter-passing", title: "Parameter Passing Mechanisms", isPage: true },
],
},
{ id: "ch21", title: "Unit 3 Solved PYQs" },
];
149 changes: 149 additions & 0 deletions app/sem5/cd/content/ch14-expr-eval-example.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,149 @@
import React from "react";
import ExpandingBox from "../components/ExpandingBox";

export const LexicalAnalyzerGenContent = () => {
// Wait, let's look at the filename mapping in page.tsx for ch14-expr-eval-example:
// "ch4-lexical-analyzer-gen": LexicalAnalyzerGenContent,
// Oh! Wait!
// In page.tsx:
// "ch4-lexical-analyzer-gen": LexicalAnalyzerGenContent,
// We need to name the exported component for ch14-expr-eval-example. Let's make sure it matches what we import.
// In implementation_plan.md I proposed:
// ch14-expr-eval-example -> Let's name the component: ExprEvalExampleContent
// Let's verify what I proposed or what would make sense. I will import ExprEvalExampleContent in page.tsx.
// Let's export ExprEvalExampleContent from here!
return (
<div className="course-content">
<h2 className="section-heading">Worked Example: Expression Evaluation</h2>
<p className="p-text">
Let us trace how an expression is parsed and evaluated using only synthesized attributes. We will use a standard arithmetic grammar with a synthesized attribute <code>.val</code>.
</p>

<h2 className="section-heading">Grammar &amp; Semantic Rules</h2>
<div className="overflow-x-auto my-3">
<table className="w-full text-left border-collapse text-sm text-[#e2d1c1]">
<thead>
<tr className="border-b border-[#c7a669]/40 bg-[#ebdcb0]/10">
<th className="p-2 font-bold text-[#c7a669]">Production Rule</th>
<th className="p-2 font-bold text-[#c7a669]">Semantic Rule (Synthesized <code>.val</code>)</th>
</tr>
</thead>
<tbody>
<tr className="border-b border-[#c7a669]/10">
<td className="p-2 font-mono">L &rarr; E \n</td>
<td className="p-2 font-mono">L.val = E.val; print(L.val)</td>
</tr>
<tr className="border-b border-[#c7a669]/10">
<td className="p-2 font-mono">E &rarr; E_1 + T</td>
<td className="p-2 font-mono">E.val = E_1.val + T.val</td>
</tr>
<tr className="border-b border-[#c7a669]/10">
<td className="p-2 font-mono">E &rarr; T</td>
<td className="p-2 font-mono">E.val = T.val</td>
</tr>
<tr className="border-b border-[#c7a669]/10">
<td className="p-2 font-mono">T &rarr; T_1 * F</td>
<td className="p-2 font-mono">T.val = T_1.val * F.val</td>
</tr>
<tr className="border-b border-[#c7a669]/10">
<td className="p-2 font-mono">T &rarr; F</td>
<td className="p-2 font-mono">T.val = F.val</td>
</tr>
<tr className="border-b border-[#c7a669]/10">
<td className="p-2 font-mono">F &rarr; ( E )</td>
<td className="p-2 font-mono">F.val = E.val</td>
</tr>
<tr>
<td className="p-2 font-mono">F &rarr; digit</td>
<td className="p-2 font-mono">F.val = digit.lexval</td>
</tr>
</tbody>
</table>
</div>

<h2 className="section-heading">Step-by-Step Trace of: <code>3 + 5 * 2</code></h2>
<p className="p-text">
Here is how the compiler evaluates <code>3 + 5 * 2</code> during a bottom-up (postorder) traversal of the parse tree:
</p>

<ExpandingBox title="Bottom-Up Evaluation Walkthrough" defaultOpen={true}>
<ol className="list-decimal list-inside p-text space-y-3 ml-2">
<li>
<strong>Lexical Scan:</strong> Lexer yields the tokens:
<code className="bg-black/30 px-1 rounded text-[#ebdcb0] ml-1">digit(3)</code>,
<code className="bg-black/30 px-1 rounded text-[#ebdcb0] ml-1">+</code>,
<code className="bg-black/30 px-1 rounded text-[#ebdcb0] ml-1">digit(5)</code>,
<code className="bg-black/30 px-1 rounded text-[#ebdcb0] ml-1">*</code>,
<code className="bg-black/30 px-1 rounded text-[#ebdcb0] ml-1">digit(2)</code>.
</li>
<li>
<strong>First leaf reduction:</strong> <code>F &rarr; digit(3)</code>.
<br />
<span className="text-sm text-[#ebdcb0]">Calculation:</span> <code>F.val = digit.lexval = 3</code>.
</li>
<li>
<strong>Promote term:</strong> <code>T &rarr; F</code>.
<br />
<span className="text-sm text-[#ebdcb0]">Calculation:</span> <code>T.val = F.val = 3</code>.
</li>
<li>
<strong>Initialize expression:</strong> <code>E &rarr; T</code>.
<br />
<span className="text-sm text-[#ebdcb0]">Calculation:</span> <code>E.val = T.val = 3</code> (Left subtree completed).
</li>
<li>
<strong>Second leaf reduction:</strong> <code>F &rarr; digit(5)</code>.
<br />
<span className="text-sm text-[#ebdcb0]">Calculation:</span> <code>F.val = 5</code>.
</li>
<li>
<strong>Promote second term:</strong> <code>T_1 &rarr; F</code>.
<br />
<span className="text-sm text-[#ebdcb0]">Calculation:</span> <code>T_1.val = 5</code>.
</li>
<li>
<strong>Third leaf reduction:</strong> <code>F &rarr; digit(2)</code>.
<br />
<span className="text-sm text-[#ebdcb0]">Calculation:</span> <code>F.val = 2</code>.
</li>
<li>
<strong>Multiplication reduction:</strong> <code>T &rarr; T_1 * F</code>.
<br />
<span className="text-sm text-[#ebdcb0]">Calculation:</span> <code>T.val = T_1.val * F.val = 5 * 2 = 10</code>.
</li>
<li>
<strong>Addition reduction:</strong> <code>E &rarr; E_1 + T</code>.
<br />
<span className="text-sm text-[#ebdcb0]">Calculation:</span> <code>E.val = E_1.val + T.val = 3 + 10 = 13</code>.
</li>
<li>
<strong>Halting step:</strong> <code>L &rarr; E \n</code>.
<br />
<span className="text-sm text-[#ebdcb0]">Calculation:</span> <code>L.val = 13</code>, triggering the print action.
</li>
</ol>
</ExpandingBox>

<ExpandingBox title="Parse Tree Structure Visual">
<p className="p-text mb-3">
The annotated parse tree showing the values flowing upwards:
</p>
<pre className="bg-[#ebdcb0]/5 text-[#e2d1c1] p-4 rounded font-mono text-sm leading-relaxed border border-[#c7a669]/20">
{` L [val=13]
|
E [val=13]
/ | \\
E [val=3] + T [val=10]
| / | \\
T [val=3] T [val=5] * F [val=2]
| | |
F [val=3] F [val=5] digit [lexval=2]
| |
digit(3) digit(5)`}
</pre>
</ExpandingBox>
</div>
);
};

export const ExprEvalExampleContent = LexicalAnalyzerGenContent;
141 changes: 141 additions & 0 deletions app/sem5/cd/content/ch16-sdt-postfix-trace.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,141 @@
import React from "react";
import ExpandingBox from "../components/ExpandingBox";

export const NfaToDfaSubsetContent = () => {
// Wait, let's export SdtPostfixTraceContent from here so page.tsx can import it as:
// import { SdtPostfixTraceContent } from "../content/ch16-sdt-postfix-trace";
return (
<div className="course-content">
<h2 className="section-heading">SDT Trace: Infix to Postfix</h2>
<p className="p-text">
Let us trace how an infix expression is converted into postfix notation during syntax analysis using embedded actions.
</p>

<h2 className="section-heading">SDT Rules for Postfix Translation</h2>
<div className="overflow-x-auto my-3">
<table className="w-full text-left border-collapse text-sm text-[#e2d1c1]">
<thead>
<tr className="border-b border-[#c7a669]/40 bg-[#ebdcb0]/10">
<th className="p-2 font-bold text-[#c7a669]">Production</th>
<th className="p-2 font-bold text-[#c7a669]">Embedded Action</th>
</tr>
</thead>
<tbody>
<tr className="border-b border-[#c7a669]/10">
<td className="p-2 font-mono">E &rarr; E_1 + T</td>
<td className="p-2 font-mono">&#123; print(&apos;+&apos;) &#125;</td>
</tr>
<tr className="border-b border-[#c7a669]/10">
<td className="p-2 font-mono">E &rarr; T</td>
<td className="p-2 font-mono"><span className="text-[#ac9e91] font-sans">no action</span></td>
</tr>
<tr className="border-b border-[#c7a669]/10">
<td className="p-2 font-mono">T &rarr; T_1 * F</td>
<td className="p-2 font-mono">&#123; print(&apos;*&apos;) &#125;</td>
</tr>
<tr className="border-b border-[#c7a669]/10">
<td className="p-2 font-mono">T &rarr; F</td>
<td className="p-2 font-mono"><span className="text-[#ac9e91] font-sans">no action</span></td>
</tr>
<tr className="border-b border-[#c7a669]/10">
<td className="p-2 font-mono">F &rarr; ( E )</td>
<td className="p-2 font-mono"><span className="text-[#ac9e91] font-sans">no action</span></td>
</tr>
<tr className="border-b border-[#c7a669]/10">
<td className="p-2 font-mono">F &rarr; id</td>
<td className="p-2 font-mono">&#123; print(id.name) &#125;</td>
</tr>
<tr>
<td className="p-2 font-mono">F &rarr; num</td>
<td className="p-2 font-mono">&#123; print(num.val) &#125;</td>
</tr>
</tbody>
</table>
</div>

<h2 className="section-heading">Step-by-Step Execution for: <code>a + b * c</code></h2>
<p className="p-text">
Below is the step-by-step reduction trace demonstrating when each print statement is executed:
</p>

<ExpandingBox title="Execution Trace Walkthrough" defaultOpen={true}>
<ol className="list-decimal list-inside p-text space-y-3 ml-2">
<li>
<strong>First reduction:</strong> <code>F &rarr; id(a)</code>.
<br />
<span className="text-[#ebdcb0] text-sm">Action executed:</span> <code>print(a)</code>.
<br />
<span className="text-[#b9fa82] text-sm font-semibold">Output so far:</span> <code>a</code>.
</li>
<li>
<strong>Promote to term:</strong> <code>T &rarr; F</code> (No action).
</li>
<li>
<strong>Promote to expression:</strong> <code>E &rarr; T</code> (No action).
</li>
<li>
<strong>Second reduction:</strong> <code>F &rarr; id(b)</code>.
<br />
<span className="text-[#ebdcb0] text-sm">Action executed:</span> <code>print(b)</code>.
<br />
<span className="text-[#b9fa82] text-sm font-semibold">Output so far:</span> <code>a b</code>.
</li>
<li>
<strong>Promote to left factor:</strong> <code>T_1 &rarr; F</code> (No action).
</li>
<li>
<strong>Third reduction:</strong> <code>F &rarr; id(c)</code>.
<br />
<span className="text-[#ebdcb0] text-sm">Action executed:</span> <code>print(c)</code>.
<br />
<span className="text-[#b9fa82] text-sm font-semibold">Output so far:</span> <code>a b c</code>.
</li>
<li>
<strong>Product reduction:</strong> <code>T &rarr; T_1 * F</code>.
<br />
<span className="text-[#ebdcb0] text-sm">Action executed:</span> <code>print(*)</code>.
<br />
<span className="text-[#b9fa82] text-sm font-semibold">Output so far:</span> <code>a b c *</code>.
</li>
<li>
<strong>Sum reduction:</strong> <code>E &rarr; E_1 + T</code>.
<br />
<span className="text-[#ebdcb0] text-sm">Action executed:</span> <code>print(+)</code>.
<br />
<span className="text-[#b9fa82] text-sm font-semibold">Final Output:</span> <code>a b c * +</code>.
</li>
</ol>
</ExpandingBox>

<ExpandingBox title="Execution Order Parse Tree">
<p className="p-text mb-3">
The numbered circles indicate the order in which nodes are reduced and their semantic actions are triggered:
</p>
<pre className="bg-[#ebdcb0]/5 text-[#e2d1c1] p-4 rounded font-mono text-sm leading-relaxed border border-[#c7a669]/20">
{` E [8: print('+')]
/ | \\
E + T [7: print('*')]
| / | \\
T T * F [6: print('c')]
| | |
F F id(c)
| |
id(a) id(b)
[1:a] [4:b]

Reductions occur in this order:
1. F -> id(a) [Prints 'a']
2. T -> F
3. E -> T
4. F -> id(b) [Prints 'b']
5. T -> F
6. F -> id(c) [Prints 'c']
7. T -> T * F [Prints '*']
8. E -> E + T [Prints '+']`}
</pre>
</ExpandingBox>
</div>
);
};

export const SdtPostfixTraceContent = NfaToDfaSubsetContent;
Loading