Example #1
0
def main():
    g = pyauparser.Grammar.load_file("data/operator.egt")

    # parse string and call handler at every parsing event
    # usually use reduce event for processing
    try:
        def callback(ret, p):
            if ret == pyauparser.parser.ParseResultType.SHIFT:
                print "Shift\t{0}".format(p.top)
            elif ret == pyauparser.parser.ParseResultType.REDUCE:
                print "Reduce\t{0}".format(p.reduction)
            elif ret == pyauparser.parser.ParseResultType.ACCEPT:
                print "Accept\t{0}".format(p.top)
            elif ret == pyauparser.parser.ParseResultType.ERROR:
                print "Error\t{0}".format(p.error_info)
        pyauparser.parse_string(g, "-2*(3+4)-5", handler=callback)
    except pyauparser.ParseError as e:
        print e

    # instead of handling parse events, create a whole parse
    # tree from string and use it for furthur processing,
    # which is convinient and powerful.
    try:
        tree = pyauparser.parse_string_to_tree(g, "-2*(3+4)-5")
        tree.dump()
    except pyauparser.ParseError as e:
        print e
Example #2
0
def main():
    g = pyauparser.Grammar.load_file("data/operator.egt")

    # build a whole parse tree from string
    try:
        tree = pyauparser.parse_string_to_tree(g, "-2*(3+4)-5")
        tree.dump()
        print
    except pyauparser.ParseError as e:
        print e
        return

    # evaluate a parse tree by traverse nodes
    def evaluate(node):
        r = lambda s: g.get_production(s).index
        h = {
            r('<E> ::= <E> + <M>'): lambda c: e(c[0]) + e(c[2]),
            r('<E> ::= <E> - <M>'): lambda c: e(c[0]) - e(c[2]),
            r('<E> ::= <M>'):       lambda c: e(c[0]),
            r('<M> ::= <M> * <N>'): lambda c: e(c[0]) * e(c[2]),
            r('<M> ::= <M> / <N>'): lambda c: e(c[0]) / e(c[2]),
            r('<M> ::= <N>'):       lambda c: e(c[0]),
            r('<N> ::= - <V>'):     lambda c: -e(c[1]),
            r('<N> ::= <V>'):       lambda c: e(c[0]),
            r('<V> ::= Num'):       lambda c: int(c[0].token.lexeme),
            r('<V> ::= ( <E> )'):   lambda c: e(c[1]),
        }
        def e(node):
            handler = h[node.production.index]
            return handler(node.childs)
        return e(node)

    result = evaluate(tree)
    print "Result = {0}".format(result)
Example #3
0
def main():
    # with grammar_operator module,
    # a grammar instance could be constructed without any .egt file.
    g = grammar_operator.load()

    try:
        tree = pyauparser.parse_string_to_tree(g, "-2*(3+4)-5")
        tree.dump()
    except pyauparser.ParseError as e:
        print e
Example #4
0
def main():
    g = pyauparser.Grammar.load_file("data/list.egt")

    g.get_production('<List> ::= [ <List1> ]').sr_merge_child = - True
    g.get_production('<List> ::= { <List2> }').sr_merge_child = - True

    print("********** TreeBuilder [a,b,c] **********")
    pyauparser.parse_string_to_tree(g, "[a,b,c]").dump()
    print()

    print("********** SimplifiedTreeBuilder [a,b,c] **********")
    pyauparser.parse_string_to_stree(g, "[a,b,c]").dump()
    print()

    print("********** TreeBuilder {a;b;c;} **********")
    pyauparser.parse_string_to_tree(g, "{a;b;c;}").dump()
    print()

    print("********** SimplifiedTreeBuilder {a;b;c;} **********")
    pyauparser.parse_string_to_stree(g, "{a;b;c;}").dump()
    print()
Example #5
0
def main():
    g = pyauparser.Grammar.load_file("data/list.egt")

    g.get_production('<List> ::= [ <List1> ]').sr_merge_child =-True
    g.get_production('<List> ::= { <List2> }').sr_merge_child =-True

    print "********** TreeBuilder [a,b,c] **********"
    pyauparser.parse_string_to_tree(g, "[a,b,c]").dump()
    print

    print "********** SimplifiedTreeBuilder [a,b,c] **********"
    pyauparser.parse_string_to_stree(g, "[a,b,c]").dump()
    print

    print "********** TreeBuilder {a;b;c;} **********"
    pyauparser.parse_string_to_tree(g, "{a;b;c;}").dump()
    print

    print "********** SimplifiedTreeBuilder {a;b;c;} **********"
    pyauparser.parse_string_to_stree(g, "{a;b;c;}").dump()
    print
Example #6
0
def main():
    g = pyauparser.Grammar.load_file("data/operator.egt")

    # build a whole parse tree from string
    try:
        tree = pyauparser.parse_string_to_tree(g, "-2*(3+4)-5")
        tree.dump()
        print()
    except pyauparser.ParseError as e:
        print(e)
        return

    # evaluate a parse tree by traverse nodes
    def evaluate(node):
        r = lambda s: g.get_production(s).index
        h = {
            r('<E> ::= <E> + <M>'): lambda c: e(c[0]) + e(c[2]),
            r('<E> ::= <E> - <M>'): lambda c: e(c[0]) - e(c[2]),
            r('<E> ::= <M>'): lambda c: e(c[0]),
            r('<M> ::= <M> * <N>'): lambda c: e(c[0]) * e(c[2]),
            r('<M> ::= <M> / <N>'): lambda c: e(c[0]) / e(c[2]),
            r('<M> ::= <N>'): lambda c: e(c[0]),
            r('<N> ::= - <V>'): lambda c: -e(c[1]),
            r('<N> ::= <V>'): lambda c: e(c[0]),
            r('<V> ::= Num'): lambda c: int(c[0].token.lexeme),
            r('<V> ::= ( <E> )'): lambda c: e(c[1]),
        }

        def e(node):
            handler = h[node.production.index]
            return handler(node.childs)

        return e(node)

    result = evaluate(tree)
    print("Result = {0}".format(result))