def prepare_symbols_and_productions(): f = open('grammer.txt', 'r') lines = f.readlines() terminal = False production = False for l in lines: if l.strip() == '*terminals': terminal = True production = False continue if l.strip() == '*productions': terminal = False production = True continue if l.strip() == '*end': break if terminal: TERMINAL_SET.update([l.strip()]) if production: left = l.split('::=')[0].strip() NON_TERMINAL_SET.update([left]) try: right = l.split('::=')[1].strip() if right == '': raise IndexError p = Production(left, right.split(' ')) except IndexError: p = Production(left, ['null']) PRODUCTION_LIST.append(p) for s in TERMINAL_SET: sym = Symbol(s, sym_type='T') SYMBOL_DICT[s] = sym for s in NON_TERMINAL_SET: sym = Symbol(s, sym_type='N') SYMBOL_DICT[s] = sym
def prepare_symbols_and_productions(): f = open("grammer.txt", "r") lines = f.readlines() terminal = False production = False for l in lines: if l.strip() == "*terminals": terminal = True production = False continue if l.strip() == "*productions": terminal = False production = True continue if l.strip() == "*end": break if terminal: TERMINAL_SET.update([l.strip()]) if production: left = l.split("::=")[0].strip() NON_TERMINAL_SET.update([left]) try: right = l.split("::=")[1].strip() if right == "": raise IndexError p = Production(left, right.split(" ")) except IndexError: p = Production(left, ["null"]) PRODUCTION_LIST.append(p) for s in TERMINAL_SET: sym = Symbol(s, sym_type="T") SYMBOL_DICT[s] = sym for s in NON_TERMINAL_SET: sym = Symbol(s, sym_type="N") SYMBOL_DICT[s] = sym
def atom(token): if token == '#t': return True if token == '#f': return False if token.startswith('#\\'): return Char(token) # TODO (RCZ) - Should I cut off the #\ ? try: return ast.literal_eval(token) except ValueError: pass except SyntaxError: pass return Symbol(token)