annotate test/testpyy.py @ 389:2ec730e45ea1

Added check for recursive struct
author Windel Bouwman
date Fri, 16 May 2014 12:29:31 +0200
parents 4d204f6f7d4e
children fb3c1f029b30
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
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
3 from pyyacc import EPS, EOF, calculate_first_sets
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:
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 319
diff changeset
40 p = g.generate_parser()
184
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):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 319
diff changeset
55 p = g.generate_parser()
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'
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 319
diff changeset
66 p = g.generate_parser()
194
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):
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 319
diff changeset
79 g.generate_parser()
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'
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 319
diff changeset
89 g.generate_parser()
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'
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 319
diff changeset
96 p = g.generate_parser()
194
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'
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 319
diff changeset
108 p = g.generate_parser()
194
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'
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 319
diff changeset
120 p = g.generate_parser()
194
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
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
126 def testEpsSequence(self):
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
127 """ Test epsilon terminal for use in sequences """
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
128 g = Grammar(['a'])
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
129 g.add_production('aas', [])
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
130 g.add_production('aas', ['aas', 'a'])
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
131 g.start_symbol = 'aas'
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 319
diff changeset
132 p = g.generate_parser()
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
133 tokens = genTokens(['a', 'a', 'a'])
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
134 p.parse(tokens)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
135 tokens = genTokens([])
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
136 p.parse(tokens)
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
137
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
138 def test_cb(self):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
139 """ Test callback of one rule and order or parameters """
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
140 self.cb_called = False
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
141 def cb(a, c, b):
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
142 self.cb_called = True
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 284
diff changeset
143 self.assertEqual(a.val, 'a')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 284
diff changeset
144 self.assertEqual(b.val, 'b')
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 284
diff changeset
145 self.assertEqual(c.val, 'c')
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
146 g = Grammar(['a', 'b', 'c'])
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
147 g.add_production('goal', ['a', 'c', 'b'], cb)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
148 g.start_symbol = 'goal'
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 319
diff changeset
149 p = g.generate_parser()
195
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
150 tokens = genTokens(['a', 'c', 'b'])
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
151 p.parse(tokens)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
152 self.assertTrue(self.cb_called)
37ac6c016e0f Expanded asm subsystem
Windel Bouwman
parents: 194
diff changeset
153
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
154
185
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
155 class testExpressionGrammar(unittest.TestCase):
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
156 def setUp(self):
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
157 g = Grammar(['EOF', 'identifier', '(', ')', '+', '*', 'num'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
158 g.add_production('input', ['expression'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
159 g.add_production('expression', ['term'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
160 g.add_production('expression', ['expression', '+', 'term'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
161 g.add_production('term', ['factor'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
162 g.add_production('term', ['term', '*', 'factor'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
163 g.add_production('factor', ['(', 'expression', ')'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
164 g.add_production('factor', ['identifier'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
165 g.add_production('factor', ['num'])
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
166 g.start_symbol = 'input'
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
167 self.g = g
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
168
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
169 def testFirstSimpleGrammar(self):
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
170 # 1. define a simple grammar:
319
8d07a4254f04 Work on burg
Windel Bouwman
parents: 318
diff changeset
171 first = calculate_first_sets(self.g)
185
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
172 self.assertEqual(first['input'], {'identifier', '(', 'num'})
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
173 self.assertEqual(first['term'], {'identifier', '(', 'num'})
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
174
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
175 def testCanonical(self):
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
176 s0 = self.g.initialItemSet()
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
177 s, gt = self.g.genCanonicalSet(s0)
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
178 # Must result in 12 sets:
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
179 self.assertEqual(len(s), 24)
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
180
318
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 284
diff changeset
181
e84047f29c78 Add burg and yacc initial attempts
Windel Bouwman
parents: 284
diff changeset
182 class testParserGenerator(unittest.TestCase):
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
183 """ Tests several parts of the parser generator """
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
184 def setUp(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
185 g = Grammar(['(', ')'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
186 g.add_production('goal', ['list'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
187 g.add_production('list', ['list', 'pair'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
188 g.add_production('list', ['pair'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
189 g.add_production('pair', ['(', 'pair', ')'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
190 g.add_production('pair', ['(', ')'])
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
191 g.start_symbol = 'goal'
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
192 self.g = g
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
193
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
194 def testFirstSet(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
195 for a in ['(', ')', EOF, 'EPS']:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
196 self.assertEqual(self.g.first[a], {a})
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
197 for nt in ['list', 'pair', 'goal']:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
198 self.assertEqual(self.g.first[nt], {'('})
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
199
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
200 def testInitItemSet(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
201 p0, p1, p2, p3, p4 = self.g.productions
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
202 s0 = self.g.initialItemSet()
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
203 self.assertEqual(len(s0), 9) # 9 with the goal rule included!
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
204 self.assertIn(Item(p0, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
205 self.assertIn(Item(p1, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
206 self.assertIn(Item(p1, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
207 self.assertIn(Item(p2, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
208 self.assertIn(Item(p2, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
209 self.assertIn(Item(p3, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
210 self.assertIn(Item(p3, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
211 self.assertIn(Item(p4, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
212 self.assertIn(Item(p4, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
213
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
214 def testCanonical(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
215 s0 = self.g.initialItemSet()
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
216 s, gt = self.g.genCanonicalSet(s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
217 # Must result in 12 sets:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
218 self.assertEqual(len(s), 12)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
219
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
220 def testClosure(self):
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
221 p0, p1, p2, p3, p4 = self.g.productions
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
222 s0 = set()
185
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
223 s0.add(Item(p0, 0, EOF))
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
224 self.assertEqual(len(s0), 1) # 1 rule
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
225 self.assertIn(Item(p0, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
226
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
227 # Invoke closure on set:
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
228 s0 = self.g.closure(s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
229 self.assertIn(Item(p0, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
230 self.assertIn(Item(p1, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
231 self.assertIn(Item(p1, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
232 self.assertIn(Item(p2, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
233 self.assertIn(Item(p2, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
234 self.assertIn(Item(p3, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
235 self.assertIn(Item(p3, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
236 self.assertIn(Item(p4, 0, EOF), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
237 self.assertIn(Item(p4, 0, '('), s0)
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
238
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
239 def testParser(self):
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 185
diff changeset
240 tokens = ['(', '(', ')', ')', '(', ')']
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
241 # 3. build parser:
341
4d204f6f7d4e Rewrite of assembler parts
Windel Bouwman
parents: 319
diff changeset
242 p = self.g.generate_parser()
185
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
243 self.assertEqual(len(p.goto_table), 5)
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
244 self.assertEqual(len(p.action_table), 19)
51a6440d6398 Fixed LR(1) parser
Windel Bouwman
parents: 184
diff changeset
245
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
246 # 4. feed input:
191
6b2bec5653f1 Added assembler testset
Windel Bouwman
parents: 185
diff changeset
247 p.parse(genTokens(tokens))
184
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
248
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
249 if __name__ == '__main__':
fe2b72381a83 Added testset for pyy
Windel Bouwman
parents:
diff changeset
250 unittest.main()