Mercurial > lcfOS
view python/testpyy.py @ 184:fe2b72381a83
Added testset for pyy
author | Windel Bouwman |
---|---|
date | Fri, 24 May 2013 16:13:23 +0200 |
parents | |
children | 51a6440d6398 |
line wrap: on
line source
import unittest, pprint from pyyacc import Grammar, Item, EOF class testLR(unittest.TestCase): def setUp(self): pass def testSimpleGrammar(self): # 1. define a simple grammar: g = Grammar(['EOF', 'identifier', '(', ')', '+', '*']) g.add_production('input', ['expression']) g.add_production('expression', ['term']) g.add_production('expression', ['expression', '+', 'term']) g.add_production('term', ['factor']) g.add_production('term', ['term', '*', 'factor']) g.add_production('factor', ['(', 'expression', ')']) g.add_production('factor', ['identifier']) g.start_symbol = 'input' # 2. define input: tokens = ['identifier', '+', 'identifier', '+', 'identifier', 'EOF'] # 3. build parser: p = g.genParser() # 4. feed input: p.parse(tokens) class testPG(unittest.TestCase): """ Tests several parts of the parser generator """ def setUp(self): g = Grammar(['(', ')']) g.add_production('goal', ['list']) g.add_production('list', ['list', 'pair']) g.add_production('list', ['pair']) g.add_production('pair', ['(', 'pair', ')']) g.add_production('pair', ['(', ')']) g.start_symbol = 'goal' g.first = g.calcFirstSets() self.g = g def testFirstSet(self): for a in ['(', ')', EOF, 'EPS']: self.assertEqual(self.g.first[a], {a}) for nt in ['list', 'pair', 'goal']: self.assertEqual(self.g.first[nt], {'('}) def testInitItemSet(self): p0, p1, p2, p3, p4 = self.g.productions s0 = self.g.initialItemSet() self.assertEqual(len(s0), 9) # 9 with the goal rule included! self.assertIn(Item(p0, 0, EOF), s0) self.assertIn(Item(p1, 0, EOF), s0) self.assertIn(Item(p1, 0, '('), s0) self.assertIn(Item(p2, 0, EOF), s0) self.assertIn(Item(p2, 0, '('), s0) self.assertIn(Item(p3, 0, EOF), s0) self.assertIn(Item(p3, 0, '('), s0) self.assertIn(Item(p4, 0, EOF), s0) self.assertIn(Item(p4, 0, '('), s0) def testCanonical(self): s0 = self.g.initialItemSet() s, gt = self.g.genCanonicalSet(s0) pprint.pprint(s) # Must result in 12 sets: self.assertEqual(len(s), 12) def testClosure(self): p0, p1, p2, p3, p4 = self.g.productions s0 = set() for p in self.g.productionsForName(self.g.start_symbol): s0.add(Item(p, 0, EOF)) self.assertEqual(len(s0), 1) # 1 rule self.assertIn(Item(p0, 0, EOF), s0) # Invoke closure on set: s0 = self.g.closure(s0) self.assertIn(Item(p0, 0, EOF), s0) self.assertIn(Item(p1, 0, EOF), s0) self.assertIn(Item(p1, 0, '('), s0) self.assertIn(Item(p2, 0, EOF), s0) self.assertIn(Item(p2, 0, '('), s0) self.assertIn(Item(p3, 0, EOF), s0) self.assertIn(Item(p3, 0, '('), s0) self.assertIn(Item(p4, 0, EOF), s0) self.assertIn(Item(p4, 0, '('), s0) def testParser(self): tokens = ['(', '(', ')', '(', ')', ')', 'EOF'] # 3. build parser: p = self.g.genParser() # 4. feed input: p.parse(tokens) if __name__ == '__main__': unittest.main()