Compilers

What is the role of lexical analysis in a compiler?

  1. AChecking, in tree form, whether the program's statements follow the grammar
  2. BSplitting the characters of source code into tokens such as identifiers and operators
  3. 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.
Practice this topic Back to topic

More Compilers questions

  1. What is done during syntax analysis (parsing) in a compiler?
  2. Which is the best example of an error found during semantic analysis?
  3. Which describes an abstract syntax tree (AST)?
  4. What is the main advantage of using an intermediate representation (IR), independent of both source language and CPU, inside a compiler?
  5. Which describes a basic block?