Compilers

Which describes an abstract syntax tree (AST)?

  1. AIt keeps everything from the source, including parentheses and separators, as nodes
  2. BIt omits symbols irrelevant to meaning and expresses operator precedence through its shape
  3. CIt corresponds one-to-one with CPU instructions and can be executed directly
Show answer and explanation

Answer: B. It omits symbols irrelevant to meaning and expresses operator precedence through its shape

Parsing following the grammar rules yields a tree that includes parentheses and separators (a concrete syntax tree), which carries much information later phases do not need. An AST keeps only the operations and statements needed for meaning; in (a + b) * c, for example, the addition node sits under the multiplication node, so the order of evaluation is clear without parentheses. Code corresponding to CPU instructions is generated in later phases.

  • A. Keeping parentheses describes a concrete syntax tree.
  • B. Correct. It drops unneeded symbols and shows precedence through its shape.
  • C. CPU-level code comes from later phases.
Practice this topic Back to topic

More Compilers questions

  1. What is the main advantage of using an intermediate representation (IR), independent of both source language and CPU, inside a compiler?
  2. Which describes a basic block?
  3. In a control-flow graph, what do the nodes and edges represent?
  4. Which is a characteristic of an IR in SSA form?
  5. Converting the code above to SSA form inserts an expression like "x3 = φ(x1, x2)" after the if statement. What does the φ function do?