Compiler Construction Introduction
- 41Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the primary function of a compiler?
What is the primary function of a compiler?
41 questions
- 1
What is the primary function of a compiler?
- A
To store source code permanently in computer memory
- B
To translate source code into machine or object code
- C
To replace all programming languages with assembly language
- D
To execute source code directly without translation
Correct answer
BTo translate source code into machine or object code
Explanation
A compiler translates a program written in a high-level source language into machine-level or object code.
- A
- 2
What primarily distinguishes a high-level language from machine code?
- A
A high-level language is designed to be readable and understandable by humans
- B
A high-level language directly consists of binary machine instructions
- C
A high-level language eliminates the need for any translation process
- D
A high-level language can only be executed on one specific processor
Correct answer
AA high-level language is designed to be readable and understandable by humans
Explanation
High-level languages are designed for human readability and productivity, whereas machine code is directly understood by the computer.
- A
- 3
Why do high-level languages generally improve programmer productivity?
- A
They automatically eliminate the need for program design
- B
They hide many machine-level details from the programmer
- C
They allow programs to run without any hardware support
- D
They guarantee that every generated program is optimal
Correct answer
BThey hide many machine-level details from the programmer
Explanation
High-level languages let programmers focus on solving problems rather than managing many low-level machine details.
- A
- 4
Which sequence best represents the broad compilation process described in the lecture?
- A
Assembly code, source code, intermediate representation, executable code
- B
High-level source, intermediate representation, optimized representation, assembly code
- C
Executable code, source code, optimized representation, assembly code
- D
Intermediate representation, executable code, source code, machine code
Correct answer
BHigh-level source, intermediate representation, optimized representation, assembly code
Explanation
A typical compiler uses intermediate representations between the high-level source program and the generated assembly or machine code.
- A
- 5
Why does a compiler typically use intermediate representations?
- A
They prevent the compiler from checking the meaning of a program
- B
They ensure that source programs never need to be parsed
- C
They provide structured forms that support analysis, optimization, and code generation
- D
They replace the need for target-machine instructions
Correct answer
CThey provide structured forms that support analysis, optimization, and code generation
Explanation
Intermediate representations provide useful structured forms between source code and target code, allowing analysis, transformations, and later code generation.
- A
- 6
Which statement best describes compiler optimization as presented in the lecture?
- A
It links object files together to form an executable program
- B
It guarantees that the compiler always produces the mathematically fastest possible code
- C
It applies transformations intended to improve code efficiency, but does not guarantee globally optimal code
- D
It translates source code into tokens without changing its structure
Correct answer
CIt applies transformations intended to improve code efficiency, but does not guarantee globally optimal code
Explanation
Compiler optimization applies enhancements and transformations to improve code, but compilers generally do not guarantee optimal code in every case.
- A
- 7
What is the main instructional emphasis of the compiler course described in the lecture?
- A
Proving theoretical results about computational complexity
- B
Understanding the basic translation from high-level programs to machine or assembly code
- C
Designing hardware circuits for modern processors
- D
Studying only advanced object-oriented language features
Correct answer
BUnderstanding the basic translation from high-level programs to machine or assembly code
Explanation
The course focuses primarily on basic translation from high-level language constructs to machine-level code, while also covering some optimization.
- A
- 8
A program contains the assignment z=x+y, where x, y, and z are memory variables. Which sequence best describes a typical low-level implementation?
- A
Store x and y into memory, subtract them, and discard the result
- B
Load x and y into registers, add them, and store the result into z
- C
Load z into a register, multiply it by x, and store it into y
- D
Add the variable names as text and place the resulting string into z
Correct answer
BLoad x and y into registers, add them, and store the result into z
Explanation
A typical implementation loads the operands into registers, performs the addition, and stores the resulting register value back into the memory location for z.
- A
- 9
In actual assembly code, what commonly replaces a source-level local variable name?
- A
The complete high-level expression containing the variable
- B
A natural-language description of the variable's purpose
- C
A memory address or an offset relative to the current activation record
- D
The name of the compiler that generated the program
Correct answer
CA memory address or an offset relative to the current activation record
Explanation
Assembly does not generally use source-level variable names; local variables are represented by memory addresses or offsets relative to an activation record pointer.
- A
- 10
What is the role of a linker when a program contains multiple source files?
- A
It checks whether operators and operands form valid expressions
- B
It divides each source file into lexical tokens
- C
It assigns every source variable directly to a CPU register
- D
It combines object files and resolves references to produce an executable
Correct answer
DIt combines object files and resolves references to produce an executable
Explanation
Each source file may be compiled into an object file, and the linker combines those object files, resolves references, and produces an executable.
- A
- 11
Why may an object file not be executable by itself?
- A
It is always written in a language intended only for humans
- B
It contains only comments and no translated instructions
- C
It may still require code from other object files or libraries
- D
It cannot contain any memory addresses or machine instructions
Correct answer
CIt may still require code from other object files or libraries
Explanation
An object file may contain unresolved references to other source files or library functions, so linking is needed before execution.
- A
- 12
What does linking with a library primarily accomplish?
- A
It transforms every loop into a conditional statement
- B
It converts individual characters into lexical tokens
- C
It combines the program with implementations of referenced library functions
- D
It determines whether a statement has a valid grammatical structure
Correct answer
CIt combines the program with implementations of referenced library functions
Explanation
The linker combines the program's object code with required library code and resolves references to those functions.
- A
- 13
Which compiler component is primarily associated with parsing source code and constructing an abstract syntax tree?
- A
The linker
- B
The instruction scheduler
- C
The register allocator
- D
The front end
Correct answer
DThe front end
Explanation
The front end analyzes the high-level program, including scanning and parsing, and constructs an intermediate representation such as an abstract syntax tree.
- A
- 14
Which compiler component is primarily responsible for generating target instructions from an intermediate representation?
- A
The back end
- B
The library loader
- C
The lexical scanner
- D
The source-code editor
Correct answer
AThe back end
Explanation
The back end performs tasks such as instruction selection, instruction scheduling, register allocation, and assembly generation.
- A
- 15
Which task belongs most directly to the compiler back end?
- A
Dividing source characters into tokens
- B
Identifying whether two data types are compatible
- C
Determining whether a source sentence is grammatically valid
- D
Choosing target instructions for an intermediate representation
Correct answer
DChoosing target instructions for an intermediate representation
Explanation
Instruction selection is a back-end task that chooses suitable target-machine instructions during code generation.
- A
- 16
What is the purpose of instruction scheduling?
- A
To determine an effective ordering for the generated instructions
- B
To determine whether a variable name is legally spelled
- C
To combine object files into a single executable
- D
To divide a program into words and punctuation symbols
Correct answer
ATo determine an effective ordering for the generated instructions
Explanation
Instruction scheduling finds an appropriate order for instructions, potentially improving how efficiently the target processor executes them.
- A
- 17
What is the purpose of register allocation?
- A
To determine which characters form an identifier
- B
To decide which variables or values should be stored in processor registers
- C
To decide which source files should be linked together
- D
To determine whether a sentence has a meaningful interpretation
Correct answer
BTo decide which variables or values should be stored in processor registers
Explanation
Register allocation assigns selected variables or intermediate values to the limited registers available on the processor.
- A
- 18
Why is register allocation necessary in many programs?
- A
Registers can hold only punctuation symbols from the source code
- B
Source programs contain no variables until after linking
- C
Programs commonly contain more variables than the processor has registers
- D
Processors cannot store any values in memory
Correct answer
CPrograms commonly contain more variables than the processor has registers
Explanation
Because the number of program variables can exceed the number of available registers, the compiler must decide which values receive registers.
- A
- 19
What is an abstract syntax tree?
- A
A hierarchical tree representation of a program's syntactic structure
- B
A collection of library functions used during execution
- C
A binary encoding of every source-code character
- D
A list of machine addresses produced by the linker
Correct answer
AA hierarchical tree representation of a program's syntactic structure
Explanation
An abstract syntax tree is a tree-shaped intermediate representation that captures the hierarchical structure of expressions and statements.
- A
- 20
For the expression x=aimesb+cimesd, which operation should be represented at the root of the expression tree?
- A
The multiplication of a and b
- B
The addition combining the two multiplication results
- C
The assignment of a directly to x
- D
The multiplication of c and d
Correct answer
BThe addition combining the two multiplication results
Explanation
Multiplication has higher precedence than addition, so the two products are formed first and then combined by the addition at the root of the expression tree.
- A
- 21
How can an optimizer use an abstract syntax tree or another intermediate representation?
- A
It can replace the representation with library documentation
- B
It can prevent the back end from generating any instructions
- C
It can use the representation only to display source code to the programmer
- D
It can transform the representation to improve code and remove redundancies
Correct answer
DIt can transform the representation to improve code and remove redundancies
Explanation
The optimizer analyzes and transforms intermediate representations to improve code quality and eliminate redundancies before code generation.
- A
- 22
Why do compilers commonly use multiple intermediate representations?
- A
Multiple representations eliminate the need for machine-specific code generation
- B
Multiple representations ensure that no source program can be executed
- C
Each representation corresponds to a separate programming language used by the programmer
- D
Different representations support different analyses, transformations, and code-generation tasks
Correct answer
DDifferent representations support different analyses, transformations, and code-generation tasks
Explanation
Compilers may use several intermediate forms because different stages benefit from different structures and levels of detail.
- A
- 23
Which sequence correctly describes the expression-tree structure for x=aimesb+cimesd?
- A
Assign a to x, multiply b and c, then add d
- B
Multiply a and b, multiply c and d, add the products, then assign to x
- C
Add a and b, multiply the result by c, then assign d to x
- D
Multiply all four operands first, then perform the assignment to x
Correct answer
BMultiply a and b, multiply c and d, add the products, then assign to x
Explanation
The tree reflects the two multiplication subexpressions, their addition, and the final assignment to x.
- A
- 24
What does scanning do during compilation?
- A
It assigns all variables to hardware registers
- B
It divides the source character sequence into tokens
- C
It determines whether the program's operations are meaningful
- D
It constructs the final executable from object files
Correct answer
BIt divides the source character sequence into tokens
Explanation
Scanning, or lexical analysis, breaks the input character sequence into tokens such as identifiers, operators, and keywords.
- A
- 25
Suppose the identifier
xy23appears in a program. How would scanning normally treat it?- A
As an assignment statement
- B
As four separate numeric tokens
- C
As one identifier token
- D
As a complete expression tree
Correct answer
CAs one identifier token
Explanation
The characters forming
xy23together constitute one variable-name token when they follow the language's identifier rules. - A
- 26
What does parsing do after scanning has produced tokens?
- A
It removes all operators so only variable names remain
- B
It converts every token directly into a processor register
- C
It uses the tokens to construct grammatical structures such as statements and expressions
- D
It links the tokens with external library implementations
Correct answer
CIt uses the tokens to construct grammatical structures such as statements and expressions
Explanation
Parsing organizes tokens according to the language grammar and constructs larger structures such as expressions, statements, and programs.
- A
- 27
Which comparison between scanning and parsing is correct?
- A
Scanning checks program meaning, whereas parsing links object files
- B
Scanning generates machine instructions, whereas parsing allocates registers
- C
Scanning operates at the token or word level, whereas parsing constructs grammatical sentences or structures
- D
Scanning performs optimization, whereas parsing loads library functions
Correct answer
CScanning operates at the token or word level, whereas parsing constructs grammatical sentences or structures
Explanation
Scanning forms tokens from characters, while parsing uses those tokens to construct syntactic structures according to the grammar.
- A
- 28
What is the main concern of semantic analysis?
- A
Whether object files contain enough machine instructions
- B
Whether source characters can be grouped into identifier tokens
- C
Whether the target processor has a graphical display
- D
Whether syntactically valid program structures have valid meanings
Correct answer
DWhether syntactically valid program structures have valid meanings
Explanation
Semantic analysis checks meaning-related constraints after parsing, such as type compatibility and valid use of constructs.
- A
- 29
A program adds two incompatible objects and assigns the result to an integer variable. What kind of error is this most likely to be?
- A
A linking error caused by too many registers
- B
A semantic error caused by a type mismatch
- C
A lexical error caused by an invalid character
- D
A scheduling error caused by an incorrect instruction order
Correct answer
BA semantic error caused by a type mismatch
Explanation
The statement may be syntactically well formed, but the incompatible types make its meaning invalid, producing a semantic type error.
- A
- 30
Which example best illustrates a statement that is syntactically valid but semantically meaningless?
- A
A program missing a required closing delimiter
- B
A grammatically structured operation that adds incompatible types
- C
A variable name containing an illegal character
- D
A source file containing an unrecognized keyword
Correct answer
BA grammatically structured operation that adds incompatible types
Explanation
A type-mismatched operation can conform to the grammatical structure of a language while still having no valid meaning.
- A
- 31
Which set contains the essential programming-language constructs emphasized in the lecture?
- A
Expressions, assignments, conditionals, and loops
- B
Comments, whitespace, libraries, and executable files
- C
Registers, cache lines, buses, and processor cores
- D
Objects, inheritance, exceptions, and packages
Correct answer
AExpressions, assignments, conditionals, and loops
Explanation
The basic language targeted by the course includes expressions, assignments, conditionals such as if/else, and loops.
- A
- 32
Why are conditionals considered an essential programming-language construct?
- A
They convert every variable into a processor register
- B
They combine object files into an executable
- C
They allow a program to choose behavior based on a condition
- D
They divide source characters into lexical tokens
Correct answer
CThey allow a program to choose behavior based on a condition
Explanation
Conditionals such as if/else allow programs to select different actions depending on whether a condition holds.
- A
- 33
Why are loops considered essential for meaningful programs?
- A
They ensure that every instruction executes exactly once
- B
They allow a program to repeat operations according to a control condition
- C
They translate source code directly into library functions
- D
They replace the need for expressions and assignments
Correct answer
BThey allow a program to repeat operations according to a control condition
Explanation
Loops provide repetition, allowing a program to perform operations multiple times under the control of a condition or iteration rule.
- A
- 34
What is the basic compiler-building task described for the course project?
- A
Build a compiler that translates a language with basic constructs into assembly
- B
Build a linker that combines arbitrary operating systems
- C
Build a processor that executes source code without compilation
- D
Build a parser for advanced object-oriented features only
Correct answer
ABuild a compiler that translates a language with basic constructs into assembly
Explanation
The required project is a basic compiler for a language containing expressions, assignments, conditionals, and loops, translating them into assembly code.
- A
- 35
Which language features were explicitly outside the basic compiler requirement?
- A
Expressions and assignments
- B
Advanced features such as objects and inheritance
- C
Conditionals and loops
- D
Basic arithmetic operations
Correct answer
BAdvanced features such as objects and inheritance
Explanation
The basic requirement excludes advanced features such as objects and inheritance, focusing instead on essential constructs.
- A
- 36
Which theoretical concepts from computation are directly applied in compiler construction?
- A
Only computer graphics and three-dimensional geometry
- B
Finite automata, regular expressions, grammars, and pushdown automata
- C
Only numerical integration and differential equations
- D
Only database normalization and transaction processing
Correct answer
BFinite automata, regular expressions, grammars, and pushdown automata
Explanation
Compiler construction applies concepts including finite automata, regular expressions, grammars, and pushdown automata.
- A
- 37
Which compiler activity most directly uses regular expressions and finite automata?
- A
Choosing the final order of machine instructions
- B
Assigning variables to physical registers
- C
Recognizing and forming lexical tokens during scanning
- D
Combining object files into an executable
Correct answer
CRecognizing and forming lexical tokens during scanning
Explanation
Regular expressions and finite automata are commonly used to describe and recognize token patterns during lexical analysis.
- A
- 38
Which compiler activity most directly uses grammars and pushdown-style structure?
- A
Scheduling instructions for a processor pipeline
- B
Parsing tokens into nested syntactic structures
- C
Linking a program with library functions
- D
Loading operands from memory into registers
Correct answer
BParsing tokens into nested syntactic structures
Explanation
Grammars describe valid syntactic structures, and parsing uses them to organize tokens into nested expressions and statements.
- A
- 39
What is the relationship between theory and compiler construction described in the lecture?
- A
Compiler construction uses theory only to study hardware circuits
- B
Compiler construction avoids formal models such as grammars and automata
- C
Compiler construction is an application of theoretical concepts from computation
- D
Compiler construction makes all theoretical concepts irrelevant
Correct answer
CCompiler construction is an application of theoretical concepts from computation
Explanation
The lecture presents compilers as practical systems built using theoretical tools such as automata, regular expressions, and grammars.
- A
- 40
Why does the lecture caution against calling theoretical work useless?
- A
Every theoretical result immediately produces an executable program
- B
Theoretical ideas may later enable practical applications that are not yet known
- C
Theoretical work always produces optimal compiler code
- D
Theory eliminates the need to understand implementation details
Correct answer
BTheoretical ideas may later enable practical applications that are not yet known
Explanation
The lecture emphasizes that theoretical knowledge can open new possibilities and may lead to important applications much later.
- A
- 41
Which statement best contrasts the role of theory in this compiler course with its role in a theory course?
- A
The compiler course replaces grammars and automata with hardware design
- B
The compiler course studies only the pumping lemma and no implementation
- C
The compiler course applies theoretical concepts to build a real system rather than focusing primarily on proving theory
- D
The compiler course completely rejects theoretical concepts used in computation
Correct answer
CThe compiler course applies theoretical concepts to build a real system rather than focusing primarily on proving theory
Explanation
The course applies concepts from computation theory to compiler construction rather than concentrating on theoretical proofs such as the pumping lemma.
- A

Ask anything
We've read this whole study set, so questions about it get the best answers.