Пример #1
0
    a := a + 1 * c; 
    c := 6 - a + c + a * 0 + OMEGA; 
    a := (a) + 1 + c; 
    [|
         [   y  : a; 
             z  : c; 
             y1 : c |];
         [|
             [   y : c
             |
                 y := 2;
                 [   xx : y; 
                     z1 : xx |]]]]]
"""

    s = grammar.S(parser.pushstream(lex.lex(reader.stringreader(program))))
    s.parse()
    assert s.atend()
    s.visit(grammar.parentset)
    s.visit(grammar.tokenset)
    s.reform([exp.simplify_depth], [exp.simplify_arith])
    s.visit(grammar.parentset)
    s.visit(block.blockset)
    s.visit(block.declsset)
    s.visit(block.namecheck)
    print(s.pprint())

    def trymatch(node, parent):
        ctx = context([node], 0, {})
        if te.match(ctx):
            print("{}: {} ({})".format(node.printexp(), ctx.out, ctx.pos))
Пример #2
0
                                                  stmt.stmts, block.decls]]):
        return [node.children[0]]
    elif any([
            parser.istoken(node, t)
            for t in ["[", "]", "(", ")", ";", "|", ":", ":="]
    ]):
        return []
    else:
        return [node]


import trex
import reader

identities = [
    trex.trex(parser.pushstream(lex.lex(reader.stringreader(s)))) for s in [
        '"0"       ; "+" ; x:.       ;^>add>',  # 0 + x == x
        'x:.       ; "+" ; "0"       ;^>add>',  # x + 0 == x
        'x:.       ; "-" ; "0"       ;^>add>',  # x - 0 == x
        'x:"OMEGA" ; "+" ; .         ;^>add>',  # OMEGA + x == OMEGA
        'x:"OMEGA" ; "-" ; .         ;^>add>',  # OMEGA - x == OMEGA
        '.         ; "+" ; x:"OMEGA" ;^>add>',  # x + OMEGA == OMEGA
        '"1"       ; "*" ; x:.       ;^>mul>',  # 1 * x == x
        'x:.       ; "*" ; "1"       ;^>mul>',  # x * 1 == x
        'x:.       ; "/" ; "1"       ;^>mul>',  # x / 1 == x
        'x:"0"     ; "*" ; .         ;^>mul>',  # 0 * x == 0
        'x:"0"     ; "/" ; .         ;^>mul>',  # 0 / x == 0
        '.         ; "*" ; x:"0"     ;^>mul>'
    ]
]  # x * 0 == 0
Пример #3
0
    if len(node.children) == 1 and \
       any([isinstance(node, ntype) for ntype in [exp, logic, order, add, mul, 
                                                  unary, atom, stmt.stmt, 
                                                  stmt.stmts, block.decls]]):
        return [node.children[0]]
    elif any([parser.istoken(node, t) for t in ["[", "]", "(", ")", ";", "|",
                                                ":", ":="]]):
        return []
    else:
        return [node]

import trex
import reader

identities = [trex.trex(parser.pushstream(
    lex.lex(reader.stringreader(s)))) for s in 
              ['"0"       ; "+" ; x:.       ;^>add>',     # 0 + x == x
               'x:.       ; "+" ; "0"       ;^>add>',     # x + 0 == x
               'x:.       ; "-" ; "0"       ;^>add>',     # x - 0 == x
               'x:"OMEGA" ; "+" ; .         ;^>add>',     # OMEGA + x == OMEGA
               'x:"OMEGA" ; "-" ; .         ;^>add>',     # OMEGA - x == OMEGA
               '.         ; "+" ; x:"OMEGA" ;^>add>',     # x + OMEGA == OMEGA

               '"1"       ; "*" ; x:.       ;^>mul>',     # 1 * x == x
               'x:.       ; "*" ; "1"       ;^>mul>',     # x * 1 == x
               'x:.       ; "/" ; "1"       ;^>mul>',     # x / 1 == x
               'x:"0"     ; "*" ; .         ;^>mul>',     # 0 * x == 0
               'x:"0"     ; "/" ; .         ;^>mul>',     # 0 / x == 0
               '.         ; "*" ; x:"0"     ;^>mul>']]    # x * 0 == 0

assert all(i.parse() and i.atend() for i in identities)
Пример #4
0
    a := a + 1 * c; 
    c := 6 - a + c + a * 0 + OMEGA; 
    a := (a) + 1 + c; 
    [|
         [   y  : a; 
             z  : c; 
             y1 : c |];
         [|
             [   y : c
             |
                 y := 2;
                 [   xx : y; 
                     z1 : xx |]]]]]
"""
    
    s = grammar.S(parser.pushstream(lex.lex(reader.stringreader(program))))
    s.parse()
    assert s.atend() 
    s.visit(grammar.parentset)
    s.visit(grammar.tokenset)
    s.reform([exp.simplify_depth], [exp.simplify_arith])
    s.visit(grammar.parentset)
    s.visit(block.blockset)
    s.visit(block.declsset)
    s.visit(block.namecheck)
    print(s.pprint())

    def trymatch(node, parent):
        ctx = context([node], 0, {})
        if te.match(ctx):
            print("{}: {} ({})".format(node.printexp(), ctx.out, ctx.pos))