Beispiel #1
0
def main():
    bnf = '''
    E := E + T | T
    T := T * F | F
    F := ( E ) | id
    '''
    grammar = bnf_parser.parse(bnf)
    # print(str_lr(grammar, SLR1AlgorithmSuit))
    demo_parse(grammar, SLR1AlgorithmSuit, 'id * id + id'.split())
Beispiel #2
0
def main():
    bnf = '''
    S := X a | b
    X := X c | S d | @
    '''
    from bnf_parser import parse
    grammar = parse(bnf)
    elim_grammar = eliminate(grammar)
    print(grammar)
    print(elim_grammar)
Beispiel #3
0
def main():
    bnf = '''
    E  := T E'
    E' := + T E' | @
    T  := F T'
    T' := * F T' | @
    F  := ( E ) | id
    '''
    _demo_parse(bnf_parser.parse(bnf), 'id + id * id'.split())

    bnf = '''
    S  := 'i' E 't' S S' | a
    S' := 'e' S | @
    E  := b
    '''
    _demo_construction(bnf)
Beispiel #4
0
def main():
    args = parse_input(sys.argv[1:])
    grammar = bnf_parser.parse(open(args.bnf).read())

    if args.left_elim:
        grammar = left_recursion_eliminator.eliminate(grammar)
    if args.grammar:
        print(grammar)

    context = dict(grammar=grammar)
    builder = dict(
        first=lambda: ll1.construct_first(grammar),
        follow=lambda: ll1.construct_follow(grammar, get('first')),
        ll1_table=lambda: ll1.construct_table(grammar, get('first'), get('follow')),
        ll1_conflict=lambda: ll1.construct_conflicts(get('ll1_table')),

        lr_grammar=lambda: lr.construct_argumented_grammar(grammar),
        lr0_suit=lambda: lr.LR0AlgorithmSuit(get('lr_grammar')),
        slr1_suit=lambda: lr.SLR1AlgorithmSuit(get('lr_grammar')),
        lr1_suit=lambda: lr.LR1AlgorithmSuit(get('lr_grammar')),

        lr0_state=lambda: lr.construct_states(get('lr_grammar'), get('lr0_suit')),
        lr1_state=lambda: lr.construct_states(get('lr_grammar'), get('lr1_suit')),
        lr0_table=lambda: lr.construct_table(get('lr_grammar'), get('lr0_state'), get('lr0_suit')),
        lr1_table=lambda: lr.construct_table(get('lr_grammar'), get('lr1_state'), get('lr1_suit')),
        slr1_table=lambda: lr.construct_table(get('lr_grammar'), get('lr0_state'),
                                              get('slr1_suit')),
    )

    def get(key):
        if key not in context:
            context[key] = builder[key]()
        return context[key]

    process_ll(args, get)
    process_lr(args, get)
    process_lr1(args, get)
    process_parse(args, get)
Beispiel #5
0
def _demo_construction(bnf):
    grammar = bnf_parser.parse(bnf)
    print(grammar)
    print(str_ll1(grammar))