diff python/testpyy.py @ 194:b01429a5d695

Fixed test
author Windel Bouwman
date Wed, 29 May 2013 22:36:37 +0200
parents 6cd6260789a1
children 37ac6c016e0f
line wrap: on
line diff
--- a/python/testpyy.py	Sun May 26 23:58:59 2013 +0200
+++ b/python/testpyy.py	Wed May 29 22:36:37 2013 +0200
@@ -1,18 +1,16 @@
 import unittest, pprint
-from pyyacc import Grammar, Item, EOF, ParserGenerationException
+from pyyacc import Grammar, Item, ParserGenerationException, ParserException, EPS, EOF
 from ppci import Token
 
 def genTokens(lst):
     for t in lst:
-        yield Token(t, t, 0)
+        yield Token(t, t)
 
 class testLR(unittest.TestCase):
-    def setUp(self):
-        pass
-
+    """ Test basic LR(1) parser generator constructs """
     def testSimpleGrammar(self):
         # 1. define a simple grammar:
-        g = Grammar(['EOF', 'identifier', '(', ')', '+', '*'])
+        g = Grammar(['identifier', '(', ')', '+', '*'])
         g.add_production('input', ['expression'])
         g.add_production('expression', ['term'])
         g.add_production('expression', ['expression', '+', 'term'])
@@ -30,7 +28,7 @@
     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 = Grammar(['id'])
         g.add_production('goal', ['a'])
         g.add_production('a', ['b'])
         g.add_production('a', ['c'])
@@ -40,16 +38,22 @@
         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'])
+        """ Must be handled automatically by doing shift """
+        g = Grammar([EOF, 'if', 'then', 'else', 'ass'])
+        # Ambiguous grammar:
+        g.add_production('if_stmt', ['if', 'then', 'stmt'])
+        g.add_production('if_stmt', ['if', 'then', 'stmt', 'else', 'stmt'])
+        g.add_production('stmt', ['if_stmt'])
+        g.add_production('stmt', ['ass'])
         g.start_symbol = 'stmt'
-        with self.assertRaises(ParserGenerationException):
-            g.genParser()
+        p = g.genParser()
+        # Ambiguous program:
+        tokens = genTokens(['if', 'then','if', 'then', 'ass', 'else', 'ass' ])
+        p.parse(tokens)
+
     def testUndefinedTerminal(self):
         """ Test correct behavior when a terminal is undefined """
-        g = Grammar([EOF, 'b'])
+        g = Grammar(['b'])
         g.add_production('goal', ['a'])
         g.add_production('a', ['b'])
         g.add_production('a', ['c'])
@@ -65,6 +69,41 @@
         g.add_production('a', ['c'])
         g.start_symbol = 'goal'
         g.genParser()
+    def testEmpty(self):
+        """ Test empty token stream """
+        g = Grammar([','])
+        g.add_production('input', [','])
+        g.start_symbol = 'input'
+        p = g.genParser()
+        tokens = genTokens([])
+        with self.assertRaises(ParserException):
+            p.parse(tokens)
+        
+    def testEps(self):
+        """ Test epsilon terminal """
+        g = Grammar(['a', 'b'])
+        g.add_production('input', ['optional_a', 'b'])
+        g.add_production('optional_a', ['a'])
+        g.add_production('optional_a', [])
+        g.start_symbol = 'input'
+        p = g.genParser()
+        tokens = genTokens(['b'])
+        p.parse(tokens)
+
+    def testEps2(self):
+        g = Grammar(['id', ':'])
+        g.add_production('input', ['opt_lab', 'ins', 'op1'])
+        g.add_production('input', ['ins', 'op1'])
+        g.add_production('opt_lab', ['id', ':'])
+        g.add_production('ins', ['id'])
+        g.add_production('op1', ['id'])
+        g.start_symbol = 'input'
+        p = g.genParser()
+        tokens = genTokens(['id', ':', 'id', 'id'])   # i.e. "lab_0: inc rax" 
+        p.parse(tokens)
+        tokens = genTokens(['id', 'id'])   # i.e. "inc rax"
+        p.parse(tokens)
+
 
 class testExpressionGrammar(unittest.TestCase):
     def setUp(self):