From 6bf4b90c90f15f4ab60833bddf5b5756d1a6b1f6 Mon Sep 17 00:00:00 2001 From: Elizabeth Alexander Hunt Date: Thu, 2 Jul 2026 11:55:17 -0700 Subject: Init --- Homework/cs5300/compiler/submit/SymbolTable.java | 63 ++++++++++++++++++++++++ 1 file changed, 63 insertions(+) create mode 100644 Homework/cs5300/compiler/submit/SymbolTable.java (limited to 'Homework/cs5300/compiler/submit/SymbolTable.java') diff --git a/Homework/cs5300/compiler/submit/SymbolTable.java b/Homework/cs5300/compiler/submit/SymbolTable.java new file mode 100644 index 0000000..7ca6b27 --- /dev/null +++ b/Homework/cs5300/compiler/submit/SymbolTable.java @@ -0,0 +1,63 @@ +package submit; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; + +/* + * Code formatter project + * CS 4481 + */ +/** + * + */ +public class SymbolTable { + + private final HashMap table; + private SymbolTable parent; + private final List children; + + public SymbolTable() { + table = new HashMap<>(); + parent = null; + children = new ArrayList<>(); + } + + public void addSymbol(String id, SymbolInfo symbol) { + table.put(id, symbol); + } + + /** + * Returns null if no symbol with that id is in this symbol table or an + * ancestor table. + * + * @param id + * @return + */ + public SymbolInfo find(String id) { + if (table.containsKey(id)) { + return table.get(id); + } + if (parent != null) { + return parent.find(id); + } + return null; + } + + /** + * Returns the new child. + * + * @return + */ + public SymbolTable createChild() { + SymbolTable child = new SymbolTable(); + children.add(child); + child.parent = this; + return child; + } + + public SymbolTable getParent() { + return parent; + } + +} -- cgit v1.3