Lexical Analysis · Compiler Design
8 questions · asked in 7 of 27 years (2000–2026)
MCQ 7 · NAT 11-mark 7 · 2-mark 1
2000Questions per year2026
Questions 1–5 of 8
A lexical analyzer uses the following token definitions
For the string given below,
the number of tokens (excluding ws) that will be produced by the lexical analyzer is ________. (answer in integer)
For the string given below,
the number of tokens (excluding ws) that will be produced by the lexical analyzer is ________. (answer in integer)
Answer: 13
Use maximal munch (take the longest possible match at each point). Split the string at whitespace and look at each group:
The total number of tokens .
Answer: 13
The total number of tokens .
Answer: 13
Which ONE of the following statements is FALSE regarding the symbol table?
Answer: (C)
Let us check each statement about the symbol table:
- It keeps track of the scope of variables — true. This is one of its main jobs.
- It can be built with a BST — true. BSTs (or hash tables) are common ways to build it.
- It is created during lexical analysis — true. Entries are usually first added when identifiers are tokenized.
- "Symbol table is not required after the parsing phase" — FALSE. The symbol table is needed long after parsing. Semantic analysis (type checking), intermediate code generation and code optimization (for example register allocation) all keep using it.
Answer: "Symbol table is not required after the parsing phase" is FALSE.
- It keeps track of the scope of variables — true. This is one of its main jobs.
- It can be built with a BST — true. BSTs (or hash tables) are common ways to build it.
- It is created during lexical analysis — true. Entries are usually first added when identifiers are tokenized.
- "Symbol table is not required after the parsing phase" — FALSE. The symbol table is needed long after parsing. Semantic analysis (type checking), intermediate code generation and code optimization (for example register allocation) all keep using it.
Answer: "Symbol table is not required after the parsing phase" is FALSE.
Consider the following statements.
I. Symbol table is accessed only during lexical analysis and syntax analysis.
II. Compilers for programming languages that support recursion necessarily need heap storage for memory allocation in the run-time environment.
III. Errors violating the condition 'any variable must be declared before its use' are detected during syntax analysis.
Which of the above statements is/are TRUE?
I. Symbol table is accessed only during lexical analysis and syntax analysis.
II. Compilers for programming languages that support recursion necessarily need heap storage for memory allocation in the run-time environment.
III. Errors violating the condition 'any variable must be declared before its use' are detected during syntax analysis.
Which of the above statements is/are TRUE?
Answer: (D)
Let us check each statement:
- I: later phases also use the symbol table, such as semantic analysis and code generation FALSE.
- II: recursion needs a stack of activation records, and not a heap FALSE.
- III: the "declare before use" rule is a context-sensitive check made in semantic analysis FALSE.
Answer: None of I, II and III
- I: later phases also use the symbol table, such as semantic analysis and code generation FALSE.
- II: recursion needs a stack of activation records, and not a heap FALSE.
- III: the "declare before use" rule is a context-sensitive check made in semantic analysis FALSE.
Answer: None of I, II and III
A lexical analyzer uses the following patterns to recognize three tokens over the alphabet {a,b,c}.
Note that 'x?' means 0 or 1 occurrence of the symbol x. Note also that the analyzer outputs the token that matches the longest possible prefix.
If the string bbaacabc is processed by the analyzer, which one of the following is the sequence of tokens it outputs?
Note that 'x?' means 0 or 1 occurrence of the symbol x. Note also that the analyzer outputs the token that matches the longest possible prefix.
If the string bbaacabc is processed by the analyzer, which one of the following is the sequence of tokens it outputs?
Answer: (D)
Use the longest-prefix rule on the string bbaacabc:
- T1 matches "bba" and T2 matches "bb". But T3 = c?(b|a)*c matches "bbaac", which is the longest. So T3
- The string left is "abc". T3 matches "abc", so T3
Answer: T3T3
- T1 matches "bba" and T2 matches "bb". But T3 = c?(b|a)*c matches "bbaac", which is the longest. So T3
- The string left is "abc". T3 matches "abc", so T3
Answer: T3T3
The lexical analysis for a modern computer language such as Java needs the power of which one of the following machine models in a necessary and sufficient sense?
Answer: (A)
Regular expressions can describe tokens such as identifiers, numbers and keywords. So finite automata are all that is needed, and they are also required.
Answer: Finite state automata
Answer: Finite state automata
Keep practising