Cs606 Objectives
Cs606 Objectives
1. According to Reason #1, understanding compilers and languages helps you become a better programmer by understanding:
a) Data structures and algorithms
b) Application functionality
c) Code structure, language semantics, and relation between source and machine code
d) Optimizing for human readability
Answer: c)
2. Reason #2 highlights that building a compiler provides an opportunity to:
a) Learn new mathematical models
b) Practice graph algorithms
c) Put theoretical knowledge of languages and grammar into practice
d) Study regular expressions and automata
Answer: c)
3. Creating a compiler, as described in Reason #3, involves writing a large computer program that manipulates:
a) Simple data structures
b) Complex data structures and sophisticated algorithms
c) Only integer variables
d) Named constructs like procedures
Answer: b)
4. The programming languages mentioned that one will learn more about during compiler creation are:
a) Java and Python
b) C++ and Intel x86 assembly language
c) C# and SQL
d) HTML and CSS
Answer: b)
5. According to the text, a "compiler" translates information from:
a) One human language to another (e.g., Russian to English)
b) A computer program in one language to another representation
c) A Word file to PDF
d) PDF to Postscript
Answer: b)
6. Which of the following is not considered a "Typical Compiler Example" in the provided text?
a) C++ Builder
b) Microsoft Visual Studio Express
c) VBCC
d) Application converting Word to PDF
Answer: d)
7. In the context of this course, "typical compilation" refers to translation from:
a) Human language to human language
b) High-level languages to low-level object code and machine code
c) One document format to another
d) Assembly code to C code
Answer: b)
8. The C function expr(int n) is optimized for:
a) Hardware it is to run on
b) Machine instructions
c) Human readability and comprehension
d) Unnamed memory locations
Answer: c)
9. The C function expr(int n) uses named constructs such as:
a) Registers and unnamed memory locations
b) Machine instructions and assembly directives
c) Variables and procedures
d) pushl and movl
Answer: c)
10. The assembly code generated by the C compiler gcc for the Intel platform is optimized for:
a) Human readability
b) Human comprehension
c) The hardware it is to run on
d) Grammar of a programming language
Answer: c)
11. Compared to C code, assembly code is generally:
a) Easier to understand by humans
b) Optimized for human readability
c) Much harder to understand by humans
d) Consists of higher-level constructs
Answer: c)
12. A key "issue in compilation" is that the translation of code from human-readable form to machine code must be:
a) Unique
b) "Ideal"
c) "Correct," executing precisely the same computation
d) Human-understandable
Answer: c)
13. According to the text, does an algorithm exist for an "ideal translation" from source to destination language?
a) Yes, always
b) Only for simple programs
c) No
d) Sometimes, depending on the language
Answer: c)
14. To manage the complex process of translation, given the differences between source language and generated code, translation is
carried out in:
a) A single pass
b) Multiple passes
c) Only two passes
d) A random number of passes
Answer: b)
15. What is the primary purpose of Reason #1 for studying compilers?
a) To learn new programming languages
b) To improve debugging skills
c) To become a better programmer by understanding underlying language mechanics
d) To optimize code for faster execution
Answer: c)
16. Which of the following is a key takeaway from Reason #3 regarding programming experience?
a) Compiler creation is only useful for C++ programmers.
b) The experience gained is transferable to other languages and architectures.
c) It primarily teaches basic data structures.
d) It focuses on theoretical algorithms only.
Answer: b)
17. The text states that applications converting a Word file to PDF would be called:
a) Compilers
b) Interpreters
c) Translators
d) Debuggers
Answer: c)
18. What does the assembly code for the expr function primarily use for computation?
a) Named variables like d and n
b) Human-readable expressions
c) Registers and unnamed memory locations
d) High-level language constructs
Answer: c)
19. Why is the translation process from source language to machine code considered complex?
a) The source language and generated code are identical.
b) There is always a unique and ideal translation.
c) The source language and generated code are very different.
d) It only involves simple data manipulations.
Answer: c)
20. What is the final operation performed by the _expr assembly function before returning?
a) imull %eax,%edx
b) movl %edx,%eax
c) leave followed by ret
d) pushl %ebp
Answer: c)
21. The phrase "optimized for human readability and comprehension" best describes:
a) Intel x86 assembly code
b) The C function expr
c) Machine code
d) Unnamed memory locations
Answer: b)
LECTURE # 2
Lecture 4
1. Which architecture made the job of the compiler harder when it came to generating efficient machine code due to its rich set of
instructions and addressing modes?
a) RISC
b) CISC
c) ARM
d) MIPS
Answer: b) CISC
2. Why are registers important in a CPU for providing high-speed access to operands?
a) They are larger than memory.
b) Memory access is slower than register access.
c) They are used for long-term storage.
d) They hold program instructions.
Answer: b) Memory access is slower than register access.
3. The problem of optimal register allocation is classified as:
a) P-Complete
b) NP-Hard
c) NP-Complete
d) Solvable in polynomial time
Answer: c) NP-Complete
4. What is the primary goal of instruction scheduling in modern processors?
a) To increase memory usage.
b) To avoid hardware stalls and interlocks.
c) To reduce the number of functional units.
d) To simplify compiler design.
Answer: b) To avoid hardware stalls and interlocks.
5. Optimal instruction scheduling is NP-Complete in:
a) Only specific cases.
b) Nearly all cases.
c) Only for single-processor systems.
d) Only for RISC architectures.
Answer: b) Nearly all cases.
6. How many stages do most modern compilers contain?
a) One
b) Two
c) Three
d) Four
Answer: c) Three
7. What is the intermediate stage in a three-pass compiler primarily used for?
a) Lexical analysis
b) Code generation
c) Code improvement or optimization
d) Syntax analysis
Answer: c) Code improvement or optimization
8. What is the middle end of a compiler generally termed?
a) The Front-end
b) The Back-end
c) The Optimizer
d) The Linker
Answer: c) The Optimizer
9. Which of the following is NOT a typical transformation performed by the optimizer?
a) Discover & propagate some constant value.
b) Generate machine code.
c) Remove useless or unreachable code.
d) Move a computation to a less frequently executed place.
Answer: b) Generate machine code.
10. The primary goal of the middle end (optimizer) is to reduce:
a) Compile time.
b) Running time of the compiled code.
c) Debugging time.
d) Source code size.
Answer: b) Running time of the compiled code.
11. What additional improvements may the middle end achieve besides reducing running time?
a) Debugging information and error reporting.
b) Space usage and power consumption.
c) User interface design.
d) Network communication protocols.
Answer: b) Space usage and power consumption.
12. The executable code typically runs as a process in what environment?
a) An Integrated Development Environment (IDE).
b) A virtual machine.
c) An Operating System Environment.
d) A hardware emulator.
Answer: c) An Operating System Environment.
13. What resources might an application need from the OS during runtime?
a) Compiler source code.
b) Dynamic memory allocation and input output.
c) Hardware schematics.
d) Assembly language instructions.
Answer: b) Dynamic memory allocation and input output.
14. Compilers need intimate knowledge of the runtime system to make effective use of:
a) User preferences and settings.
b) Runtime environment and machine resources.
c) Network topology.
d) Database schemas.
Answer: b) Runtime environment and machine resources.
15. Which of the following is an issue in the context of the role of the run-time system?
a) Syntax checking.
b) Memory management.
c) Semantic analysis.
d) Lexical analysis.
Answer: b) Memory management.
16. What is a typical run-time system issue related to memory?
a) Stack overflow detection.
b) Garbage collection.
c) Register overflow.
d) Cache invalidation.
Answer: b) Garbage collection.
17. If the underlying architecture has multiple processors, what might an application want to do?
a) Use only one processor.
b) Use them for parallel execution.
c) Disable them.
d) Allocate them to the OS only.
Answer: b) Use them for parallel execution.
18. What is a key aspect of support for parallelism in the context of run-time systems?
a) Sequential execution.
b) Parallel threads.
c) Single-core processing.
d) Compiler optimization techniques.
Answer: b) Parallel threads.
19. What is necessary for processes that communicate with each other and share resources?
a) Debugging tools.
b) Communication and synchronization mechanisms.
c) Code obfuscation.
d) Source code encryption.
Answer: b) Communication and synchronization mechanisms.
20. What is an example of a "specialized use" for which some registers are pre-allocated and thus not available to the back end for
general use?
a) Storing temporary computation results.
b) Holding constant values.
c) Program counter.
d) Function arguments.
Answer: c) Program counter.
Lecture 5
1. Which component is the first part of the front-end of a compiler?
a) Parser
b) IR
c) Scanner
d) Code Generator
Answer: c) Scanner
2. What is the main task of the scanner?
a) To generate intermediate representation.
b) To break a program into a stream of tokens.
c) To perform syntax analysis.
d) To optimize the code.
Answer: b) To break a program into a stream of tokens.
3. The activity of breaking a stream of characters into a stream of tokens is called:
a) Syntax analysis
b) Semantic analysis
c) Lexical analysis
d) Code generation
Answer: c) Lexical analysis
4. A token contains more than just the words extracted from the input; it also includes their:
a) Length
b) Memory address
c) Role or syntactic category
d) ASCII value
Answer: c) Role or syntactic category
5. In the C language sentence if(b == 0) a = b, what is "if" classified as?
a) Variable
b) Identifier
c) Keyword
d) Symbol
Answer: c) Keyword
6. In the C language sentence if(b == 0) a = b, what is "==" classified as?
a) Assignment operator
b) Boolean operator
c) Identifier
d) Keyword
Answer: b) Boolean operator
7. Which of the following is an example of an Identifier token?
a) if
b) 2.0
c) maxsize
d) {
Answer: c) maxsize
8. Which of the following is an example of a Keyword token?
a) 1000
b) while
c) y11
d) “error”
Answer: b) while
9. Which of the following is an example of a Symbol token?
a) 2
b) else
c) “enter x”
d) *
Answer: d) *
10. What is the approach called when a scanner is written by hand-coding?
a) Automated Lexer
b) Lexer Generator
c) Ad-hoc Lexer
d) Finite Automata Lexer
Answer: c) Ad-hoc Lexer
11. When hand-writing a scanner, what technique is often needed to decide where one token ends and the next begins?
a) Backtracking
b) Look-ahead
c) Recursion
d) Memoization
Answer: b) Look-ahead
12. In the provided C++ Lexer template, what is the purpose of the next character?
a) To store the entire input stream.
b) To store the previously read character.
c) To act as a look-ahead character.
d) To indicate the end of the file.
Answer: c) To act as a look-ahead character.
13. The idChar function in the Lexer template checks if a character is suitable for an identifier. Which of the following characters
does it allow?
a) @
b) $
c) _
d) %
Answer: c) _
14. What is a problem encountered when hand-writing a lexer, as mentioned in the lecture, regarding the first character?
a) Not knowing the token type from the first character.
b) Difficulty in reading the first character.
c) The first character is always a number.
d) The first character is always a symbol.
Answer: a) Not knowing the token type from the first character.
15. If a token begins with "i", what ambiguity might arise in a hand-written lexer?
a) Is it a symbol or an integer?
b) Is it a float or a string?
c) Is it an identifier "i" or keyword "if"?
d) Is it a comment or a variable?
Answer: c) Is it an identifier "i" or keyword "if"?
16. If a token begins with "=", what ambiguity might arise in a hand-written lexer?
a) Is it an identifier or a keyword?
b) Is it "=" or "=="?
c) Is it a float or an integer?
d) Is it a string or a comment?
Answer: b) Is it "=" or "=="?
17. What is the most frequently used approach for generating efficient tokenizers automatically?
a) Manual coding for each token.
b) Using a lexer generator.
c) Applying neural networks.
d) Employing cryptographic algorithms.
Answer: b) Using a lexer generator.
18. What is the output of the scanner that serves as input for the parser?
a) Source code
b) Errors
c) Stream of tokens
d) Intermediate Representation (IR)
Answer: c) Stream of tokens
19. In the example if(b == 0) a = b, how many distinct tokens are there?
a) 5
b) 7
c) 9
d) 11
Answer: c) 9 (if, (, b, ==, 0, ), a, =, b)
20. What is the role of blanks between words in a natural language sentence, similar to how spaces are treated in programming
languages by the scanner?
a) They are classified as keywords.
b) They are ignored.
c) They are considered as tokens.
d) They indicate an error.
Answer: b) They are ignored.
Lecture 6
1. What is the most popular method for specifying tokens in lexical analysis?
a) Context-Free Grammars
b) Regular Languages
c) Turing Machines
d) Natural Language Processing
Answer: b) Regular Languages
2. Which of the following is NOT a reason why Regular Languages are popular for specifying tokens?
a) They are based on simple and useful theory.
b) They require complex algorithms for implementation.
c) They are easy to understand.
d) Efficient implementations exist for generating lexical analyzers.
Answer: b) They require complex algorithms for implementation.
3. In the context of languages, what is ∑ (Sigma) defined as?
a) A set of strings.
b) An alphabet (a set of characters).
c) A regular expression.
d) A finite automaton.
Answer: b) An alphabet (a set of characters).
4. A language over ∑ is a set of:
a) Characters drawn from ∑.
b) Strings of characters drawn from ∑.
c) Regular expressions.
d) Finite automata.
Answer: b) Strings of characters drawn from ∑.
5. For lexical analysis, what type of languages are compilers interested in?
a) Context-sensitive languages
b) Recursive languages
c) Regular languages
d) Natural languages
Answer: c) Regular languages
6. How are regular languages typically described?
a) Using parse trees.
b) Using regular expressions.
c) Using semantic rules.
d) Using syntax diagrams.
Answer: b) Using regular expressions.
7. In regular expressions, what does R∣S signify?
a) Concatenation of R and S.
b) Either R or S.
c) Zero or more occurrences of R.
d) One or more occurrences of S.
Answer: b) Either R or S.
8. What does the regular expression R∗ denote?
a) Zero or one R.
b) One or more R's.
c) Concatenation of R zero or more times.
d) Exactly R.
Answer: c) Concatenation of R zero or more times.
9. Which regular expression extension means "zero or one R"?
a) R∗
b) R+
c) R?
d) (R)
Answer: c) R?
10. What does [a−z] represent in a regular expression?
a) Concatenation of 'a' and 'z'.
b) Any character from 'a' to 'z'.
c) The literal string "a-z".
d) A range from 'a' to 'z' inclusive.
Answer: d) A range from 'a' to 'z' inclusive.
11. What language is denoted by the regular expression (ab)∗?
a) "ab" "abab" "ababab" ...
b) "" "ab" "abab" ... (including empty string)
c) "a" "b" "ab"
d) "a" or "b" any number of times.
Answer: b) "" "ab" "abab" ... (including empty string)
12. What language is denoted by the regular expression (a∣ϵ)b?
a) "a" "b"
b) "ab" "b"
c) "a" "ab"
d) "ab"
Answer: b) "ab" "b"
13. Which regular expression correctly describes an integer token as defined in the lecture?
a) digit+
b) digit digit*
c) digit*
d) digit?
Answer: b) digit digit*
14. What is the regular expression for an identifier as given in the lecture?
a) [a-zA-Z0-9_]*
b) [a-zA-Z_][a-zA-Z0-9_]
c) [a-zA-Z_][a-zA-Z0-9_]*
d) [a-zA-Z_]+
Answer: c) [a-zA-Z_][a-zA-Z0-9_]*
15. What mechanism is used to determine if an input string belongs to the language denoted by a regular expression?
a) A parser.
b) An optimizer.
c) An acceptor.
d) A code generator.
Answer: c) An acceptor.
16. Which of the following is NOT a component of a Finite Automaton (FA)?
a) An output alphabet.
b) An input alphabet ∑.
c) A set of states.
d) A set of accepting (final) states.
Answer: a) An output alphabet.
17. How does a finite automaton accept a string?
a) By halting at the start state.
b) By following transitions labeled with characters in the string from a start state to some accepting state.
c) By generating an error.
d) By transforming the string.
Answer: b) By following transitions labeled with characters in the string from a start state to some accepting state.
18. In a Finite Automaton, what are the states from which the automaton begins processing an input string?
a) Final states.
b) Intermediate states.
c) Start (initial) states.
d) Dead states.
Answer: c) Start (initial) states.
19. A Finite Automaton accepts a string if it ends in which type of state?
a) An initial state.
b) A non-accepting state.
c) An error state.
d) An accepting (final) state.
Answer: d) An accepting (final) state.
20. The regular expression R+ is a convenient notation for:
a) ϵ∣R
b) RR∗
c) R∣R
d) RR
Answer: b) RR∗
Lecture 7
1. How can a Finite Automaton (FA) be encoded for simple and efficient implementation?
a) As a flow chart.
b) As a transition table.
c) As a linked list.
d) As an array of characters.
Answer: b) As a transition table.
2. In a transition table, what do the rows typically correspond to?
a) Input characters.
b) States.
c) Output symbols.
d) Accepted strings.
Answer: b) States.
3. What do the cells of a transition table contain?
a) The input character.
b) The previous state.
c) The next state.
d) An error message.
Answer: c) The next state.
4. The C++ code for FA simulation uses trans_table[state][c]. What does c represent here?
a) The current state.
b) An error state.
c) The input character.
d) The accept state.
Answer: c) The input character.
5. What is the strategy for building a lexical analyzer based on regular expressions?
a) Directly implementing REs in code.
b) Building a FA for regular expressions.
c) Converting REs into parse trees.
d) Using a Turing machine.
Answer: b) Building a FA for regular expressions.
6. The actual algorithm for converting Regular Expressions (RE) into Finite Automata (FA) first builds what?
a) Deterministic Finite Automaton (DFA).
b) Non-Deterministic Finite Automaton (NFA).
c) A context-free grammar.
d) A syntax tree.
Answer: b) Non-Deterministic Finite Automaton (NFA).
7. After an NFA is built from an RE, what is the next step in the typical conversion process to get a table-encoded automaton?
a) Convert NFA to a parse tree.
b) Convert NFA to a DFA.
c) Minimize the NFA.
d) Directly encode the NFA into a table.
Answer: b) Convert NFA to a DFA.
8. What characteristic allows an NFA to have multiple transitions for one input in a given state?
a) Determinism.
b) Non-determinism.
c) Strict state management.
d) Limited input alphabet.
Answer: b) Non-determinism.
9. What are e-moves (epsilon-moves) in an NFA?
a) Transitions that consume any input character.
b) Transitions that move to an error state.
c) Transitions that occur without consuming any input.
d) Transitions that signify the end of the input.
Answer: c) Transitions that occur without consuming any input.
10. How is the acceptance of an NFA for a given string achieved?
a) If it consumes all input and ends in any state.
b) If it can get into a final state after consuming the string.
c) If it has no ϵ-moves.
d) If it takes only one path through the state graph.
Answer: b) If it can get into a final state after consuming the string.
11. In Deterministic Finite Automata (DFA), how many transitions are there per input per state?
a) Zero.
b) Multiple.
c) Exactly one.
d) Variable.
Answer: c) Exactly one.
12. Which type of Finite Automata (FA) has no ϵ-moves?
a) NFA.
b) DFA.
c) Both NFA and DFA.
d) Neither NFA nor DFA.
Answer: b) DFA.
13. What is a key advantage of DFAs over NFAs in terms of implementation?
a) DFAs are simpler to construct.
b) DFAs are table-driven and easier to implement.
c) DFAs can be exponentially smaller.
d) DFAs recognize more languages.
Answer: b) DFAs are table-driven and easier to implement.
14. For a given language, which type of automaton can be simpler (have fewer states)?
a) DFA.
b) NFA.
c) Both are always the same complexity.
d) Depends on the specific language.
Answer: b) NFA.
15. What is the name of the algorithm for RE to NFA construction?
a) Hopcroft’s Algorithm.
b) Dijkstra's Algorithm.
c) Thompson’s Construction.
d) Knuth-Morris-Pratt Algorithm.
Answer: c) Thompson’s Construction.
16. What is the procedure used to convert an NFA into a DFA?
a) Minimization.
b) Subset construction.
c) Union operation.
d) Concatenation.
Answer: b) Subset construction.
17. What algorithm is used to minimize the number of states in the resulting DFA?
a) Thompson’s Construction.
b) Hopcroft’s Algorithm.
c) Knuth-Morris-Pratt Algorithm.
d) Lexer Generator Algorithm.
Answer: b) Hopcroft’s Algorithm.
18. When constructing an NFA for RE ab, how are the NFAs for a and b combined?
a) Using a direct transition from the start state to the end state.
b) Using an ϵ-move from the accepting state of a to the start state of b.
c) By taking the union of their states.
d) By merging their initial states.
Answer: b) Using an ϵ-move from the accepting state of a to the start state of b.
19. In the context of RE to NFA construction, NFAs are typically combined using what type of transitions?
a) Direct transitions.
b) Input-consuming transitions.
c) ϵ-moves.
d) Backtracking transitions.
Answer: c) ϵ-moves.
20. What was the publication year for Thompson's Construction algorithm in CACM?
a) 1958
b) 1968
c) 1978
d) 1988
Answer: b) 1968
Lecture 8
1. What is the algorithm for NFA to DFA construction called?
a) Hopcroft's Algorithm
b) Thompson's Construction
c) Subset Construction
d) Dijkstra's Algorithm
Answer: c) Subset Construction
2. In the transition table of an NFA, each entry is a:
a) Single state.
b) Set of states.
c) Boolean value.
d) Character.
Answer: b) Set of states.
3. In the transition table of a DFA, each entry is a:
a) Single state.
b) Set of states.
c) Boolean value.
d) Character.
Answer: a) Single state.
4. What does each DFA state correspond to in the NFA-to-DFA construction?
a) A single NFA state.
b) A set of NFA states.
c) An input symbol.
d) A final state.
Answer: b) A set of NFA states.
5. What is ε-closure(T) defined as?
a) The set of NFA states reachable from a start state by input a.
b) The set of NFA states reachable from some NFA state s in T on ϵ-transitions alone.
c) The set of NFA states reachable from T by any input.
d) The set of all NFA states.
Answer: b) The set of NFA states reachable from some NFA state s in T on ϵ-transitions alone.
6. What does move(T,a) represent?
a) The set of NFA states reachable from T by ϵ-transitions.
b) The set of NFA states reachable from the start state on input a.
c) The set of NFA states to which there is a transition on input a from some NFA state s in T.
d) The final state of the DFA.
Answer: c) The set of NFA states to which there is a transition on input a from some NFA state s in T.
7. Before seeing the first input symbol, an NFA can be in any state within which set?
a) move(T,a)
b) ε-closure(s0)
c) F (final states)
d) S (set of NFA states)
Answer: b) ε-closure(s0)
8. After seeing input a and accounting for ϵ-transitions, an NFA can be in any of the states in which set?
a) move(T,a)
b) ε-closure(move(T,a))
c) ε-closure(T)
d) T
Answer: b) ε-closure(move(T,a))
9. What is the initial state of the DFA (s0') in the subset construction algorithm?
a) The start state of the NFA (s0).
b) The set of all NFA final states.
c) ε-closure(s0).
d) An empty set.
Answer: c) ε-closure(s0).
10. In the subset construction algorithm, what happens if a newly computed set U is not already in the DFA states S'?
a) It is discarded.
b) It is added to S' as an unmarked state.
c) It becomes the new start state.
d) The algorithm terminates.
Answer: b) It is added to S' as an unmarked state.
11. How are the final states (F') of the DFA determined in the subset construction algorithm?
a) Any DFA state is a final state.
b) If a DFA state S contains an NFA start state.
c) If a DFA state S contains an NFA final state.
d) Only the initial DFA state is a final state.
Answer: c) If a DFA state S contains an NFA final state.
12. For the NFA for (a | b )*abb, the start state of the equivalent DFA (A) is ε-closure(0). What is the resulting set of states for A?
a) {0}
b) {0,1,2,4,7}
c) {1,2,3,4,6,7,8}
d) {1,2,4,5,6,7}
Answer: b) {0,1,2,4,7}
13. When computing Dtran[A,a] for the example NFA (a | b )*abb, move(A,a) results in which NFA states?
a) {0,1}
b) {2,7}
c) {3,8}
d) {5}
Answer: c) {3,8}
14. For the example NFA (a | b )*abb, Dtran[A,a] is the set B. What are the states in set B?
a) {0,1,2,4,7}
b) {1,2,3,4,6,7,8}
c) {1,2,4,5,6,7}
d) {1,2,4,5,6,7,9}
Answer: b) {1,2,3,4,6,7,8}
15. For the example NFA (a | b )*abb, Dtran[A,b] is the set C. What NFA state does the move on 'b' from a state in A (specifically
state 4) lead to before ϵ-closure?
a) State 0
b) State 3
c) State 5
d) State 8
Answer: c) State 5
16. What is the maximum number of different subsets of a set of 11 NFA states (in the context of the example) that the algorithm
needs to consider?
a) 11
b) 22
c) 211
d) 11!
Answer: c) 211
17. In the example DFA for (a | b )*abb, which DFA state is the start state?
a) B
b) C
c) D
d) A
Answer: d) A
18. In the example DFA for (a | b )*abb, which DFA state is the accepting state?
a) B
b) C
c) D
d) E
Answer: d) E
19. Why is E the accepting state in the example DFA for (a | b )*abb?
a) Because it is the last state created.
b) Because it contains NFA state 0.
c) Because it contains NFA state 10.
d) Because it is the only state with outgoing transitions.
Answer: c) Because it contains NFA state 10.
20. According to the example transition table, if the DFA is in state C and receives input 'a', what is the next state?
a) A
b) B
c) C
d) D
Answer: b) B
Lecture 9
1. What algorithm is used to minimize DFA states?
a) Thompson's Construction
b) Subset Construction
c) Hopcroft's Algorithm
d) Dijkstra's Algorithm
Answer: c) Hopcroft's Algorithm
2. The core idea behind DFA minimization is to find:
a) Optimal transitions.
b) Groups of equivalent states.
c) Smallest possible alphabet.
d) Shortest input strings.
Answer: b) Groups of equivalent states.
3. In a minimized DFA, how many states are there for each group of equivalent states from the initial DFA?
a) Zero
b) One
c) Multiple
d) It varies
Answer: b) One
4. The optimized acceptor structure starts with converting Regular Expressions (RE) to NFA, then to DFA. What is the final step
before simulation?
a) NFA Minimization
b) RE expansion
c) Minimized DFA
d) Source code generation
Answer: c) Minimized DFA
5. What do lexical analyzers (scanners) return at their output?
a) A parse tree.
b) A sequence of matching tokens (or an error).
c) Intermediate code.
d) Optimized machine code.
Answer: b) A sequence of matching tokens (or an error).
6. When multiple tokens match an input string, which one does the lexical analyzer typically return?
a) The first matching token.
b) The shortest matching token.
c) The longest matching token.
d) A random matching token.
Answer: c) The longest matching token.
7. For cases where there are multiple longest matches (e.g., "==" and "="), what do lexical analyzer generators use to resolve
ambiguity?
a) Backtracking.
b) Rules for assigning priorities.
c) User input.
d) Random selection.
Answer: b) Rules for assigning priorities.
8. Which of the following is a popular lexical analyzer generator that generates output in C or C++?
a) Jlex
b) Bison
c) Flex
d) Yacc
Answer: c) Flex
9. Jflex is a lexical analyzer generator written in which programming language?
a) C++
b) Python
c) Java
d) C#
Answer: c) Java
10. What is the customary file extension for Flex input files?
a) .txt
b) .flex
c) .l
d) .y
Answer: c) .l
11. How many sections does a Flex input specification file consist of, separated by %%?
a) One
b) Two
c) Three
d) Four
Answer: c) Three
12. What content is typically found in the first section of a Flex specification file?
a) Token definitions and actions.
b) User code.
c) C or C++ and flex definitions.
d) Error handling routines.
Answer: c) C or C++ and flex definitions.
13. In the provided lex.l example, what does D [0-9] define?
a) A token named D.
b) A regular expression for any digit.
c) A constant value.
d) An integer variable.
Answer: b) A regular expression for any digit.
14. What does id {L}({L}|{D})* define in the lex.l example?
a) A keyword.
b) A floating-point number.
c) An identifier pattern.
d) A string literal.
Answer: c) An identifier pattern.
15. In the lex.l example, what is the action associated with the token "void"?
a) Print "void".
b) Return TOK_VOID.
c) Go to the next state.
d) Ignore the input.
Answer: b) Return TOK_VOID.
16. What is the purpose of the line [\n]|[\t]|[ ] ; in the lex.l specification?
a) To recognize string literals.
b) To define arithmetic operators.
c) To ignore whitespace characters (newline, tab, space).
d) To return an error if encountered.
Answer: c) To ignore whitespace characters (newline, tab, space).
17. The main.cpp code uses lex.yylex(). What does this function call typically do?
a) Parses the input stream.
b) Generates the next token.
c) Prints the current line.
d) Initializes the lexer.
Answer: b) Generates the next token.
18. What does lex.YYText() typically return in the Flex-generated code?
a) The type of the current token.
b) The lexeme (matched text) of the current token.
c) The line number.
d) An error message.
Answer: b) The lexeme (matched text) of the current token.
19. In the main.cpp example, the loop continues as long as tc is not equal to what value?
a) 1
b) 0
c) -1
d) EOF
Answer: b) 0
20. Which command compiles the Flex-generated C++ code (lex.cpp)?
a) flex lex.l
b) g++ -o lex.exe lex.o main.o
c) g++ -c lex.cpp
d) g++ -c main.cpp
Answer: c) g++ -c lex.cpp
Lecture 10
1. Which module is the second component of the front-end of a compiler?
a) Scanner
b) Code Generator
c) Parser
d) Optimizer
Answer: c) Parser
2. What is the input to the parser?
a) Source code as a stream of characters.
b) Intermediate Representation (IR).
c) A stream of words (tokens).
d) Machine code.
Answer: c) A stream of words (tokens).
3. What is the primary function of the parser?
a) To break input into tokens.
b) To check for grammatical correctness (syntactic well-formedness).
c) To perform code optimization.
d) To allocate registers.
Answer: b) To check for grammatical correctness (syntactic well-formedness).
4. Besides checking syntactic correctness, what else does the parser guide?
a) Lexical analysis.
b) Context-sensitive ("semantic") analysis.
c) Register allocation.
d) Instruction scheduling.
Answer: b) Context-sensitive ("semantic") analysis.
5. What does the parser typically build for the source program?
a) An assembly file.
b) Machine code.
c) Intermediate Representation (IR).
d) A symbol table.
Answer: c) Intermediate Representation (IR).
6. In the analogy of "He wrote the program", what would "wrote" be classified as in terms of its role?
a) Subject
b) Object
c) Verb
d) Noun
Answer: c) Verb
7. What does the parser ensure about sentences of a programming language?
a) They are optimized for speed.
b) They abide by the syntax of the language.
c) They are short and concise.
d) They use specific variable names.
Answer: b) They abide by the syntax of the language.
8. If there are errors in the program's syntax, what will the parser do?
a) Ignore them.
b) Automatically fix them.
c) Detect and report them.
d) Pass them to the linker.
Answer: c) Detect and report them.
9. Consider the code segment: int* foo(int i, int j)). What kind of error is )) after j?
a) Lexical error.
b) Semantic error.
c) Syntax error.
d) Runtime error.
Answer: c) Syntax error.
10. What kind of errors is a scanner (based on regular expressions) not able to detect?
a) Lexical errors.
b) Typos in variable names.
c) Syntax errors.
d) Correctly formed tokens.
Answer: c) Syntax errors.
11. What is the output of the scanner that acts as input for the parser?
a) Source code.
b) Machine code.
c) Tokens.
d) Errors.
Answer: c) Tokens.
12. The phrase "syntactically well formed" refers to the correctness of the input's:
a) Meaning.
b) Grammar.
c) Variable types.
d) Memory usage.
Answer: b) Grammar.
13. Which part of the compiler front-end is responsible for type checking?
a) Scanner.
b) Parser.
c) Code Generator.
d) Linker.
Answer: b) Parser (as it guides context-sensitive/semantic analysis).
14. In the example if ( b <= 0 ) a = b, what is b <= 0 identified as by the parser's understanding of syntax?
a) An assignment.
b) An identifier.
c) A boolean expression.
d) A keyword.
Answer: c) A boolean expression.
15. What is the fundamental difference in the type of errors detected by a scanner versus a parser?
a) Scanners find logic errors, parsers find runtime errors.
b) Scanners find token errors, parsers find structural (grammatical) errors.
c) Scanners find memory errors, parsers find I/O errors.
d) Scanners find semantic errors, parsers find lexical errors.
Answer: b) Scanners find token errors, parsers find structural (grammatical) errors.
16. What does "IR" stand for in the context of a compiler's front-end?
a) Initial Recognition.
b) Intermediate Representation.
c) Instruction Register.
d) Input Reader.
Answer: b) Intermediate Representation.
17. The given for loop in the example code segment for(k=0; i j; ) contains a syntax error. What is missing or incorrect between i and
j?
a) An assignment operator.
b) A comparison operator.
c) A semicolon.
d) A variable declaration.
Answer: b) A comparison operator (e.g., < or >).
18. The fi( i > j ) line in the example code segment illustrates a syntax error related to:
a) A misspelled keyword.
b) Missing parentheses.
c) An incorrect operator.
d) An undeclared variable.
Answer: a) A misspelled keyword (fi instead of if).
19. The components of the front-end shown in the diagram are source code -> scanner -> tokens -> parser -> IR. What are the
outputs if errors are detected?
a) Only errors from the scanner.
b) Only errors from the parser.
c) Errors from both scanner and parser.
d) Errors only if IR cannot be built.
Answer: c) Errors from both scanner and parser.
20. The analogy of "noun verb article noun" for "He wrote the program" relates to what concept in programming language parsing?
a) Lexical analysis.
b) Semantic analysis.
c) Syntactic categories or parts of speech.
d) Code optimization.
Answer: c) Syntactic categories or parts of speech.
Lecture 11
1. What type of error is described in Line 1 (int* foo(int i, int j))) of the example C++ function?
a) Semantic error
b) Lexical error
c) Syntax error
d) Runtime error
Answer: c) Syntax error
2. In the example for(k=0; i j; ), the boolean expression i j is an example of what type of error?
a) Semantic error
b) Lexical error
c) Syntax error
d) Logical error
Answer: c) Syntax error
3. The sentence "He wrote the computer" is given as an example of what kind of error in English?
a) Syntactic error
b) Lexical error
c) Semantic error
d) Grammatical error
Answer: c) Semantic error
4. In the C++ function example, the local variable sum not being declared is a:
a) Syntax error
b) Lexical error
c) Semantic error
d) Runtime error
Answer: c) Semantic error
5. The returned value not matching the function's return type (int* vs. sum) is an example of what type of error?
a) Syntax error
b) Lexical error
c) Semantic error
d) Logical error
Answer: c) Semantic error
6. What is the main role of the parser regarding sequences of tokens?
a) To optimize them for speed.
b) To transform them into machine code.
c) To distinguish between valid and invalid sequences.
d) To execute them immediately.
Answer: c) To distinguish between valid and invalid sequences.
7. What mathematical model is used to represent the syntax of a language for parsing?
a) Finite Automata (FA)
b) Regular Expressions (RE)
c) Grammar (G)
d) Turing Machine
Answer: c) Grammar (G)
8. What type of grammars can represent the syntax of most programming languages?
a) Regular Grammars
b) Context-Free Grammars (CFG)
c) Context-Sensitive Grammars
d) Unrestricted Grammars
Answer: b) Context-Free Grammars (CFG)
9. A Context-Free Grammar (CFG) is defined as a four-tuple. Which of the following is NOT one of its components?
a) S (start symbol)
b) T (set of terminals)
c) L (language generated)
d) P (set of productions)
Answer: c) L (language generated)
10. Why are Regular Expressions (RE) not used to represent the syntax of programming languages?
a) They are too complex to understand.
b) Finite automata (based on REs) cannot remember the number of times a state has been visited.
c) They are not efficient for implementation.
d) They cannot describe simple patterns.
Answer: b) Finite automata (based on REs) cannot remember the number of times a state has been visited.
11. In the SheepNoise CFG, SheepNoise -> SheepNoise baa | baa, what are SheepNoise and baa?
a) SheepNoise is a terminal, baa is a non-terminal.
b) Both are terminals.
c) SheepNoise is a non-terminal, baa is a terminal.
d) Both are start symbols.
Answer: c) SheepNoise is a non-terminal, baa is a terminal.
12. The process of using production rules as rewriting rules to create sentences of a language is called:
a) Tokenization
b) Semantic analysis
c) Derivation
d) Optimization
Answer: c) Derivation
13. What does expr -> expr op expr represent in the grammar for arithmetic expressions?
a) An assignment operation.
b) A recursive definition for an expression involving an operator.
c) A definition of a number.
d) An identifier.
Answer: b) A recursive definition for an expression involving an operator.
14. The notation for describing grammar rules, similar to the one used in Algol-60, was developed by John Backus and adapted by
Peter Naur. What is it called?
a) Context-Free Grammar (CFG)
b) Regular Expression (RE)
c) Backus-Naur Form (BNF)
d) Extended BNF (EBNF)
Answer: c) Backus-Naur Form (BNF)
15. What is the process of discovering a derivation called?
a) Lexical analysis
b) Parsing
c) Code generation
d) Optimization
Answer: b) Parsing
16. Which type of derivation involves replacing the leftmost non-terminal (NT) at each step?
a) Rightmost
b) Arbitrary
c) Leftmost
d) Parallel
Answer: c) Leftmost
17. If two derivations produce different parse trees, what is the implication mentioned in the lecture?
a) They will lead to different token streams.
b) They imply different evaluation orders.
c) They signify a lexical error.
d) They can only happen with regular expressions.
Answer: b) They imply different evaluation orders.
18. In the arithmetic expression grammar, op can be replaced by which symbols?
a) num or id
b) +, -, *, or /
c) expr
d) ( or )
Answer: b) +, -, *, or /
19. The provided derivation for x - 2 * y is an example of which type of derivation?
a) Rightmost
b) Leftmost
c) Bottom-up
d) Random
Answer: b) Leftmost
20. The concept of "meaning" of a sentence in programming languages falls under which analysis phase?
a) Lexical Analysis
b) Syntactic Analysis
c) Semantic Analysis
d) Code Generation
Answer: c) Semantic Analysis