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

UNIT III

Uploaded by

sharmila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
8 views

UNIT III

Uploaded by

sharmila
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 19

UNIT III INTERMEDIATE CODE GENERATION

SET A

Fill in the Blanks (5 x 1 = 5 Marks)

1. ________ attributes depend on the child nodes of a parse tree in syntax-


directed definitions.
Answer: Synthesized
2. Three-Address Code uses at most ________ operands per instruction.
Answer: Three
3. The representation of a program in tree form with operators as internal
nodes is called a ________.
Answer: Syntax Tree
4. ________ type checking is performed during the execution of a program.
Answer: Dynamic
5. In TAC, instructions can be represented as quadruples, triples, or ________
triples.
Answer: Indirect

Multiple Choice Questions (5 x 1 = 5 Marks)

1. Which of the following is not an intermediate representation?


a) Syntax Tree
b) Abstract Syntax Tree
c) Three-Address Code
d) Machine Code
Answer: d) Machine Code
2. Which type of attributes in syntax-directed definitions depend on
parent or sibling nodes?
a) Synthesized
b) Inherited
c) Static
d) Dynamic
Answer: b) Inherited
3. Which of the following is an example of Three-Address Code?
a) MOV A, B
b) t1 = a + b
c) ADD R1, R2
d) (a, b, c)
Answer: b) t1 = a + b
4. Static type checking is performed at:
a) Compile time
b) Runtime
c) During linking
d) After execution
Answer: a) Compile time
5. In the expression x = a + b * c, the multiplication is evaluated:
a) After addition
b) Before addition
c) Simultaneously with addition
d) Depends on the syntax tree
Answer: b) Before addition

Match the Following (5 x 1 = 5 Marks)

Column A Column B
1. Synthesized Attributes a) Depend on child nodes
2. Syntax Tree b) Intermediate representation
3. Type Checking c) Ensures compatibility of types
4. Static Type Checking d) Performed at compile time
5. Dynamic Type Checking e) Performed during execution

Answer:
1 - a, 2 - b, 3 - c, 4 - d, 5 - e

True or False Statements (5 x 1 = 5 Marks)

1. In syntax-directed definitions, synthesized attributes can depend on


parent nodes.
Answer: False
2. Syntax trees are used to represent the hierarchical structure of a
program.
Answer: True
3. Three-address code instructions can use more than three operands.
Answer: False
4. Type checking ensures type safety in expressions and statements.
Answer: True
5. Static type checking occurs during runtime.
Answer: False

Scenario-Based Questions (5 x 1 = 5 Marks)

1. If a programmer writes a + 2.5, but a is declared as an integer, how does


type checking handle this?
Answer: The type checker performs implicit type conversion (coercion)
to convert a to a float, allowing the addition.
2. During parsing, the syntax tree for the expression x = a + b * c is
generated. Which operation is evaluated first?
Answer: The multiplication (b * c) is evaluated first based on operator
precedence.
3. A symbol table is updated with new variable declarations. How is this
information used in type checking?
Answer: The type checker uses the symbol table to verify that variables
are used with their declared types.
4. An error occurs when a Boolean is used in an arithmetic operation. How
does type checking handle this?
Answer: The type checker reports a type mismatch error, as Booleans
cannot be used in arithmetic operations.
5. A syntax tree is generated for the expression (a + b) * c. What
intermediate code is produced?
Answer:

css
Copy code
t1 = a + b
t2 = t1 * c
SET B
Fill in the Blanks:

1. A ____________ is a directed graph representing the structure of a source


program, which is useful in generating intermediate code.
Answer: Syntax Tree
2. The ____________ of an expression determines the order in which the
semantic rules are applied to calculate values or generate intermediate
code.
Answer: Evaluation Order
3. ____________ is a low-level intermediate representation where each
instruction consists of a single operator and at most three operands.
Answer: Three Address Code
4. ____________ is the process of verifying that variables are declared before
use and that operations are applied to compatible data types in a
program.
Answer: Type Checking
5. ____________ is the process of converting high-level programming
language expressions into a form that can be more easily translated into
machine code.
Answer: Translation of Expressions

Multiple Choice Questions:

1. Which of the following is the correct representation of an intermediate


