184
|
1 import unittest, pprint
|
|
2 from pyyacc import Grammar, Item, EOF
|
|
3
|
|
4
|
|
5 class testLR(unittest.TestCase):
|
|
6 def setUp(self):
|
|
7 pass
|
|
8
|
|
9 def testSimpleGrammar(self):
|
|
10 # 1. define a simple grammar:
|
|
11 g = Grammar(['EOF', 'identifier', '(', ')', '+', '*'])
|
|
12 g.add_production('input', ['expression'])
|
|
13 g.add_production('expression', ['term'])
|
|
14 g.add_production('expression', ['expression', '+', 'term'])
|
|
15 g.add_production('term', ['factor'])
|
|
16 g.add_production('term', ['term', '*', 'factor'])
|
|
17 g.add_production('factor', ['(', 'expression', ')'])
|
|
18 g.add_production('factor', ['identifier'])
|
|
19 g.start_symbol = 'input'
|
|
20 # 2. define input:
|
|
21 tokens = ['identifier', '+', 'identifier', '+', 'identifier', 'EOF']
|
|
22 # 3. build parser:
|
|
23 p = g.genParser()
|
|
24 # 4. feed input:
|
|
25 p.parse(tokens)
|
|
26
|
185
|
27 class testExpressionGrammar(unittest.TestCase):
|
|
28 def setUp(self):
|
|
29 g = Grammar(['EOF', 'identifier', '(', ')', '+', '*', 'num'])
|
|
30 g.add_production('input', ['expression'])
|
|
31 g.add_production('expression', ['term'])
|
|
32 g.add_production('expression', ['expression', '+', 'term'])
|
|
33 g.add_production('term', ['factor'])
|
|
34 g.add_production('term', ['term', '*', 'factor'])
|
|
35 g.add_production('factor', ['(', 'expression', ')'])
|
|
36 g.add_production('factor', ['identifier'])
|
|
37 g.add_production('factor', ['num'])
|
|
38 g.start_symbol = 'input'
|
|
39 self.g = g
|
|
40
|
|
41 def testFirstSimpleGrammar(self):
|
|
42 # 1. define a simple grammar:
|
|
43 first = self.g.calcFirstSets()
|
|
44 self.assertEqual(first['input'], {'identifier', '(', 'num'})
|
|
45 self.assertEqual(first['term'], {'identifier', '(', 'num'})
|
|
46
|
|
47 def testCanonical(self):
|
|
48 s0 = self.g.initialItemSet()
|
|
49 s, gt = self.g.genCanonicalSet(s0)
|
|
50 # Must result in 12 sets:
|
|
51 self.assertEqual(len(s), 24)
|
|
52
|
184
|
53 class testPG(unittest.TestCase):
|
|
54 """ Tests several parts of the parser generator """
|
|
55 def setUp(self):
|
|
56 g = Grammar(['(', ')'])
|
|
57 g.add_production('goal', ['list'])
|
|
58 g.add_production('list', ['list', 'pair'])
|
|
59 g.add_production('list', ['pair'])
|
|
60 g.add_production('pair', ['(', 'pair', ')'])
|
|
61 g.add_production('pair', ['(', ')'])
|
|
62 g.start_symbol = 'goal'
|
|
63 self.g = g
|
|
64
|
|
65 def testFirstSet(self):
|
|
66 for a in ['(', ')', EOF, 'EPS']:
|
|
67 self.assertEqual(self.g.first[a], {a})
|
|
68 for nt in ['list', 'pair', 'goal']:
|
|
69 self.assertEqual(self.g.first[nt], {'('})
|
|
70
|
|
71 def testInitItemSet(self):
|
|
72 p0, p1, p2, p3, p4 = self.g.productions
|
|
73 s0 = self.g.initialItemSet()
|
|
74 self.assertEqual(len(s0), 9) # 9 with the goal rule included!
|
|
75 self.assertIn(Item(p0, 0, EOF), s0)
|
|
76 self.assertIn(Item(p1, 0, EOF), s0)
|
|
77 self.assertIn(Item(p1, 0, '('), s0)
|
|
78 self.assertIn(Item(p2, 0, EOF), s0)
|
|
79 self.assertIn(Item(p2, 0, '('), s0)
|
|
80 self.assertIn(Item(p3, 0, EOF), s0)
|
|
81 self.assertIn(Item(p3, 0, '('), s0)
|
|
82 self.assertIn(Item(p4, 0, EOF), s0)
|
|
83 self.assertIn(Item(p4, 0, '('), s0)
|
|
84
|
|
85 def testCanonical(self):
|
|
86 s0 = self.g.initialItemSet()
|
|
87 s, gt = self.g.genCanonicalSet(s0)
|
|
88 # Must result in 12 sets:
|
|
89 self.assertEqual(len(s), 12)
|
|
90
|
|
91 def testClosure(self):
|
|
92 p0, p1, p2, p3, p4 = self.g.productions
|
|
93 s0 = set()
|
185
|
94 s0.add(Item(p0, 0, EOF))
|
184
|
95 self.assertEqual(len(s0), 1) # 1 rule
|
|
96 self.assertIn(Item(p0, 0, EOF), s0)
|
|
97
|
|
98 # Invoke closure on set:
|
|
99 s0 = self.g.closure(s0)
|
|
100 self.assertIn(Item(p0, 0, EOF), s0)
|
|
101 self.assertIn(Item(p1, 0, EOF), s0)
|
|
102 self.assertIn(Item(p1, 0, '('), s0)
|
|
103 self.assertIn(Item(p2, 0, EOF), s0)
|
|
104 self.assertIn(Item(p2, 0, '('), s0)
|
|
105 self.assertIn(Item(p3, 0, EOF), s0)
|
|
106 self.assertIn(Item(p3, 0, '('), s0)
|
|
107 self.assertIn(Item(p4, 0, EOF), s0)
|
|
108 self.assertIn(Item(p4, 0, '('), s0)
|
|
109
|
|
110 def testParser(self):
|
185
|
111 tokens = ['(', '(', ')', ')', '(', ')', EOF]
|
184
|
112 # 3. build parser:
|
|
113 p = self.g.genParser()
|
185
|
114 self.assertEqual(len(p.goto_table), 5)
|
|
115 self.assertEqual(len(p.action_table), 19)
|
|
116
|
184
|
117 # 4. feed input:
|
|
118 p.parse(tokens)
|
|
119
|
|
120 if __name__ == '__main__':
|
|
121 unittest.main()
|
|
122
|
|
123
|