Atcd - Unit 3
Atcd - Unit 3
UNIT –3
Intermediate Code is an abstract, machine-independent code that lies between the high-
level source code and the machine-level code generated by a compiler. It simplifies the
process of code optimization and enables portability across different machine
architectures, as the intermediate code can be converted to machine code specific to
each target architecture.
There are several types of intermediate code forms, each serving different purposes in
simplifying translation or optimization. Here’s a breakdown of common forms with
examples for each:
+
/ \
a *
/ \
b c
Here, * has a higher precedence than +, so it appears deeper in the tree, ensuring the
correct order of operations.
2. Three-Address Code (TAC)
t1 = b * c
t2 = a + t1
Here, t1 and t2 are temporary variables holding intermediate values. This sequence
allows the code to be executed in the correct order and enables straightforward
optimizations like common subexpression elimination.
3. Quadruples
Definition: Quadruples are a type of intermediate code form where each instruction
is represented as a four-part tuple (operator, arg1, arg2, result). Quadruples
explicitly list the operation and its operands, making them easy to read and
manipulate.
Use: Useful for tracking temporary results and transformations during optimization.
( *, b, c, t1 )
( +, a, t1, t2 )
4. Triples
(0) *, b, c
(1) +, a, (0)
Here, (0) refers to the result of the first operation (b * c), which is then used in the next
operation.
Example:
o Prefix: + a * b c
o Postfix: a b c * +
2. Define Type Checking and Type Conversion. Give one example of each.
Type Checking
Example:
int a = 5;
string b = "Hello";
a = a + b; // Error: incompatible types
In this example, the compiler detects a type mismatch (int + string) and
produces a compilation error.
Example:
a = 5
b = "Hello"
c = a + b # Runtime error: TypeError: unsupported operand type(s)
Type Conversion
Example:
int a = 5;
float b = 3.2;
float result = a + b; // Implicit conversion: a is converted to float
before the addition
here, a is automatically promoted to float before the addition.
Example:
x = 5.7
y = int(x) # Explicit conversion: float to int, results in y = 5
Three-Address Code (TAC) represents statements in the form of instructions with at most
three operands. Each instruction typically has:
TAC is useful for breaking complex expressions down into simpler, manageable steps,
allowing for better optimization and machine code generation.
Generating TAC for a < b or c < d or e > f
This expression involves logical comparisons (<, >) and logical OR (or) operators. We'll
break it down into intermediate steps using temporary variables to hold intermediate
results.
TAC Instructions:
t1 = a < b
t2 = c < d
t3 = e > f
t4 = t1 or t2
t5 = t4 or t3
Explanation:
Definition: Each rule has a single non-terminal on the left (e.g., A→γA \rightarrow
\gammaA→γ).
Recognizers: Pushdown Automaton (PDA).
Example: Balanced parentheses or nested structures.
Use: Widely used in programming languages and parsers for nested constructs.
Definition: Rules are of the form A→aBA \rightarrow aBA→aB or A→aA \rightarrow
aA→a.
Recognizers: Finite Automaton (FA).
Example: Binary strings with an even number of 0s.
Use: Simple patterns, used in lexical analysis and regular expressions.
5. What is Overloading?
Overloading refers to the ability to define multiple functions or operators with the same
name but with different parameters or behaviors. This allows the programmer to use the
same function or operator name to perform different tasks based on the context, improving
code readability and reducing the need for creating unique names for similar operations.
1. Function Overloading: Defining multiple functions with the same name but
different signatures (number or types of arguments).
2. Operator Overloading: Giving custom behavior to operators when they are applied
to user-defined data types (classes/structures).
1. Function Overloading
How It Works:
Functions with the same name are differentiated by their signatures (the number
and types of parameters).
The function called depends on the number and types of arguments passed during
the function call.
2. Operator Overloading
How It Works:
You define how an operator should behave when applied to objects of your custom
data types.
Operator overloading is often used for objects that represent mathematical
concepts, like complex numbers, matrices, or custom strings.
OVERVIEW :
Function Overloading allows defining functions with the same name but different
parameters to perform similar but distinct tasks.
Operator Overloading lets you define custom behavior for operators when working with
objects of user-defined types, making them more intuitive to use with operators like +, -, *,
etc.