Compiler Components Overview
- 54Unfamiliar
- 0Learning
- 0Familiar
- 0Mastered
What is the primary goal of the compiler overview?
What is the primary goal of the compiler overview?
54 questions
- 1
What is the primary goal of the compiler overview?
- A
To construct a complete compiler before studying any individual phase
- B
To compare programming languages without examining compiler structure
- C
To implement every compiler algorithm in complete technical detail
- D
To understand the major compiler components and what each component does
Correct answer
DTo understand the major compiler components and what each component does
Explanation
The overview is intended to establish the big picture of a compiler, including its main parts and the function of each part, while postponing detailed implementation methods.
- A
- 2
In computer science, what is usually the answer to the question of how a system performs a task?
- A
An algorithm that specifies the steps used by the system
- B
A grammar that lists all valid statements in the system
- C
A target machine that executes the system's output
- D
A symbol table that stores the system's identifiers
Correct answer
AAn algorithm that specifies the steps used by the system
Explanation
The lecture states that, in computer science, the answer to how a system performs something is usually an algorithm.
- A
- 3
Which three parts form the conceptual organization of an optimizing compiler?
- A
The lexer, the debugger, and the target processor
- B
The scanner, the parser, and the symbol table
- C
The front end, the optimizer, and the backend
- D
The grammar, the automaton, and the code generator
Correct answer
CThe front end, the optimizer, and the backend
Explanation
The conceptual view divides an optimizing compiler into a front end, an optimization phase, and a backend.
- A
- 4
What is the primary role of the compiler backend?
- A
To verify that identifiers follow lexical naming rules
- B
To divide the source program into tokens and lexemes
- C
To store declarations and their types in a symbol table
- D
To generate target-machine code from an intermediate representation
Correct answer
DTo generate target-machine code from an intermediate representation
Explanation
The backend is the code-generation portion of the compiler and is responsible for producing machine-specific code.
- A
- 5
Why should the three-part organization of a compiler be treated as conceptual rather than universal?
- A
Different compilers can combine components or organize them differently
- B
Compiler components are determined only by the programming language syntax
- C
The front end and backend are always omitted from production compilers
- D
Every compiler must contain exactly three separate executables
Correct answer
ADifferent compilers can combine components or organize them differently
Explanation
The three-part model is a conceptual view. Actual compilers may have different designs, including different ways of combining or separating components.
- A
- 6
What does an intermediate representation primarily provide during compilation?
- A
A representation between source-level code and target-machine code
- B
A list of only the source program's comments and formatting
- C
A replacement for all lexical and syntactic analysis
- D
A final representation that can execute on every processor directly
Correct answer
AA representation between source-level code and target-machine code
Explanation
Intermediate representations provide one or more stages between the high-level source program and the final machine or assembly code.
- A
- 7
Which statement about intermediate representations is correct?
- A
An abstract syntax tree is the only possible intermediate representation
- B
Intermediate representations are used only after final machine code is generated
- C
A compiler may use multiple intermediate representations during code generation
- D
Every compiler must use only one abstract syntax tree as its representation
Correct answer
CA compiler may use multiple intermediate representations during code generation
Explanation
A compiler may use several intermediate representations, and an abstract syntax tree is only one possible form.
- A
- 8
What information does an abstract syntax tree emphasize for an assignment such as X equals A plus B?
- A
The hierarchical operations of addition and assignment
- B
The exact spacing and punctuation used in the source file
- C
The processor-specific instruction encoding for the expression
- D
The physical memory addresses of every variable
Correct answer
AThe hierarchical operations of addition and assignment
Explanation
The abstract syntax tree represents the structure of the computation, such as adding A and B and assigning the result to X.
- A
- 9
What is the main purpose of scanning or lexing?
- A
To divide the program's character stream into tokens or lexemes
- B
To verify whether operations have compatible operand types
- C
To select physical machine instructions for the program
- D
To determine whether a sequence of tokens forms a valid statement
Correct answer
ATo divide the program's character stream into tokens or lexemes
Explanation
Scanning is the low-level analysis that views the program as a character string and divides it into tokens or lexemes.
- A
- 10
How can a compiler view a source program during lexical analysis?
- A
As a symbol table containing only declarations
- B
As a collection of physical registers awaiting allocation
- C
As one large string of characters to be divided into tokens
- D
As a completed machine-code sequence to be scheduled
Correct answer
CAs one large string of characters to be divided into tokens
Explanation
For compilation purposes, the entire program can be viewed as one large string, regardless of its size, and the scanner analyzes that string.
- A
- 11
Why can a scanner recognize the boundary between a variable name and an equals sign?
- A
The semantic analyzer determines that equals must end every identifier
- B
The parser first converts every equals sign into a memory address
- C
The lexical rules allow certain characters in identifiers but exclude the equals sign
- D
The optimizer removes all nonalphabetic characters before scanning
Correct answer
CThe lexical rules allow certain characters in identifiers but exclude the equals sign
Explanation
Lexical rules define what identifiers may contain. Since the equals sign is not allowed within an identifier, it marks the end of that token and begins another token.
- A
- 12
What is a lexeme in the context of compilation?
- A
A sequence of source characters recognized as one token
- B
A grammar rule describing a complete programming statement
- C
A table entry containing a variable's type and scope
- D
A tree node representing a machine instruction
Correct answer
AA sequence of source characters recognized as one token
Explanation
A lexeme is a recognized sequence of characters, such as an identifier or operator, that functions as a token.
- A
- 13
Which sequence correctly describes the tokens in X12 equals A3 plus B45?
- A
Assignment operator, identifier, plus operator, identifier, identifier
- B
Identifier, assignment operator, identifier, plus operator, identifier
- C
Variable declaration, assignment statement, expression, operator, number
- D
Identifier, arithmetic expression, identifier, statement, operator
Correct answer
BIdentifier, assignment operator, identifier, plus operator, identifier
Explanation
The scanner identifies X12, the equals sign, A3, the plus sign, and B45 as separate lexical tokens.
- A
- 14
What formal tool is used to define the rules for what tokens may consist of?
- A
Regular expressions
- B
Instruction schedules
- C
Register-allocation graphs
- D
Activation records
Correct answer
ARegular expressions
Explanation
The lecture explains that lexical rules, such as the structure of variable names, are defined using regular expressions.
- A
- 15
What formal computational model is associated with implementing lexical recognition?
- A
Finite-state automata
- B
Symbol tables
- C
Abstract syntax trees
- D
Target-machine schedulers
Correct answer
AFinite-state automata
Explanation
The implementation of lexical recognition using regular-expression-defined rules requires finite-state automata.
- A
- 16
Why might a language avoid requiring spaces between tokens?
- A
Avoiding spaces prevents the scanner from using regular expressions
- B
Avoiding spaces allows variable names to contain every operator
- C
Avoiding required spaces makes programming easier even though scanning becomes harder
- D
Avoiding spaces guarantees that every token has the same length
Correct answer
CAvoiding required spaces makes programming easier even though scanning becomes harder
Explanation
Requiring spaces can simplify the compiler's work, but it makes writing programs more burdensome. Many languages therefore do not require spaces in every token boundary.
- A
- 17
What is the main purpose of parsing, or syntactic analysis?
- A
To decide whether a statement's operations are meaningful for its data types
- B
To assign memory offsets to all variables before declarations are processed
- C
To determine whether tokens can form valid statements according to the language grammar
- D
To translate every source character directly into a physical machine instruction
Correct answer
CTo determine whether tokens can form valid statements according to the language grammar
Explanation
Parsing takes the tokens produced by scanning and uses grammar rules to determine whether they form valid syntactic structures.
- A
- 18
What is the programming-language counterpart of a sentence in a natural language?
- A
A statement
- B
A token
- C
A character
- D
A register
Correct answer
AA statement
Explanation
The lecture compares tokens to words and programming-language statements to sentences in natural language.
- A
- 19
What is the programming-language counterpart of a word in a natural language?
- A
A token or lexeme
- B
A complete statement
- C
An activation record
- D
A semantic type
Correct answer
AA token or lexeme
Explanation
Tokens or lexemes are analogous to words because they are the basic units that the parser combines into statements.
- A
- 20
What formal structure defines the legal syntactic statements of a programming language?
- A
A finite memory frame
- B
A grammar
- C
A register file
- D
A cache
Correct answer
BA grammar
Explanation
A grammar consists of rules that define which combinations of tokens form legal statements in a programming language.
- A
- 21
Why is an expression such as an identifier followed by an assignment and another expression potentially valid?
- A
A grammar may contain a rule allowing an expression to be assigned to an identifier
- B
Semantic analysis always converts invalid expressions into valid assignments
- C
The scanner treats all identifiers and operators as one combined token
- D
Every sequence containing an equals sign is automatically valid syntax
Correct answer
AA grammar may contain a rule allowing an expression to be assigned to an identifier
Explanation
Such an assignment is valid when the language grammar includes a rule allowing an identifier on the left to receive an expression on the right.
- A
- 22
What makes an expression such as A plus B times multiply syntactically invalid?
- A
Its variables necessarily have incompatible data types
- B
It does not match any valid production rule in the language grammar
- C
Its operators are always converted directly into memory addresses
- D
Its tokens cannot be recognized by any regular expression
Correct answer
BIt does not match any valid production rule in the language grammar
Explanation
The expression is syntactically invalid because the token sequence does not correspond to any rule in the grammar of the language.
- A
- 23
What does it mean if a program passes scanning but has not yet passed parsing?
- A
Its syntax is valid, but its operations may still have incompatible types
- B
Its optimization is complete, but code generation has not yet started
- C
Its individual tokens are valid, but their arrangement may still violate syntax rules
- D
Its machine instructions have been selected, but registers remain unallocated
Correct answer
CIts individual tokens are valid, but their arrangement may still violate syntax rules
Explanation
Passing scanning means that the lexical parts of the program are valid. Parsing must still determine whether those tokens form legal statements.
- A
- 24
What does semantic analysis determine after parsing?
- A
Whether source characters can be grouped into identifiers and operators
- B
Whether virtual registers can be mapped to physical registers
- C
Whether tokens can be arranged into grammatical statements
- D
Whether syntactically valid statements are meaningful under the language's semantic rules
Correct answer
DWhether syntactically valid statements are meaningful under the language's semantic rules
Explanation
Semantic analysis checks meaning-related conditions, such as whether operand and assignment types are compatible.
- A
- 25
Which example best illustrates a semantic error rather than a syntax error?
- A
Using an equals sign to separate an identifier from an expression
- B
Ending an otherwise valid assignment statement with a semicolon
- C
Adding an object representing a student to an object representing a car without a defined meaning
- D
Writing an identifier followed by a plus operator and another identifier
Correct answer
CAdding an object representing a student to an object representing a car without a defined meaning
Explanation
The statement may be grammatically well formed but meaningless if the language does not define addition between those object types.
- A
- 26
Why can a syntactically correct statement still prevent code generation?
- A
The statement may be semantically meaningless, so the compiler cannot determine valid operations
- B
The optimizer may have already eliminated every intermediate representation
- C
The scanner may not have divided its characters into tokens
- D
The parser may not have received any grammar rules
Correct answer
AThe statement may be semantically meaningless, so the compiler cannot determine valid operations
Explanation
Code generation requires meaningful operations. If types or other semantic properties are inconsistent, the compiler does not know what code should be generated.
- A
- 27
Why is semantic analysis generally more global than syntax checking?
- A
It depends exclusively on the target processor's instruction set
- B
It replaces the entire program with one token before checking its meaning
- C
It examines only the immediately adjacent characters surrounding each operator
- D
It may need to connect a use of a variable with a declaration located much earlier in the program
Correct answer
DIt may need to connect a use of a variable with a declaration located much earlier in the program
Explanation
Semantic analysis may need information from declarations that are separated from a statement by substantial amounts of code.
- A
- 28
What information can a symbol table store for a declared variable?
- A
Only the final machine instruction that uses the variable
- B
The complete execution history of every value assigned to it
- C
Its name, type, scope, and other information needed during compilation
- D
Only the number of characters in its source-level name
Correct answer
CIts name, type, scope, and other information needed during compilation
Explanation
The symbol table stores information about declared variables, including their types and scopes, so later uses can be checked.
- A
- 29
How does semantic analysis use the symbol table when checking a variable use?
- A
It schedules the variable's machine instruction before parsing
- B
It removes the variable from the abstract syntax tree
- C
It converts the variable name directly into a grammar production
- D
It looks up the variable to retrieve properties such as its type
Correct answer
DIt looks up the variable to retrieve properties such as its type
Explanation
When a variable is used, the compiler consults the symbol table to find its declared properties and determine whether the use is meaningful.
- A
- 30
Under which condition does the front end produce an abstract syntax tree for further compilation?
- A
The program passes scanning but fails both parsing and semantic analysis
- B
The program passes scanning, parsing, and semantic analysis
- C
The program contains at least one identifier and one operator
- D
The program has been optimized for a specific target processor
Correct answer
BThe program passes scanning, parsing, and semantic analysis
Explanation
The front end provides the intermediate representation only after the program successfully passes all three phases: scanning, parsing, and semantic analysis.
- A
- 31
What is the intended machine dependence of the optimization phase?
- A
Its transformations should ideally be independent of the target machine
- B
Its transformations are performed only after physical registers are assigned
- C
Its transformations are defined entirely by the source program's identifier names
- D
Its transformations must always use the exact instructions of the target machine
Correct answer
AIts transformations should ideally be independent of the target machine
Explanation
Optimization is ideally machine independent, although real compilers may include some machine-dependent parameters in this phase.
- A
- 32
What is the purpose of common subexpression elimination?
- A
To remove every expression that contains more than one operator
- B
To replace all variables with their original source-level declarations
- C
To ensure that each processor receives a different source program
- D
To compute a repeated subexpression once and reuse its result
Correct answer
DTo compute a repeated subexpression once and reuse its result
Explanation
Common subexpression elimination detects a repeated computation, such as A plus B, computes it once, and reuses the temporary result.
- A
- 33
How could common subexpression elimination transform X equals A plus B plus C and Y equals D divided by A plus B?
- A
Compute T equals X plus Y, then replace A, B, C, and D with T
- B
Replace both statements with X equals C and Y equals D
- C
Compute T equals A plus B, then use T in both remaining expressions
- D
Compute A plus B separately again before assigning each result
Correct answer
CCompute T equals A plus B, then use T in both remaining expressions
Explanation
The repeated subexpression A plus B can be computed once as T, after which X can use T plus C and Y can use D divided by T.
- A
- 34
Why is a compiler optimization not guaranteed to improve performance?
- A
An optimization may improve one cost while worsening another
- B
Optimizations are applied only to syntactically invalid programs
- C
Compiler transformations cannot affect the generated machine code
- D
Every optimization necessarily increases the number of source-level variables
Correct answer
AAn optimization may improve one cost while worsening another
Explanation
A transformation can reduce recomputation but increase memory traffic or register pressure, so its overall performance effect is not guaranteed to be positive.
- A
- 35
When is common subexpression elimination most likely to improve performance?
- A
When the transformation forces the temporary to occupy many registers
- B
When the repeated expression is recomputed more cheaply than it is retrieved
- C
When the reused temporary can remain in a fast register
- D
When the reused temporary must be loaded repeatedly from main memory
Correct answer
CWhen the reused temporary can remain in a fast register
Explanation
The transformation is beneficial when the temporary remains in a register, avoiding the cost of repeatedly recomputing the expression or loading it from slower storage.
- A
- 36
How can common subexpression elimination harm performance on a machine with limited registers?
- A
Keeping a temporary alive guarantees that every load becomes a cache miss
- B
Keeping a temporary alive may prevent other variables from staying in registers
- C
The transformation makes all arithmetic operations machine independent
- D
The transformation eliminates the symbol table before semantic analysis
Correct answer
BKeeping a temporary alive may prevent other variables from staying in registers
Explanation
A temporary that remains live for a long period can reserve a scarce register, forcing other variables into memory and potentially slowing their use.
- A
- 37
Why might loading a temporary from memory be slower than recomputing a simple addition?
- A
Memory access, especially from main memory, can take far longer than a simple arithmetic operation
- B
Main memory operates at the same speed as the processor's arithmetic units
- C
Temporary values cannot be stored in registers after optimization
- D
Addition operations always require more instructions than memory accesses
Correct answer
AMemory access, especially from main memory, can take far longer than a simple arithmetic operation
Explanation
A simple addition may take very little time, whereas loading a value from memory, particularly when it is not cached, can be orders of magnitude slower.
- A
- 38
Why can an apparently machine-independent optimization still depend on machine-specific parameters?
- A
Its benefit may depend on cache behavior and the number of available registers
- B
Its result is determined entirely before the target machine is selected
- C
Its implementation requires replacing every grammar with a finite automaton
- D
Its correctness depends only on the spelling of source-level identifiers
Correct answer
AIts benefit may depend on cache behavior and the number of available registers
Explanation
Whether an optimization helps can depend on machine resources such as cache size, cache speed, and register availability.
- A
- 39
Which backend activity is responsible for choosing an efficient sequence of target instructions?
- A
Lexical analysis
- B
Instruction selection
- C
Semantic analysis
- D
Symbol-table construction
Correct answer
BInstruction selection
Explanation
Instruction selection chooses actual instructions or instruction sequences that implement the intermediate representation efficiently.
- A
- 40
What is the purpose of register allocation?
- A
To determine whether an expression follows the language grammar
- B
To map virtual registers in an intermediate form to physical machine registers
- C
To identify repeated subexpressions before an abstract syntax tree is built
- D
To divide source characters into identifiers and operators
Correct answer
BTo map virtual registers in an intermediate form to physical machine registers
Explanation
Before register allocation, compilers may use virtual registers. Register allocation maps them to the limited physical registers of the target machine.
- A
- 41
Why can a compiler temporarily assume that it has infinitely many registers?
- A
Real processors actually provide unlimited registers during compilation
- B
Infinite registers are required for lexical analysis to recognize identifiers
- C
This simplifies earlier code-generation steps before register allocation
- D
The assumption eliminates the need to generate machine-specific code
Correct answer
CThis simplifies earlier code-generation steps before register allocation
Explanation
The infinite-register assumption is a convenient abstraction used before register allocation maps virtual registers onto real, limited registers.
- A
- 42
What is the main distinction between virtual and physical registers?
- A
Virtual registers are abstract compiler locations, whereas physical registers exist in the target processor
- B
Virtual registers store source comments, whereas physical registers store grammar rules
- C
Virtual registers are used by scanners, whereas physical registers are used by parsers
- D
Virtual registers exist in hardware, whereas physical registers exist only in the abstract syntax tree
Correct answer
AVirtual registers are abstract compiler locations, whereas physical registers exist in the target processor
Explanation
Virtual registers are temporary abstractions used by the compiler. Physical registers are the actual limited storage locations provided by the target machine.
- A
- 43
What does instruction selection consider when a target machine supports complex memory operands?
- A
Whether several simple instructions can be replaced by a more efficient complex instruction
- B
Whether all virtual registers can be removed before optimization
- C
Whether a complete program can be represented as one lexical token
- D
Whether a syntactically valid assignment has compatible variable types
Correct answer
AWhether several simple instructions can be replaced by a more efficient complex instruction
Explanation
A machine may support an instruction that operates directly on memory operands, replacing a sequence such as load, load, and multiply. Choosing between these alternatives is instruction selection.
- A
- 44
Why might a compiler first generate abstract or generic assembly?
- A
Generic instructions provide a common low-level form before target-specific instruction selection
- B
Generic assembly contains source-level variable names that real machines require
- C
Generic assembly is the final code that executes identically on every processor
- D
Generic assembly eliminates the need for register allocation and scheduling
Correct answer
AGeneric instructions provide a common low-level form before target-specific instruction selection
Explanation
A compiler may first use basic, broadly available operations in an abstract assembly form, then select actual instructions for the target machine.
- A
- 45
Why is the backend especially complicated for a compiler that targets many processors?
- A
It must use a separate source language for every variable in the program
- B
It must handle common code-generation logic plus differences among target architectures
- C
It must eliminate all intermediate representations before semantic analysis
- D
It must repeat lexical analysis independently for every identifier occurrence
Correct answer
BIt must handle common code-generation logic plus differences among target architectures
Explanation
A multi-target compiler needs mechanisms for shared backend behavior as well as processor-specific handling for architectures such as x86, ARM, or GPUs.
- A
- 46
Which component of the backend is genuinely machine dependent?
- A
The backend, including instruction selection, scheduling, and register allocation
- B
The scanner, including the recognition of identifiers and operators
- C
The semantic analyzer, including type consistency checks
- D
The parser, including the use of grammar productions
Correct answer
AThe backend, including instruction selection, scheduling, and register allocation
Explanation
The backend is the truly machine-dependent part because it must account for the target processor's instructions, registers, and execution behavior.
- A
- 47
What is the purpose of instruction scheduling?
- A
To reorder instructions to reduce delays and hide operation latencies
- B
To identify the declared type of every variable in the source program
- C
To determine which characters belong to each lexical token
- D
To construct grammar rules for valid programming-language statements
Correct answer
ATo reorder instructions to reduce delays and hide operation latencies
Explanation
Instruction scheduling changes the order of instructions when dependencies permit, helping the processor perform useful work while waiting for slower operations.
- A
- 48
Why might a load instruction motivate instruction reordering?
- A
A load instruction proves that the program has a semantic type error
- B
A load instruction always changes the syntax of the source program
- C
A load may take several cycles, so independent work can be performed during its latency
- D
A load instruction cannot access registers and must be replaced by a grammar rule
Correct answer
CA load may take several cycles, so independent work can be performed during its latency
Explanation
Loads can have multi-cycle latency. Scheduling another independent load or operation during that time can avoid idle cycles and improve performance.
- A
- 49
What is the relationship between source-level code and final machine code in the compiler pipeline?
- A
The compiler skips intermediate forms and always translates source code in one step
- B
The compiler keeps the representation equally abstract throughout every phase
- C
The compiler gradually transforms abstract, high-level representations into concrete target code
- D
The compiler converts machine code into source code before performing lexical analysis
Correct answer
CThe compiler gradually transforms abstract, high-level representations into concrete target code
Explanation
Compilation proceeds through increasingly concrete representations, moving from source code and abstract trees toward actual instructions for a real machine.
- A
- 50
Why do real assembly languages generally use addresses instead of source-level variable names?
- A
Assembly languages cannot represent arithmetic operations on variables
- B
Addresses are required because variables are always stored in processor registers
- C
Source-level variable names are meaningful only to the parser and never to the compiler
- D
Machine instructions operate on concrete storage locations allocated for variables
Correct answer
DMachine instructions operate on concrete storage locations allocated for variables
Explanation
Real assembly for a specific processor uses concrete locations, such as memory addresses or offsets, rather than symbolic source-level variable names.
- A
- 51
Under the stated assumption for local variables, where are variables generally stored?
- A
In the instruction scheduler, with each variable associated with a latency
- B
In the grammar, with each variable associated with a production
- C
On the stack, with each variable associated with an offset
- D
In the lexer, with each variable associated with a regular expression only
Correct answer
COn the stack, with each variable associated with an offset
Explanation
The lecture assumes that local variables are stored on the stack and that each has an offset within the relevant stack frame.
- A
- 52
If local variables are 32 bits each, what illustrative stack offsets were given for A, B, and C?
- A
A at offset 4, B at offset 8, and C at offset 12
- B
A at offset 0, B at offset 4, and C at offset 8
- C
A at offset 1, B at offset 2, and C at offset 3
- D
A at offset 0, B at offset ocho, and C at offset 16
Correct answer
BA at offset 0, B at offset 4, and C at offset 8
Explanation
The example assigns consecutive four-byte offsets: A at 0, B at 4, and C at 8, assuming each variable occupies 32 bits.
- A
- 53
Relative to what is a local variable's stack offset interpreted?
- A
A base address associated with the current function's stack frame or activation record
- B
The location of the first grammar rule used to parse the function
- C
The number of optimization transformations applied to the variable
- D
The first character of the variable's source-level name
Correct answer
AA base address associated with the current function's stack frame or activation record
Explanation
A local variable's offset is interpreted relative to a base pointer for the current function's stack frame, also called its activation record.
- A
- 54
Why might a compiler preserve symbolic information after constructing an abstract syntax tree?
- A
The target processor requires source-level names in every machine instruction
- B
Debuggers need variable names and related information to provide useful debugging support
- C
The parser cannot operate unless all source names remain in the final assembly
- D
Optimization is impossible unless every variable retains its original spelling
Correct answer
BDebuggers need variable names and related information to provide useful debugging support
Explanation
Although symbolic information is not required merely to generate code after the tree is built, compilers preserve it so debuggers can identify source variables and locations.
- A

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