language?
a) Abstract Syntax Tree
b) Assembly Language
c) Three Address Code
d) High-Level Language
Answer: c) Three Address Code
2. What is the primary purpose of type checking in a compiler?
a) To ensure the program executes faster
b) To ensure that variable types are compatible for operations
c) To optimize the program
d) To generate machine code
Answer: b) To ensure that variable types are compatible for operations
3. Which of the following is NOT a feature of a Syntax Directed Definition
(SDD)?
a) It associates each grammar rule with semantic actions
b) It is used to describe how to translate a language
c) It specifies how tokens are recognized
d) It defines the syntax of a language
Answer: c) It specifies how tokens are recognized
4. The type of a variable in a syntax tree is checked during ____________ in
the compilation process.
a) Parsing
b) Lexical Analysis
c) Type Checking
d) Intermediate Code Generation
Answer: c) Type Checking
5. Which of the following is true about Three Address Code?
a) It uses one operator and three operands
b) It is a high-level programming language
c) It is specific to object-oriented programming
d) It is used only for type checking
Answer: a) It uses one operator and three operands

Match the Following:

Column A Column B
a) Representing expressions and statements in an
1. Syntax Tree
intermediate form
b) Ensures operations are applied to compatible
2. Type Checking
types
c) Intermediate representation of code used for
3. Three Address Code
optimization
4. Translation of d) Conversion of expressions into machine-
Expressions compatible code
5. Syntax Directed e) Defines the translation of a language using
Definitions (SDDs) semantic rules

Answer:

1. Syntax Tree → a) Representing expressions and statements in an


intermediate form
2. Type Checking → b) Ensures operations are applied to compatible types
3. Three Address Code → c) Intermediate representation of code used for
optimization
4. Translation of Expressions → d) Conversion of expressions into
machine-compatible code
5. Syntax Directed Definitions (SDDs) → e) Defines the translation of a
language using semantic rules

True or False:

1. In Three Address Code, each instruction can have at most three


operands and one operator.
Answer: True
2. Syntax Directed Definitions (SDDs) do not associate semantic actions
with grammar rules.
Answer: False
3. Type checking ensures that variables are used only after they are
declared in a program.
Answer: True
4. The evaluation order in Syntax Directed Definitions determines the
order in which semantic actions are applied.
Answer: True
5. Intermediate code is a high-level representation of a program that can
be directly executed on hardware.
Answer: False

One Mark Descriptive Questions:

1. What is the role of Syntax Directed Definitions (SDDs) in compiler


construction?
Answer: Syntax Directed Definitions (SDDs) provide a formal way to
associate semantic actions with the grammar rules of a language. They
guide the translation process by specifying how the syntax of a program
should be translated into intermediate or target code.
2. What is Three Address Code, and why is it used?
Answer: Three Address Code (TAC) is an intermediate representation
in which each instruction consists of at most one operator and three
operands (e.g., a variable, a constant, or another expression). It is used
in compilers as an abstraction of the source code to simplify
optimization and code generation.
3. Explain the concept of type checking in a compiler.
Answer: Type checking is the process in a compiler that ensures that
variables are used with compatible data types, preventing type errors
such as adding an integer to a string. It verifies that operations are
performed on correct data types and ensures the correctness of the
program.
4. What is the significance of the evaluation order in Syntax Directed
Definitions?
Answer: The evaluation order in Syntax Directed Definitions
determines the sequence in which semantic rules are applied when
translating the syntax tree. It ensures that the required values or actions
are computed in the correct order, which is crucial for generating
accurate intermediate code.
5. How does the translation of expressions work in a compiler?
Answer: Translation of expressions involves converting high-level
expressions into intermediate code or machine code. The compiler
breaks down expressions into a form that is easier to optimize and
translate into executable code, ensuring the expression's semantics are
preserved.

SET C
I. Fill in the Blanks:

1. A ____________ is an intermediate representation of a program, where


each statement typically involves a single operator and at most three
operands.
Answer: Three Address Code
2. ____________ is the process of ensuring that expressions in a program
adhere to correct type rules, such as ensuring an integer is not added to
a string.
Answer: Type Checking
3. A ____________ is a hierarchical tree structure representing the syntactic
structure of a program, used in intermediate code generation.
Answer: Syntax Tree
4. In a syntax-directed definition, semantic rules are associated with
____________ in the grammar.
Answer: Productions
5. The ____________ in a compiler specifies the order in which semantic rules
must be applied to a syntax tree to generate the final translation.
Answer: Evaluation Order

II. Multiple Choice Questions:

1. Which of the following is true about Syntax Directed Definitions (SDDs)?


