PLY
関数定義可能。変数スコープあり。コードを理解できていないので、意味(動作)から推測(IntpFrame クラスはなし) class Node: def exec_list(self, nodes): for x in nodes: x.evaluate() class RootNode(Node): def __init__(self, tree): self.tree = tree …
関数定義可能に。ただし、変数スコープなし、グローバル class Node: def exec_list(self, nodes): for x in nodes: x.evaluate() class RootNode(Node): def __init__(self, tree): self.tree = tree def evaluate(self): self.exec_list(self.tree) class …
class Node: def exec_list(self, nodes): for x in nodes: x.evaluate() class RootNode(Node): def __init__(self, tree): self.tree = tree def evaluate(self): self.exec_list(self.tree) class FuncallNode(Node): def __init__(self, func, args): se…
四則演算ができるように #!/usr/bin/env python from math import * import fileinput import ply.lex as lex tokens = ( 'NUMBER', 'STRING', 'IDENT', 'EOL', ) literals = "(),=+-*/" def t_NUMBER(t): r'\d+' try: t.value = int(t.value) except ValueE…
#!/usr/bin/env python from math import * import fileinput import ply.lex as lex tokens = ( 'NUMBER', 'STRING', 'IDENT', 'EOL', ) literals = "(),=" def t_NUMBER(t): r'\d+' try: t.value = int(t.value) except ValueError: print "Integer value …
未知変数は引数のない関数呼び出しと解釈 #!/usr/bin/env python from math import * def foo(): print "foo" import string import ply.lex as lex tokens = ( 'NUMBER', 'STRING', 'IDENT', # 'EOL', ) literals = "(),=" def t_NUMBER(t): r'\d+' try: t.…
#!/usr/bin/env python from math import * import string import ply.lex as lex tokens = ( 'NUMBER', 'STRING', 'IDENT', # 'EOL', ) literals = "(),=" def t_NUMBER(t): r'\d+' try: t.value = int(t.value) except ValueError: print "Integer value t…
#!/usr/bin/env python from math import * import string import ply.lex as lex tokens = ( 'NUMBER', 'STRING', 'IDENT', # 'EOL', ) literals = "(),=" def t_NUMBER(t): r'\d+' try: t.value = int(t.value) except ValueError: print "Integer value t…
#!/usr/bin/env python import string import ply.lex as lex tokens = ( 'NUMBER', 'STRING', 'IDENT', # 'EOL', ) literals = "(),=" def t_NUMBER(t): r'\d+' try: t.value = int(t.value) except ValueError: print "Integer value too large", t.value …
定数 PI, E を追加 #!/usr/bin/env python import ply.lex as lex tokens = ( 'NUM', 'VAR', 'FNCT', ) literals = ['+', '-', '*', '/', '^', '(', ')', '='] def t_NUM(t): r'\d+(\.\d+)*' try: t.value = float(t.value) except ValueError: print "Integ…
#!/usr/bin/env python import ply.lex as lex tokens = ( 'NUM', 'VAR', 'FNCT', ) literals = ['+', '-', '*', '/', '^', '(', ')', '='] def t_NUM(t): r'\d+(\.\d+)*' try: t.value = float(t.value) except ValueError: print "Integer value too large…
#!/usr/bin/env python import ply.lex as lex tokens = ( 'NUM', ) literals = ['+', '-', '*', '/', '^', '(', ')'] def t_NUM(t): r'\d+(\.\d+)*' try: t.value = float(t.value) except ValueError: print "Integer value too large", t.value t.value =…
#!/usr/bin/env python import ply.lex as lex tokens = ( 'NUM', ) literals = ['+', '-', '*', '/', '^', 'n'] def t_NUM(t): r'\d+(\.\d+)*' try: t.value = float(t.value) except ValueError: print "Integer value too large", t.value t.value = 0 re…
「lex.lex(optimize=1)」「yacc.yacc(optimize=1)」 #!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', ) t_A = r'A' t_B = r'B' t_C = r'C' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0] t.lexer.…
#!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', ) def t_A(t): r'A' print t.lexer return t t_B = r'B' t_C = r'C' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0] t.lexer.skip(1) lex.lex() im…
#!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', ) t_A = r'A' t_B = r'B' t_C = r'C' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0] t.lexer.skip(1) lex.lex() import ply.yacc as yacc def p_i…
「yacc.yacc(debug=1, debugfile="debugging.out")」 #!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', ) t_A = r'A' t_B = r'B' t_C = r'C' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0] t.lexer…
「yacc.parse(debug=1)」 #!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', ) t_A = r'A' t_B = r'B' t_C = r'C' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0] t.lexer.skip(1) lex.lex() import …
「yacc.yacc(write_tables=0)」 #!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', ) t_A = r'A' t_B = r'B' t_C = r'C' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0] t.lexer.skip(1) lex.lex() i…
「yacc.yacc(tabmodule="foo", outputdir="/tmp")」 #!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', ) t_A = r'A' t_B = r'B' t_C = r'C' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0] t.lexer.…
「yacc.yacc(tabmodule="foo")」 デフォルトは parsetab.py に出力される #!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', ) t_A = r'A' t_B = r'B' t_C = r'C' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" %…
「yacc.yacc(debug=0)」 parser.out が出力されなくなる #!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', ) t_A = r'A' t_B = r'B' t_C = r'C' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0] t.le…
「yacc.parse(s, lexer = Lexer())」 #!/usr/bin/env python tokens = ( 'A', 'B', 'C', ) class LexToken: def __init__(self, t, value, lineno, lexpos): self.type = t self.value = value self.lineno = lineno self.lexpos = lexpos class Lexer: def …
「yacc.yacc(method="SLR")」 #!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', ) t_A = r'A' t_B = r'B' t_C = r'C' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0] t.lexer.skip(1) lex.lex() imp…
まんま #!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', 'D', 'X', ) t_A = r'A' t_B = r'B' t_C = r'C' t_D = r'D' t_X = r'X' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0] t.lexer.skip(1) le…
自分で書き下す必要があるようだほぼ、まんま #!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', 'D', ) t_A = r'A' t_B = r'B' t_C = r'C' t_D = r'D' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0…
#!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', 'C', ) t_A = r'A' t_B = r'B' t_C = r'C' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0] t.lexer.skip(1) lex.lex() import ply.yacc as yacc def p_i…
良く分かっていない #!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', ) t_A = r'A' t_B = r'B' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0] t.lexer.skip(1) lex.lex() import ply.yacc as yacc def …
試したが、失敗…(原因不明) #!/usr/bin/env python import ply.lex as lex tokens = ( 'A', ) t_A = r'A' t_ignore = " \t" literals = ['+', '<'] def t_error(t): print "Illegal character '%s'" % t.value[0] t.lexer.skip(1) lex.lex() import ply.yacc…
#!/usr/bin/env python import ply.lex as lex tokens = ( 'A', 'B', ) t_A = r'A' t_B = r'B' t_ignore = " \t" def t_error(t): print "Illegal character '%s'" % t.value[0] t.lexer.skip(1) lex.lex() import ply.yacc as yacc def p_input(p): 'input …