-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathsymtab.py
executable file
·45 lines (35 loc) · 1.1 KB
/
symtab.py
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
# (C) William W. Cohen and Carnegie Mellon University, 2016
#
# a symbol table
#
import logging
class SymbolTable(object):
"""A symbol table mapping strings to/from integers in the range
1..N inclusive."""
def __init__(self,initSymbols=[]):
self.reservedSymbols = set()
self._symbolList = [None]
self._nextId = 0
self._idDict = {}
for s in initSymbols:
self.insert(s)
def insert(self,symbol):
"""Insert a symbol."""
if symbol not in self._idDict:
self._nextId += 1
self._idDict[symbol] = self._nextId
self._symbolList += [symbol]
def getSymbolList(self):
"""Get an array of all defined symbols."""
return self._symbolList[1:]
def getSymbol(self,id):
return self._symbolList[id]
def hasId(self,symbol):
return symbol in self._idDict
def getId(self,symbol):
"""Get the numeric id, between 1 and N, of a symbol.
"""
self.insert(symbol)
return self._idDict[symbol]
def getMaxId(self):
return self._nextId