Compilers
Which describes an abstract syntax tree (AST)?
- AIt keeps everything from the source, including parentheses and separators, as nodes
- BIt omits symbols irrelevant to meaning and expresses operator precedence through its shape
- 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.