Example #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)
Example #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)
Example #3
0
from grammar import Grammar
from makell import make_ll_parser

preface = "from toycalc.lex import tokenizer"

rules = {
    # stat -> HELLO | IF cond THEN stat optional_else
    "stat": [(("HELLO",), "lambda ctx, a: 'hello'"),
             (("IF", "cond", "THEN", "stat", "optional_else"),
              "lambda ctx, a, b, c, d, e: ('if', b, d, e)")],

    # cond -> TRUE | FALSE
    "cond": [(("TRUE",), "lambda ctx, a: True"),
             (("FALSE",), "lambda ctx, a: False")],

    # optional_else -> ELSE stat | epsilon
    "optional_else": [(("ELSE", "stat"), "lambda ctx, a, b: b"),
                      ((), "lambda ctx: None")],
}

properties = {
    "default_start": "stat",
    "llconflicts": {
        ("optional_else", "ELSE"): ("ELSE", "stat"),
    },
}

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