annotate test/testpyy.py @ 318:e84047f29c78

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