a) SDDs are used to define the syntax of a language
b) SDDs associate semantic actions with grammar rules
c) SDDs define the lexical structure of a language
d) SDDs are used for generating machine code
Answer: b) SDDs associate semantic actions with grammar rules
2. What is the main advantage of using Three Address Code in a compiler?
a) It simplifies the parsing process
b) It is close to the machine language
c) It provides an intermediate representation that is easier to optimize
d) It directly executes on the hardware
Answer: c) It provides an intermediate representation that is easier to
optimize
3. In a Syntax Tree, the nodes typically represent ____________.
a) Tokens and expressions
b) Operators and operands
c) Both tokens and expressions
d) Only expressions
Answer: c) Both tokens and expressions
4. What is the primary function of type checking in a compiler?
a) To ensure variables are correctly declared before use
b) To ensure operations between compatible data types
c) To check for syntax errors in expressions
d) To generate intermediate code for expressions
Answer: b) To ensure operations between compatible data types
5. What does the evaluation order in Syntax Directed Definitions
determine?
a) The sequence in which tokens are parsed
b) The sequence of applying semantic rules for code generation
c) The order of generating machine code
d) The structure of the intermediate code
Answer: b) The sequence of applying semantic rules for code
generation

III. Match the Following:

Column A Column B
a) Intermediate representation for program
1. Syntax Tree
optimization
b) Ensures variables are used with compatible
2. Type Checking
data types
c) Represents the hierarchical structure of a
3. Three Address Code
program
d) Determines the order of applying semantic
4. Evaluation Order
actions
5. Syntax Directed Definitions e) Associates semantic actions with grammar
(SDDs) productions

Answer:

1. Syntax Tree → c) Represents the hierarchical structure of a program


2. Type Checking → b) Ensures variables are used with compatible data
types
3. Three Address Code → a) Intermediate representation for program
optimization
4. Evaluation Order → d) Determines the order of applying semantic
actions
5. Syntax Directed Definitions (SDDs) → e) Associates semantic actions
with grammar productions

IV.True or False:

1. Syntax Directed Definitions (SDDs) are used to define the lexical rules of
a language.
Answer: False
2. Three Address Code is a low-level intermediate representation that is
close to machine code.
Answer: True
3. Type Checking is only concerned with verifying that variables are
declared in the source program.
Answer: False
4. The Evaluation Order in Syntax Directed Definitions affects the
sequence of applying semantic rules during code generation.
Answer: True
5. Syntax Trees are used in a compiler to represent the tokenization of
source code.
Answer: False

V.One Mark Descriptive Questions:

1. What are Syntax Directed Definitions (SDDs) and how are they
used in compilers?
Answer: Syntax Directed Definitions (SDDs) are a set of rules used in
compilers to associate semantic actions with grammar rules. They guide
the translation of a program from its syntax to intermediate or machine
code, with each production in the grammar having a semantic rule that
specifies how to compute or generate code.
2. What is Three Address Code and why is it considered an important
intermediate representation in compilers?
Answer: Three Address Code (TAC) is an intermediate representation
used in compilers where each instruction typically involves one
operator and up to three operands. It simplifies optimization and
transformation to machine code, as it provides a simple, linear
representation of the program's logic, facilitating easier manipulation.
3. Explain the concept of Type Checking in a compiler.
Answer: Type checking is the process in a compiler where the types of
variables and expressions are verified to ensure that operations are
applied correctly according to the language's type system. This process
helps prevent type errors such as adding an integer to a string and
ensures that the program adheres to its type rules.
4. What is the role of Evaluation Order in Syntax Directed
Definitions?
Answer: The evaluation order in Syntax Directed Definitions specifies
the sequence in which semantic rules are applied to a syntax tree. This
order is crucial for correctly generating intermediate code or
performing computations, ensuring that dependent expressions or
variables are handled before they are used.
5. How does a Syntax Tree help in the compilation process?
Answer: A Syntax Tree represents the hierarchical structure of a
program, where each node corresponds to a construct in the source
code. It helps in understanding the program's syntactic structure and is
used in subsequent stages like type checking, optimization, and code
generation.
SET D

I.Fill in the Blanks:

1. A ____________ is an intermediate representation of a program, consisting


of operations involving at most three operands, and is commonly used
to simplify code generation.
Answer: Three Address Code
2. The process of ____________ ensures that all variables in a program are
used consistently according to their data types.
Answer: Type Checking
3. In the context of compilers, ____________ is the process of converting a
program's high-level code into a more intermediate form that is easier
to manipulate and optimize.
Answer: Translation of Expressions
4. The ____________ of a program is represented by a tree structure where
each node corresponds to a construct in the program, such as an
expression or statement.
Answer: Syntax Tree
5. A ____________ associates a set of semantic actions with grammar
productions to specify how the translation of a language should
proceed.
Answer: Syntax Directed Definition

