def load_grammar(options, default=None): file = options.get('grammar', default) if file is None: raise CommandUsageError() if file == 'stdin.tpeg': data = sys.stdin.read() options['basepath'] = file return pegpy.grammar(data, **options) return pegpy.grammar(file, **options)
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 peg = pegpy.grammar(''' Expression = Product (^{ '+' Product #Add })* Product = Value (^{ '*' Value #Mul })* Value = { [0-9]+ #Int } ''') parser = pegpy.generate(peg) def calc(t): if t == 'Int': return int(str(t)) if t == 'Add': return calc(t[0]) + calc(t[1]) if t == 'Mul': return calc(t[0]) * calc(t[1]) print(f'TODO{t.tag}') return 0 #t = parser('1+2*3+4*5') #print(repr(t)) #print(calc(t)) def runb(s: str): print('TODO', s) def main():
import pegpy #peg = pegpy.grammar(""" #Expression = Product (^{ '+' Product #Add })* #Product = Value (^{ '*' Value #Mul })* #Value = { [0-9]+ #Int } #""") peg = pegpy.grammar("chibi.tpeg") parser = pegpy.generate(peg) class Expr(object): @classmethod def new(cls, v): if isinstance(v, Expr): return v return Val(v) class Val(Expr): __slot__ = ["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 peg = pegpy.grammar(""" Expression=Product(^{’+’Product#Add})* Product=Value(^{’*’Value#Mul})* Value={[0-9]+#Int} """) parser = pegpy.generate(peg) def calc(t): if t == ’Int’: return int(str(t)) elif t == ’Add’: return calc(t[0]) + calc(t[1]) elif t == ’Mul’: return calc(t[0]) * calc(t[1]) else: print(f’TODO {t.tag})’ return 0 def main(): s = input('$ ') t = parser(s) print(calc(t))
import pathlib as Path import pegpy #from pegpy.tpeg import ParseTree peg = pegpy.grammar('cj.tpeg') parser = pegpy.generate(peg) model_dir = 'nlp_dict/entity_vector.model.bin' tree = parser('跳ねている') # print(repr(tree)) print('@debug(input): ', list(tree)) # 赤と赤色(赤は赤でマッピング、赤色だと黄色でマッピング) degree_dict = {'たいして': 'little', 'さほど': 'little', 'あまり': 'little', 'そんなに': 'little', 'ちっとも': 'neg', '少しも': 'neg', '全然': 'neg', '全く': 'neg', '決して': 'neg', 'よく': 'more', 'かなり': 'more', 'けっこう': 'more', '相当': 'more', 'めちゃくちゃ': 'more', 'めっちゃ': 'more', '少し': 'little', 'ちょっぴり': 'little',