0% found this document useful (0 votes)
13 views

Assignment_10_Compiler Solution

The document contains an assignment for a Compiler Design course with multiple-choice questions focusing on activation records, intermediate code generation, and related concepts. Each question includes an answer and an explanation detailing the reasoning behind the correct choice. The assignment emphasizes the implications of static vs. dynamic activation records, the role of intermediate languages, and the management of function calls in programming languages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Assignment_10_Compiler Solution

The document contains an assignment for a Compiler Design course with multiple-choice questions focusing on activation records, intermediate code generation, and related concepts. Each question includes an answer and an explanation detailing the reasoning behind the correct choice. The assignment emphasizes the implications of static vs. dynamic activation records, the role of intermediate languages, and the management of function calls in programming languages.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

NPTEL Online Certification Courses Indian

Institute of Technology Kharagpur

Compiler Design
Assignment- Week 10
TYPE OF QUESTION:MCQ
Number ofquestions:13 Total mark: 13 X 1 = 13

1.

Ans: c)

Explanation:

An activation record (also called an activation frame) is a data structure used in a


program's runtime stack to store information about function calls, including local
variables, parameters, return addresses, and saved registers.

If the activation record is static, this means that each function has a fixed memory
location allocated at compile time rather than dynamically allocating space on the stack
during execution. This has significant implications:

• (A) Passing parameters → Possible


o Parameters can be passed using registers or a statically allocated memory
area.
• (B) Creating local variables → Possible
o Local variables can be allocated in fixed memory locations determined at
compile time.
• **(C) Supporting recursion → NOT possible
o Recursion requires multiple instances of an activation record for the same
function, each with different values for local variables and return
addresses. If activation records are static, a new instance cannot be
created dynamically for each recursive call, making recursion impossible.
• (D) None of the other options → Incorrect
o Since recursion is not possible, option (D) is false.

2.

Ans: b)

Explanation:

The control link (also known as the dynamic link) in an activation record is a pointer to
the activation record of the calling (parent) function. It helps maintain the correct
function call sequence and is crucial for returning to the correct activation record after a
function call completes.

Here's how the options relate:

• (A) Current activation record → Incorrect


o The control link does not point to the current activation record itself.
Instead, it links to the previous (parent) activation record.
• (B) Parent activation record → Correct
o The control link stores the address of the activation record of the calling
function (parent), allowing the program to return correctly when the
called function completes execution.
• (C) Child activation record → Incorrect
o A function may call multiple child functions, but the control link does not
track child activation records. Instead, it always links to the caller.
• (D) None of the other options → Incorrect
o Since (B) is correct, (D) is false.

3.

Ans: b)
Explanation:

Intermediate Code Generation (ICG) is an optional phase in a compiler. It is used to


create an intermediate representation (IR) between the source code and the final
machine code. The purpose of ICG is to improve portability and ease optimization.

However, a compiler can be designed without an intermediate representation by directly


translating source code into machine code (such as in Just-In-Time (JIT) compilers or
interpreters). Therefore, ICG is not mandatory but is commonly used in multi-stage
compilation.

Here's an analysis of the options:

• (A) Must → Incorrect


o While intermediate code is beneficial, some compilers (e.g., direct
interpreters) do not generate it.
• (B) Optional → Correct
o Many compilers use an intermediate representation, but it is not strictly
required. Some compilers translate source code directly into machine
code.
• (C) Depends on language → Incorrect
o While the complexity of intermediate code generation can depend on the
language, the use of an intermediate representation is a design choice
rather than a necessity dictated by the language.
• (D) None of the other options → Incorrect
4.

Ans: a)
Explanation:

P-code (Pseudo-code) is an intermediate code representation used in stack-based virtual


machines, such as the Pascal P-machine. It is designed to be executed on an abstract
stack-based architecture, meaning that operands are pushed onto and popped from a
stack rather than using registers or memory addresses directly.

Here's how the options relate:

• (A) Stack-based machine → Correct


o P-code is designed for execution in stack-based virtual machines, where
operations involve pushing and popping values from a stack.
• (B) Accumulator-based machine → Incorrect
o An accumulator-based machine primarily uses a single register (the
accumulator) for arithmetic operations, whereas P-code relies on a stack
for computation.
• (C) Two operand addresses → Incorrect
o In a two-address machine, instructions explicitly reference two operands
in memory or registers. P-code primarily works with stack operations
rather than direct memory addresses.
• (D) None of the other options → Incorrect

5.

Ans: b)

Explanation:
6. Access link points to the