II.Multiple Choice Questions:

1. What is the main purpose of a Syntax Tree in the compilation process?


a) To check the syntax of the program
b) To represent the hierarchical structure of the program
c) To generate machine code directly
d) To optimize the program
Answer: b) To represent the hierarchical structure of the program
2. In Three Address Code (TAC), each instruction involves at most:
a) One operator and two operands
b) Two operators and one operand
c) One operator and three operands
d) Three operators and one operand
Answer: c) One operator and three operands
3. Which of the following statements best describes type checking in a
compiler?
a) It checks if the program follows the correct grammar
b) It ensures that variables are declared before use
c) It ensures that operators are applied to operands of the correct type
d) It generates the intermediate code for the program
Answer: c) It ensures that operators are applied to operands of the
correct type
4. The evaluation order in Syntax Directed Definitions affects the order in
which:
a) Tokens are scanned
b) Grammar rules are applied
c) Semantic actions are executed
d) Syntax errors are detected
Answer: c) Semantic actions are executed
5. The primary goal of an intermediate language, such as Three Address
Code, is to:
a) Make the source code more readable
b) Provide a platform-independent representation of the program
c) Ensure type compatibility
d) Directly generate machine code
Answer: b) Provide a platform-independent representation of the
program

III.Match the Following:

Column A Column B
1. Syntax Tree a) Ensures type safety during translation
b) Intermediate representation used for
2. Type Checking
optimization
3. Three Address Code c) Hierarchical representation of a program
4. Syntax Directed d) Associates semantic actions with grammar
Definition (SDD) rules
5. Translation of e) Converts expressions to intermediate form for
Expressions easier translation

Answer:

1. Syntax Tree → c) Hierarchical representation of a program


2. Type Checking → a) Ensures type safety during translation
3. Three Address Code → b) Intermediate representation used for
optimization
4. Syntax Directed Definition (SDD) → d) Associates semantic actions with
grammar rules
5. Translation of Expressions → e) Converts expressions to intermediate
form for easier translation

IV.True or False:

1. Syntax Directed Definitions are used to directly generate machine code


from the source code.
Answer: False
2. Three Address Code is often used because it provides a simple
intermediate representation that is easy to optimize.
Answer: True
3. Type Checking only ensures that variables are declared before they are
used in the program.
Answer: False
4. Syntax Trees help in representing the program structure, which is later
used in code optimization and generation.
Answer: True
5. The evaluation order in Syntax Directed Definitions determines the
order in which tokens are recognized in the source code.
Answer: False

V.One Mark Descriptive Questions:

1. What is the role of Syntax Directed Definitions (SDDs) in the


compilation process?
Answer: Syntax Directed Definitions (SDDs) are used to associate
semantic rules with the grammar productions of a language. They define
how the program should be translated, specifying the actions to take
during translation, such as generating intermediate code or performing
type checks.
2. What is Three Address Code, and how does it help in the
translation of expressions?
Answer: Three Address Code (TAC) is an intermediate representation
used in compilers where each instruction contains at most one operator
and three operands. TAC simplifies the translation of expressions by
breaking down complex expressions into simpler operations, facilitating
optimization and easier code generation.
3. Explain the importance of type checking in a compiler.
Answer: Type checking ensures that the variables and expressions in a
program are used according to their data types. It prevents errors such
as performing arithmetic on incompatible data types (e.g., adding an
integer to a string) and ensures that operations are performed on valid
types.
4. How does the evaluation order in Syntax Directed Definitions affect
the translation process?
Answer: The evaluation order in Syntax Directed Definitions
determines the sequence in which semantic actions are applied to the
syntax tree. This order ensures that dependent computations (such as
evaluating operands before applying operators) are performed
correctly, leading to accurate intermediate code generation.
5. Describe how a Syntax Tree is used in a compiler's intermediate
code generation phase.
Answer: A Syntax Tree represents the hierarchical structure of a
program, with each node corresponding to a language construct. During
intermediate code generation, the compiler traverses the syntax tree to
generate intermediate code (e.g., Three Address Code), which simplifies
optimization and eventual machine code generation.

SET - E

I.Fill in the Blanks:


