import pegpy #from pegpy.tpeg import ParseTree peg = pegpy.grammar('chibi.tpeg') parser = pegpy.generate(peg) ''' tree = parser('1+2*3') print(repr(tree)) tree = parser('1@2*3') print(repr(tree)) ''' class Expr(object): @classmethod def new(cls, v): if isinstance(v, Expr): return v return Val(v) class Val(Expr): __slots__ = ['value'] def __init__(self, value): self.value = value def __repr__(self): return f'Val({self.value})' def eval(self, env: dict): return self.value
import pegpy parser = pegpy.generate('cj.tpeg') t = parser('赤いボールと緑のたぬき') print(t) print(repr(t)) print('tag', t.tag) print('子ノードの数', len(t)) print('0番目', t[0]) for sub in t: print(sub) def conv(t): if t.tag == 'S': print(t) def Verb(t): #Verbを処理 #処理を書く koinu(t[0]) func = globals() def koinu(t): if t.tag in func: func[t.tag](t) #Verb else: