UNKNOWN = 0
NTERM = 1
TOKEN = 2
ALIAS = -9991

class Symbol:
    def __init__(self, tag, ident):
        self.tag = tag
        self.value = 0
        self.prec = 0
        self.assoc = 0
        self.user_token_number = 0
        self.alias = None    # points to another symbol
        self.ident = ident
        return

class SymbolTable:
    def __init__(self):
        self.tags = {}
        self.symbols = []
        
    def generate(self, key):
        sym = self.tags.get(key)
        if sym is None:
            sym = Symbol(key, UNKNOWN)
            self.tags[key] = sym
            self.symbols.append(sym)
            if key[0] in ['"', '\'']:
                if key[0] != key[-1]:
                    raise SystemExit('malformed string: %s' % key)
                if key[0] == "'":
                    if len(key) > 3:
                    # Strictly for compatibility for bison
                        raise SystemExit('malformed character: %s' % key)
                    sym.user_token_number = ord(key[1])
                sym.ident = TOKEN
        return sym
