annotate python/testpyy.py @ 185:51a6440d6398

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