(A) Current activation record


(B) Parent activation record
(C) Child activation record
(D) None of the other options
Ans: b)
Explanation:

The access link (also called a static link) is used in languages that support nested
procedures or static scoping. It points to the activation record of the lexically enclosing
function (parent function) rather than the function that called it.

Why (B) Parent activation record is correct:

• In static scoping, a function can be nested inside another function, and it needs
access to the non-local variables of the lexically enclosing function.
• The access link helps in reaching these non-local variables.
• The control link (dynamic link), on the other hand, helps with returning to the
caller, but that's different from the access link.

7. If pointer is supported in the high-level language,


(A) Must also be supported in the intermediate language
(B) May not be supported in the intermediate language
(C) Depends on language
(D) None of the other options

Ans: b)
Explanation:

High-level languages (HLL) like C, C++, and Rust support pointers explicitly. However,
the intermediate language (IL) used in a compiler does not necessarily have to support
pointers in the same way. Instead, the IL might represent pointers using addresses,
references, or other constructs that serve a similar purpose without direct pointer
manipulation.

For example:

• Some ILs, like LLVM IR, support explicit pointer operations.


• Other ILs, such as Java bytecode, do not use raw pointers but instead rely on
references and garbage collection.
• Some functional and managed languages (e.g., Python, Java) may eliminate direct
pointer support at the IL level.

8.

Ans: a)

9.

Ans: c)
Explanation:

Explanation:

The frame pointer (FP) is a register that points to the current activation record (stack
frame) in memory. It serves as a stable reference point for accessing local variables,
function parameters, and saved registers within the current function call.

How the frame pointer works:

• When a function is called, a new activation record is created on the stack.


• The frame pointer is updated to point to the start of this activation record.
• Local variables and temporary storage are accessed using negative offsets from
FP.
• Function parameters (passed by the caller) are accessed using positive offsets
from FP.
10 An intermediate language should be
.

(A) Close to target machine


(B) Machine independent
(C) All operators of high-level language supported
(D) All of the other options
Ans: b)

Explanation:

An intermediate language (IL) is typically designed to be machine-independent, meaning


it can be used across multiple hardware architectures before being translated into
machine-specific code.

Evaluating each option:

• (A) Close to target machine → Incorrect


o Some ILs (like LLVM IR) are low-level and close to machine code, but
many ILs (like Java bytecode, .NET CIL) are abstract and machine-
independent.
o The main purpose of an IL is to provide a general representation that
works across different machines, so it is not necessarily close to the target
machine.
• (B) Machine independent → Correct
o ILs like Java bytecode, LLVM IR, and .NET CIL are designed to be portable
and work across different platforms.
o Machine-specific optimizations come later when the IL is compiled into
actual machine code.
• (C) All operators of high-level language supported → Incorrect
o Not all high-level language (HLL) features are directly supported in IL.
o Many HLL constructs (like object-oriented features, closures, or high-level
loops) are translated into simpler, lower-level constructs before or during
IL generation.
o IL is often a simpler, more restricted set of operations compared to HLLs.
• (D) All of the other options → Incorrect

11.

Which of the following is a key purpose of the stack pointer in an activation record?

(A) To store the base address of the current function


(B) To manage dynamic memory allocation
(C) To track the top of the runtime stack
(D) To store global variables

Answer: (C)

Explanation: The stack pointer (SP) keeps track of the top of the stack, ensuring that
function calls, local variable allocation, and return addresses are managed properly. It
helps in pushing and popping activation records dynamically during execution.
12.

What happens if recursion is attempted in a system with a static activation record?

(A) It executes successfully but inefficiently


(B) It results in incorrect execution since previous function calls get overwritten
(C) It executes normally without issues
(D) It leads to infinite recursion by default

Answer: (B)

Explanation: A static activation record means that activation records are allocated in a
fixed location, making it impossible to handle multiple function calls at different
recursion depths. When recursion is attempted, new function calls overwrite the existing
activation record, leading to incorrect execution.

13.

What is the primary advantage of using intermediate code in a compiler?

(A) It simplifies code generation for multiple target machines


(B) It improves the execution speed of the compiled program
(C) It eliminates the need for optimization
(D) It ensures that all high-level language features are directly mapped to assembly

Answer: (A) It simplifies code generation for multiple target machines

Explanation: Intermediate code acts as an abstraction between the high-level source


code and the final machine code, making it easier to generate target-specific code for
different architectures. It also helps in portability and optimization before generating
the final executable code.

END of Assignment

You might also like