annotate python/testpyy.py @ 191:6b2bec5653f1

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