Optimize Basic Block with DAG Analysis
Optimize Basic Block with DAG Analysis
Consider the following basic block, Draw a directed acyclic graph and identify local common
sub-expressions. After eliminating the common sub-expressions, re-write the basic block.
t1 = a + b
if t1 > c goto L1
goto L2
L1: t2 = a + b
x = t2
goto L3
L2: t3 = c - d
x = t3
L3: t4 = a + b
y = t4
t5 = x + y
z = t5
vbnet
`t1 = a + b
if t1 > c goto L1
goto L2
L1: t2 = a + b
x = t2
goto L3
L2: t3 = c - d
x = t3
L3: t4 = a + b
y = t4
t5 = x + y
z = t5
`
`y = t4` → again `a + b`
`t5 = x + y` → x + y → new operation node
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 1/45
Nodes for: `a`, `b`, `c`, `d`
Node for `a+b` (shared among `t1`, `t2`, `t4`)
Thus, `a+b` is a common sub-expression and can be computed once and reused.
plaintext
`t1 = a + b
if t1 > c goto L1
goto L2
L1: x = t1
goto L3
L2: t3 = c - d
x = t3
L3: y = t1
t5 = x + y
z = t5
`
Explanation:
Would you also like me to draw the DAG diagram visually for you? 📈
It’ll be clearer if you need it for your notes!
yes
Image created
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 2/45
20. How to construction a DAG from the expression Also discuss the significance of using DAGs in
analyzing and optimizing mathematical expressions.
x = (a * b) + (c * d);
y = (a * b) - (c * d);
z = (a * b) + (c * d);
w = x + y;
Given:
ini
`x = (a * b) + (c * d);
y = (a * b) - (c * d);
z = (a * b) + (c * d);
w = x + y;
`
We can see that `(a * b)` and `(c * d)` are repeated sub-expressions used multiple times.
We create nodes:
Internal nodes:
`-` node combining `(a * b)` and `(c * d)` → used for `y`
👉 Important: Since `(a * b)` and `(c * d)` are common, we compute once and reuse.
css
` + (w)
/ \
+ (x/z) - (y)
/ \ / \
(a*b) (c*d)(a*b) (c*d)
/ \ / \
a b c d
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 3/45
`(a*b)` and `(c*d)` are computed once and reused.
Would you also like me to draw a neat DAG diagram image 📈 for this one too? It’ll look super clean for
your notes! 🎯
19. Discuss about simple code generation algorithm. Also generate the code for
x- a/(b+c)-d*(e+f)
2. Output: A sequence of simple machine instructions (like `LOAD`, `MUL`, `ADD`, `SUB`, `STORE`).
Main steps:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 4/45
Reuse results if already computed.
📌 Given Expression:
We are asked to generate code for:
ini
`x = a / (b + c) - d * (e + f)
`
ruby
` -
/ \
/ \
/ \
/ *
/ \ / \
a + d +
/ \ / \
b c e f
`
4. Compute `d * (e + f)`
5. Subtract results from steps 2 and 4
`LOAD R0, b ; R0 = b
ADD R0, c ; R0 = b + c
LOAD R1, a ; R1 = a
DIV R1, R0 ; R1 = a / (b + c)
LOAD R2, e ; R2 = e
ADD R2, f ; R2 = e + f
LOAD R3, d ; R3 = d
MUL R3, R2 ; R3 = d * (e + f)
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 5/45
📌 Summary of Operations:
6 Loads
4 Arithmetic Operations (ADD, DIV, ADD, MUL, SUB)
1 Store
Would you also like me to show how this could be optimized further using minimum registers or stack-
based code (like in real-world compilers)? 🎯
It gets even cleaner! 🚀
18. Create DAG and three – address code for the following C program.
int main() {
int a, b, c, d, e, f;
a = 5;
b = 10;
c = a + b;
d = a * b;
e = c + d;
f = e - e;
return f;
}
📌 1. Given C Program:
c
`int main() {
int a, b, c, d, e, f;
a = 5;
b = 10;
c = a + b;
d = a * b;
e = c + d;
f = e - e;
return f;
}
`
`a = 5` → constant assignment
`b = 10` → constant assignment
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 6/45
`c = a + b`
`d = a * b`
`e = c + d`
`f = e - e`
Now:
Leaf nodes: 5, 10
Operation nodes:
`c = a + b`
`d = a * b`
`e = c + d`
` (f) = (e - e)
/ \
(e) (e)
|
(c + d)
/ \
(a+b) (a*b)
/ \ / \
(5) (10) (5) (10)
`
Notice:
`a` is 5
`b` is 10
`c = a+b`
`d = a*b`
`e = c+d`
plaintext
`t1 = 5 ; a = 5
t2 = 10 ; b = 10
t3 = t1 + t2 ; c = a + b
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 7/45
t4 = t1 * t2 ; d = a * b
t5 = t3 + t4 ; e = c + d
t6 = t5 - t5 ; f = e - e (which is 0)
return t6
`
📌 5. Extra Optimization
Since `e - e` is always 0, you can optimize further:
plaintext
`t1 = 5 ; a = 5
t2 = 10 ; b = 10
t3 = t1 + t2 ; c = a + b
t4 = t1 * t2 ; d = a * b
t5 = t3 + t4 ; e = c + d
t6 = 0 ; f = 0
return t6
`
Would you like me to also show you a neat diagram for this DAG 📈?
It'll be super useful if you're preparing notes or assignments! 📚✨
17. In what systematic manner can the identification of loops within a basic block be approached,
considering various algorithmic techniques and heuristics, and how might these methods be
refined to accommodate complex loop structures and improve the accuracy of loop detection for
subsequent optimization efforts?
Alright, you’re asking a really deep and important question — it’s all about how to systematically
detect loops inside a basic block (or more generally, in a control flow graph) so that compilers can
optimize better.
Strength reduction
Parallelization
Dead code elimination
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 8/45
Challenge:
Loops aren't always simple `for` or `while` — complex loops can have multiple entry/exit points.
Formally:
Loop = {v} ∪ {nodes that can reach u without leaving v's dominator tree}
📌 3. Algorithm Summary
🔵 Algorithm to find loops:
1. Build CFG.
2. Perform DFS and classify edges.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 9/45
3. Find all back edges (u → v) where v dominates u.
Nested loops
Technique Purpose
Breaks the CFG into strongly connected regions (sets of basic blocks with
Interval Analysis
cycles). Useful for recognizing irregular loops.
Natural Loop Detection with Verifies if the CFG is reducible (i.e., every loop has a single entry). Makes
Reducibility Checks optimization simpler.
If multiple back edges exist to the same header, combine them into one
Heuristic for Multiple Back Edges
larger loop.
Techniques like "loop peeling" and "node splitting" help deal with multiple-
Unstructured Loop Handling
entry/multiple-exit loops.
📌 6. Quick Visualization
🔵 Simple loop (detected by back edge):
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 10/45
rust
or
✅ Final Thought:
👉 Accurate loop detection is the heart of making compilers optimize fast code.
👉 Better loop detection = better performance of your compiled programs!
Would you like me to also show a small real example (like a C program with complex nested loops) and
actually trace the detection step-by-step? 🚀
It'll make this super clear with a real CFG! 🎯
16. Develop a program that calculates the dot product of two vectors and Optimize the provided
code by removing common sub expressions.
Alright! Let's first develop the basic program to calculate the dot product of two vectors, and then
optimize it by removing common sub-expressions.
`#include <stdio.h>
int main() {
int n, i;
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 11/45
printf("Enter size of vectors: ");
scanf("%d", &n);
int dot_product = 0;
for (i = 0; i < n; i++) {
dot_product = dot_product + (a[i] * b[i]);
}
`a[i]` and `b[i]` themselves are expressions like `(x + y)` or `(p + q)`.
But if `a[i]` or `b[i]` were complex expressions, we could precompute them outside.
`a[i] = x + y;
b[i] = x + y;
`
scss
`(x + y) * (x + y)
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 12/45
Here, `(x + y)` is repeated — so we can optimize:
`t = x + y;
dot_product = dot_product + (t * t);
`
`#include <stdio.h>
int main() {
int n, i;
int x, y;
printf("Enter size of vectors and values of x, y: ");
scanf("%d %d %d", &n, &x, &y);
int dot_product = 0;
int t = x + y; // common sub-expression
📌 5. Summary
Stage Action
Write basic dot product Loop multiply and sum
Identify expressions Check if repeated calculations exist
✅ Final Thought:
👉 If vector elements themselves are simple variables, optimization isn't critical.
👉 If elements are complex expressions, optimize by computing once and reusing!
Would you also like me to show a real-world version where dot product is used for vectors of
structures or dynamic memory allocation (malloc), which is very common in big software projects? 🚀
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 13/45
🎯
It's really cool for project-level coding!
📌 1. Corrected Code:
c
`#include <stdio.h>
int main() {
int n, k = 0, i;
scanf("%d", &n);
k = 1;
if (i == n)
printf("number is prime");
else
printf("number is not prime");
}
`
✅ I fixed:
The syntax: missing commas, wrong quote marks, typo `I` → `i`, etc.
The message: `number is not printed` → should be `number is not prime`.
`int n, k = 0, i;
scanf("%d", &n);
i = 2;
`
`k = 1;
`
Basic Block 5 (B5): (After loop breaks or completes, check prime or not)
`if (i == n)
printf("number is prime");
else
printf("number is not prime");
`
📌 3. Flow Graph
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 15/45
Now, we can draw the control flow graph (CFG) showing the relationship between basic blocks:
scss
` [B1]
↓
[B2] → (i >= n) → [B4]
↓ (i < n)
[B3] → (n % i == 0) → [B5]
↓ (n % i != 0)
[B2]
[B4] → [B5]
`
✅ Explanation:
B1 → B2 (after initialization)
B2 checks `i < n`:
If true → B3
If false → B4
B3 checks if `n % i == 0`:
📌 4. Summary Table
Basic Block Code Outgoing
B1 Variable initialization + scanf → B2
✅ Final Conclusion:
You have 5 basic blocks and a flow graph with branches at condition checks inside the loop and after.
Would you like me to quickly draw this flow graph diagram nicely (like a box-and-arrow diagram 📈), so
you can directly use it for your notes or assignments? 🎯
It will look very clean! 🚀
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 16/45
13. Identify the basic block in the given code. How will you apply various transformations to
optimize the basic block?
int x = 7;
int y = 14;
int z = x + y;
int p = z * 2;
int q = p / 2;
return q;
📌 1. Given Code:
c
`int x = 7;
int y = 14;
int z = x + y;
int p = z * 2;
int q = p / 2;
return q;
`
Transformation Purpose
Constant Folding Precompute expressions with constants
Now step-by-step:
`int x = 7; // constant
int y = 14; // constant
int z = 7 + 14; // z = 21
int p = 21 * 2; // p = 42
int q = 42 / 2; // q = 21
return q;
`
`q = 21`
So just:
`return 21;
`
`return 21;
`
📌 5. Important Points
Constant Folding saves runtime computation by doing arithmetic at compile-time.
Dead Code Elimination happened because intermediate variables `x`, `y`, `z`, `p` became
useless after folding.
✅ Final Answer:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 18/45
Basic block = Entire given code (no branches).
After optimization =
`return 21;
`
Would you like me to also show a quick visual of how the intermediate 3-address code (TAC) would look
before and after optimization? 📜
🎯✨
It’s really helpful if you want full marks in exams!
11. State the purpose of activation tree during compilation. Develop an activation tree for the C
code to compute factorial of five.
Tracks function calls and their lifetimes (which call is active at a given time).
Function inlining
Tail-call optimization
Stack management
✅ In short: it models function call sequences to help the compiler generate efficient and correct code.
📌 2. Write Simple C Code for Factorial of 5
Here’s a simple recursive factorial program:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 19/45
c
`#include <stdio.h>
int fact(int n) {
if (n == 0)
return 1;
else
return n * fact(n - 1);
}
int main() {
int result = fact(5);
printf("%d\n", result);
return 0;
}
`
` fact(5)
|
fact(4)
|
fact(3)
|
fact(2)
|
fact(1)
|
fact(0)
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 20/45
📌 5. Diagram (Visual)
Here's a simple tree view:
scss
`fact(5)
└── fact(4)
└── fact(3)
└── fact(2)
└── fact(1)
└── fact(0)
`
✅ Final Summary:
Concept Explanation
Activation Tree Purpose Track function calls, manage stack, return control after calls, optimize functions
Activation Tree for fact(5) Shows nested calls: `fact(5)` → `fact(4)` → ... → `fact(0)`
Would you also like me to show how the stack frames are created and destroyed based on this
activation tree? 📚
It's a cool next step and very useful for exam/assignment! 🚀✨
🎯
Alright, let's tackle this carefully!
We will generate the Intermediate Representations (IR) you asked for:
a) Syntax Tree
b) Postfix Notation
c) Three-Address Code (TAC)
📌 1. Given C Code:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 21/45
c
`void main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d ", i);
}
}
`
`i = 1;` → Initialization
`i <= 10;` → Condition check
📌 3. a) Syntax Tree
We focus mainly on the loop.
The syntax tree for the loop (`for`) looks like this:
arduino
` for
/ | \
i = 1 i <= 10 i++
|
printf(i)
`
✅ Explanation:
Root is `for`
First child: Initialization `i = 1`
Second child: Condition `i <= 10`
Third child:
Body: `printf(i)`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 22/45
Postfix for the for-loop body:
perl
`1 i =
i 10 <=
(i "%d " printf)
i 1 + i =
goto ConditionCheck
`
✅ Step-by-step meaning:
Assign `i = 1`
Check `i <= 10`
If true, do `printf`
Increment `i`
Go back to condition check
✅ Explanation:
L1: start of loop
Check condition: if `i > 10`, exit loop (jump to `L2`)
Call printf with `i` as a parameter
Increment i (i++)
Go back to L1 to check the condition again
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 23/45
Form Representation
Postfix Notation `1 i =`, `i 10 <=`, `(i "%d" printf)`, `i 1 + i =`, `goto condition`
Three-Address Code Sequence of small steps with labels and branches
✅ Final Notes:
Syntax Tree shows hierarchy (tree structure)
Would you also like me to draw the syntax tree diagram properly (with circles/branches) 📈 for your
notes or submission?
It looks really neat and earns extra points! 🚀🎯
Got it — you want the Three-Address Code (TAC) for the for-loop more clearly and properly! 🔥
Let’s redo it carefully and step-by-step.
📌 Program Reminder:
c
`void main() {
int i;
for (i = 1; i <= 10; i++) {
printf("%d ", i);
}
}
`
`1. i = 1 // Initialization
2. L1: if i > 10 goto L2 // Condition check
3. param i // Pass i as parameter
4. call printf // Call printf function
5. i = i + 1 // Increment i
6. goto L1 // Repeat the loop
7. L2: // Exit point
`
📌 Line-by-Line Explanation:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 24/45
Line Action
1 Set `i` to `1` (initialize loop variable)
2 Start of loop (Label L1), check if `i > 10`. If true, jump to `L2` (exit).
3 Send `i` to `printf` (parameter passing).
4 Call `printf` to print the value of `i`.
5 Increment `i` by `1`.
6 Go back to L1 to check the condition again.
`i = 1
L1: if i > 10 goto L2
param i
call printf
i = i + 1
goto L1
L2:
`
✅ Notes:
We use labels (L1 and L2) for jumps.
Each `for` loop is broken into:
Initialization
Condition check
Body execution
Increment
Repeat
Would you also like me to show how this TAC would look like in a flowchart? 🚀✨ It’s super clean if
you’re submitting this for assignments! 🎯
9. What is back patching? How it can be used to generate code for boolean expressions and
declaration statements?
Later, once the actual label is known, we fill in (patch) the missing jump addresses.
🔵 Example:
Imagine generating code for:
`if (a > b) {
x = 1;
} else {
x = 2;
}
`
When you generate the code for `if (a > b)`, you don't know where to jump after the true or
false part.
So you backpatch those jumps later when the blocks are ready.
Jump on Purpose
True If condition is satisfied (true), jump to a "then" block.
🔵 Example:
For expression `a > b`, the code might be like:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 26/45
cpp
✅ Example:
c
`int a = 5 + b;
`
We may need to backpatch the address of `b` once we finish its evaluation.
📌 6. Summary Table
Aspect Details
Backpatching Filling missing addresses (goto targets) once labels are known.
Would you like me to show a real TAC example using backpatching for a full `if-else` or `while` loop
too? 🎯 It will make it crystal clear with code!🚀✨
(Useful if you have a practical/assignment too!)
📌 Expression:
plaintext
`x = (a + b) * (c - d)
`
`t1 = a + b
t2 = c - d
t3 = t1 * t2
x = t3
`
✅ Meaning:
First compute `a + b`, store in `t1`
Compute `c - d`, store in `t2`
Multiply `t1` and `t2`, store in `t3`
Assign `t3` to `x`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 28/45
📌 2. Quadruple Representation
👉 Quadruple: Each instruction is a 4-tuple:
`(Operator, Argument1, Argument2, Result)`
- c d t2
* t1 t2 t3
= t3 — x
✅ Format:
scss
`(+, a, b, t1)
(-, c, d, t2)
(*, t1, t2, t3)
(=, t3, -, x)
`
📌 3. Triple Representation
👉 Triple: Each instruction is a 3-tuple but uses instruction numbers instead of temporary names.
Index Operator Arg1 Arg2
0 + a b
1 - c d
2 * (0) (1)
3 = (2) x
✅ Format:
scss
`(0) (+, a, b)
(1) (-, c, d)
(2) (*, (0), (1))
(3) (=, (2), x)
`
Note:
✅ Instructions are the same as triples, but now you refer through the pointer table.
Indirect triple format:
plaintext
`Pointer Table:
0 → (0) (+, a, b)
1 → (1) (-, c, d)
2 → (2) (*, (0), (1))
3 → (3) (=, (2), x)
`
✅ Final Tip:
TAC and Quadruple use explicit temporary names.
Would you also like me to draw a flow diagram showing how these steps happen? 🎯 (It looks super
clean for notes or assignments!) 🚀✨
🎯
Just say "yes" if you want!
how how would you generate the intermediate code for the flow of control statement? Explain with
example
Alright! Let’s explain how to generate intermediate code for flow of control statements — clearly
and with an example 🔥 .
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 30/45
📌 What is Flow of Control?
Flow of control in programming means:
To generate Intermediate Code (like Three-Address Code, TAC), we break these flow-control
statements into:
`if (a > b)
x = y + z;
else
x = y - z;
`
📌 Step-by-Step Breakdown:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 31/45
Step 1: Check condition `a > b`
If true, jump to `L1`
Else, jump to `L2`
Step 2:
`L1` (True block): `x = y + z`, then jump to `L3` (after the if-else).
Step 3:
`L2` (False block): `x = y - z`.
Step 4:
`L3` marks the exit (common end) point.
`while (a < b) {
a = a + 1;
}
`
📌 Step-by-Step Breakdown:
Step 1: Label `L1` marks the beginning of the loop.
Step 2: If `a >= b`, jump to `L2` (exit loop).
📌 Summary Table
Statement Strategy
if (cond) `if cond goto TrueLabel else goto FalseLabel`
if-else Jump to different labels depending on condition
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 32/45
Statement Strategy
while Label at beginning, condition checked, body, jump back
for Initialization → Condition check → Body → Increment → Repeat
break/continue Use labels to jump out of loop or skip to next iteration
Would you like me to also show how a `for` loop (like `for (i = 0; i < n; i++)`) is converted into
intermediate code too? 🚀(It’s super handy!)
Just say “yes”!🎯
6. Generate the intermediate code for the following code segment along with the required syntax
directed translation scheme.
do {
n /= 10;
++count;
} while (n != 0);
printf("Number of digits: %d", count);
📌 Given C Code:
c
`do {
n /= 10;
++count;
} while (n != 0);
printf("Number of digits: %d", count);
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 33/45
plaintext
`S → do S1 while (B) ;
{
L1: [Link]
L2: if ([Link]) goto L1
}
`
Meaning:
`L1: n = n / 10
count = count + 1
if n != 0 goto L1
printf "Number of digits: %d", count
`
`S → do S1 while (B) ;
{
L1: [Link]
if [Link] goto L1
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 34/45
}
`
✅ Intermediate Code:
plaintext
`L1: n = n / 10
count = count + 1
if n != 0 goto L1
printf "Number of digits: %d", count
`
🎯 Quick Tip:
In `do-while`, body executes at least once (because condition check is after the body).
That's why condition is checked after the block (not before like `while` loops).
Would you also like me to show how the flow graph (control flow diagram) looks for this? 🚀 It’s super
neat for visual understanding! 🎯
Just say "yes"!
4. Illustrate the storage organization memory in the perspective of compiler writer with neat
diagram.
"Illustrate the storage organization of memory from a compiler writer’s perspective with a
neat diagram."
📚 Memory Layout
Section Purpose
Text (Code) Segment Stores compiled program code (instructions).
Data Segment Stores global and static variables initialized by the programmer.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 35/45
Section Purpose
BSS Segment Stores uninitialized global and static variables.
Heap For dynamic memory allocation (`malloc`, `new`, etc.). Grows upwards.
Stack For function calls, local variables, return addresses. Grows downwards.
pgsql
2. Data Segment
4. Heap
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 36/45
Memory is managed manually or through garbage collection.
Grows upward during runtime.
5. Stack
Would you like me to also show a real-world C code example and map its variables into these memory
regions? 🚀 It’ll make this even more crystal clear!
Just say "yes"! 🎯
5. Show the step by step translation of the expression (a + b) * (c + d) using the syntax-directed
translation rules. Identify the synthesized and inherited attributes from the grammar and justify the
reason for its kind.
Alright! Let's dive deep and explain this step-by-step — super clearly! 🚀
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 37/45
You asked:
Show the step-by-step translation of the expression `(a + b) * (c + d)` using syntax-
directed translation rules. Also identify synthesized and inherited attributes and justify
them.
plaintext
`E → E1 + T
E → E1 * T
E → T
T → (E)
T → id
`
Where:
Where:
`place` is a synthesized attribute representing the location (temporary variable) holding the
result.
`newtemp()` generates a new temporary variable like `t1`, `t2`, etc.
`emit()` generates intermediate code.
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 38/45
🎯 First handle `(a + b)`:
`[Link] = b`
`emit(t1 = a + b)`
✅ Code generated:
plaintext
`t1 = a + b
`
`[Link] = d`
`emit(t2 = c + d)`
✅ Code generated:
plaintext
`t2 = c + d
`
Apply `E → E1 * T`:
`emit(t3 = t1 * t2)`
✅ Code generated:
plaintext
`t3 = t1 * t2
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 39/45
📌 Final Intermediate Code:
plaintext
`t1 = a + b
t2 = c + d
t3 = t1 * t2
`
✅ In this example:
Only synthesized attributes are used.
`[Link]`, `[Link]` are computed bottom-up.
No need to pass anything from parent to child → hence no inherited attributes here.
📌 Step 5: Why?
We only needed to compute results step-by-step as we moved upward (bottom to top) in the
tree.
No external context or extra data needed to pass into a rule.
📌 Final Summary
Step Result
Parse `a+b` → `t1 = a + b`
Parse `c+d` → `t2 = c + d`
`(a + b) * (c + d)
→ t1 = a + b
→ t2 = c + d
→ t3 = t1 * t2
`
Would you like me to also draw the full parse tree for this expression (with attribute values)? 🌟 It will
look amazing for your notes or assignments! 🎯
Just say "yes!" if you want it!🚀
Variables
Constants
Functions
Arrays, etc.
The compiler decides when and where to allocate memory using storage allocation strategies.
✅ The three main strategies are:
Strategy Purpose
1. Static Allocation For fixed-size data known at compile time
2. Stack Allocation For dynamic data with a LIFO (Last-In-First-Out) behavior
3. Heap Allocation For dynamic data with arbitrary lifetime (flexible but expensive)
📚 1. Static Allocation
➔ Definition:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 41/45
➔ Where Used:
Global variables
Static local variables
Constants
Functions
➔ Example:
➔ Advantages:
➔ Disadvantages:
📚 2. Stack Allocation
➔ Definition:
➔ Where Used:
Return addresses
➔ Example:
`void func() {
int x = 5; // Allocated on stack
}
`
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 42/45
Every time `func()` is called:
➔ Advantages:
➔ Disadvantages:
Cannot allocate memory whose lifetime extends beyond a function (e.g., returning local address is
dangerous).
📚 3. Heap Allocation
➔ Definition:
Memory is allocated dynamically at runtime using functions like `malloc()`, `calloc()` (in C) or
`new` (in C++/Java).
No specific order — memory blocks can be allocated and freed at any time.
➔ Where Used:
Graphs
➔ Example:
➔ Advantages:
➔ Disadvantages:
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 43/45
📌 Quick Comparison Table
Feature Static Stack Heap
When Allocated Compile-time Run-time Run-time
void fun() {
int x; // Stack Allocation (local)
int* y = malloc(sizeof(int)); // Heap Allocation (dynamic)
}
`
✅ Here:
`g` → static
`x` → stack
`y` → pointer to heap memory
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 44/45
Would you also like a neat diagram (memory layout) that shows static, stack, and heap together
visually?🌟 It looks super good for notes or presentations! 🎯
🚀
Just say "yes"!
Printed using ChatGPT to PDF, powered by PDFCrowd HTML to PDF API. 45/45