def from_json(cls, file): with open(file, 'r') as f: spec = json.load(f) terms = spec.get('T') nonterms = spec.get('N') prods = spec.get('P') axiom = spec.get('S') if None in [terms, nonterms, prods, axiom]: raise ValueError('Incompatible specification') terms = set(map(Symbol.new, terms)) nonterms = set(map(Symbol.new, nonterms)) prods = list(map(Production.from_string, prods)) axiom = Symbol.new(axiom) return cls(terms, nonterms, prods, axiom)