-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCompilerSymbolTable.java
More file actions
44 lines (36 loc) · 1.53 KB
/
CompilerSymbolTable.java
File metadata and controls
44 lines (36 loc) · 1.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import java.util.HashMap;
// 1. First, we need our blueprint for the variable information
class SymbolInfo {
String type;
String scope;
String memoryAddress;
public SymbolInfo(String type, String scope, String memoryAddress) {
this.type = type;
this.scope = scope;
this.memoryAddress = memoryAddress;
}
public String toString() {
return "[Type: " + type + " | Scope: " + scope + " | Address: " + memoryAddress + "]";
}
}
// 2. Now the actual program
public class CompilerSymbolTable {
public static void main(String[] args) {
HashMap<String, SymbolInfo> symbolTable = new HashMap<>();
// 3. The compiler reads: int age = 19;
symbolTable.put("age", new SymbolInfo("int", "global", "0x00A1"));
// 4. The compiler reads a new line: String age = "Daniel";
// It's trying to declare 'age' again!
String newVariableToRead = "age";
// 5. The Hash Table Error Check:
System.out.println("Checking code for errors...\n");
if (symbolTable.containsKey(newVariableToRead)) {
// It instantly knows "age" is already taken!
System.out.println("COMPILER ERROR: Variable '" + newVariableToRead + "' is already defined!");
System.out.println("Existing details: " + symbolTable.get(newVariableToRead));
} else {
symbolTable.put(newVariableToRead, new SymbolInfo("String", "local", "0x00B2"));
System.out.println("Variable saved successfully.");
}
}
}