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