annotate test/testpyy.py @ 399:a7c444404df9

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