comparison test/testpyy.py @ 341:4d204f6f7d4e devel

Rewrite of assembler parts
author Windel Bouwman
date Fri, 28 Feb 2014 18:07:14 +0100
parents 8d07a4254f04
children fb3c1f029b30
comparison
equal deleted inserted replaced
340:c7cc54c0dfdf 341:4d204f6f7d4e
35 g.add_production('factor', ['identifier']) 35 g.add_production('factor', ['identifier'])
36 g.start_symbol = 'input' 36 g.start_symbol = 'input'
37 # 2. define input: 37 # 2. define input:
38 tokens = genTokens(['identifier', '+', 'identifier', '+', 'identifier']) 38 tokens = genTokens(['identifier', '+', 'identifier', '+', 'identifier'])
39 # 3. build parser: 39 # 3. build parser:
40 p = g.genParser() 40 p = g.generate_parser()
41 # 4. feed input: 41 # 4. feed input:
42 p.parse(tokens) 42 p.parse(tokens)
43 43
44 def testReduceReduceConflict(self): 44 def testReduceReduceConflict(self):
45 """ Check if a reduce-reduce conflict is detected """ 45 """ Check if a reduce-reduce conflict is detected """
50 g.add_production('a', ['c']) 50 g.add_production('a', ['c'])
51 g.add_production('b', ['id']) 51 g.add_production('b', ['id'])
52 g.add_production('c', ['id']) 52 g.add_production('c', ['id'])
53 g.start_symbol = 'goal' 53 g.start_symbol = 'goal'
54 with self.assertRaises(ParserGenerationException): 54 with self.assertRaises(ParserGenerationException):
55 p = g.genParser() 55 p = g.generate_parser()
56 56
57 def testShiftReduceConflict(self): 57 def testShiftReduceConflict(self):
58 """ Must be handled automatically by doing shift """ 58 """ Must be handled automatically by doing shift """
59 g = Grammar([EOF, 'if', 'then', 'else', 'ass']) 59 g = Grammar([EOF, 'if', 'then', 'else', 'ass'])
60 # Ambiguous grammar: 60 # Ambiguous grammar:
61 g.add_production('if_stmt', ['if', 'then', 'stmt']) 61 g.add_production('if_stmt', ['if', 'then', 'stmt'])
62 g.add_production('if_stmt', ['if', 'then', 'stmt', 'else', 'stmt']) 62 g.add_production('if_stmt', ['if', 'then', 'stmt', 'else', 'stmt'])
63 g.add_production('stmt', ['if_stmt']) 63 g.add_production('stmt', ['if_stmt'])
64 g.add_production('stmt', ['ass']) 64 g.add_production('stmt', ['ass'])
65 g.start_symbol = 'stmt' 65 g.start_symbol = 'stmt'
66 p = g.genParser() 66 p = g.generate_parser()
67 # Ambiguous program: 67 # Ambiguous program:
68 tokens = genTokens(['if', 'then','if', 'then', 'ass', 'else', 'ass']) 68 tokens = genTokens(['if', 'then','if', 'then', 'ass', 'else', 'ass'])
69 p.parse(tokens) 69 p.parse(tokens)
70 70
71 def testUndefinedTerminal(self): 71 def testUndefinedTerminal(self):
74 g.add_production('goal', ['a']) 74 g.add_production('goal', ['a'])
75 g.add_production('a', ['b']) 75 g.add_production('a', ['b'])
76 g.add_production('a', ['c']) 76 g.add_production('a', ['c'])
77 g.start_symbol = 'goal' 77 g.start_symbol = 'goal'
78 with self.assertRaises(ParserGenerationException): 78 with self.assertRaises(ParserGenerationException):
79 g.genParser() 79 g.generate_parser()
80 80
81 def testRedefineTerminal(self): 81 def testRedefineTerminal(self):
82 """ Test correct behavior when a terminal is redefined """ 82 """ Test correct behavior when a terminal is redefined """
83 g = Grammar([EOF, 'b', 'c']) 83 g = Grammar([EOF, 'b', 'c'])
84 g.add_production('goal', ['a']) 84 g.add_production('goal', ['a'])
85 with self.assertRaises(ParserGenerationException): 85 with self.assertRaises(ParserGenerationException):
86 g.add_production('b', ['c']) # Not allowed 86 g.add_production('b', ['c']) # Not allowed
87 g.add_production('a', ['c']) 87 g.add_production('a', ['c'])
88 g.start_symbol = 'goal' 88 g.start_symbol = 'goal'
89 g.genParser() 89 g.generate_parser()
90 90
91 def testEmpty(self): 91 def testEmpty(self):
92 """ Test empty token stream """ 92 """ Test empty token stream """
93 g = Grammar([',']) 93 g = Grammar([','])
94 g.add_production('input', [',']) 94 g.add_production('input', [','])
95 g.start_symbol = 'input' 95 g.start_symbol = 'input'
96 p = g.genParser() 96 p = g.generate_parser()
97 tokens = genTokens([]) 97 tokens = genTokens([])
98 with self.assertRaises(ParserException): 98 with self.assertRaises(ParserException):
99 p.parse(tokens) 99 p.parse(tokens)
100 100
101 def testEps(self): 101 def testEps(self):
103 g = Grammar(['a', 'b']) 103 g = Grammar(['a', 'b'])
104 g.add_production('input', ['optional_a', 'b']) 104 g.add_production('input', ['optional_a', 'b'])
105 g.add_production('optional_a', ['a']) 105 g.add_production('optional_a', ['a'])
106 g.add_production('optional_a', []) 106 g.add_production('optional_a', [])
107 g.start_symbol = 'input' 107 g.start_symbol = 'input'
108 p = g.genParser() 108 p = g.generate_parser()
109 tokens = genTokens(['b']) 109 tokens = genTokens(['b'])
110 p.parse(tokens) 110 p.parse(tokens)
111 111
112 def testEps2(self): 112 def testEps2(self):
113 g = Grammar(['id', ':']) 113 g = Grammar(['id', ':'])
115 g.add_production('input', ['ins', 'op1']) 115 g.add_production('input', ['ins', 'op1'])
116 g.add_production('opt_lab', ['id', ':']) 116 g.add_production('opt_lab', ['id', ':'])
117 g.add_production('ins', ['id']) 117 g.add_production('ins', ['id'])
118 g.add_production('op1', ['id']) 118 g.add_production('op1', ['id'])
119 g.start_symbol = 'input' 119 g.start_symbol = 'input'
120 p = g.genParser() 120 p = g.generate_parser()
121 tokens = genTokens(['id', ':', 'id', 'id']) # i.e. "lab_0: inc rax" 121 tokens = genTokens(['id', ':', 'id', 'id']) # i.e. "lab_0: inc rax"
122 p.parse(tokens) 122 p.parse(tokens)
123 tokens = genTokens(['id', 'id']) # i.e. "inc rax" 123 tokens = genTokens(['id', 'id']) # i.e. "inc rax"
124 p.parse(tokens) 124 p.parse(tokens)
125 125
127 """ Test epsilon terminal for use in sequences """ 127 """ Test epsilon terminal for use in sequences """
128 g = Grammar(['a']) 128 g = Grammar(['a'])
129 g.add_production('aas', []) 129 g.add_production('aas', [])
130 g.add_production('aas', ['aas', 'a']) 130 g.add_production('aas', ['aas', 'a'])
131 g.start_symbol = 'aas' 131 g.start_symbol = 'aas'
132 p = g.genParser() 132 p = g.generate_parser()
133 tokens = genTokens(['a', 'a', 'a']) 133 tokens = genTokens(['a', 'a', 'a'])
134 p.parse(tokens) 134 p.parse(tokens)
135 tokens = genTokens([]) 135 tokens = genTokens([])
136 p.parse(tokens) 136 p.parse(tokens)
137 137
144 self.assertEqual(b.val, 'b') 144 self.assertEqual(b.val, 'b')
145 self.assertEqual(c.val, 'c') 145 self.assertEqual(c.val, 'c')
146 g = Grammar(['a', 'b', 'c']) 146 g = Grammar(['a', 'b', 'c'])
147 g.add_production('goal', ['a', 'c', 'b'], cb) 147 g.add_production('goal', ['a', 'c', 'b'], cb)
148 g.start_symbol = 'goal' 148 g.start_symbol = 'goal'
149 p = g.genParser() 149 p = g.generate_parser()
150 tokens = genTokens(['a', 'c', 'b']) 150 tokens = genTokens(['a', 'c', 'b'])
151 p.parse(tokens) 151 p.parse(tokens)
152 self.assertTrue(self.cb_called) 152 self.assertTrue(self.cb_called)
153 153
154 154
237 self.assertIn(Item(p4, 0, '('), s0) 237 self.assertIn(Item(p4, 0, '('), s0)
238 238
239 def testParser(self): 239 def testParser(self):
240 tokens = ['(', '(', ')', ')', '(', ')'] 240 tokens = ['(', '(', ')', ')', '(', ')']
241 # 3. build parser: 241 # 3. build parser:
242 p = self.g.genParser() 242 p = self.g.generate_parser()
243 self.assertEqual(len(p.goto_table), 5) 243 self.assertEqual(len(p.goto_table), 5)
244 self.assertEqual(len(p.action_table), 19) 244 self.assertEqual(len(p.action_table), 19)
245 245
246 # 4. feed input: 246 # 4. feed input:
247 p.parse(genTokens(tokens)) 247 p.parse(genTokens(tokens))