Exemplo n.º 1
0
def test_parser_brackets() -> None:
    brackets = ContextFreeGrammar(terminals={'(', ')'},
                                  non_terminals={'A'},
                                  start='A',
                                  rules={'A': ['(A)', '', 'AA']})
    parser = Parser(brackets)
    assert parser.is_in_language('')
    assert parser.is_in_language('()')
    assert parser.is_in_language("()()(())")
    assert not parser.is_in_language("())((())")
Exemplo n.º 2
0
def test_parser_polynoms() -> None:
    polynoms = ContextFreeGrammar(terminals={'x', '^', 'n', '+'},
                                  non_terminals={'m', 'p'},
                                  start='p',
                                  rules={
                                      'p': ['m', 'p+m'],
                                      'm': ['x', 'x^n']
                                  })
    parser = Parser(polynoms)
    assert parser.is_in_language("x^n+x+x^n")
    assert not parser.is_in_language("x^x^n+x+x^n")
Exemplo n.º 3
0
def test_parser_arithmetic() -> None:
    arithm = ContextFreeGrammar(terminals={'(', ')', '+', '*', '1', '2'},
                                non_terminals={'t', 's', 'c'},
                                start='s',
                                rules={
                                    'c': ['1', '2'],
                                    't': ['c', 't*c', '(s)'],
                                    's': ['t', 's+t']
                                })
    parser = Parser(arithm)
    assert parser.is_in_language("1*2+2")
    assert parser.is_in_language("(1*2+2*1)")
    assert not parser.is_in_language("1+(2*1*)")
Exemplo n.º 4
0
    def test_construct_pda(self):
        cfg = ContextFreeGrammar()
        cfg.add_rule('S', 'aR')
        cfg.add_rule('R', 'aRb')
        cfg.add_rule('R', '!')

        pda = cfg.construct_pda()

        start_transitions = {'!': [(State.Loop, ['S', '$'], None)]}
        loop_transitions = {
            '!': [(State.Loop, ['a', 'R'], 'S'),
                  (State.Loop, ['a', 'R', 'b'], 'R'),
                  (State.Loop, None, 'R'),
                  (State.Accept, None, '$')],
            'a': [(State.Loop, None, 'a')],
            'b': [(State.Loop, None, 'b')]
        }
        assert_transitions = {State.Start: start_transitions,
                              State.Loop: loop_transitions}

        self.assertEqual(assert_transitions, pda.transitions)
Exemplo n.º 5
0
    def test_simulate(self):
        # First input file
        cfg = ContextFreeGrammar()
        cfg.add_rule('S', 'aSb')
        cfg.add_rule('S', '!')

        expr1 = 'ab'
        expr2 = 'aabb'
        expr3 = '!'
        expr4 = 'aaaabbbb'
        expr5 = 'ababa'
        expr6 = 'bbaaa'
        self.assertTrue(cfg.simulate(expr1))
        self.assertTrue(cfg.simulate(expr2))
        self.assertTrue(cfg.simulate(expr3))
        self.assertTrue(cfg.simulate(expr4))
        self.assertFalse(cfg.simulate(expr5))
        self.assertFalse(cfg.simulate(expr6))

        # Second input file
        cfg = ContextFreeGrammar()
        cfg.add_rule('S', 'a_N_tells_a_N_O')
        cfg.add_rule('N', 'boy')
        cfg.add_rule('N', 'girl')
        cfg.add_rule('O', 'a_story')
        cfg.add_rule('O', 'that_S')

        expr1 = 'a_boy_tells_a_girl_a_story'
        expr2 = 'a_boy_tells_a_girl_that_a_boy_tells_a_girl_a_story'
        expr3 = 'a_girl_tells_a_boy_a_story'
        expr4 = 'a_girl_tells_a_boy_that_a_boy_tells_a_girl_that'
        expr5 = 'a_story_tells_a_boy_a_story'
        expr6 = 'a_boy_tells_a_tells_a_boy_a_story'
        self.assertTrue(cfg.simulate(expr1))
        self.assertTrue(cfg.simulate(expr2))
        self.assertTrue(cfg.simulate(expr3))
        self.assertFalse(cfg.simulate(expr4))
        self.assertFalse(cfg.simulate(expr5))
        self.assertFalse(cfg.simulate(expr6))

        # Third input file
        cfg = ContextFreeGrammar()
        cfg.add_rule('S', 'XY')
        cfg.add_rule('X', 'AX')
        cfg.add_rule('X', '!')
        cfg.add_rule('A', 'a')
        cfg.add_rule('Y', 'BY')
        cfg.add_rule('Y', '!')
        cfg.add_rule('B', 'b')

        expr1 = 'a'
        expr2 = 'b'
        expr3 = 'aaaaaaaabbbbbbbbb'
        expr4 = '!'
        expr5 = 'bbbbbbbbbbbbbbbbbbbbbb'
        expr6 = 'ababab'
        self.assertTrue(cfg.simulate(expr1))
        self.assertTrue(cfg.simulate(expr2))
        self.assertTrue(cfg.simulate(expr3))
        self.assertTrue(cfg.simulate(expr4))
        self.assertTrue(cfg.simulate(expr5))
        self.assertFalse(cfg.simulate(expr6))
Exemplo n.º 6
0
from grammar import ContextFreeGrammar
from parser import Lr0Parser

grammar = ContextFreeGrammar("input.json", "example")
parser = Lr0Parser(grammar)

def display_cannonical_states():
    print("\nBelow is the cannonical collection: ")
    state_index = 0
    cannonical_collection = parser.get_canonical_collection()
    for state in cannonical_collection:
        print('s' + str(state_index) + ' = ' + str([x[0] + '->' + str(''.join(x[1])) for x in state]))
        state_index += 1

def parsing_an_input_sequence(sequence):
    print("\nParsing the input sequence: " + str(sequence))
    out = parser.parse(sequence)
    res = ""
    for i in out:
        aux = ""
        for c in grammar.production_rules[int(i)][1]:
            aux += c
        if res == "":
            res += grammar.production_rules[int(i)][0] + '->' + aux
        else:
            res += ', ' + grammar.production_rules[int(i)][0] + '->' + aux

    print("\nHow we got there: ")
    for production in res.split(", "):
        print(production)