Mercurial > lcfOS
comparison python/testpyy.py @ 191:6b2bec5653f1
Added assembler testset
author | Windel Bouwman |
---|---|
date | Sun, 26 May 2013 15:28:07 +0200 |
parents | 51a6440d6398 |
children | 6cd6260789a1 |
comparison
equal
deleted
inserted
replaced
190:65dda7e7e8bd | 191:6b2bec5653f1 |
---|---|
1 import unittest, pprint | 1 import unittest, pprint |
2 from pyyacc import Grammar, Item, EOF | 2 from pyyacc import Grammar, Item, EOF |
3 from ppci import Token | |
3 | 4 |
5 def genTokens(lst): | |
6 for t in lst: | |
7 yield Token(t, t, 0) | |
4 | 8 |
5 class testLR(unittest.TestCase): | 9 class testLR(unittest.TestCase): |
6 def setUp(self): | 10 def setUp(self): |
7 pass | 11 pass |
8 | 12 |
16 g.add_production('term', ['term', '*', 'factor']) | 20 g.add_production('term', ['term', '*', 'factor']) |
17 g.add_production('factor', ['(', 'expression', ')']) | 21 g.add_production('factor', ['(', 'expression', ')']) |
18 g.add_production('factor', ['identifier']) | 22 g.add_production('factor', ['identifier']) |
19 g.start_symbol = 'input' | 23 g.start_symbol = 'input' |
20 # 2. define input: | 24 # 2. define input: |
21 tokens = ['identifier', '+', 'identifier', '+', 'identifier', 'EOF'] | 25 tokens = genTokens(['identifier', '+', 'identifier', '+', 'identifier']) |
22 # 3. build parser: | 26 # 3. build parser: |
23 p = g.genParser() | 27 p = g.genParser() |
24 # 4. feed input: | 28 # 4. feed input: |
25 p.parse(tokens) | 29 p.parse(tokens) |
26 | 30 |
106 self.assertIn(Item(p3, 0, '('), s0) | 110 self.assertIn(Item(p3, 0, '('), s0) |
107 self.assertIn(Item(p4, 0, EOF), s0) | 111 self.assertIn(Item(p4, 0, EOF), s0) |
108 self.assertIn(Item(p4, 0, '('), s0) | 112 self.assertIn(Item(p4, 0, '('), s0) |
109 | 113 |
110 def testParser(self): | 114 def testParser(self): |
111 tokens = ['(', '(', ')', ')', '(', ')', EOF] | 115 tokens = ['(', '(', ')', ')', '(', ')'] |
112 # 3. build parser: | 116 # 3. build parser: |
113 p = self.g.genParser() | 117 p = self.g.genParser() |
114 self.assertEqual(len(p.goto_table), 5) | 118 self.assertEqual(len(p.goto_table), 5) |
115 self.assertEqual(len(p.action_table), 19) | 119 self.assertEqual(len(p.action_table), 19) |
116 | 120 |
117 # 4. feed input: | 121 # 4. feed input: |
118 p.parse(tokens) | 122 p.parse(genTokens(tokens)) |
119 | 123 |
120 if __name__ == '__main__': | 124 if __name__ == '__main__': |
121 unittest.main() | 125 unittest.main() |
122 | 126 |
123 | 127 |