comparison python/testpyy.py @ 184:fe2b72381a83

Added testset for pyy
author Windel Bouwman
date Fri, 24 May 2013 16:13:23 +0200
parents
children 51a6440d6398
comparison
equal deleted inserted replaced
183:4fd075e8259c 184:fe2b72381a83
1 import unittest, pprint
2 from pyyacc import Grammar, Item, EOF
3
4
5 class testLR(unittest.TestCase):
6 def setUp(self):
7 pass
8
9 def testSimpleGrammar(self):
10 # 1. define a simple grammar:
11 g = Grammar(['EOF', 'identifier', '(', ')', '+', '*'])
12 g.add_production('input', ['expression'])
13 g.add_production('expression', ['term'])
14 g.add_production('expression', ['expression', '+', 'term'])
15 g.add_production('term', ['factor'])
16 g.add_production('term', ['term', '*', 'factor'])
17 g.add_production('factor', ['(', 'expression', ')'])
18 g.add_production('factor', ['identifier'])
19 g.start_symbol = 'input'
20 # 2. define input:
21 tokens = ['identifier', '+', 'identifier', '+', 'identifier', 'EOF']
22 # 3. build parser:
23 p = g.genParser()
24 # 4. feed input:
25 p.parse(tokens)
26
27 class testPG(unittest.TestCase):
28 """ Tests several parts of the parser generator """
29 def setUp(self):
30 g = Grammar(['(', ')'])
31 g.add_production('goal', ['list'])
32 g.add_production('list', ['list', 'pair'])
33 g.add_production('list', ['pair'])
34 g.add_production('pair', ['(', 'pair', ')'])
35 g.add_production('pair', ['(', ')'])
36 g.start_symbol = 'goal'
37 g.first = g.calcFirstSets()
38 self.g = g
39
40 def testFirstSet(self):
41 for a in ['(', ')', EOF, 'EPS']:
42 self.assertEqual(self.g.first[a], {a})
43 for nt in ['list', 'pair', 'goal']:
44 self.assertEqual(self.g.first[nt], {'('})
45
46 def testInitItemSet(self):
47 p0, p1, p2, p3, p4 = self.g.productions
48 s0 = self.g.initialItemSet()
49 self.assertEqual(len(s0), 9) # 9 with the goal rule included!
50 self.assertIn(Item(p0, 0, EOF), s0)
51 self.assertIn(Item(p1, 0, EOF), s0)
52 self.assertIn(Item(p1, 0, '('), s0)
53 self.assertIn(Item(p2, 0, EOF), s0)
54 self.assertIn(Item(p2, 0, '('), s0)
55 self.assertIn(Item(p3, 0, EOF), s0)
56 self.assertIn(Item(p3, 0, '('), s0)
57 self.assertIn(Item(p4, 0, EOF), s0)
58 self.assertIn(Item(p4, 0, '('), s0)
59
60 def testCanonical(self):
61 s0 = self.g.initialItemSet()
62 s, gt = self.g.genCanonicalSet(s0)
63 pprint.pprint(s)
64 # Must result in 12 sets:
65 self.assertEqual(len(s), 12)
66
67 def testClosure(self):
68 p0, p1, p2, p3, p4 = self.g.productions
69 s0 = set()
70 for p in self.g.productionsForName(self.g.start_symbol):
71 s0.add(Item(p, 0, EOF))
72 self.assertEqual(len(s0), 1) # 1 rule
73 self.assertIn(Item(p0, 0, EOF), s0)
74
75 # Invoke closure on set:
76 s0 = self.g.closure(s0)
77 self.assertIn(Item(p0, 0, EOF), s0)
78 self.assertIn(Item(p1, 0, EOF), s0)
79 self.assertIn(Item(p1, 0, '('), s0)
80 self.assertIn(Item(p2, 0, EOF), s0)
81 self.assertIn(Item(p2, 0, '('), s0)
82 self.assertIn(Item(p3, 0, EOF), s0)
83 self.assertIn(Item(p3, 0, '('), s0)
84 self.assertIn(Item(p4, 0, EOF), s0)
85 self.assertIn(Item(p4, 0, '('), s0)
86
87 def testParser(self):
88 tokens = ['(', '(', ')', '(', ')', ')', 'EOF']
89 # 3. build parser:
90 p = self.g.genParser()
91 # 4. feed input:
92 p.parse(tokens)
93
94 if __name__ == '__main__':
95 unittest.main()
96
97