Compilers
What is the role of lexical analysis in a compiler?
- AChecking, in tree form, whether the program's statements follow the grammar
- BSplitting the characters of source code into tokens such as identifiers and operators
- CChecking that variable types match and that no undefined names are used
Show answer and explanation
Answer: B. Splitting the characters of source code into tokens such as identifiers and operators
Lexical analysis is the first phase of a compiler. It splits, for example, "count = count + 1;" into the tokens identifier count, symbol =, identifier count, symbol +, number 1 and symbol ;. Whitespace and comments are discarded here. Token rules can be written as regular expressions, so a finite automaton processes them quickly. Checking grammar in tree form is the next phase, parsing, and checking types and names is semantic analysis.
- A. Checking grammar in tree form is parsing.
- B. Correct. It splits characters into tokens.
- C. Checking types and names is semantic analysis.