Which data structure is used by the compiler for managing variables and their…
2019
Which data structure is used by the compiler for managing variables and their attributes?
- A.
Binary tree
- B.
Link list
- C.
Symbol table
- D.
Parse table
Attempted by 436 students.
Show answer & explanation
Correct answer: C
Answer: Symbol table
Why this is correct: The symbol table is the compiler's central data structure for storing identifiers (variable names, function names, etc.) and their attributes. It provides a mapping from each name to metadata the compiler needs.
Common attributes stored: type information, scope level, memory location or offset, declaration line, and other semantic information.
Core operations supported: insert (on declaration), lookup (on use), and delete (on scope exit if needed).
Typical implementations: hash tables for fast average-case lookup; balanced trees or scoped chaining when ordering or worst-case guarantees are required.
How the compiler uses it across phases:
During parsing and semantic analysis: insert declarations and perform lookups for type checking and scope resolution.
During code generation: use stored memory locations and type/layout information to allocate storage and generate correct addressing.
Why the other choices are not correct:
Binary tree: a possible low-level implementation detail in some cases, but not the conceptual structure used by compilers for managing identifiers and their attributes. The correct conceptual answer is symbol table.
Link list: inefficient for name-based lookup because it requires linear search; not used as the primary structure for symbol management.
Parse table: used by parsers to decide parsing actions based on grammar rules and states; it does not store variable attributes.