Symbol Tables and Identifiers
What You'll Learn
You'll learn how compilers use symbol tables to track identifiers and distinguish them from reserved keywords during lexical analysis.
The Core Idea
When a lexical analyzer encounters a name like count or while, it needs to answer two questions: Is this a keyword? Or is it an identifier (a user-defined name)?
A symbol table is a data structure—typically a hash table or binary search tree—that stores information about identifiers encountered in your source code. Think of it as a phonebook for your program's names: variable names, function names, class names, etc.
How It Works
-
Keyword Checking First: When the lexer sees a potential identifier, it checks if the lexeme matches a reserved keyword (like
if,while,return). Keywords are usually stored in a separate lookup structure. -
Symbol Table Insertion: If it's not a keyword, the lexer treats it as an identifier and either:
- Finds it in the symbol table (already declared)
- Adds a new entry (first occurrence)
-
Storing Metadata: The symbol table entry typically includes:
- The identifier's name (lexeme)
- Its type (integer, string, etc.)—filled in during later compilation phases
- Scope information
- Memory location
- Other attributes
The Analogy
Imagine a library catalog system. When someone mentions a book title, you first check if it's a special reserved section (like "Reference" or "Archives")—these are your keywords. If not, you look it up in the catalog or create a new card for it—that's your symbol table.
Example Process
Input: "while count < 10"
while→ Check table → Found as keyword → Return WHILE tokencount→ Not a keyword → Look up/insert in symbol table → Return IDENTIFIER token<→ Return LESS_THAN token10→ Return NUMBER token
Key Takeaway: Symbol tables manage identifier information and enable the lexer to distinguish user-defined names from language keywords, forming the foundation for semantic analysis in later compilation phases.