annotate test/testpyy.py @ 299:674789d9ff37

Added a doc
author Windel Bouwman
date Sun, 01 Dec 2013 18:37:23 +0100
parents 05184b95fa16
children e84047f29c78
rev   line source
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
1 import unittest, pprint
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
2 from pyyacc import Grammar, Item, ParserGenerationException, ParserException, EPS, 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:
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
7 yield Token(t, t)
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):
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
10 """ Test basic LR(1) parser generator constructs """
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
11 def testSimpleGrammar(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
12 # 1. define a simple grammar:
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
13 g = Grammar(['identifier', '(', ')', '+', '*'])
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
14 g.add_production('input', ['expression'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
15 g.add_production('expression', ['term'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
16 g.add_production('expression', ['expression', '+', 'term'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
17 g.add_production('term', ['factor'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
18 g.add_production('term', ['term', '*', 'factor'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
19 g.add_production('factor', ['(', 'expression', ')'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
20 g.add_production('factor', ['identifier'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
21 g.start_symbol = 'input'
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
22 # 2. define input:
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 185
diff changeset
23 tokens = genTokens(['identifier', '+', 'identifier', '+', 'identifier'])
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
24 # 3. build parser:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
25 p = g.genParser()
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
26 # 4. feed input:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
27 p.parse(tokens)
192
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
28 def testReduceReduceConflict(self):
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
29 """ Check if a reduce-reduce conflict is detected """
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
30 # Define a grammar with an obvious reduce-reduce conflict:
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
31 g = Grammar(['id'])
192
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
32 g.add_production('goal', ['a'])
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
33 g.add_production('a', ['b'])
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
34 g.add_production('a', ['c'])
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
35 g.add_production('b', ['id'])
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
36 g.add_production('c', ['id'])
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
37 g.start_symbol = 'goal'
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
38 with self.assertRaises(ParserGenerationException):
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
39 p = g.genParser()
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
40 def testShiftReduceConflict(self):
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
41 """ Must be handled automatically by doing shift """
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
42 g = Grammar([EOF, 'if', 'then', 'else', 'ass'])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
43 # Ambiguous grammar:
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
44 g.add_production('if_stmt', ['if', 'then', 'stmt'])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
45 g.add_production('if_stmt', ['if', 'then', 'stmt', 'else', 'stmt'])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
46 g.add_production('stmt', ['if_stmt'])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
47 g.add_production('stmt', ['ass'])
192
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
48 g.start_symbol = 'stmt'
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
49 p = g.genParser()
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
50 # Ambiguous program:
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
51 tokens = genTokens(['if', 'then','if', 'then', 'ass', 'else', 'ass'])
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
52 p.parse(tokens)
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
53
192
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
54 def testUndefinedTerminal(self):
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
55 """ Test correct behavior when a terminal is undefined """
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
56 g = Grammar(['b'])
192
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
57 g.add_production('goal', ['a'])
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
58 g.add_production('a', ['b'])
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
59 g.add_production('a', ['c'])
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
60 g.start_symbol = 'goal'
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
61 with self.assertRaises(ParserGenerationException):
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
62 g.genParser()
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
63 def testRedefineTerminal(self):
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
64 """ Test correct behavior when a terminal is redefined """
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
65 g = Grammar([EOF, 'b', 'c'])
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
66 g.add_production('goal', ['a'])
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
67 with self.assertRaises(ParserGenerationException):
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
68 g.add_production('b', ['c']) # Not allowed
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
69 g.add_production('a', ['c'])
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
70 g.start_symbol = 'goal'
6cd6260789a1 Added more tests for parser generator
Windel Bouwman
parents: 191
diff changeset
71 g.genParser()
194
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
72 def testEmpty(self):
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
73 """ Test empty token stream """
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
74 g = Grammar([','])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
75 g.add_production('input', [','])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
76 g.start_symbol = 'input'
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
77 p = g.genParser()
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
78 tokens = genTokens([])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
79 with self.assertRaises(ParserException):
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
80 p.parse(tokens)
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
81
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
82 def testEps(self):
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
83 """ Test epsilon terminal """
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
84 g = Grammar(['a', 'b'])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
85 g.add_production('input', ['optional_a', 'b'])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
86 g.add_production('optional_a', ['a'])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
87 g.add_production('optional_a', [])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
88 g.start_symbol = 'input'
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
89 p = g.genParser()
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
90 tokens = genTokens(['b'])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
91 p.parse(tokens)
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
92
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
93 def testEps2(self):
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
94 g = Grammar(['id', ':'])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
95 g.add_production('input', ['opt_lab', 'ins', 'op1'])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
96 g.add_production('input', ['ins', 'op1'])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
97 g.add_production('opt_lab', ['id', ':'])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
98 g.add_production('ins', ['id'])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
99 g.add_production('op1', ['id'])
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
100 g.start_symbol = 'input'
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
101 p = g.genParser()
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
102 tokens = genTokens(['id', ':', 'id', 'id']) # i.e. "lab_0: inc rax"
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
103 p.parse(tokens)
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
104 tokens = genTokens(['id', 'id']) # i.e. "inc rax"
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
105 p.parse(tokens)
b01429a5d695 Fixed test
Windel Bouwman
parents: 192
diff changeset
106
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
107 def test_cb(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
108 """ Test callback of one rule and order or parameters """
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
109 self.cb_called = False
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
110 def cb(a, c, b):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
111 self.cb_called = True
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
112 self.assertEqual(a, 'a')
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
113 self.assertEqual(b, 'b')
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
114 self.assertEqual(c, 'c')
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
115 g = Grammar(['a', 'b', 'c'])
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
116 g.add_production('goal', ['a', 'c', 'b'], cb)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
117 g.start_symbol = 'goal'
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
118 p = g.genParser()
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
119 tokens = genTokens(['a', 'c', 'b'])
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
120 p.parse(tokens)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
121 self.assertTrue(self.cb_called)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
122
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
123
185
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
124 class testExpressionGrammar(unittest.TestCase):
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
125 def setUp(self):
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
126 g = Grammar(['EOF', 'identifier', '(', ')', '+', '*', 'num'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
127 g.add_production('input', ['expression'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
128 g.add_production('expression', ['term'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
129 g.add_production('expression', ['expression', '+', 'term'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
130 g.add_production('term', ['factor'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
131 g.add_production('term', ['term', '*', 'factor'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
132 g.add_production('factor', ['(', 'expression', ')'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
133 g.add_production('factor', ['identifier'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
134 g.add_production('factor', ['num'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
135 g.start_symbol = 'input'
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
136 self.g = g
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
137
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
138 def testFirstSimpleGrammar(self):
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
139 # 1. define a simple grammar:
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
140 first = self.g.calcFirstSets()
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
141 self.assertEqual(first['input'], {'identifier', '(', 'num'})
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
142 self.assertEqual(first['term'], {'identifier', '(', 'num'})
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
143
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
144 def testCanonical(self):
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
145 s0 = self.g.initialItemSet()
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
146 s, gt = self.g.genCanonicalSet(s0)
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
147 # Must result in 12 sets:
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
148 self.assertEqual(len(s), 24)
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
149
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
150 class testPG(unittest.TestCase):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
151 """ Tests several parts of the parser generator """
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
152 def setUp(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
153 g = Grammar(['(', ')'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
154 g.add_production('goal', ['list'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
155 g.add_production('list', ['list', 'pair'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
156 g.add_production('list', ['pair'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
157 g.add_production('pair', ['(', 'pair', ')'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
158 g.add_production('pair', ['(', ')'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
159 g.start_symbol = 'goal'
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
160 self.g = g
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
161
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
162 def testFirstSet(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
163 for a in ['(', ')', EOF, 'EPS']:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
164 self.assertEqual(self.g.first[a], {a})
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
165 for nt in ['list', 'pair', 'goal']:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
166 self.assertEqual(self.g.first[nt], {'('})
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
167
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
168 def testInitItemSet(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
169 p0, p1, p2, p3, p4 = self.g.productions
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
170 s0 = self.g.initialItemSet()
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
171 self.assertEqual(len(s0), 9) # 9 with the goal rule included!
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
172 self.assertIn(Item(p0, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
173 self.assertIn(Item(p1, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
174 self.assertIn(Item(p1, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
175 self.assertIn(Item(p2, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
176 self.assertIn(Item(p2, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
177 self.assertIn(Item(p3, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
178 self.assertIn(Item(p3, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
179 self.assertIn(Item(p4, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
180 self.assertIn(Item(p4, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
181
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
182 def testCanonical(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
183 s0 = self.g.initialItemSet()
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
184 s, gt = self.g.genCanonicalSet(s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
185 # Must result in 12 sets:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
186 self.assertEqual(len(s), 12)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
187
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
188 def testClosure(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
189 p0, p1, p2, p3, p4 = self.g.productions
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
190 s0 = set()
185
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
191 s0.add(Item(p0, 0, EOF))
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
192 self.assertEqual(len(s0), 1) # 1 rule
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
193 self.assertIn(Item(p0, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
194
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
195 # Invoke closure on set:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
196 s0 = self.g.closure(s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
197 self.assertIn(Item(p0, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
198 self.assertIn(Item(p1, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
199 self.assertIn(Item(p1, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
200 self.assertIn(Item(p2, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
201 self.assertIn(Item(p2, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
202 self.assertIn(Item(p3, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
203 self.assertIn(Item(p3, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
204 self.assertIn(Item(p4, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
205 self.assertIn(Item(p4, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
206
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
207 def testParser(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 185
diff changeset
208 tokens = ['(', '(', ')', ')', '(', ')']
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
209 # 3. build parser:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
210 p = self.g.genParser()
185
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
211 self.assertEqual(len(p.goto_table), 5)
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
212 self.assertEqual(len(p.action_table), 19)
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
213
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
214 # 4. feed input:
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 185
diff changeset
215 p.parse(genTokens(tokens))
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
216
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
217 if __name__ == '__main__':
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
218 unittest.main()
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
219
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
220