Esempio n. 1
0
 def test_derivation_rightmost_wrongsymbol(self):
     G = Grammar.from_string("""
     E -> M | A | n
     M -> E * E
     A -> E + E
     """)
     d = Derivation(G).rightmost(0).rightmost(3)
     with self.assertRaisesRegex(ValueError, 'Cannot apply M'):
         d.rightmost(3)
Esempio n. 2
0
 def test_derivation_rightmost(self):
     G = Grammar.from_string("""
         S -> A B
         A -> a 
         B -> b
     """)
     d = Derivation(G).rightmost(0).rightmost(2).rightmost(1)
     steps = ((0, 0), (2, 1), (1, 0))
     self.assertEqual(steps, d.steps())
Esempio n. 3
0
 def test_derivation_repr(self):
     G = Grammar.from_string("""
         S -> A B
         A -> a
         B -> b
     """, False)
     d = Derivation(G)
     for prod, pos in [(0, 0), (1, 0), (2, 1)]: d = d.step(prod, pos)
     self.assertEqual('S -> A B -> a B -> a b', str(d))
Esempio n. 4
0
 def test_derivation_leftmost_allterminals(self):
     G = Grammar.from_string("""
     E -> M | A | n
     M -> E * E
     A -> E + E
     """)
     d = Derivation(G).leftmost(1).leftmost(4).leftmost(0).leftmost(3).leftmost(2).leftmost(2).leftmost(2)
     with self.assertRaisesRegex(ValueError, 'there are no nonterminals'):
         d.leftmost(2)
Esempio n. 5
0
 def test_derivation_steps_list(self):
     G = Grammar.from_string("""
         S -> A B
         A -> a
         B -> b
     """, False)
     steps = ((0, 0), (1, 0), (2, 1))
     d = Derivation(G).step(steps)
     self.assertEqual(steps, d.steps())
Esempio n. 6
0
 def test_derivation_sf(self):
     G = Grammar.from_string("""
         S -> A B
         A -> a
         B -> b
     """, False)
     d = Derivation(G)
     for prod, pos in [(0, 0), (1, 0), (2, 1)]: d = d.step(prod, pos)
     self.assertEqual(('a', 'b'), d.sentential_form())
Esempio n. 7
0
 def test_derivation_byprod(self):
     G = Grammar.from_string("""
         S -> A B
         A -> a
         B -> b
     """)
     d = Derivation(G)
     p = Production('S', ('A', 'B'))
     self.assertEqual(d.leftmost(p).sentential_form(), ('A', 'B'))
Esempio n. 8
0
 def test_derivation_leftmost_list(self):
     G = Grammar.from_string("""
         S -> A B
         A -> a
         B -> b
     """)
     d = Derivation(G).leftmost([0, 1, 2])
     steps = ((0, 0), (1, 0), (2, 1))
     self.assertEqual(steps, d.steps())
Esempio n. 9
0
 def test_derivation_wrong_step(self):
     G = Grammar.from_string("""
         S -> A B
         A -> a
         B -> b
     """, False)
     d = Derivation(G)
     steps = ((0, 0), (1, 1))
     with self.assertRaisesRegex(ValueError, 'at position'):
         for prod, pos in steps: d = d.step(prod, pos)
Esempio n. 10
0
 def test_derivation_steps(self):
     G = Grammar.from_string("""
         S -> A B
         A -> a
         B -> b
     """, False)
     d = Derivation(G)
     steps = ((0, 0), (1, 0), (2, 1))
     for prod, pos in steps: d = d.step(prod, pos)
     self.assertEqual(steps, d.steps())
Esempio n. 11
0
 def test_derivation_possible_steps_pos(self):
     G = Grammar.from_string("""
         S -> A B
         A -> a
         B -> b
     """, False)
     expected = [(2, 1)]
     actual = list(Derivation(G).step(0,0).possible_steps(pos = 1))
     self.assertEqual(expected, actual)
Esempio n. 12
0
 def test_derivation_eq(self):
     G = Grammar.from_string("""
         S -> A B
         A -> a
         B -> b
     """, False)
     d0 = Derivation(G)
     for prod, pos in [(0, 0), (1, 0), (2, 1)]: d0 = d0.step(prod, pos)
     d1 = Derivation(G)
     for prod, pos in [(0, 0), (1, 0), (2, 1)]: d1 = d1.step(prod, pos)
     self.assertEqual(d0, d1)
Esempio n. 13
0
 def test_derivation_hash(self):
     G = Grammar.from_string("""
         S -> A B
         A -> a
         B -> b
     """, False)
     d0 = Derivation(G)
     for prod, pos in [(0, 0), (1, 0), (2, 1)]: d0 = d0.step(prod, pos)
     d1 = Derivation(G)
     for prod, pos in [(0, 0), (1, 0), (2, 1)]: d1 = d1.step(prod, pos)
     S = {d0: 1, d1: 2}
     self.assertEqual(1, len(S))
Esempio n. 14
0
 def test_derivation_rightmost_ncf(self):
     with self.assertRaisesRegex(
             ValueError, 'derivation on a non context-free grammar'):
         Derivation(Grammar.from_string('S -> s\nT U ->s',
                                        False)).rightmost(0)
Esempio n. 15
0
 def test_derivation_eqo(self):
     self.assertFalse(Derivation(Grammar.from_string('S -> s')) == object())
Esempio n. 16
0
    p = pos
    copy = deriv
    deriv = deriv.leftmost(3)
    match('b', cont)
    pos = p
    deriv = copy


def parse_S(cont):
    S_alt0(cont)
    S_alt1(cont)


def parse_A(cont):
    A_alt0(cont)


def parse_B(cont):
    B_alt0(cont)


if __name__ == '__main__':
    s = """
	S -> A
	S -> a B
	A -> a
	B -> b
	"""
    rest = sys.argv[1]
    deriv = Derivation(Grammar.from_string(s))
    parse_S(end_match)
Esempio n. 17
0
    return False


def parse_Eh():
    global deriv
    succ_alt = alt_Eh_0(2)
    if succ_alt: return True
    return False


if __name__ == '__main__':
    rest = sys.argv[1]
    if parse_E():
        s = """
    	E -> Eh Ets
    	Ets -> Et #
    	Eh -> T
    	T -> t
    	Et -> - T
   		E -> Eh #
    	Ets -> Et Ets
    	Et -> + T
    	"""
        G = Grammar.from_string(s)
        d = Derivation(G)
        for rule in deriv:
            d = d.leftmost(rule)
        print(d)
        ProductionGraph(d)
    else:
        print("Invalid Expression")
Esempio n. 18
0
def S_alt1(deriv, rest, cont):
    def t(deriv, rest):
        parse_B(deriv, rest, cont)

    match(deriv, 'a', rest, t)


def parse_B(deriv, rest, cont):
    B_alt0(deriv.leftmost(3), rest, cont)


def parse_A(deriv, rest, cont):
    A_alt0(deriv.leftmost(2), rest, cont)


def parse_S(deriv, rest, cont):
    S_alt0(deriv.leftmost(0), rest, cont)
    S_alt1(deriv.leftmost(1), rest, cont)


if __name__ == '__main__':
    s = """
	S -> A
	S -> a B
	A -> a
	B -> b
	"""
    rest = sys.argv[1]
    parse_S(Derivation(Grammar.from_string(s)), rest, end_match)