示例#1
0
def make_mgr_parser(input_file, initial_states, output_file):
    """Creates a parser from a .mgr grammar file."""

    print("=====", input_file, "->", output_file, "=====")

    grammar = mgr.read.read_grammar(input_file, initial_states)

    type = grammar.properties.get('type')
    if type == 'LL':
        output = makell.make_ll_parser(grammar)
    elif type == 'LR':
        output = makelr.make_lr_parser(grammar)
    elif type:
        raise Exception('File %r specifies unknown "type"' % input_file)
    else:
        raise Exception('File %r does not specify "type"' % input_file)

    with open(output_file, 'w') as f:
        f.write(output)
示例#2
0
def make_mgr_parser(input_file, initial_states, output_file):
    """Creates a parser from a .mgr grammar file."""

    print("=====", input_file, "->", output_file, "=====")

    grammar = mgr.read.read_grammar(input_file, initial_states)

    type = grammar.properties.get('type')
    if type == 'LL':
        output = makell.make_ll_parser(grammar)
    elif type == 'LR':
        output = makelr.make_lr_parser(grammar)
    elif type:
        raise Exception('File %r specifies unknown "type"' % input_file)
    else:
        raise Exception('File %r does not specify "type"' % input_file)

    with open(output_file, 'w') as f:
        f.write(output)
示例#3
0
    # stat -> HELLO | IF cond THEN stat | IF cond THEN stat ELSE stat
    "stat": [(("HELLO",), "lambda ctx, a: 'hello'"),
             (("IF", "cond", "THEN", "stat"),
              "lambda ctx, a, b, c, d: ('if', b, d, None)"),
             (("IF", "cond", "THEN", "stat", "ELSE", "stat"),
              "lambda ctx, a, b, c, d, e, f: ('if', b, d, f)")],

    # cond -> cond AND cond | cond OR cond | TRUE | FALSE
    "cond": [(("cond", "AND", "cond"), "lambda ctx, a, b, c: (a, 'and', c)"),
             (("cond", "OR", "cond"), "lambda ctx, a, b, c: (a, 'or', c)"),
             (("TRUE",), "lambda ctx, a: True"),
             (("FALSE",), "lambda ctx, a: False")],
}

properties = {
    "default_start": "stat",
    "precedence": [
        [
            ("left", ["OR"]),
            ("left", ["AND"]),
        ],
        [
            ("precedence", [("stat", ("IF", "cond", "THEN", "stat"))]),
            ("precedence", ["ELSE"]),
        ],
    ],
}

my_grammar = Grammar(preface, properties, rules)
print(make_lr_parser(my_grammar))
示例#4
0
    "stat": [(("HELLO", ), "lambda ctx, a: 'hello'"),
             (("IF", "cond", "THEN", "stat"),
              "lambda ctx, a, b, c, d: ('if', b, d, None)"),
             (("IF", "cond", "THEN", "stat", "ELSE", "stat"),
              "lambda ctx, a, b, c, d, e, f: ('if', b, d, f)")],

    # cond -> cond AND cond | cond OR cond | TRUE | FALSE
    "cond": [(("cond", "AND", "cond"), "lambda ctx, a, b, c: (a, 'and', c)"),
             (("cond", "OR", "cond"), "lambda ctx, a, b, c: (a, 'or', c)"),
             (("TRUE", ), "lambda ctx, a: True"),
             (("FALSE", ), "lambda ctx, a: False")],
}

properties = {
    "default_start":
    "stat",
    "precedence": [
        [
            ("left", ["OR"]),
            ("left", ["AND"]),
        ],
        [
            ("precedence", [("stat", ("IF", "cond", "THEN", "stat"))]),
            ("precedence", ["ELSE"]),
        ],
    ],
}

my_grammar = Grammar(preface, properties, rules)
print(make_lr_parser(my_grammar))