Mercurial > lcfOS
diff python/testpyy.py @ 192:6cd6260789a1
Added more tests for parser generator
author | Windel Bouwman |
---|---|
date | Sun, 26 May 2013 23:19:27 +0200 |
parents | 6b2bec5653f1 |
children | b01429a5d695 |
line wrap: on
line diff
--- a/python/testpyy.py Sun May 26 15:28:07 2013 +0200 +++ b/python/testpyy.py Sun May 26 23:19:27 2013 +0200 @@ -1,5 +1,5 @@ import unittest, pprint -from pyyacc import Grammar, Item, EOF +from pyyacc import Grammar, Item, EOF, ParserGenerationException from ppci import Token def genTokens(lst): @@ -27,6 +27,44 @@ p = g.genParser() # 4. feed input: p.parse(tokens) + def testReduceReduceConflict(self): + """ Check if a reduce-reduce conflict is detected """ + # Define a grammar with an obvious reduce-reduce conflict: + g = Grammar([EOF, 'id']) + g.add_production('goal', ['a']) + g.add_production('a', ['b']) + g.add_production('a', ['c']) + g.add_production('b', ['id']) + g.add_production('c', ['id']) + g.start_symbol = 'goal' + with self.assertRaises(ParserGenerationException): + p = g.genParser() + def testShiftReduceConflict(self): + g = Grammar([EOF, 'if', 'then', 'else']) + g.add_production('if_stmt', ['if', 'then']) + g.add_production('if_stmt', ['if', 'then', 'else']) + g.add_production('stmt', ['if_stmt', 'else']) + g.start_symbol = 'stmt' + with self.assertRaises(ParserGenerationException): + g.genParser() + def testUndefinedTerminal(self): + """ Test correct behavior when a terminal is undefined """ + g = Grammar([EOF, 'b']) + g.add_production('goal', ['a']) + g.add_production('a', ['b']) + g.add_production('a', ['c']) + g.start_symbol = 'goal' + with self.assertRaises(ParserGenerationException): + g.genParser() + def testRedefineTerminal(self): + """ Test correct behavior when a terminal is redefined """ + g = Grammar([EOF, 'b', 'c']) + g.add_production('goal', ['a']) + with self.assertRaises(ParserGenerationException): + g.add_production('b', ['c']) # Not allowed + g.add_production('a', ['c']) + g.start_symbol = 'goal' + g.genParser() class testExpressionGrammar(unittest.TestCase): def setUp(self):