1. A ____________ represents the intermediate steps between the source code
and the final machine code, making it easier to optimize and generate
target code.
Answer: Intermediate Language
2. In Syntax Directed Definitions, the semantic rules associated with each
grammar production are typically used to perform ____________.
Answer: Translation
3. The process of ____________ ensures that operations are applied only to
operands of compatible types, and prevents type errors during
compilation.
Answer: Type Checking
4. A ____________ is a directed acyclic graph that represents the syntactic
structure of a program and is used during code generation.
Answer: Syntax Tree
5. ____________ is the intermediate representation that divides complex
operations into simpler, manageable expressions involving at most
three operands.
Answer: Three Address Code

II.Multiple Choice Questions:

1. What is the main purpose of Three Address Code in the compilation


process?
a) To directly execute the program
b) To simplify optimization and code generation
c) To handle syntax errors in the code
d) To generate machine-specific code
Answer: b) To simplify optimization and code generation
2. Which of the following is NOT part of Syntax Directed Definitions?
a) Grammar rules
b) Semantic actions
c) Parsing actions
d) Translation rules
Answer: c) Parsing actions
3. Type checking in a compiler ensures that:
a) Only syntax errors are detected
b) Variables are declared before use
c) Operations are performed on compatible data types
d) Intermediate code is generated correctly
Answer: c) Operations are performed on compatible data types
4. In the context of Syntax Directed Definitions, semantic actions are
applied:
a) Before parsing
b) After parsing
c) During parsing
d) During the final code generation phase
Answer: b) After parsing
5. Which of the following is an example of an intermediate language used
by compilers?
a) Assembly language
b) High-level language
c) Syntax Tree
d) Three Address Code
Answer: d) Three Address Code

III.Match the Following:

Column A Column B
1. Syntax Directed
a) Simplifies the intermediate code generation
Definition
b) Ensures compatibility of data types during
2. Three Address Code
compilation
3. Type Checking c) Defines translation actions for grammar rules
d) Represents the hierarchical structure of a
4. Syntax Tree
program
e) A platform-independent representation of the
5. Intermediate Language
program

Answer:

1. Syntax Directed Definition → c) Defines translation actions for grammar


rules
2. Three Address Code → a) Simplifies the intermediate code generation
3. Type Checking → b) Ensures compatibility of data types during
compilation
4. Syntax Tree → d) Represents the hierarchical structure of a program
5. Intermediate Language → e) A platform-independent representation of
the program

IV.True or False:

1. Syntax Directed Definitions are used to directly generate machine code.


Answer: False
2. Type checking is performed before parsing in a compiler.
Answer: False
3. Three Address Code simplifies the process of generating machine code
by providing a low-level intermediate representation.
Answer: True
4. The primary function of a Syntax Tree is to help with error detection
during parsing.
Answer: False
5. Semantic actions in Syntax Directed Definitions are typically associated
with grammar productions and are used for code generation.
Answer: True

V.One Mark Descriptive Questions:

1. What are Syntax Directed Definitions (SDDs), and why are they
important in the compilation process?
Answer: Syntax Directed Definitions (SDDs) are a set of rules that
associate semantic actions with grammar productions. These actions
guide the translation of source code into intermediate or machine code,
allowing the compiler to process the syntax of the language while
applying appropriate semantic rules for translation, type checking, and
optimization.
2. Explain the concept of Three Address Code and its significance in a
compiler's intermediate code generation.
Answer: Three Address Code (TAC) is an intermediate representation
used in compilers, where each instruction consists of a single operator
and at most three operands. TAC simplifies the translation of
expressions and allows for easier optimization, as it breaks down
complex expressions into smaller, more manageable instructions.
3. What is the role of type checking in a compiler, and how does it
prevent errors during the compilation process?
Answer: Type checking ensures that operations in a program are
performed on operands of compatible data types, preventing type
errors such as adding an integer to a string. It helps the compiler verify
that the program adheres to the specified type rules of the language,
reducing the likelihood of runtime errors.
4. How does a Syntax Tree assist in the generation of intermediate
code in a compiler?
Answer: A Syntax Tree represents the hierarchical structure of a
program, where each node corresponds to a language construct (e.g., an
expression or statement). It is used during the intermediate code
generation phase to break down the program into smaller, manageable
parts, making it easier to generate optimized code and detect semantic
errors.
5. What is an intermediate language, and why is it used in compilers?
Answer: An intermediate language is a representation of a program
that lies between the high-level source code and machine code. It is used
in compilers to provide a platform-independent form of the program
that can be easily optimized before being translated into machine-
specific code, ensuring efficiency and portability.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy