Compiler Design
Compiler Design
Instructions: Choose the best option for each question. All questions within each
topic are unique.
Lexical Analysis
* Question: The first phase of a compiler is responsible for breaking the source
code into a stream of tokens. This phase is known as:
A) Syntax Analysis
B) Semantic Analysis
C) Lexical Analysis
D) Code Generation
* Question: What is the primary output of the lexical analyzer?
A) A parse tree
B) A stream of tokens
C) Optimized machine code
D) A symbol table
* Question: A lexeme in compiler design refers to:
A) The abstract representation of a token.
B) A sequence of characters in the source program that matches the pattern for a
token.
C) The set of rules defining valid tokens.
D) An error detected during scanning.
* Question: Regular expressions are a formal way to describe:
A) The grammatical structure of a programming language.
B) The semantic meaning of program constructs.
C) Patterns of strings that constitute tokens.
D) The control flow of a program.
* Question: Which mathematical model is typically used to implement a lexical
analyzer?
A) Pushdown Automata
B) Turing Machine
C) Finite Automata
D) Linear Bounded Automata
* Question: The process of grouping characters into meaningful sequences (tokens)
is called:
A) Parsing
B) Scanning
C) Code Generation
D) Optimization
* Question: What is the purpose of a token in lexical analysis?
A) To represent the semantic value of a lexeme.
B) To categorize a lexeme into a meaningful unit for the parser.
C) To store the memory address of an identifier.
D) To indicate a syntax error.
* Question: Which of the following components of a compiler interacts directly
with the source code characters?
A) Parser
B) Semantic Analyzer
C) Lexical Analyzer
D) Code Optimizer
* Question: The pattern for a token is typically described using:
A) Context-Free Grammars
B) Regular Definitions
C) Syntax Trees
D) Flowcharts
* Question: What is the role of buffer management in lexical analysis?
A) To handle input/output operations for the entire compiler.
B) To efficiently read input characters from the source program.
C) To store generated tokens before sending them to the parser.
D) To manage the symbol table entries.
* Question: Consider the regular expression (a|b)*. Which of the following strings
is NOT matched by this expression?
A) ababa
B) bba
C) aaa
D) acb
* Question: The regular expression a+ matches:
A) Any number of 'a's, including zero.
B) Exactly one 'a'.
C) One or more 'a's.
D) Zero or one 'a'.
* Question: Which of the following is an example of a lexical error?
A) Missing semicolon at the end of a statement.
B) Using an undeclared variable.
C) A misspelled keyword (e.g., whille instead of while).
D) Type mismatch in an assignment.
* Question: A Deterministic Finite Automaton (DFA) has the property that:
A) From any state, there can be multiple transitions on the same input symbol.
B) From any state, there is exactly one transition for each input symbol.
C) It can have epsilon transitions.
D) It can recognize context-free languages.
* Question: The process of converting a regular expression into a Non-
deterministic Finite Automaton (NFA) is part of:
A) Syntax analysis.
B) Lexical analysis.
C) Semantic analysis.
D) Code generation.
* Question: What is the primary advantage of using an NFA over a DFA for
representing regular expressions in the initial construction phase?
A) NFAs are always smaller than DFAs.
B) NFAs are easier to construct directly from regular expressions.
C) NFAs can recognize more languages than DFAs.
D) NFAs are faster to simulate.
* Question: The regular expression [0-9]+ typically matches:
A) Any single digit.
B) Any sequence of one or more digits.
C) Any sequence of zero or more digits.
D) Only integer numbers.
* Question: What is the start state in a Finite Automaton?
A) Any state from which a transition is possible.
B) The state where the automaton begins processing input.
C) A state that indicates acceptance of a string.
D) A state with no outgoing transitions.
* Question: Which of the following is used to define identifiers in most
programming languages?
A) (letter)(letter|digit)*
B) digit+
C) (a|b)*
D) (letter)*
* Question: The transition function of a Finite Automaton maps:
A) A state and an input symbol to a set of states (for NFA) or a single state
(for DFA).
B) An input string to a boolean value (accept/reject).
C) A state to its next state.
D) An input symbol to a token.
* Question: What is the purpose of lookahead in lexical analysis?
A) To read the entire source file at once.
B) To peek at one or more characters beyond the current lexeme to decide on the
current token.
C) To predict the next token.
D) To store previously recognized tokens.
* Question: The regular expression a*b matches:
A) Any string containing 'a' followed by 'b'.
B) Zero or more 'a's followed by exactly one 'b'.
C) Exactly one 'a' followed by one 'b'.
D) Any string ending with 'b'.
* Question: Which tool is commonly used to generate lexical analyzers
automatically from regular expressions?
A) Yacc/Bison
B) Lex/Flex
C) GCC
D) GDB
* Question: A final state (or accepting state) in a Finite Automaton signifies
that:
A) The automaton has reached an error condition.
B) The input string processed so far forms a valid token.
C) There are no more transitions possible from this state.
D) The automaton is about to terminate.
* Question: The closure operation (Kleene star, *) in regular expressions means:
A) One or more occurrences.
B) Zero or one occurrence.
C) Zero or more occurrences.
D) Concatenation.
* Question: What is the result of applying the regular expression (ab)* to the
string ababab?
A) Match
B) No Match
C) Partial Match
D) Error
* Question: The plus operation (+) in regular expressions means:
A) Zero or more occurrences.
B) Zero or one occurrence.
C) One or more occurrences.
D) Concatenation.
* Question: What is the role of the symbol table during lexical analysis?
A) It is created by the lexical analyzer to store tokens.
B) It is used by the lexical analyzer to check if an identifier is already
declared.
C) The lexical analyzer enters new identifiers into it.
D) It is not directly used by the lexical analyzer.
* Question: The epsilon transition in an NFA allows the automaton to:
A) Transition to another state without consuming an input symbol.
B) Match any input symbol.
C) Indicate an error.
D) Loop indefinitely.
* Question: The regular expression (a|b) matches:
A) The string "ab".
B) The string "a" or the string "b".
C) Any string containing "a" and "b".
D) Any string starting with "a" and ending with "b".
* Question: What is the primary task of a scanner generator (like Lex/Flex)?
A) To generate a parser from a grammar.
B) To generate a lexical analyzer from a set of regular expressions.
C) To optimize machine code.
D) To perform semantic analysis.
* Question: The longest match rule in lexical analysis states that:
A) The scanner should always find the longest possible token that matches a
pattern.
B) The scanner should prioritize keywords over identifiers.
C) The scanner should match the shortest possible token.
D) The scanner should match the token that appears first in the source code.
* Question: Which of the following is a disadvantage of using a DFA directly
constructed from a regular expression for lexical analysis?
A) It can be larger than an equivalent NFA.
B) It cannot handle all regular expressions.
C) It is slower to simulate than an NFA.
D) It cannot detect lexical errors.
* Question: The regular expression a? matches:
A) Zero or more 'a's.
B) Exactly one 'a'.
C) One or more 'a's.
D) Zero or one 'a'.
* Question: What is the purpose of disambiguation rules in lexical analysis (e.g.,
for keywords vs. identifiers)?
A) To resolve conflicts when a lexeme could match multiple token patterns.
B) To define the order of operations.
C) To handle comments in the source code.
D) To optimize the token stream.
* Question: The concatenation operation in regular expressions (e.g., ab) means:
A) a or b.
B) a followed by b.
C) Zero or more a's followed by b.
D) Zero or one a followed by b.
* Question: Which type of Finite Automaton is generally simpler to construct from
a regular expression but potentially harder to simulate directly?
A) DFA
B) NFA
C) Both are equally simple.
D) Neither.
* Question: The input buffer in a lexical analyzer is typically divided into two
halves to facilitate:
A) Faster disk I/O.
B) Efficient lookahead and backtracking.
C) Parallel processing.
D) Error reporting.
* Question: Consider the regular expression (a|b)*abb. Which of the following
strings is matched?
A) ab
B) aab
C) babb
D) aba
* Question: What is the role of sentinels in input buffering for lexical analysis?
A) To mark the end of a line.
B) To indicate the end of the input file and prevent buffer overflow during
lookahead.
C) To separate tokens.
D) To store error messages.
* Question: The regular expression [A-Za-z_][A-Za-z0-9_]* defines patterns for:
A) Integer literals.
B) Floating-point numbers.
C) Valid identifiers.
D) Keywords.
* Question: What is the main characteristic of a Non-deterministic Finite
Automaton (NFA)?
A) It has a unique next state for every input symbol.
B) It can have multiple transitions from a state on the same input symbol, or
epsilon transitions.
C) It always accepts the longest possible string.
D) It can count arbitrary numbers.
* Question: The lexical analysis phase is also known as:
A) Parsing
B) Scanning
C) Semantic checking
D) Code optimization
* Question: If a lexical analyzer encounters a sequence of characters that does
not match any valid token pattern, it typically reports a:
A) Syntax error.
B) Semantic error.
C) Lexical error (or invalid token error).
D) Logical error.
* Question: The transition table of a DFA represents:
A) The set of all possible input strings.
B) How the automaton changes state based on current state and input symbol.
C) The list of all tokens.
D) The grammar rules.
* Question: Which of the following is a common task performed by the lexical
analyzer?
A) Building a parse tree.
B) Type checking.
C) Removing whitespace and comments.
D) Generating machine code.
* Question: The regular expression (0|1)* matches:
A) Any binary string of length at least one.
B) Any binary string, including the empty string.
C) Only binary strings of even length.
D) Only binary strings of odd length.
* Question: What is the error recovery strategy typically employed by a lexical
analyzer?
A) Skipping characters until a valid token pattern is found.
B) Reporting all errors at once.
C) Attempting to correct the error automatically.
D) Stopping compilation immediately upon the first error.
* Question: The grep command in Unix/Linux uses what concept for pattern matching?
A) Context-free grammars.
B) Regular expressions.
C) Pushdown automata.
D) Semantic analysis.
* Question: What is the primary input to a lexical analyzer?
A) Abstract Syntax Tree.
B) Source code characters.
C) Intermediate code.
D) User commands.
* Question: The lexical analyzer is also sometimes referred to as a:
A) Parser.
B) Scanner.
C) Code generator.
D) Optimizer.
* Question: Which of the following is a common issue that a lexical analyzer needs
to handle, especially in languages like C++?
A) Operator overloading.
B) The "longest match" rule for keywords versus identifiers.
C) Type conversions.
D) Function call resolution.
* Question: The finite set of states in a Finite Automaton represents:
A) The set of all possible input symbols.
B) The memory of the automaton.
C) The different configurations the automaton can be in.
D) The set of all valid tokens.
* Question: What is the significance of the start state in a DFA?
A) It's the only accepting state.
B) It's the state from which all transitions must originate.
C) It's the initial configuration of the automaton before processing any input.
D) It's the state reached after processing the entire input.
* Question: The regular expression a.b (where . is a wildcard for any single
character) matches:
A) The string "ab".
B) Any three-character string starting with 'a' and ending with 'b'.
C) Any string containing 'a' and 'b'.
D) The string "a" followed by any character, followed by "b".
* Question: What is the purpose of the lexeme attribute (or token value)
associated with a token?
A) To indicate its type (e.g., ID, INT, KEYWORD).
B) To store the actual character string that formed the token.
C) To provide its memory address.
D) To define its scope.
* Question: Which of the following regular expressions represents an unsigned
integer?
A) [0-9]*
B) [0-9]+
C) [0-9]
D) digit
* Question: The table-driven approach to lexical analysis involves:
A) Using a hardcoded finite automaton.
B) Storing the DFA's transition function in a table for efficient lookup.
C) Generating a new DFA for each input.
D) Using a symbol table to drive the scanning process.
* Question: What happens if a lexical analyzer, while scanning, encounters a
character that is not part of any valid token pattern?
A) It ignores the character.
B) It attempts to correct the character.
C) It reports a lexical error.
D) It passes the character directly to the parser.
* Question: The concept of maximal munch is another name for which rule in lexical
analysis?
A) Shortest match rule.
B) Longest match rule.
C) Priority rule.
D) Left-to-right scan rule.
Solutions: Compiler Design - Lexical Analysis
* C) Lexical Analysis
* B) A stream of tokens
* B) A sequence of characters in the source program that matches the pattern for a
token.
* C) Patterns of strings that constitute tokens.
* C) Finite Automata
* B) Scanning
* B) To categorize a lexeme into a meaningful unit for the parser.
* C) Lexical Analyzer
* B) Regular Definitions
* B) To efficiently read input characters from the source program.
* D) acb (Because (a|b)* only matches sequences of 'a's and 'b's, 'c' is not
allowed.)
* C) One or more 'a's.
* C) A misspelled keyword (e.g., whille instead of while).
* B) From any state, there is exactly one transition for each input symbol.
* B) Lexical analysis.
* B) NFAs are easier to construct directly from regular expressions.
* B) Any sequence of one or more digits.
* B) The state where the automaton begins processing input.
* A) (letter)(letter|digit)*
* A) A state and an input symbol to a set of states (for NFA) or a single state
(for DFA).
* B) To peek at one or more characters beyond the current lexeme to decide on the
current token.
* B) Zero or more 'a's followed by exactly one 'b'.
* B) Lex/Flex
* B) The input string processed so far forms a valid token.
* C) Zero or more occurrences.
* A) Match
* C) One or more occurrences.
* C) The lexical analyzer enters new identifiers into it. (It recognizes
identifiers and enters them; checking for declaration is typically semantic
analysis.)
* A) Transition to another state without consuming an input symbol.
* B) The string "a" or the string "b".
* B) To generate a lexical analyzer from a set of regular expressions.
* A) The scanner should always find the longest possible token that matches a
pattern.
* A) It can be larger than an equivalent NFA.
* D) Zero or one 'a'.
* A) To resolve conflicts when a lexeme could match multiple token patterns.
* B) a followed by b.
* B) NFA
* B) Efficient lookahead and backtracking.
* C) babb
* B) To indicate the end of the input file and prevent buffer overflow during
lookahead.
* C) Valid identifiers.
* B) It can have multiple transitions from a state on the same input symbol, or
epsilon transitions.
* B) Scanning
* C) Lexical error (or invalid token error).
* B) How the automaton changes state based on current state and input symbol.
* C) Removing whitespace and comments.
* B) Any binary string, including the empty string.
* A) Skipping characters until a valid token pattern is found.
* B) Regular expressions.
* B) Source code characters.
* B) Scanner.
* B) The "longest match" rule for keywords versus identifiers.
* C) The different configurations the automaton can be in.
* C) It's the initial configuration of the automaton before processing any input.
* D) The string "a" followed by any character, followed by "b".
* B) To store the actual character string that formed the token.
* B) [0-9]+
* B) Storing the DFA's transition function in a table for efficient lookup.
* C) It reports a lexical error.
* B) Longest match rule.
Syntax Analysis
* Question: The phase of a compiler that checks for the grammatical structure of
the program according to the language's rules is:
A) Lexical Analysis
B) Syntax Analysis
C) Semantic Analysis
D) Code Generation
* Question: What is the primary output of the syntax analyzer?
A) A stream of tokens
B) A parse tree (or syntax tree)
C) Optimized machine code
D) A symbol table
* Question: Which formal grammar is primarily used to describe the syntax of
programming languages?
A) Regular Grammars
B) Context-Free Grammars (CFG)
C) Context-Sensitive Grammars
D) Unrestricted Grammars
* Question: A non-terminal symbol in a Context-Free Grammar represents:
A) A basic token of the language.
B) A syntactic variable that can be replaced by other symbols according to
production rules.
C) A keyword in the language.
D) The final output of the parser.
* Question: The process of constructing a parse tree from a stream of tokens is
called:
A) Scanning
B) Lexing
C) Parsing
D) Generating
* Question: Which of the following is a characteristic of top-down parsing?
A) It builds the parse tree from leaves to root.
B) It starts from the start symbol and tries to derive the input string.
C) It uses a stack to store input symbols.
D) It is always more powerful than bottom-up parsing.
* Question: What is the main issue caused by left recursion in a grammar for a
top-down parser (e.g., recursive descent)?
A) It leads to ambiguity.
B) It causes infinite loops during parsing.
C) It makes the grammar context-sensitive.
D) It increases the size of the parse tree.
* Question: The technique of eliminating common prefixes from alternative
productions in a grammar, like transforming A -> αβ | αγ to A -> αA', A' -> β | γ,
is known as:
A) Left recursion elimination
B) Left factoring
C) Right recursion
D) Backtracking
* Question: Which of the following is an example of a top-down parsing technique
that does not involve backtracking?
A) Recursive Descent Parser with backtracking
B) Shift-Reduce Parser
C) LL(1) Parser
D) LR Parser
* Question: What does LL(1) stand for in parsing?
A) Left-to-left, 1 lookahead.
B) Left-to-right scan, Leftmost derivation, 1 token lookahead.
C) Left-to-right scan, Rightmost derivation, 1 token lookahead.
D) Lookahead Left, 1 token.
* Question: The FIRST set of a non-terminal A is the set of:
A) All terminal symbols that can follow A in any sentential form.
B) All terminal symbols that can begin a string derived from A.
C) All non-terminal symbols that can be derived from A.
D) The first token in the input stream.
* Question: The FOLLOW set of a non-terminal A is the set of:
A) All terminal symbols that can begin a string derived from A.
B) All terminal symbols that can immediately follow A in some sentential form.
C) All non-terminal symbols that can follow A.
D) The last token in the input stream.
* Question: A grammar is LL(1) if and only if for any two distinct productions A \
to \alpha and A \to \beta:
A) FIRST(\alpha) \cap FIRST(\beta) = \emptyset
B) FIRST(\alpha) \cap FOLLOW(A) = \emptyset
C) FIRST(\alpha) \cap FIRST(\beta) \ne \emptyset
D) FIRST(\alpha) \cap FOLLOW(A) \ne \emptyset
* Question: Which of the following is a bottom-up parsing technique?
A) Predictive Parsing
B) Recursive Descent
C) LR Parsing
D) LL Parsing
* Question: In Shift-Reduce parsing, the parser performs a shift action when:
A) The top of the stack matches the right-hand side of a production.
B) It moves the next input symbol onto the stack.
C) It accepts the input string.
D) It encounters an error.
* Question: In Shift-Reduce parsing, the parser performs a reduce action when:
A) It moves the next input symbol onto the stack.
B) The top of the stack matches the right-hand side of a production, which is
then replaced by the left-hand side.
C) It accepts the input string.
D) It encounters an error.
* Question: A Shift/Reduce conflict in LR parsing occurs when:
A) The parser can perform a shift or a reduce action in the same state for the
same lookahead symbol.
B) The parser can reduce by two or more different production rules.
C) The parser can shift two different symbols.
D) The parser reaches the end of the input.
* Question: A Reduce/Reduce conflict in LR parsing occurs when:
A) The parser can perform a shift or a reduce action.
B) The parser can reduce by two or more different production rules for the same
input and stack top.
C) The parser can shift two different symbols.
D) The parser encounters an error state.
* Question: Which type of LR parser is the most powerful (can parse the largest
class of grammars)?
A) SLR (Simple LR)
B) LALR (Look-Ahead LR)
C) Canonical LR (CLR)
D) All are equally powerful.
* Question: Which type of LR parser is most commonly used in practice due to its
balance of power and table size?
A) SLR
B) LALR
C) CLR
D) LR(0)
* Question: The LR(0) items are sets of productions with a dot indicating the
progress of parsing. The closure operation on an LR(0) item set adds:
A) All productions whose left-hand side is the symbol immediately after the dot.
B) All productions whose right-hand side matches the symbol after the dot.
C) All productions that can be derived from the current item.
D) All terminal symbols in the input.
* Question: The goto function in LR parsing maps:
A) A state and a terminal symbol to a new state.
B) A state and a non-terminal symbol to a new state.
C) An input symbol to a token.
D) A state to an action.
* Question: What is the significance of the dot (.) in an LR item (e.g., A ->
α.β)?
A) It indicates the end of the production.
B) It shows the part of the production that has been matched so far.
C) It marks the start symbol.
D) It represents an empty string.
* Question: A parser generator tool like Yacc/Bison takes as input:
A) Regular expressions and generates a lexical analyzer.
B) A Context-Free Grammar and generates a parser.
C) Source code and generates machine code.
D) Intermediate code and generates optimized code.
* Question: Which of the following is a disadvantage of recursive descent parsing?
A) It cannot handle left recursion.
B) It is difficult to implement.
C) It is not suitable for small grammars.
D) It requires large parsing tables.
* Question: The ambiguity of a grammar means that:
A) It contains left recursion.
B) It contains left factoring.
C) A single input string can have more than one parse tree.
D) It cannot be parsed by any parser.
* Question: What is the primary method to resolve left recursion in a grammar for
top-down parsing?
A) Left factoring.
B) Replacing left-recursive productions with right-recursive ones and new non-
terminals.
C) Adding FIRST and FOLLOW sets.
D) Using a shift-reduce parser.
* Question: The panic mode error recovery strategy in parsing involves:
A) Attempting to fix the error automatically.
B) Deleting input symbols one by one until a synchronizing token is found.
C) Inserting missing tokens.
D) Stopping compilation immediately.
* Question: Which parsing technique is generally more powerful and can handle a
larger class of grammars (including ambiguous grammars, with disambiguation rules)?
A) LL Parsing
B) Recursive Descent Parsing
C) LR Parsing
D) Predictive Parsing
* Question: The parse table for an LL(1) parser typically contains entries for:
A) (State, Terminal) -> Action
B) (Non-terminal, Terminal) -> Production
C) (State, Non-terminal) -> New State
D) (Terminal, Non-terminal) -> Action
* Question: A handle in bottom-up parsing is:
A) The start symbol of the grammar.
B) A substring that matches the right-hand side of a production and whose
reduction leads to a step in the rightmost derivation.
C) The current input token.
D) An error encountered during parsing.
* Question: The action table in an LR parser typically contains entries for:
A) (State, Non-terminal) -> New State
B) (State, Terminal) -> Action (Shift, Reduce, Accept, Error)
C) (Non-terminal, Terminal) -> Production
D) (Terminal, Terminal) -> Action
* Question: What is the purpose of the goto table in an LR parser?
A) To determine the next state after a shift action.
B) To determine the next state after a reduce action (mapping a state and a non-
terminal to a new state).
C) To handle error recovery.
D) To manage the input buffer.
* Question: A grammar that is LR(k) means it can be parsed by an LR parser using:
A) Exactly k tokens of lookahead.
B) At most k tokens of lookahead.
C) At least k tokens of lookahead.
D) No lookahead.
* Question: The augmented grammar for an LR parser adds a new start production
S' \to .S to:
A) Resolve conflicts.
B) Simplify the grammar.
C) Provide a unique start state and acceptance condition.
D) Handle left recursion.
* Question: Which of the following is a context-free grammar production rule?
A) A -> aBc
B) AB -> CD
C) aA -> b
D) A -> ε (epsilon, empty string)
* Question: What is the primary role of the stack in a shift-reduce parser?
A) To store the entire input string.
B) To store the sequence of tokens and non-terminals that have been processed so
far.
C) To keep track of error messages.
D) To store the grammar rules.
* Question: A grammar that cannot be parsed by an LL(1) parser due to a conflict
is said to be:
A) Ambiguous.
B) Left-recursive.
C) Not LL(1).
D) Context-sensitive.
* Question: What is backtracking in parsing?
A) Moving back in the input stream.
B) Retrying a different production rule when a choice leads to a dead end.
C) Undoing a reduction.
D) Moving from a child node to a parent node in the parse tree.
* Question: Which type of parser constructs the parse tree from the root down to
the leaves?
A) LR Parser.
B) Shift-Reduce Parser.
C) Top-down Parser.
D) Bottom-up Parser.
* Question: The FIRST and FOLLOW sets are crucial for constructing parsing tables
for:
A) LR parsers only.
B) LL parsers only.
C) Both LL and LR parsers.
D) Recursive descent parsers only.
* Question: What is the main difference between SLR and LALR parsers?
A) SLR is more powerful than LALR.
B) LALR uses more lookahead than SLR.
C) LALR has smaller tables than CLR but the same power as CLR for practical
grammars.
D) SLR can handle ambiguous grammars, LALR cannot.
* Question: The parse tree explicitly shows:
A) The sequence of tokens.
B) The hierarchical structure of the input string according to the grammar.
C) The semantic meaning of the program.
D) The optimized machine code.
* Question: Which type of parsing is typically more efficient in terms of time
complexity for a given grammar?
A) Top-down parsing with backtracking.
B) Bottom-up parsing (e.g., LR parsers).
C) Recursive descent parsing without lookahead.
D) Brute-force parsing.
* Question: A terminal symbol in a Context-Free Grammar represents:
A) A syntactic category that can be expanded.
B) A basic token or literal in the language.
C) The start symbol of the grammar.
D) A variable that holds a value.
* Question: The handle pruning technique is associated with:
A) Top-down parsing.
B) Bottom-up parsing.
C) Lexical analysis.
D) Code optimization.
* Question: What is the purpose of error productions in a grammar for parsing?
A) To define valid syntax.
B) To explicitly specify how to recover from certain syntax errors.
C) To generate error messages.
D) To prevent infinite loops.
* Question: Which of the following is a limitation of LL(1) parsers?
A) They cannot handle left recursion.
B) They cannot handle left factoring.
C) They require a large lookahead.
D) They are always slower than LR parsers.
* Question: The canonical collection of LR(0) items is used to build the:
A) FIRST and FOLLOW sets.
B) LL(1) parsing table.
C) LR parsing tables (Action and Goto tables).
D) Symbol table.
* Question: What is the role of synchronizing tokens in panic mode error recovery?
A) To indicate where the error occurred.
B) To help the parser resume parsing after an error.
C) To correct the error automatically.
D) To highlight the error in the source code.
* Question: The parser typically receives its input from which component of the
compiler?
A) Code Generator.
B) Semantic Analyzer.
C) Lexical Analyzer.
D) Optimizer.
* Question: A rightmost derivation is associated with which type of parsing?
A) Top-down parsing.
B) Leftmost parsing.
C) Bottom-up parsing.
D) Predictive parsing.
* Question: What is the main characteristic of an LR(k) parser?
A) It performs a leftmost derivation in reverse.
B) It performs a rightmost derivation in reverse.
C) It uses k tokens of lookahead to make parsing decisions.
D) Both B and C.
* Question: Which of the following grammars is ambiguous?
A) E -> E + E | id
B) E -> E + T | T
T -> T * F | F
F -> id
C) S -> aS | b
D) S -> (S) | ε
* Question: The predictive parser is a type of:
A) Bottom-up parser.
B) Top-down parser without backtracking.
C) Shift-reduce parser.
D) LR parser.
* Question: What is the purpose of the parse stack in an LR parser?
A) To store the input tokens.
B) To store the states of the DFA and grammar symbols.
C) To store the parse tree.
D) To store error messages.
* Question: The handle in bottom-up parsing is always the right-hand side of a
production that is:
A) The longest match.
B) The shortest match.
C) The one that allows a reduction to be made.
D) The one that is immediately followed by a terminal.
* Question: Which parsing conflict arises when the parser cannot decide whether to
shift the next input symbol or reduce the stack content?
A) Reduce/Reduce conflict.
B) Shift/Reduce conflict.
C) Ambiguity conflict.
D) Left recursion conflict.
* Question: The LL(1) parsing table entry M[A, a] (for non-terminal A and terminal
a) contains:
A) The next state.
B) The production rule A -> α to be used.
C) A shift action.
D) A reduce action.
* Question: What is the primary advantage of LR parsers over LL parsers?
A) They are simpler to implement.
B) They can handle a larger class of grammars.
C) They are always faster.
D) They do not require FIRST and FOLLOW sets.
Solutions: Compiler Design - Syntax Analysis
* B) Syntax Analysis
* B) A parse tree (or syntax tree)
* B) Context-Free Grammars (CFG)
* B) A syntactic variable that can be replaced by other symbols according to
production rules.
* C) Parsing
* B) It starts from the start symbol and tries to derive the input string.
* B) It causes infinite loops during parsing.
* B) Left factoring
* C) LL(1) Parser
* B) Left-to-right scan, Leftmost derivation, 1 token lookahead.
* B) All terminal symbols that can begin a string derived from A.
* B) All terminal symbols that can immediately follow A in some sentential form.
* A) FIRST(\alpha) \cap FIRST(\beta) = \emptyset
* C) LR Parsing
* B) It moves the next input symbol onto the stack.
* B) The top of the stack matches the right-hand side of a production, which is
then replaced by the left-hand side.
* A) The parser can perform a shift or a reduce action in the same state for the
same lookahead symbol.
* B) The parser can reduce by two or more different production rules for the same
input and stack top.
* C) Canonical LR (CLR)
* B) LALR
* A) All productions whose left-hand side is the symbol immediately after the dot.
* B) A state and a non-terminal symbol to a new state.
* B) It shows the part of the production that has been matched so far.
* B) A Context-Free Grammar and generates a parser.
* A) It cannot handle left recursion.
* C) A single input string can have more than one parse tree.
* B) Replacing left-recursive productions with right-recursive ones and new non-
terminals.
* B) Deleting input symbols one by one until a synchronizing token is found.
* C) LR Parsing
* B) (Non-terminal, Terminal) -> Production
* B) A substring that matches the right-hand side of a production and whose
reduction leads to a step in the rightmost derivation.
* B) (State, Terminal) -> Action (Shift, Reduce, Accept, Error)
* B) To determine the next state after a reduce action (mapping a state and a non-
terminal to a new state).
* A) Exactly k tokens of lookahead.
* C) Provide a unique start state and acceptance condition.
* A) A -> aBc
* B) To store the sequence of tokens and non-terminals that have been processed so
far.
* C) Not LL(1).
* B) Retrying a different production rule when a choice leads to a dead end.
* C) Top-down Parser.
* C) Both LL and LR parsers.
* C) LALR has smaller tables than CLR but the same power as CLR for practical
grammars.
* B) The hierarchical structure of the input string according to the grammar.
* B) Bottom-up parsing (e.g., LR parsers).
* B) A basic token or literal in the language.
* B) Bottom-up parsing.
* B) To explicitly specify how to recover from certain syntax errors.
* A) They cannot handle left recursion.
* C) LR parsing tables (Action and Goto tables).
* B) To help the parser resume parsing after an error.
* C) Lexical Analyzer.
* C) Bottom-up parsing.
* D) Both B and C.
* A) E -> E + E | id
* B) Top-down parser without backtracking.
* B) To store the states of the DFA and grammar symbols.
* C) The one that allows a reduction to be made.
* B) Shift/Reduce conflict.
* B) The production rule A -> α to be used.
* B) They can handle a larger class of grammars.
Syntax-Directed Translation
* Question: Syntax-Directed Translation (SDT) schemes primarily associate:
A) Regular expressions with tokens.
B) Semantic rules or actions with grammar productions.
C) Error messages with syntax errors.
D) Machine instructions with intermediate code.
* Question: The output of the syntax analysis phase, which is often the input to
the semantic analysis and intermediate code generation phases, is typically:
A) A stream of tokens.
B) A parse tree or an abstract syntax tree.
C) Machine code.
D) A symbol table.
* Question: An attribute in Syntax-Directed Translation is:
A) A property associated with a grammar symbol.
B) A terminal symbol.
C) A production rule.
D) A state in a finite automaton.
* Question: What are the two main types of attributes in SDT?
A) Lexical and Syntactic.
B) Inherited and Synthesized.
C) Input and Output.
D) Static and Dynamic.
* Question: A synthesized attribute is an attribute whose value at a node in a
parse tree is computed from:
A) The values of its children nodes.
B) The values of its parent node.
C) The values of its sibling nodes.
D) The global symbol table.
* Question: An inherited attribute is an attribute whose value at a node in a
parse tree is computed from:
A) The values of its children nodes.
B) The values of its parent node and/or its left siblings.
C) The global symbol table.
D) The input token stream.
* Question: An S-attributed SDT scheme uses only:
A) Inherited attributes.
B) Synthesized attributes.
C) Both inherited and synthesized attributes.
D) Lexical attributes.
* Question: An L-attributed SDT scheme allows attributes to be evaluated in a
single pass of a:
A) Bottom-up parser.
B) Top-down parser.
C) Lexical analyzer.
D) Code optimizer.
* Question: Which of the following intermediate code representations is a linear
sequence of instructions, each with at most three operands?
A) Parse Tree
B) Abstract Syntax Tree
C) Three-Address Code
D) Postfix Notation
* Question: The postfix notation (Reverse Polish Notation) for the infix
expression A + B * C is:
A) + A B * C
B) A B + C *
C) A B C * +
D) A * B + C
* Question: What is the primary advantage of using intermediate code in a
compiler?
A) It simplifies the lexical analysis phase.
B) It makes the compiler more portable and facilitates optimization.
C) It directly generates executable machine code.
D) It eliminates the need for a symbol table.
* Question: A parse tree explicitly represents:
A) The sequence of tokens.
B) The hierarchical grammatical structure of the input string.
C) The semantic meaning of the program.
D) The optimized machine code.
* Question: An Abstract Syntax Tree (AST) differs from a parse tree by:
A) Including all syntactic details like parentheses and semicolons.
B) Omitting unnecessary syntactic details and representing the program's
structure more abstractly.
C) Being generated by the lexical analyzer.
D) Being a linear representation of the code.
* Question: Which intermediate code representation is a graphical representation
where interior nodes are operators and leaf nodes are operands?
A) Three-Address Code.
B) Postfix Notation.
C) Syntax Tree (Parse Tree or AST).
D) Quadruples.
* Question: The three-address code instruction t1 = a + b implies:
A) t1 is assigned the sum of a and b.
B) a, b, and t1 are all addresses.
C) It's a jump instruction.
D) It's a comparison.
* Question: Postfix translation with a top-down parser typically involves:
A) Emitting operators before operands.
B) Emitting operators after operands.
C) Using a stack to store operators.
D) Using a queue to store operands.
* Question: The quadruple representation of three-address code consists of how
many fields?
A) 2
B) 3
C) 4
D) 5
* Question: What are the fields in a typical quadruple?
A) Operator, Result, Arg1, Arg2.
B) Opcode, Operand1, Operand2.
C) Label, Instruction, Target.
D) Token, Lexeme, Line Number, Column Number.
* Question: The triple representation of intermediate code differs from quadruples
by:
A) Using only two fields.
B) Not using temporary variables explicitly; instead, results are referred to by
their instruction number.
C) Using more than four fields.
D) Being a graphical representation.
* Question: The indirect triple representation uses a list of pointers to triples.
This is primarily to facilitate:
A) Faster code generation.
B) Easier code optimization (reordering instructions without moving large
blocks).
C) Simpler parsing.
D) Reduced memory usage.
* Question: In a syntax-directed definition (SDD), semantic rules are associated
with:
A) Terminal symbols.
B) Non-terminal symbols.
C) Production rules.
D) Lexemes.
* Question: Which phase of the compiler is responsible for constructing the symbol
table and performing type checking?
A) Lexical Analysis.
B) Syntax Analysis.
C) Semantic Analysis.
D) Intermediate Code Generation.
* Question: A type checking error (e.g., assigning a string to an integer
variable) is detected during:
A) Lexical Analysis.
B) Syntax Analysis.
C) Semantic Analysis.
D) Code Generation.
* Question: What kind of information is typically stored in the symbol table for
an identifier?
A) Its value.
B) Its type, scope, and memory location.
C) The number of times it is used.
D) Its associated regular expression.
* Question: The postfix notation for (A + B) * (C - D) is:
A) A B + C D - *
B) A B + * C D -
C) A B C D + - *
D) * + A B - C D
* Question: A syntax tree is a condensed form of a parse tree where:
A) All internal nodes are terminals.
B) Internal nodes represent operations and child nodes represent operands.
C) Only leaf nodes are important.
D) It includes all non-terminal symbols.
* Question: When implementing syntax-directed translation, semantic actions are
typically embedded within the grammar rules. These actions are executed:
A) Before parsing begins.
B) After the entire parse tree is built.
C) As soon as the parser recognizes the corresponding production rule.
D) During code optimization.
* Question: The intermediate code generation phase bridges the gap between:
A) Lexical analysis and syntax analysis.
B) Front end and back end of the compiler.
C) Source code and assembly code.
D) Error handling and symbol table management.
* Question: What is short-circuit evaluation (e.g., for && and || operators)
related to in intermediate code generation?
A) Generating code that evaluates expressions fully before checking conditions.
B) Generating code that evaluates only as much as needed to determine the result
of a boolean expression.
C) Optimizing arithmetic expressions.
D) Handling function calls.
* Question: The postfix translation for an expression like if (condition) stmt1
else stmt2 would involve:
A) Evaluating condition and then jumping based on its result.
B) Converting the entire if-else statement into a single postfix string.
C) Directly generating machine code.
D) Using a stack for all operations.
* Question: Which type of SDT scheme can always be implemented using a bottom-up
parser in a single pass?
A) L-attributed.
B) S-attributed.
C) Both L-attributed and S-attributed.
D) Neither.
* Question: For an L-attributed SDT, inherited attributes can depend on:
A) Only synthesized attributes of its children.
B) Only inherited attributes of its parent.
C) Inherited attributes of its parent and synthesized attributes of its left
siblings.
D) Any attribute in the entire parse tree.
* Question: The three-address code for x = y + z * w would typically be:
A) t1 = z * w
x = y + t1
B) x y z w * + =
C) + * y z w x
D) x = y + z; x = x * w;
* Question: What is the main benefit of syntax trees (ASTs) as an intermediate
representation?
A) They are easy to convert to postfix notation.
B) They are compact and represent the essential structure of the program,
simplifying subsequent phases.
C) They are directly executable.
D) They include all details of the source code.
* Question: The postfix translation for id1 = id2 + id3 would involve:
A) id1 id2 id3 + =
B) id2 id3 + id1 =
C) id1 = + id2 id3
D) + id2 id3 id1 =
* Question: Backpatching is a technique used during intermediate code generation
to:
A) Fill in missing type information.
B) Resolve forward references for labels in jump instructions.
C) Optimize the use of temporary variables.
D) Convert postfix to three-address code.
* Question: When generating three-address code for control flow statements (e.g.,
if-else, while), what kind of instructions are frequently used?
A) Assignment instructions.
B) Conditional and unconditional jump instructions.
C) Procedure call instructions.
D) Array access instructions.
* Question: The syntax-directed definition (SDD) is a high-level specification for
translation, whereas the syntax-directed translation scheme (SDT) is:
A) An implementation of an SDD.
B) A more abstract form than SDD.
C) Used only for lexical analysis.
D) A type of grammar.
* Question: Which of the following is an example of semantic analysis checking?
A) Checking for balanced parentheses.
B) Checking if a variable is declared before use.
C) Checking for misspelled keywords.
D) Checking for valid operators.
* Question: The postfix translation for if (A > B) C = D; might involve:
A) A B > if C D = ;
B) A B > LABEL_TRUE jump C D = LABEL_END jump
C) A B > false_jump L1 C D = jump L2 L1: ... L2: ...
D) if A B > C = D
* Question: What is the main purpose of intermediate code generation?
A) To produce the final executable code.
B) To provide a machine-independent representation that is easier to optimize
and translate to target code.
C) To detect all types of errors in the source code.
D) To manage the symbol table.
* Question: In a syntax tree, what do the leaf nodes typically represent?
A) Operators.
B) Non-terminals.
C) Operands (identifiers, constants).
D) Production rules.
* Question: The three-address code for an array access A[i] = x would involve:
A) t1 = i * element_size
t2 = address_of_A + t1
*t2 = x
B) A i x =
C) x = A[i]
D) A = x i
* Question: Which type of attribute is typically used to pass information down the
parse tree (e.g., type declarations, context information)?
A) Synthesized.
B) Inherited.
C) Global.
D) Local.
* Question: The postfix translation for while (condition) statement; would
involve:
A) while condition statement
B) LABEL_LOOP: condition false_jump LABEL_END statement jump LABEL_LOOP
LABEL_END:
C) condition statement while
D) statement condition while
* Question: What is the role of temporary variables in three-address code?
A) To store final results.
B) To hold intermediate results of complex expressions.
C) To represent global variables.
D) To store loop counters.
* Question: The evaluation order of attributes in an SDD is crucial. For an S-
attributed SDD, attributes can be evaluated using a:
A) Pre-order traversal.
B) Post-order traversal.
C) In-order traversal.
D) Level-order traversal.
* Question: Which intermediate representation is often used as input for the code
optimization phase?
A) Parse Tree.
B) Three-Address Code.
C) Postfix Notation.
D) Source Code.
* Question: What is the primary advantage of postfix notation as an intermediate
code?
A) It is easy for humans to read.
B) It eliminates the need for parentheses and operator precedence rules during
evaluation.
C) It directly maps to machine instructions.
D) It is a graphical representation.
* Question: The semantic analysis phase ensures that the program is:
A) Syntactically correct.
B) Lexically correct.
C) Meaningful and adheres to type rules and language constraints.
D) Optimized for performance.
* Question: When implementing a syntax-directed translator for a top-down parser,
semantic actions are typically placed:
A) Before the production rule is matched.
B) At the end of the production rule.
C) At the beginning of the production rule.
D) Anywhere within the production rule, depending on attribute dependencies.
* Question: What is a common way to represent boolean expressions in three-address
code for control flow?
A) As simple true/false values.
B) As jumps to labels based on the truth value.
C) As arithmetic operations.
D) As string literals.
* Question: The postfix translation for A = B is:
A) A B =
B) = A B
C) B A =
D) A = B
* Question: Which of the following is a type of intermediate code that is highly
structured and often used for optimization?
A) Linear code (e.g., three-address code).
B) Graphical code (e.g., DAG, AST).
C) Both A and B.
D) Only source code.
* Question: The type system of a programming language is primarily enforced during
which phase?
A) Lexical Analysis.
B) Syntax Analysis.
C) Semantic Analysis.
D) Code Generation.
* Question: How are arrays typically handled in three-address code for access
(e.g., A[i])?
A) As a single variable.
B) By calculating the memory address based on base address, index, and element
size.
C) By direct memory access without calculation.
D) As a function call.
* Question: The postfix notation for if A then B else C is (assuming if-then-else
is an operator):
A) A B C if
B) A B if C else
C) A B C else if
D) if A B C
* Question: A declaration of a variable (e.g., int x;) primarily affects which
part of the compiler's data structures?
A) The token stream.
B) The parse tree.
C) The symbol table.
D) The intermediate code.
* Question: What is the purpose of synthesized attributes in a syntax-directed
definition?
A) To pass information from parent to child nodes.
B) To pass information from child nodes up to their parent.
C) To define the scope of variables.
D) To handle error recovery.
* Question: Which of the following is NOT a common form of three-address code?
A) Quadruples.
B) Triples.
C) Indirect Triples.
D) Abstract Syntax Trees.
Solutions: Compiler Design - Syntax-Directed Translation
* B) Semantic rules or actions with grammar productions.
* B) A parse tree or an abstract syntax tree.
* A) A property associated with a grammar symbol.
* B) Inherited and Synthesized.
* A) The values of its children nodes.
* B) The values of its parent node and/or its left siblings.
* B) Synthesized attributes.
* B) Top-down parser.
* C) Three-Address Code.
* C) A B C * +
* B) It makes the compiler more portable and facilitates optimization.
* B) The hierarchical grammatical structure of the input string.
* B) Omitting unnecessary syntactic details and representing the program's
structure more abstractly.
* C) Syntax Tree (Parse Tree or AST).
* A) t1 is assigned the sum of a and b.
* B) Emitting operators after operands.
* C) 4
* A) Operator, Result, Arg1, Arg2.
* B) Not using temporary variables explicitly; instead, results are referred to by
their instruction number.
* B) Easier code optimization (reordering instructions without moving large
blocks).
* C) Production rules.
* C) Semantic Analysis.
* C) Semantic Analysis.
* B) Its type, scope, and memory location.
* A) A B + C D - *
* B) Internal nodes represent operations and child nodes represent operands.
* C) As soon as the parser recognizes the corresponding production rule.
* B) Front end and back end of the compiler.
* B) Generating code that evaluates only as much as needed to determine the result
of a boolean expression.
* C) A B > false_jump L1 C D = jump L2 L1: ... L2: ... (This represents the
control flow with jumps)
* C) Both L-attributed and S-attributed.
* C) Inherited attributes of its parent and synthesized attributes of its left
siblings.
* A) t1 = z * w
x = y + t1
* B) They are compact and represent the essential structure of the program,
simplifying subsequent phases.
* B) id2 id3 + id1 =
* B) Resolve forward references for labels in jump instructions.
* B) Conditional and unconditional jump instructions.
* A) An implementation of an SDD.
* B) Checking if a variable is declared before use.
* C) A B > false_jump L1 C D = jump L2 L1: ... L2: ... (This is a common way to
represent it in 3-address code, which is an output of SDT)
* B) To provide a machine-independent representation that is easier to optimize
and translate to target code.
* C) Operands (identifiers, constants).
* A) t1 = i * element_size
t2 = address_of_A + t1
*t2 = x
* B) Inherited.
* B) LABEL_LOOP: condition false_jump LABEL_END statement jump LABEL_LOOP
LABEL_END:
* B) To hold intermediate results of complex expressions.
* B) Post-order traversal.
* B) Three-Address Code.
* B) It eliminates the need for parentheses and operator precedence rules during
evaluation.
* C) Meaningful and adheres to type rules and language constraints.
* D) Anywhere within the production rule, depending on attribute dependencies.
(For L-attributed, it's restricted, but generally it's placed where the necessary
attributes are available).
* B) As jumps to labels based on the truth value.
* C) B A =
* C) Both A and B.
* C) Semantic Analysis.
* B) By calculating the memory address based on base address, index, and element
size.
* A) A B C if (This is a common representation, though exact forms can vary. The
if operator takes the condition, then the true-branch result, then the false-branch
result).
* C) The symbol table.
* B) To pass information from child nodes up to their parent.
* D) Abstract Syntax Trees.
Symbol Tables
* Question: What is the primary purpose of a Symbol Table in a compiler?
A) To store all the tokens generated by the lexical analyzer.
B) To store information about identifiers (names, types, scope, etc.) and their
attributes.
C) To store the intermediate code.
D) To store error messages and warnings.
* Question: Which phase of the compiler is primarily responsible for creating and
managing the symbol table?
A) Lexical Analysis
B) Syntax Analysis
C) Semantic Analysis
D) Code Generation
* Question: What kind of information is typically stored in a symbol table for a
variable identifier?
A) Its value at runtime.
B) Its name, data type, and scope.
C) The number of times it is used.
D) Its associated regular expression.
* Question: In a block-structured language (like C, Java), how are different
scopes typically handled in a symbol table?
A) A single global symbol table is used for all scopes.
B) Separate symbol tables are created for each scope, often linked
hierarchically.
C) Scopes are ignored by the symbol table.
D) Identifiers from inner scopes overwrite those from outer scopes.
* Question: When an identifier is encountered for the first time, what action does
the semantic analyzer typically take regarding the symbol table?
A) It looks up the identifier to check for previous declarations.
B) It enters the new identifier and its attributes into the symbol table.
C) It deletes the identifier from the table.
D) It reports an error immediately.
* Question: What happens when an identifier goes out of scope?
A) Its entry is permanently deleted from the symbol table.
B) Its entry is marked as inactive or removed from the current active scope.
C) Its value is reset to null.
D) It causes a lexical error.
* Question: Which data structure is commonly used to implement a symbol table for
efficient lookup, insertion, and deletion?
A) Array
B) Linked List
C) Hash Table
D) Stack
* Question: What is a lexical-phase error related to identifiers?
A) Using an undeclared identifier.
B) A syntax error in an identifier's declaration.
C) An identifier that contains illegal characters (e.g., 1variable).
D) A type mismatch involving an identifier.
* Question: A syntax-phase error related to identifiers might be:
A) A misspelled keyword.
B) An identifier used in an incorrect grammatical context (e.g., if (x +)
{ ... }).
C) An undeclared identifier.
D) Assigning a value of the wrong type to an identifier.
* Question: A semantic error related to identifiers is typically:
A) An identifier with invalid characters.
B) A missing semicolon after an identifier.
C) Using an undeclared identifier, or a type mismatch (e.g., int x; x =
"hello";).
D) An identifier that is too long.
* Question: What is the scope of an identifier?
A) Its data type.
B) Its memory address.
C) The region of the program text where it is visible and can be legally
referenced.
D) Its initial value.
* Question: In a language with nested blocks, how is the correct declaration of an
identifier resolved when multiple declarations with the same name exist in
different scopes?
A) The declaration in the outermost scope is always used.
B) The declaration in the innermost (most local) scope is used.
C) It causes an ambiguity error.
D) The first declaration encountered is used.
* Question: What information might be stored in the symbol table for a function
identifier?
A) Its return type, number and types of parameters, and local variables.
B) Only its name.
C) Its memory address only.
D) Its call count.
* Question: The lookup operation in a symbol table is used to:
A) Add a new identifier.
B) Retrieve information about an existing identifier.
C) Delete an identifier.
D) Traverse the symbol table.
* Question: Why is efficient implementation of the symbol table crucial for
compiler performance?
A) It reduces memory usage.
B) It speeds up the lexical analysis.
C) Lookups and insertions are frequent operations during parsing and semantic
analysis.
D) It simplifies code generation.
* Question: Which of the following is NOT a typical attribute stored for a
procedure/function name in a symbol table?
A) Return type.
B) Number of arguments.
C) Argument types.
D) Its runtime execution time.
* Question: When using a hash table for symbol table implementation, what is a
collision?
A) Two different identifiers having the same name.
B) Two different identifiers hashing to the same index.
C) An error during insertion.
D) A lookup failure.
* Question: Overloading (e.g., multiple functions with the same name but different
parameters) is handled in the symbol table by:
A) Storing only the first declaration.
B) Creating separate entries for each overloaded version, distinguished by their
parameter lists.
C) Reporting a re-declaration error.
D) Ignoring the additional declarations.
* Question: What is the purpose of scope resolution in compiler design?
A) To determine the data type of a variable.
B) To find the correct declaration of an identifier when multiple declarations
exist in nested scopes.
C) To check for syntax errors.
D) To allocate memory for variables.
* Question: A declaration error (e.g., int x; int x;) is typically caught by
checking the symbol table during which phase?
A) Lexical Analysis.
B) Syntax Analysis.
C) Semantic Analysis.
D) Code Optimization.
* Question: What is the lifetime of a variable?
A) Its scope.
B) The period during program execution when it exists in memory.
C) The number of times it is accessed.
D) Its initial value.
* Question: The symbol table can be organized as a list of hash tables where each
hash table corresponds to a different:
A) Data type.
B) Scope.
C) File.
D) Function.
* Question: When implementing a symbol table with a linked list of hash tables for
nested scopes, how is a lookup performed?
A) Search only the global table.
B) Search from the innermost scope outwards to the global scope.
C) Search from the global scope inwards.
D) Search all tables simultaneously.
* Question: What happens to the symbol table when a block (e.g., a function or if
block) is entered?
A) A new scope is created and pushed onto a stack of symbol tables.
B) The existing symbol table is cleared.
C) All identifiers are made global.
D) No change occurs.
* Question: What happens to the symbol table when a block is exited?
A) The symbol table for that scope is permanently deleted.
B) The symbol table for that scope is popped from the stack, making the
enclosing scope active.
C) All identifiers in that scope become global.
D) The compiler continues to use the same symbol table.
* Question: Which of the following is a common attribute for an array identifier
in the symbol table?
A) Base address, element type, and dimensions.
B) Only its name.
C) Its total size in bytes.
D) Its number of elements.
* Question: What is forward referencing in the context of symbol tables?
A) Referring to an identifier before its declaration.
B) Referring to an identifier after its declaration.
C) Referring to a global variable.
D) Referring to a function.
* Question: A use-before-declaration error is detected by the semantic analyzer
using information from:
A) The parse tree.
B) The symbol table.
C) The token stream.
D) The intermediate code.
* Question: What is the purpose of hashing in symbol table implementation?
A) To sort identifiers alphabetically.
B) To quickly map identifier names to table indices for efficient access.
C) To store the entire program.
D) To resolve scope conflicts.
* Question: Which of the following is a method for collision resolution in hash
tables?
A) Binary search.
B) Linear probing.
C) Merge sort.
D) Quick sort.
* Question: When an identifier is declared, its entry in the symbol table might
include a pointer to:
A) Its value.
B) Its corresponding entry in the parse tree.
C) Its memory location (offset from base address of activation record).
D) Its next use in the code.
* Question: The symbol table is used by which compiler phases?
A) Lexical Analysis only.
B) Syntax Analysis and Semantic Analysis.
C) Semantic Analysis and Code Generation.
D) All phases of the compiler.
* Question: What is the difference between static scope (lexical scope) and
dynamic scope?
A) Static scope is determined at runtime, dynamic at compile time.
B) Static scope is determined by program text structure, dynamic by call
sequence.
C) Static scope is for global variables, dynamic for local.
D) Static scope allows recursion, dynamic does not.
* Question: Most modern programming languages use which type of scoping?
A) Dynamic scoping.
B) Static (lexical) scoping.
C) Global scoping only.
D) No scoping.
* Question: The symbol table helps in detecting multiple declaration errors by:
A) Checking the syntax of declarations.
B) Attempting to insert an identifier that already exists in the current scope.
C) Comparing identifier names with keywords.
D) Analyzing the control flow.
* Question: What is the primary use of the symbol table during the code generation
phase?
A) To create new identifiers.
B) To retrieve information about identifiers (e.g., type, address) needed for
generating machine instructions.
C) To perform type checking.
D) To optimize the code.
* Question: The size or width of a data type (e.g., int is 4 bytes) is an
attribute that might be stored in the symbol table for:
A) Variables.
B) Functions.
C) Labels.
D) All of the above.
* Question: What happens if a lookup operation in the symbol table fails for an
identifier that is being used?
A) It's considered a valid use.
B) A "undeclared identifier" error is reported.
C) The compiler attempts to guess its type.
D) It's ignored.
* Question: The symbol table can be viewed as a mapping from:
A) Tokens to lexemes.
B) Identifier names to their attributes.
C) Production rules to semantic actions.
D) Memory addresses to values.
* Question: For a struct or class type, the symbol table might store:
A) Its name and a pointer to another symbol table containing its member
variables and methods.
B) Only its size.
C) Its inheritance hierarchy.
D) Its access specifiers (public, private).
* Question: Which method for implementing a symbol table is generally fastest for
lookups but consumes more memory for sparse data?
A) Ordered List.
B) Linked List.
C) Hash Table.
D) Binary Search Tree.
* Question: What is symbol table management?
A) The process of allocating memory for symbols.
B) The operations of inserting, deleting, and looking up entries in the symbol
table.
C) The process of resolving symbol conflicts.
D) The process of generating symbols.
* Question: A type error (e.g., trying to perform arithmetic on a boolean
variable) is caught by the semantic analyzer using information from:
A) Lexical tokens.
B) Parse tree structure.
C) Symbol table (type attributes).
D) Intermediate code.
* Question: The scope information in a symbol table is crucial for:
A) Determining operator precedence.
B) Resolving name conflicts and implementing variable visibility rules.
C) Optimizing loops.
D) Generating assembly code.
* Question: What is the primary difference between a declaration and a definition
of an identifier?
A) A declaration allocates memory, a definition does not.
B) A declaration introduces the name and type, a definition also provides
implementation/initialization.
C) A declaration is for variables, a definition is for functions.
D) There is no difference.
* Question: Which of the following is an advantage of using a Binary Search Tree
to implement a symbol table over a simple sorted array?
A) Faster worst-case lookup time.
B) Faster average-case insertion and deletion time.
C) Less memory usage.
D) Simpler implementation.
* Question: The symbol table is a central data structure that is built and used by
various phases of the compiler to maintain:
A) The source code.
B) Information about program entities.
C) The target machine instructions.
D) The parse tree.
* Question: When a compiler processes a program, the symbol table is typically:
A) Built once at the beginning and never modified.
B) Dynamically built and updated as declarations are encountered and scopes
change.
C) Only used during the final code generation phase.
D) Cleared after each line of code.
* Question: What is the purpose of nested symbol tables or a stack of symbol
tables?
A) To store different types of symbols.
B) To efficiently manage identifiers in block-scoped languages.
C) To support parallel compilation.
D) To reduce memory usage.
* Question: If an identifier is declared as const, this attribute would be stored
in the symbol table and checked during:
A) Lexical analysis.
B) Syntax analysis.
C) Semantic analysis.
D) Code optimization.
* Question: What kind of error would be reported if a program attempts to call a
function with the wrong number of arguments?
A) Lexical error.
B) Syntax error.
C) Semantic error.
D) Runtime error.
* Question: The offset of a variable within its activation record is an attribute
that might be stored in the symbol table and is crucial for:
A) Type checking.
B) Scope resolution.
C) Memory allocation during code generation.
D) Error reporting.
* Question: For a switch statement with multiple case labels, the symbol table
might store information about:
A) The switch variable's type.
B) The values of the case labels.
C) The jump targets associated with each case.
D) All of the above.
* Question: If a language allows re-declaration of identifiers in inner scopes
(shadowing), how does the symbol table support this?
A) By overwriting the outer scope's entry.
B) By creating a new entry in the current scope's table, effectively hiding the
outer one.
C) By reporting an error.
D) By linking the new declaration to the old one.
* Question: What is the consequence if the symbol table is not properly managed in
a compiler for a block-scoped language?
A) Incorrect tokenization.
B) Failure to generate a parse tree.
C) Incorrect scope resolution and potential type errors or incorrect code
generation.
D) Compiler crashing during lexical analysis.
* Question: The symbol table is a crucial component for implementing which
compiler feature?
A) Comment removal.
B) Operator precedence.
C) Name resolution and type checking.
D) Loop unrolling.
* Question: When using a linear list to implement a symbol table, the time
complexity for lookup and insertion in the worst case is:
A) O(1)
B) O(\log N)
C) O(N)
D) O(N^2)
* Question: The visibility of an identifier refers to:
A) Whether it's a global or local variable.
B) Whether it's declared or not.
C) The scope in which it can be accessed.
D) Its data type.
* Question: What kind of information is gathered about constants (e.g., 5,
"hello") and stored in the symbol table?
A) Their value and type.
B) Their name.
C) Their scope.
D) Only their value.
* Question: The symbol table can also be used to store information about labels in
the code, primarily their:
A) Name.
B) Type.
C) Memory address (or intermediate code instruction index).
D) Usage count.
Solutions: Compiler Design - Symbol Tables
* B) To store information about identifiers (names, types, scope, etc.) and their
attributes.
* C) Semantic Analysis
* B) Its name, data type, and scope.
* B) Separate symbol tables are created for each scope, often linked
hierarchically.
* B) It enters the new identifier and its attributes into the symbol table.
* B) Its entry is marked as inactive or removed from the current active scope.
* C) Hash Table
* C) An identifier that contains illegal characters (e.g., 1variable).
* B) An identifier used in an incorrect grammatical context (e.g., if (x +)
{ ... }).
* C) Using an undeclared identifier, or a type mismatch (e.g., int x; x =
"hello";).
* C) The region of the program text where it is visible and can be legally
referenced.
* B) The declaration in the innermost (most local) scope is used.
* A) Its return type, number and types of parameters, and local variables.
* B) Retrieve information about an existing identifier.
* C) Lookups and insertions are frequent operations during parsing and semantic
analysis.
* D) Its runtime execution time.
* B) Two different identifiers hashing to the same index.
* B) Creating separate entries for each overloaded version, distinguished by their
parameter lists.
* B) To find the correct declaration of an identifier when multiple declarations
exist in nested scopes.
* C) Semantic Analysis.
* B) The period during program execution when it exists in memory.
* B) Scope.
* B) Search from the innermost scope outwards to the global scope.
* A) A new scope is created and pushed onto a stack of symbol tables.
* B) The symbol table for that scope is popped from the stack, making the
enclosing scope active.
* A) Base address, element type, and dimensions.
* A) Referring to an identifier before its declaration.
* B) The symbol table.
* B) To quickly map identifier names to table indices for efficient access.
* B) Linear probing.
* C) Its memory location (offset from base address of activation record).
* C) Semantic Analysis and Code Generation.
* B) Static scope is determined by program text structure, dynamic by call
sequence.
* B) Static (lexical) scoping.
* B) Attempting to insert an identifier that already exists in the current scope.
* B) To retrieve information about identifiers (e.g., type, address) needed for
generating machine instructions.
* A) Variables.
* B) A "undeclared identifier" error is reported.
* B) Identifier names to their attributes.
* A) Its name and a pointer to another symbol table containing its member
variables and methods.
* C) Hash Table.
* B) The operations of inserting, deleting, and looking up entries in the symbol
table.
* C) Symbol table (type attributes).
* B) Resolving name conflicts and implementing variable visibility rules.
* B) A declaration introduces the name and type, a definition also provides
implementation/initialization.
* B) Faster average-case insertion and deletion time.
* B) Information about program entities.
* B) Dynamically built and updated as declarations are encountered and scopes
change.
* B) To efficiently manage identifiers in block-scoped languages.
* C) Semantic analysis.
* C) Semantic error.
* C) Memory allocation during code generation.
* D) All of the above.
* B) By creating a new entry in the current scope's table, effectively hiding the
outer one.
* C) Incorrect scope resolution and potential type errors or incorrect code
generation.
* C) Name resolution and type checking.
* C) O(N)
* C) The scope in which it can be accessed.
* A) Their value and type.
* C) Memory address (or intermediate code instruction index).
Introduction to Code Optimization & Code Generation
* Question: The primary goal of code optimization is to:
A) Detect syntax errors in the program.
B) Convert source code into an intermediate representation.
C) Improve the target code's performance (speed, size, power consumption).
D) Manage the symbol table.
* Question: Which phase of the compiler takes the intermediate representation and
maps it into the target machine's instruction set?
A) Lexical Analysis
B) Syntax Analysis
C) Code Optimization
D) Code Generation
* Question: A basic block in code optimization is defined as:
A) A sequence of instructions that ends with a jump.
B) A sequence of consecutive statements in which flow of control enters at the
beginning and leaves at the end without halt or possibility of branching except at
the end.
C) A loop in the program.
D) A function or procedure.
* Question: Loop invariant code motion is an optimization technique that:
A) Removes entire loops.
B) Moves computations that produce the same result in every iteration of a loop
to outside the loop.
C) Unrolls a loop to reduce overhead.
D) Optimizes nested loops only.
* Question: A Directed Acyclic Graph (DAG) is a useful intermediate representation
for basic blocks primarily because it helps in:
A) Representing control flow.
B) Identifying common subexpressions and dead code within the block.
C) Performing type checking.
D) Handling recursion.
* Question: What is common subexpression elimination?
A) Removing expressions that are never used.
B) Replacing multiple computations of the same expression with a single
computation and reusing its result.
C) Moving invariant code out of loops.
D) Simplifying constant expressions.
* Question: Peephole optimization is a local optimization technique that operates
on:
A) The entire program.
B) A small, fixed-size window of target instructions.
C) Only loop structures.
D) The intermediate code before code generation.
* Question: The machine model used in code generation describes:
A) The compiler's internal architecture.
B) The target hardware's instruction set, register organization, and memory
hierarchy.
C) The abstract syntax tree structure.
D) The operating system on which the compiler runs.
* Question: Register allocation is the process of:
A) Assigning memory addresses to variables.
B) Deciding which program variables should reside in machine registers.
C) Storing values in main memory.
D) Managing the stack frame.
* Question: Register assignment is the process of:
A) Determining the order of instructions.
B) Assigning specific physical registers to the chosen program variables.
C) Allocating temporary variables.
D) Freeing up registers.
* Question: What is dead code elimination?
A) Removing code that is unreachable or whose results are never used.
B) Removing comments from the code.
C) Removing redundant computations.
D) Removing loops from the code.
* Question: The code generator typically receives its input from which phase?
A) Lexical Analyzer
B) Syntax Analyzer
C) Intermediate Code Generator (and Optimizer)
D) Semantic Analyzer
* Question: Which of the following is a characteristic of a simple code generator?
A) It performs extensive optimization.
B) It aims for correctness over optimality, generating code directly from
intermediate representation.
C) It generates highly optimized code.
D) It handles complex machine-specific features.
* Question: When generating code from a DAG, the order of evaluation of nodes is
important. Which traversal strategy is often used?
A) Breadth-First Search (BFS).
B) Depth-First Search (DFS) in post-order.
C) Pre-order traversal.
D) In-order traversal.
* Question: What is strength reduction in code optimization?
A) Reducing the number of instructions.
B) Replacing an expensive operation with an equivalent cheaper one (e.g.,
multiplication by addition).
C) Reducing the strength of a variable.
D) Reducing the memory footprint.
* Question: Constant folding is an optimization technique that:
A) Replaces variables with constants.
B) Evaluates constant expressions at compile time and replaces them with their
results.
C) Converts variables to constants.
D) Removes unused constants.
* Question: What is register spilling?
A) When a register value is moved to memory because there are not enough
registers to hold all active variables.
B) When a register is accidentally overwritten.
C) When too many registers are allocated.
D) When a register is used for multiple purposes.
* Question: The code generator is often considered the back end of the compiler
because:
A) It deals with the source language.
B) It is machine-dependent and produces target code.
C) It handles error recovery.
D) It is the first phase of compilation.
* Question: What is the purpose of basic block partitioning?
A) To divide the program into functions.
B) To identify maximal sequences of straight-line code for local optimization.
C) To separate global and local variables.
D) To create loops.
* Question: Code motion is a type of optimization that:
A) Changes the order of instructions within a basic block.
B) Moves computations to a place where they are executed less frequently.
C) Removes redundant code.
D) Combines multiple instructions into one.
* Question: Which of the following is a machine-dependent optimization?
A) Common subexpression elimination.
B) Peephole optimization.
C) Loop invariant code motion.
D) Dead code elimination.
* Question: The instruction selection phase of code generation involves:
A) Choosing which variables to put in registers.
B) Mapping intermediate code operations to target machine instructions.
C) Optimizing loops.
D) Generating the symbol table.
* Question: What is a flow graph (or control flow graph)?
A) A graph representing data dependencies.
B) A graph representing the sequence of basic blocks and their possible
transfers of control.
C) A graph representing the call hierarchy of functions.
D) A graph representing the memory layout.
* Question: Data flow analysis is used in optimization to:
A) Determine the type of variables.
B) Gather information about the possible values of variables at different points
in the program.
C) Generate intermediate code.
D) Detect syntax errors.
* Question: What is the goal of global optimization?
A) Optimizing within a single basic block.
B) Optimizing across basic blocks or the entire program.
C) Optimizing only loops.
D) Optimizing only register usage.
* Question: Code generation from DAGs for basic blocks helps in:
A) Producing optimal code for the basic block, minimizing common subexpressions
and register usage.
B) Generating three-address code.
C) Performing type checking.
D) Handling function calls efficiently.
* Question: The register descriptor in a code generator tracks:
A) The type of values stored in registers.
B) Which variables are currently stored in each register.
C) The total number of available registers.
D) The memory addresses of registers.
* Question: The address descriptor in a code generator tracks:
A) The memory addresses of instructions.
B) The location(s) where the current value of a name can be found (register,
memory, etc.).
C) The address of the next instruction.
D) The base address of arrays.
* Question: What is the primary challenge in register allocation and assignment?
A) Deciding the order of instructions.
B) Minimizing memory access by effectively utilizing a limited number of
registers.
C) Generating correct machine code.
D) Handling floating-point numbers.
* Question: Loop unrolling is an optimization technique that:
A) Converts a loop into a recursive function.
B) Replicates the body of a loop multiple times to reduce loop overhead.
C) Moves invariant code out of loops.
D) Splits a large loop into smaller ones.
* Question: Which of the following is a machine-independent optimization?
A) Peephole optimization.
B) Register allocation.
C) Common subexpression elimination.
D) Instruction scheduling.
* Question: The code generation phase is often considered the most complex phase
of a compiler due to:
A) Its interaction with the lexical analyzer.
B) The need to map high-level constructs to specific machine instructions and
manage resources like registers.
C) Its role in error detection.
D) Its simplicity.
* Question: What is the purpose of control flow analysis in optimization?
A) To analyze data dependencies.
B) To determine the possible execution paths of a program.
C) To identify common subexpressions.
D) To allocate registers.
* Question: Code generation from DAGs for a basic block ensures that:
A) All common subexpressions are computed only once.
B) All dead code is eliminated.
C) The minimum number of registers is used.
D) All of the above (within the basic block).
* Question: A simple code generator typically translates each three-address code
instruction into:
A) A single machine instruction.
B) A sequence of machine instructions.
C) An optimized block of code.
D) An assembly language program.
* Question: Which type of optimization focuses on improving the performance of
loops, which are critical for many programs?
A) Peephole optimization.
B) Local optimization.
C) Loop optimization.
D) Global optimization.
* Question: Instruction scheduling is a code optimization technique that:
A) Determines the order of instructions to avoid pipeline stalls and maximize
parallelism.
B) Selects the best machine instructions for an operation.
C) Allocates registers.
D) Removes redundant instructions.
* Question: The principle of optimality in code generation states that:
A) The optimal sequence of instructions for a program is the one that uses the
fewest registers.
B) The optimal code sequence for a program is the one that is shortest.
C) An optimal sequence of instructions for a program can be generated by
generating optimal sequences for its sub-expressions.
D) The code generator should always produce the fastest code.
* Question: Code generation from DAGs can help in identifying and eliminating:
A) Type errors.
B) Syntax errors.
C) Redundant loads and stores, and common subexpressions.
D) Infinite loops.
* Question: What is the primary input to the code generation phase?
A) Source code.
B) Abstract Syntax Tree (AST) or Three-Address Code (TAC).
C) Symbol Table.
D) Regular Expressions.
* Question: Global common subexpression elimination extends the optimization
beyond:
A) A single function.
B) A single basic block.
C) A single instruction.
D) A single loop.
* Question: Copy propagation is an optimization that:
A) Replaces a variable with its value if the value is a constant.
B) Replaces a variable with another variable if they hold the same value.
C) Copies entire blocks of code.
D) Propagates errors.
* Question: The register allocation graph (or interference graph) is used in:
A) Data flow analysis.
B) Register allocation.
C) Control flow analysis.
D) Peephole optimization.
* Question: What is the purpose of live variable analysis in code optimization?
A) To identify variables that are currently in use.
B) To identify variables whose values might be used in the future.
C) To identify dead code.
D) To determine the scope of variables.
* Question: Code generation is the final phase of the compiler that produces:
A) Intermediate code.
B) Optimized source code.
C) Target machine code (e.g., assembly or machine language).
D) A symbol table.
* Question: Which optimization technique involves replacing an expensive operation
(e.g., multiplication) with a cheaper one (e.g., addition or shift) within a loop?
A) Loop unrolling.
B) Loop invariant code motion.
C) Strength reduction.
D) Dead code elimination.
* Question: The DAG for a basic block a = b + c; b = a + d; would show:
A) Two separate nodes for a.
B) b as a common subexpression.
C) A dependency where the second a uses the result of the first a.
D) A cycle.
* Question: What is the primary challenge for a simple code generator when dealing
with limited registers?
A) It cannot handle arithmetic operations.
B) It might generate inefficient code that frequently loads from and stores to
memory (spilling).
C) It cannot generate correct code.
D) It cannot handle function calls.
* Question: Code generation often involves making decisions based on the cost of
instructions. This cost is determined by:
A) The number of lines of code.
B) The execution time and memory usage of the instruction on the target machine.
C) The number of operands.
D) The type of the operation.
* Question: Which of the following is a type of local optimization?
A) Interprocedural optimization.
B) Loop optimization.
C) Peephole optimization.
D) Global optimization.
* Question: The machine model for code generation includes details about the
target machine's:
A) CPU architecture (e.g., RISC vs. CISC).
B) Register set and their types (e.g., general-purpose, floating-point).
C) Memory addressing modes.
D) All of the above.
* Question: What is the purpose of code scheduling in the code generation phase?
A) To determine which instructions to execute first.
B) To reorder instructions to minimize execution time by avoiding pipeline
stalls and maximizing instruction-level parallelism.
C) To schedule the compiler's execution.
D) To schedule memory access.
* Question: Graph coloring is a common algorithm used in:
A) Lexical analysis.
B) Syntax analysis.
C) Register allocation.
D) Semantic analysis.
* Question: The DAG representation of a basic block can help in identifying dead
code if a node in the DAG has:
A) No incoming edges.
B) No outgoing edges (and its value is not used outside the block).
C) Multiple incoming edges.
D) A cycle.
* Question: Code optimization can be performed on which intermediate
representation?
A) Source code.
B) Abstract Syntax Tree (AST).
C) Three-Address Code (TAC).
D) Both B and C.
* Question: Which of the following is an example of a machine-specific
optimization that a code generator might perform?
A) Loop unrolling.
B) Using specialized instructions (e.g., for bit manipulation, string
operations) if available on the target architecture.
C) Dead code elimination.
D) Constant folding.
* Question: The simple code generator often uses a code generation algorithm that
processes the intermediate code:
A) Randomly.
B) In a single pass, typically from left to right.
C) In multiple passes for extensive optimization.
D) By backtracking.
* Question: What is the primary benefit of register allocation?
A) Reduces the number of instructions.
B) Reduces memory access, leading to faster execution.
C) Simplifies the compiler's front end.
D) Improves code readability.
* Question: The control flow graph is essential for which type of optimization?
A) Peephole optimization.
B) Local optimization within basic blocks.
C) Global (inter-block and inter-procedural) optimization.
D) Register allocation within a basic block.
* Question: Which of the following is a typical peephole optimization?
A) Removing redundant loads/stores (e.g., MOV R1, M; MOV M, R1).
B) Moving invariant code out of loops.
C) Reordering instructions across basic blocks.
D) Converting x = y + z to x = y + t1 where t1 is z.
Solutions: Compiler Design - Code Optimization & Generation
* C) Improve the target code's performance (speed, size, power consumption).
* D) Code Generation
* B) A sequence of consecutive statements in which flow of control enters at the
beginning and leaves at the end without halt or possibility of branching except at
the end.
* B) Moves computations that produce the same result in every iteration of a loop
to outside the loop.
* B) Identifying common subexpressions and dead code within the block.
* B) Replacing multiple computations of the same expression with a single
computation and reusing its result.
* B) A small, fixed-size window of target instructions.
* B) The target hardware's instruction set, register organization, and memory
hierarchy.
* B) Deciding which program variables should reside in machine registers.
* B) Assigning specific physical registers to the chosen program variables.
* A) Removing code that is unreachable or whose results are never used.
* C) Intermediate Code Generator (and Optimizer)
* B) It aims for correctness over optimality, generating code directly from
intermediate representation.
* B) Depth-First Search (DFS) in post-order.
* B) Replacing an expensive operation with an equivalent cheaper one (e.g.,
multiplication by addition).
* B) Evaluates constant expressions at compile time and replaces them with their
results.
* A) When a register value is moved to memory because there are not enough
registers to hold all active variables.
* B) It is machine-dependent and produces target code.
* B) To identify maximal sequences of straight-line code for local optimization.
* B) Moves computations to a place where they are executed less frequently.
* B) Peephole optimization.
* B) Mapping intermediate code operations to target machine instructions.
* B) A graph representing the sequence of basic blocks and their possible
transfers of control.
* B) Gather information about the possible values of variables at different points
in the program.
* B) Optimizing across basic blocks or the entire program.
* A) Producing optimal code for the basic block, minimizing common subexpressions
and register usage.
* B) Which variables are currently stored in each register.
* B) The location(s) where the current value of a name can be found (register,
memory, etc.).
* B) Minimizing memory access by effectively utilizing a limited number of
registers.
* B) Replicates the body of a loop multiple times to reduce loop overhead.
* C) Common subexpression elimination.
* B) The need to map high-level constructs to specific machine instructions and
manage resources like registers.
* B) To determine the possible execution paths of a program.
* D) All of the above (within the basic block).
* B) A sequence of machine instructions.
* C) Loop optimization.
* A) Determines the order of instructions to avoid pipeline stalls and maximize
parallelism.
* C) An optimal sequence of instructions for a program can be generated by
generating optimal sequences for its sub-expressions.
* C) Redundant loads and stores, and common subexpressions.
* B) Abstract Syntax Tree (AST) or Three-Address Code (TAC).
* B) A single basic block.
* B) Replaces a variable with another variable if they hold the same value.
* B) Register allocation.
* B) To identify variables whose values might be used in the future.
* C) Target machine code (e.g., assembly or machine language).
* C) Strength reduction.
* C) A dependency where the second a uses the result of the first a.
* B) It might generate inefficient code that frequently loads from and stores to
memory (spilling).
* B) The execution time and memory usage of the instruction on the target machine.
* C) Peephole optimization.
* D) All of the above.
* B) To reorder instructions to minimize execution time by avoiding pipeline
stalls and maximizing instruction-level parallelism.
* C) Register allocation.
* B) No outgoing edges (and its value is not used outside the block).
* D) Both B and C.
* B) Using specialized instructions (e.g., for bit manipulation, string
operations) if available on the target architecture.
* B) In a single pass, typically from left to right.
* B) Reduces memory access, leading to faster execution.
* C) Global (inter-block and inter-procedural) optimization.
* A) Removing redundant loads/stores (e.g., MOV R1, M; MOV M, R1).