示例#1
0
    def __init__(self, start_symbol, terminals=None):
        '''Creates the syntax of some language which grammar is defined
         by the calls to the methods of this class.
         It is a high level view of the class Grammar which provides
         a much more rich and high level expressions.

         The parameters 'start_symbol' and 'terminals' are the same
         parameters of the constructor of Grammar, but a difference of Grammar,
         the 'start_symbol' is an required element.

         See the documentation of Grammar.

         '''
        if start_symbol is None:
            raise ValueError("The parameter 'start_symbol' must not be None.")

        self._grammar = Grammar(start_symbol, terminals)
示例#2
0
文件: syntax.py 项目: eldipa/dragon
   def __init__(self, start_symbol, terminals = None):
      '''Creates the syntax of some language which grammar is defined
         by the calls to the methods of this class.
         It is a high level view of the class Grammar which provides
         a much more rich and high level expressions.

         The parameters 'start_symbol' and 'terminals' are the same
         parameters of the constructor of Grammar, but a difference of Grammar,
         the 'start_symbol' is an required element.

         See the documentation of Grammar.

         '''
      if start_symbol is None:
         raise ValueError("The parameter 'start_symbol' must not be None.")

      self._grammar = Grammar(start_symbol, terminals)
示例#3
0
   def setUp(self):
      self.simple = Grammar('A', ['a'])
      
      self.simple.add_rule('A', ['M', 'a'])
      self.simple.add_empty('M')

      self.twolevels = Grammar('A', ['c'])

      self.twolevels.add_rule('A', ['B', 'c'])
      self.twolevels.add_rule('B', ['M'])
      self.twolevels.add_empty('M')

      self.no_empty = Grammar('A', ['a', 'd', 'c'])

      self.no_empty.add_rule('A', ['B', 'a'])
      self.no_empty.add_rule('B', ['d', 'C'])
      self.no_empty.add_rule('C', ['c'])

      self.follow_empty = Grammar('A', ['c', 'x'])

      self.follow_empty.add_rule('A', ['B', 'c'])
      self.follow_empty.add_rule('B', ['C', 'M'])
      self.follow_empty.add_rule('C', ['x'])
      self.follow_empty.add_empty('M')
示例#4
0
   def setUp(self):
      self.simple = Grammar('S', ('a', 'b', 's'))

      self.simple.add_rule('S', ('A', 'B', 's'))

      self.simple.add_rule('A', ('a', 'A', 'A'))
      self.simple.add_empty('A')

      self.simple.add_rule('B', ('b', 'B', 'B'))
      self.simple.add_empty('B')

      self.lrvalue = Grammar('S', ('=', '*', '(', ')', 'id'))

      self.lrvalue.add_rule('S', ['L', '=', 'R'])
      self.lrvalue.add_rule('S', ['R'])
      self.lrvalue.add_rule('L', ['*', 'R'])
      self.lrvalue.add_rule('L', ['id'])
      self.lrvalue.add_rule('R', ['L'])
      self.lrvalue.add_rule('R', ['(', 'S', ')'])

      self.simple_terminal = Grammar('S', ('$',))

      self.simple_terminal.add_rule('S', ['$'])

      self.more_complex = Grammar('S', ['a', 'b', 'c', 'd', 'q', 'r'])

      self.more_complex.add_rule('S', ['A', 'q'])
      self.more_complex.add_rule('S', ['B', 'r'])

      self.more_complex.add_rule('A', ['C', 'a'])
      self.more_complex.add_empty('A')

      self.more_complex.add_rule('B', ['D', 'b'])
      
      self.more_complex.add_rule('C', ['c'])
      self.more_complex.add_empty('C')
      
      self.more_complex.add_rule('D', ['d'])
      self.more_complex.add_empty('D')

      self.left_recursive = Grammar('E', ('+', '-', 'id', '(', ')'))

      self.left_recursive.add_rule('E', ['E', '+', 'T'])
      self.left_recursive.add_rule('E', ['E', '-', 'T'])
      self.left_recursive.add_rule('E', ['T'])

      self.left_recursive.add_rule('T', ['id'])
      self.left_recursive.add_rule('T', ['(', 'E', ')'])

      self.left_recursive_epsilon = Grammar('E', ('+', '-', 'id', '(', ')'))

      self.left_recursive_epsilon.add_rule('E', ['E', '+', 'T'])
      self.left_recursive_epsilon.add_rule('E', ['E', '-', 'T'])
      self.left_recursive_epsilon.add_rule('E', ['T'])

      self.left_recursive_epsilon.add_rule('T', ['id'])
      self.left_recursive_epsilon.add_rule('T', ['(', 'E', ')'])
      self.left_recursive_epsilon.add_empty('T')
      
      self.right_recursive = Grammar('E', ('+', '-', 'id', '(', ')'))

      self.right_recursive.add_rule('E', ['T', '+', 'E'])
      self.right_recursive.add_rule('E', ['T', '-', 'E'])
      self.right_recursive.add_rule('E', ['T'])

      self.right_recursive.add_rule('T', ['id'])
      self.right_recursive.add_rule('T', ['(', 'E', ')'])
      
      self.right_recursive_epsilon = Grammar('E', ('+', '-', 'id', '(', ')'))

      self.right_recursive_epsilon.add_rule('E', ['T', '+', 'E'])
      self.right_recursive_epsilon.add_rule('E', ['T', '-', 'E'])
      self.right_recursive_epsilon.add_rule('E', ['T'])

      self.right_recursive_epsilon.add_rule('T', ['id'])
      self.right_recursive_epsilon.add_rule('T', ['(', 'E', ')'])
      self.right_recursive_epsilon.add_empty('T')
      
      self.lrvalue_with_actions = Grammar('S', ('=', '*', '(', ')', 'id'))

      self.lrvalue_with_actions.add_rule('S', ['L', '=', 'R', lambda args: 'assign', lambda args:args])
      self.lrvalue_with_actions.add_rule('S', ['R'])
      self.lrvalue_with_actions.add_rule('L', ['*', 'R', lambda args: 'deref', lambda args:args])
      self.lrvalue_with_actions.add_rule('L', ['id'])
      self.lrvalue_with_actions.add_rule('R', ['L'])
      self.lrvalue_with_actions.add_rule('R', ['(', lambda args: 'push', 'S', lambda args: 'pop', ')', lambda args:args])
示例#5
0
class FunctionalTestFollow(unittest.TestCase):
   def setUp(self):
      self.simple = Grammar('S', ('a', 'b', 's'))

      self.simple.add_rule('S', ('A', 'B', 's'))

      self.simple.add_rule('A', ('a', 'A', 'A'))
      self.simple.add_empty('A')

      self.simple.add_rule('B', ('b', 'B', 'B'))
      self.simple.add_empty('B')

      self.lrvalue = Grammar('S', ('=', '*', '(', ')', 'id'))

      self.lrvalue.add_rule('S', ['L', '=', 'R'])
      self.lrvalue.add_rule('S', ['R'])
      self.lrvalue.add_rule('L', ['*', 'R'])
      self.lrvalue.add_rule('L', ['id'])
      self.lrvalue.add_rule('R', ['L'])
      self.lrvalue.add_rule('R', ['(', 'S', ')'])

      self.simple_terminal = Grammar('S', ('$',))

      self.simple_terminal.add_rule('S', ['$'])

      self.more_complex = Grammar('S', ['a', 'b', 'c', 'd', 'q', 'r'])

      self.more_complex.add_rule('S', ['A', 'q'])
      self.more_complex.add_rule('S', ['B', 'r'])

      self.more_complex.add_rule('A', ['C', 'a'])
      self.more_complex.add_empty('A')

      self.more_complex.add_rule('B', ['D', 'b'])
      
      self.more_complex.add_rule('C', ['c'])
      self.more_complex.add_empty('C')
      
      self.more_complex.add_rule('D', ['d'])
      self.more_complex.add_empty('D')

      self.left_recursive = Grammar('E', ('+', '-', 'id', '(', ')'))

      self.left_recursive.add_rule('E', ['E', '+', 'T'])
      self.left_recursive.add_rule('E', ['E', '-', 'T'])
      self.left_recursive.add_rule('E', ['T'])

      self.left_recursive.add_rule('T', ['id'])
      self.left_recursive.add_rule('T', ['(', 'E', ')'])

      self.left_recursive_epsilon = Grammar('E', ('+', '-', 'id', '(', ')'))

      self.left_recursive_epsilon.add_rule('E', ['E', '+', 'T'])
      self.left_recursive_epsilon.add_rule('E', ['E', '-', 'T'])
      self.left_recursive_epsilon.add_rule('E', ['T'])

      self.left_recursive_epsilon.add_rule('T', ['id'])
      self.left_recursive_epsilon.add_rule('T', ['(', 'E', ')'])
      self.left_recursive_epsilon.add_empty('T')
      
      self.right_recursive = Grammar('E', ('+', '-', 'id', '(', ')'))

      self.right_recursive.add_rule('E', ['T', '+', 'E'])
      self.right_recursive.add_rule('E', ['T', '-', 'E'])
      self.right_recursive.add_rule('E', ['T'])

      self.right_recursive.add_rule('T', ['id'])
      self.right_recursive.add_rule('T', ['(', 'E', ')'])
      
      self.right_recursive_epsilon = Grammar('E', ('+', '-', 'id', '(', ')'))

      self.right_recursive_epsilon.add_rule('E', ['T', '+', 'E'])
      self.right_recursive_epsilon.add_rule('E', ['T', '-', 'E'])
      self.right_recursive_epsilon.add_rule('E', ['T'])

      self.right_recursive_epsilon.add_rule('T', ['id'])
      self.right_recursive_epsilon.add_rule('T', ['(', 'E', ')'])
      self.right_recursive_epsilon.add_empty('T')
      
      self.lrvalue_with_actions = Grammar('S', ('=', '*', '(', ')', 'id'))

      self.lrvalue_with_actions.add_rule('S', ['L', '=', 'R', lambda args: 'assign', lambda args:args])
      self.lrvalue_with_actions.add_rule('S', ['R'])
      self.lrvalue_with_actions.add_rule('L', ['*', 'R', lambda args: 'deref', lambda args:args])
      self.lrvalue_with_actions.add_rule('L', ['id'])
      self.lrvalue_with_actions.add_rule('R', ['L'])
      self.lrvalue_with_actions.add_rule('R', ['(', lambda args: 'push', 'S', lambda args: 'pop', ')', lambda args:args])

   def test_simple(self):
      expected = follow(self.simple, 'A')
      self.assertTrue(len(expected) == 3)
      self.assertTrue('a' in expected)
      self.assertTrue('b' in expected)
      self.assertTrue('s' in expected)

      expected = follow(self.simple, 'B')
      self.assertTrue(len(expected) == 2)
      self.assertTrue('b' in expected)
      self.assertTrue('s' in expected)
      
      expected = follow(self.simple, 'S')
      self.assertTrue(len(expected) == 1)
      self.assertTrue(self.simple.EOF in expected)

   
   def test_lr_value(self):
      expected = follow(self.lrvalue, 'S')
      self.assertTrue(len(expected) == 2)
      self.assertTrue(')' in expected)
      self.assertTrue(self.lrvalue.EOF in expected)
      
      expected = follow(self.lrvalue, 'L')
      self.assertTrue(len(expected) == 3)
      self.assertTrue(')' in expected)
      self.assertTrue('=' in expected)
      self.assertTrue(self.lrvalue.EOF in expected)
      
      expected = follow(self.lrvalue, 'R')
      self.assertTrue(len(expected) == 3)
      self.assertTrue(')' in expected)
      self.assertTrue('=' in expected)
      self.assertTrue(self.lrvalue.EOF in expected)
      
   def test_more_complex(self):
      expected = follow(self.more_complex, 'A')
      self.assertTrue(len(expected) == 1)
      self.assertTrue('q' in expected)
      
      expected = follow(self.more_complex, 'B')
      self.assertTrue(len(expected) == 1)
      self.assertTrue('r' in expected)
      
      expected = follow(self.more_complex, 'S')
      self.assertTrue(len(expected) == 1)
      self.assertTrue(self.more_complex.EOF in expected)
      
      expected = follow(self.more_complex, 'C')
      self.assertTrue(len(expected) == 1)
      self.assertTrue('a' in expected)
      
      expected = follow(self.more_complex, 'D')
      self.assertTrue(len(expected) == 1)
      self.assertTrue('b' in expected)
   
   def test_left_recursive(self):
      expected = follow(self.left_recursive, 'T')
      self.assertTrue(len(expected) == 4)
      self.assertTrue('+' in expected)
      self.assertTrue('-' in expected)
      self.assertTrue(')' in expected)
      self.assertTrue(self.left_recursive.EOF in expected)
      
      expected = follow(self.left_recursive, 'E')
      self.assertTrue(len(expected) == 4)
      self.assertTrue('+' in expected)
      self.assertTrue('-' in expected)
      self.assertTrue(')' in expected)
      self.assertTrue(self.left_recursive.EOF in expected)
   
   def test_left_recursive_epsilon(self):
      expected = follow(self.left_recursive_epsilon, 'T')
      self.assertTrue(len(expected) == 4)
      self.assertTrue('+' in expected)
      self.assertTrue('-' in expected)
      self.assertTrue(')' in expected)
      self.assertTrue(self.left_recursive_epsilon.EOF in expected)
      
      expected = follow(self.left_recursive_epsilon, 'E')
      self.assertTrue(len(expected) == 4)
      self.assertTrue('+' in expected)
      self.assertTrue('-' in expected)
      self.assertTrue(')' in expected)
      self.assertTrue(self.left_recursive_epsilon.EOF in expected)
   
   def test_right_recursive(self):
      expected = follow(self.right_recursive, 'T')
      self.assertTrue(len(expected) == 4)
      self.assertTrue('+' in expected)
      self.assertTrue('-' in expected)
      self.assertTrue(')' in expected)
      self.assertTrue(self.right_recursive.EOF in expected)
      
      expected = follow(self.right_recursive, 'E')
      self.assertTrue(len(expected) == 2)
      self.assertTrue(')' in expected)
      self.assertTrue(self.right_recursive.EOF in expected)
   
   def test_right_recursive_epsilon(self):
      expected = follow(self.right_recursive_epsilon, 'T')
      self.assertTrue(len(expected) == 4)
      self.assertTrue('+' in expected)
      self.assertTrue('-' in expected)
      self.assertTrue(')' in expected)
      self.assertTrue(self.right_recursive_epsilon.EOF in expected)
      
      expected = follow(self.right_recursive_epsilon, 'E')
      self.assertTrue(len(expected) == 2)
      self.assertTrue(')' in expected)
      self.assertTrue(self.right_recursive_epsilon.EOF in expected)
   
   def test_lr_value_with_actions(self):
      expected = follow(self.lrvalue_with_actions, 'S')
      self.assertTrue(len(expected) == 2)
      self.assertTrue(')' in expected)
      self.assertTrue(self.lrvalue_with_actions.EOF in expected)
      
      expected = follow(self.lrvalue_with_actions, 'L')
      self.assertTrue(len(expected) == 3)
      self.assertTrue(')' in expected)
      self.assertTrue('=' in expected)
      self.assertTrue(self.lrvalue_with_actions.EOF in expected)
      
      expected = follow(self.lrvalue_with_actions, 'R')
      self.assertTrue(len(expected) == 3)
      self.assertTrue(')' in expected)
      self.assertTrue('=' in expected)
      self.assertTrue(self.lrvalue_with_actions.EOF in expected)
示例#6
0
    def setUp(self):
        self.simple = Grammar('S', ('a', 'b', 's'))

        self.simple.add_rule('S', ('A', 'B', 's'))

        self.simple.add_rule('A', ('a', 'A', 'A'))
        self.simple.add_empty('A')

        self.simple.add_rule('B', ('b', 'B', 'B'))
        self.simple.add_empty('B')

        self.lrvalue = Grammar('S', ('=', '*', '(', ')', 'id'))

        self.lrvalue.add_rule('S', ['L', '=', 'R'])
        self.lrvalue.add_rule('S', ['R'])
        self.lrvalue.add_rule('L', ['*', 'R'])
        self.lrvalue.add_rule('L', ['id'])
        self.lrvalue.add_rule('R', ['L'])
        self.lrvalue.add_rule('R', ['(', 'S', ')'])

        self.simple_terminal = Grammar('S', ('$', ))

        self.simple_terminal.add_rule('S', ['$'])

        self.more_complex = Grammar('S', ['a', 'b', 'c', 'd', 'q', 'r'])

        self.more_complex.add_rule('S', ['A', 'q'])
        self.more_complex.add_rule('S', ['B', 'r'])

        self.more_complex.add_rule('A', ['C', 'a'])
        self.more_complex.add_empty('A')

        self.more_complex.add_rule('B', ['D', 'b'])

        self.more_complex.add_rule('C', ['c'])
        self.more_complex.add_empty('C')

        self.more_complex.add_rule('D', ['d'])
        self.more_complex.add_empty('D')

        self.left_recursive = Grammar('E', ('+', '-', 'id', '(', ')'))

        self.left_recursive.add_rule('E', ['E', '+', 'T'])
        self.left_recursive.add_rule('E', ['E', '-', 'T'])
        self.left_recursive.add_rule('E', ['T'])

        self.left_recursive.add_rule('T', ['id'])
        self.left_recursive.add_rule('T', ['(', 'E', ')'])

        self.left_recursive_epsilon = Grammar('E', ('+', '-', 'id', '(', ')'))

        self.left_recursive_epsilon.add_rule('E', ['E', '+', 'T'])
        self.left_recursive_epsilon.add_rule('E', ['E', '-', 'T'])
        self.left_recursive_epsilon.add_rule('E', ['T'])

        self.left_recursive_epsilon.add_rule('T', ['id'])
        self.left_recursive_epsilon.add_rule('T', ['(', 'E', ')'])
        self.left_recursive_epsilon.add_empty('T')

        self.right_recursive = Grammar('E', ('+', '-', 'id', '(', ')'))

        self.right_recursive.add_rule('E', ['T', '+', 'E'])
        self.right_recursive.add_rule('E', ['T', '-', 'E'])
        self.right_recursive.add_rule('E', ['T'])

        self.right_recursive.add_rule('T', ['id'])
        self.right_recursive.add_rule('T', ['(', 'E', ')'])

        self.right_recursive_epsilon = Grammar('E', ('+', '-', 'id', '(', ')'))

        self.right_recursive_epsilon.add_rule('E', ['T', '+', 'E'])
        self.right_recursive_epsilon.add_rule('E', ['T', '-', 'E'])
        self.right_recursive_epsilon.add_rule('E', ['T'])

        self.right_recursive_epsilon.add_rule('T', ['id'])
        self.right_recursive_epsilon.add_rule('T', ['(', 'E', ')'])
        self.right_recursive_epsilon.add_empty('T')

        self.lrvalue_with_actions = Grammar('S', ('=', '*', '(', ')', 'id'))

        self.lrvalue_with_actions.add_rule(
            'S', ['L', '=', 'R', lambda args: 'assign', lambda args: args])
        self.lrvalue_with_actions.add_rule('S', ['R'])
        self.lrvalue_with_actions.add_rule(
            'L', ['*', 'R', lambda args: 'deref', lambda args: args])
        self.lrvalue_with_actions.add_rule('L', ['id'])
        self.lrvalue_with_actions.add_rule('R', ['L'])
        self.lrvalue_with_actions.add_rule('R', [
            '(', lambda args: 'push', 'S', lambda args: 'pop', ')',
            lambda args: args
        ])
示例#7
0
class FunctionalTestFirst(unittest.TestCase):
    def setUp(self):
        self.simple = Grammar('S', ('a', 'b', 's'))

        self.simple.add_rule('S', ('A', 'B', 's'))

        self.simple.add_rule('A', ('a', 'A', 'A'))
        self.simple.add_empty('A')

        self.simple.add_rule('B', ('b', 'B', 'B'))
        self.simple.add_empty('B')

        self.lrvalue = Grammar('S', ('=', '*', '(', ')', 'id'))

        self.lrvalue.add_rule('S', ['L', '=', 'R'])
        self.lrvalue.add_rule('S', ['R'])
        self.lrvalue.add_rule('L', ['*', 'R'])
        self.lrvalue.add_rule('L', ['id'])
        self.lrvalue.add_rule('R', ['L'])
        self.lrvalue.add_rule('R', ['(', 'S', ')'])

        self.simple_terminal = Grammar('S', ('$', ))

        self.simple_terminal.add_rule('S', ['$'])

        self.more_complex = Grammar('S', ['a', 'b', 'c', 'd', 'q', 'r'])

        self.more_complex.add_rule('S', ['A', 'q'])
        self.more_complex.add_rule('S', ['B', 'r'])

        self.more_complex.add_rule('A', ['C', 'a'])
        self.more_complex.add_empty('A')

        self.more_complex.add_rule('B', ['D', 'b'])

        self.more_complex.add_rule('C', ['c'])
        self.more_complex.add_empty('C')

        self.more_complex.add_rule('D', ['d'])
        self.more_complex.add_empty('D')

        self.left_recursive = Grammar('E', ('+', '-', 'id', '(', ')'))

        self.left_recursive.add_rule('E', ['E', '+', 'T'])
        self.left_recursive.add_rule('E', ['E', '-', 'T'])
        self.left_recursive.add_rule('E', ['T'])

        self.left_recursive.add_rule('T', ['id'])
        self.left_recursive.add_rule('T', ['(', 'E', ')'])

        self.left_recursive_epsilon = Grammar('E', ('+', '-', 'id', '(', ')'))

        self.left_recursive_epsilon.add_rule('E', ['E', '+', 'T'])
        self.left_recursive_epsilon.add_rule('E', ['E', '-', 'T'])
        self.left_recursive_epsilon.add_rule('E', ['T'])

        self.left_recursive_epsilon.add_rule('T', ['id'])
        self.left_recursive_epsilon.add_rule('T', ['(', 'E', ')'])
        self.left_recursive_epsilon.add_empty('T')

        self.right_recursive = Grammar('E', ('+', '-', 'id', '(', ')'))

        self.right_recursive.add_rule('E', ['T', '+', 'E'])
        self.right_recursive.add_rule('E', ['T', '-', 'E'])
        self.right_recursive.add_rule('E', ['T'])

        self.right_recursive.add_rule('T', ['id'])
        self.right_recursive.add_rule('T', ['(', 'E', ')'])

        self.right_recursive_epsilon = Grammar('E', ('+', '-', 'id', '(', ')'))

        self.right_recursive_epsilon.add_rule('E', ['T', '+', 'E'])
        self.right_recursive_epsilon.add_rule('E', ['T', '-', 'E'])
        self.right_recursive_epsilon.add_rule('E', ['T'])

        self.right_recursive_epsilon.add_rule('T', ['id'])
        self.right_recursive_epsilon.add_rule('T', ['(', 'E', ')'])
        self.right_recursive_epsilon.add_empty('T')

        self.lrvalue_with_actions = Grammar('S', ('=', '*', '(', ')', 'id'))

        self.lrvalue_with_actions.add_rule(
            'S', ['L', '=', 'R', lambda args: 'assign', lambda args: args])
        self.lrvalue_with_actions.add_rule('S', ['R'])
        self.lrvalue_with_actions.add_rule(
            'L', ['*', 'R', lambda args: 'deref', lambda args: args])
        self.lrvalue_with_actions.add_rule('L', ['id'])
        self.lrvalue_with_actions.add_rule('R', ['L'])
        self.lrvalue_with_actions.add_rule('R', [
            '(', lambda args: 'push', 'S', lambda args: 'pop', ')',
            lambda args: args
        ])

    def test_simple(self):
        expected = first(self.simple, ['A'])
        self.assertTrue(len(expected) == 2)
        self.assertTrue('a' in expected)
        self.assertTrue(self.simple.EMPTY in expected)

        expected = first(self.simple, ['B'])
        self.assertTrue(len(expected) == 2)
        self.assertTrue('b' in expected)
        self.assertTrue(self.simple.EMPTY in expected)

        expected = first(self.simple, ['S'])
        self.assertTrue(len(expected) == 3)
        self.assertTrue('s' in expected)
        self.assertTrue('b' in expected)
        self.assertTrue('a' in expected)

    def test_lr_value(self):
        expected = first(self.lrvalue, ['S'])
        self.assertTrue(len(expected) == 3)
        self.assertTrue('(' in expected)
        self.assertTrue('*' in expected)
        self.assertTrue('id' in expected)

        expected = first(self.lrvalue, ['L'])
        self.assertTrue(len(expected) == 2)
        self.assertTrue('*' in expected)
        self.assertTrue('id' in expected)

        expected = first(self.lrvalue, ['R'])
        self.assertTrue(len(expected) == 3)
        self.assertTrue('(' in expected)
        self.assertTrue('*' in expected)
        self.assertTrue('id' in expected)

    def test_more_complex(self):
        expected = first(self.more_complex, ['A'])
        self.assertTrue(len(expected) == 3)
        self.assertTrue('a' in expected)
        self.assertTrue('c' in expected)
        self.assertTrue(self.more_complex.EMPTY in expected)

        expected = first(self.more_complex, ['B'])
        self.assertTrue(len(expected) == 2)
        self.assertTrue('d' in expected)
        self.assertTrue('b' in expected)

        expected = first(self.more_complex, ['S'])
        self.assertTrue(len(expected) == 5)
        self.assertTrue('d' in expected)
        self.assertTrue('b' in expected)
        self.assertTrue('a' in expected)
        self.assertTrue('c' in expected)
        self.assertTrue('q' in expected)

    def test_left_recursive(self):
        expected = first(self.left_recursive, ['T'])
        self.assertTrue(len(expected) == 2)
        self.assertTrue('id' in expected)
        self.assertTrue('(' in expected)

        expected = first(self.left_recursive, ['E'])
        self.assertTrue(len(expected) == 2)
        self.assertTrue('id' in expected)
        self.assertTrue('(' in expected)

    def test_left_recursive_epsilon(self):
        expected = first(self.left_recursive_epsilon, ['T'])
        self.assertTrue(len(expected) == 3)
        self.assertTrue('id' in expected)
        self.assertTrue('(' in expected)
        self.assertTrue(self.left_recursive_epsilon.EMPTY in expected)

        expected = first(self.left_recursive_epsilon, ['E'])
        self.assertTrue(len(expected) == 5)
        self.assertTrue('id' in expected)
        self.assertTrue('(' in expected)
        self.assertTrue('+' in expected)
        self.assertTrue('-' in expected)
        self.assertTrue(self.left_recursive_epsilon.EMPTY in expected)

    def test_right_recursive(self):
        expected = first(self.right_recursive, ['T'])
        self.assertTrue(len(expected) == 2)
        self.assertTrue('id' in expected)
        self.assertTrue('(' in expected)

        expected = first(self.right_recursive, ['E'])
        self.assertTrue(len(expected) == 2)
        self.assertTrue('id' in expected)
        self.assertTrue('(' in expected)

    def test_right_recursive_epsilon(self):
        expected = first(self.right_recursive_epsilon, ['T'])
        self.assertTrue(len(expected) == 3)
        self.assertTrue('id' in expected)
        self.assertTrue('(' in expected)
        self.assertTrue(self.right_recursive_epsilon.EMPTY in expected)

        expected = first(self.right_recursive_epsilon, ['E'])
        self.assertTrue(len(expected) == 5)
        self.assertTrue('id' in expected)
        self.assertTrue('(' in expected)
        self.assertTrue('+' in expected)
        self.assertTrue('-' in expected)
        self.assertTrue(self.right_recursive_epsilon.EMPTY in expected)

    def test_lr_value_with_actions(self):
        expected = first(self.lrvalue_with_actions, ['S'])
        self.assertTrue(len(expected) == 3)
        self.assertTrue('(' in expected)
        self.assertTrue('*' in expected)
        self.assertTrue('id' in expected)

        expected = first(self.lrvalue_with_actions, ['L'])
        self.assertTrue(len(expected) == 2)
        self.assertTrue('*' in expected)
        self.assertTrue('id' in expected)

        expected = first(self.lrvalue_with_actions, ['R'])
        self.assertTrue(len(expected) == 3)
        self.assertTrue('(' in expected)
        self.assertTrue('*' in expected)
        self.assertTrue('id' in expected)
示例#8
0
class Syntax(object):
    '''See the __init__ method.'''
    def __init__(self, start_symbol, terminals=None):
        '''Creates the syntax of some language which grammar is defined
         by the calls to the methods of this class.
         It is a high level view of the class Grammar which provides
         a much more rich and high level expressions.

         The parameters 'start_symbol' and 'terminals' are the same
         parameters of the constructor of Grammar, but a difference of Grammar,
         the 'start_symbol' is an required element.

         See the documentation of Grammar.

         '''
        if start_symbol is None:
            raise ValueError("The parameter 'start_symbol' must not be None.")

        self._grammar = Grammar(start_symbol, terminals)

    @staticmethod
    def _symbols(symbols, sym_production=None):
        '''Utility to make 'symbols' a tuple, concatenated with 'sym_production'
         if it is not None.
         '''
        if not isinstance(symbols, (list, tuple)):
            symbols = (str(symbols), )

        if sym_production:
            return tuple(symbols) + (sym_production, )
        else:
            return tuple(symbols)

    def terminal(self, terminal, _unused):
        '''Adds a terminal element. 
         See the documentation of 'add_terminal' of Grammar.
         '''
        self._grammar.add_terminal(terminal)
        return terminal

    def rule(self, symbols, production_name):
        '''Adds a rule 'production_name' -> 'symbols'. 
         See the documentation of 'add_production' of Grammar.
         '''
        self._grammar.add_rule(production_name, self._symbols(symbols))
        return production_name

    def choice(self, alternatives, production_name):
        '''Adds multiples rules, each rule assigned to the same 'production_name'
         symbol, making each one a possible alternative.
         See the documentation of 'add_rule' of Grammar.
         '''
        if len(alternatives) <= 1:
            raise ValueError('''Expected two or more alternatives, 
                             but received '%s'.''' % str(alternatives))

        for alternative in alternatives:
            self._grammar.add_rule(production_name, self._symbols(alternative))

        return production_name

    def repeat(self, symbols, production_name):
        '''Adds a high level rule which derivates in "one or more" times
         the 'production_name' symbol.'''
        self._grammar.add_rule(production_name,
                               self._symbols(symbols, production_name))
        self._grammar.add_rule(production_name, self._symbols(symbols))
        return production_name

    def optional(self, symbols, production_name):
        '''Adds a high level rule which derivates in "zero or one" times
         the 'production_name' symbol.'''
        self._grammar.add_rule(production_name, self._symbols(symbols))
        self._grammar.add_empty(production_name)
        return production_name

    def as_grammar(self):
        '''Returns a grammar.'''
        return self._grammar
示例#9
0
class FunctionalTestSpecial(unittest.TestCase):
   def setUp(self):
      self.simple = Grammar('A', ['a'])
      
      self.simple.add_rule('A', ['M', 'a'])
      self.simple.add_empty('M')

      self.twolevels = Grammar('A', ['c'])

      self.twolevels.add_rule('A', ['B', 'c'])
      self.twolevels.add_rule('B', ['M'])
      self.twolevels.add_empty('M')

      self.no_empty = Grammar('A', ['a', 'd', 'c'])

      self.no_empty.add_rule('A', ['B', 'a'])
      self.no_empty.add_rule('B', ['d', 'C'])
      self.no_empty.add_rule('C', ['c'])

      self.follow_empty = Grammar('A', ['c', 'x'])

      self.follow_empty.add_rule('A', ['B', 'c'])
      self.follow_empty.add_rule('B', ['C', 'M'])
      self.follow_empty.add_rule('C', ['x'])
      self.follow_empty.add_empty('M')

   def test_functions_simple_grammar(self):
      expected = first(self.simple, ['A'])
      self.assertTrue(len(expected) == 1)
      self.assertTrue('a' in expected)
      
      expected = follow(self.simple, 'M')
      self.assertTrue(len(expected) == 1)
      self.assertTrue('a' in expected)
   
   def test_functions_twolevels_grammar(self):
      expected = first(self.twolevels, ['A'])
      self.assertTrue(len(expected) == 1)
      self.assertTrue('c' in expected)
      
      expected = first(self.twolevels, ['B'])
      self.assertTrue(len(expected) == 1)
      self.assertTrue(self.twolevels.EMPTY in expected)
      
      expected = follow(self.twolevels, 'B')
      self.assertTrue(len(expected) == 1)
      self.assertTrue('c' in expected)
      
      expected = follow(self.twolevels, 'M')
      self.assertTrue(expected == follow(self.twolevels, 'B'))

   def test_functions_no_empty_grammar(self):
      expected = follow(self.no_empty, 'B')
      self.assertTrue(len(expected) == 1)
      self.assertTrue('a' in expected)
      
      expected = follow(self.no_empty, 'C')
      self.assertTrue(expected == follow(self.no_empty, 'B'))

   def test_functions_follow_empty(self):
      expected = follow(self.follow_empty, 'B')
      self.assertTrue(len(expected) == 1)
      self.assertTrue('c' in expected)

      expected = follow(self.follow_empty, 'C')
      self.assertTrue(expected == follow(self.follow_empty, 'B'))
示例#10
0
文件: syntax.py 项目: eldipa/dragon
class Syntax(object):
   '''See the __init__ method.'''

   def __init__(self, start_symbol, terminals = None):
      '''Creates the syntax of some language which grammar is defined
         by the calls to the methods of this class.
         It is a high level view of the class Grammar which provides
         a much more rich and high level expressions.

         The parameters 'start_symbol' and 'terminals' are the same
         parameters of the constructor of Grammar, but a difference of Grammar,
         the 'start_symbol' is an required element.

         See the documentation of Grammar.

         '''
      if start_symbol is None:
         raise ValueError("The parameter 'start_symbol' must not be None.")

      self._grammar = Grammar(start_symbol, terminals)

   @staticmethod
   def _symbols(symbols, sym_production=None):
      '''Utility to make 'symbols' a tuple, concatenated with 'sym_production'
         if it is not None.
         '''
      if not isinstance(symbols, (list, tuple)):
         symbols = (str(symbols), )

      if sym_production:
         return tuple(symbols)+(sym_production,)
      else:
         return tuple(symbols)
         

   def terminal(self, terminal, _unused):
      '''Adds a terminal element. 
         See the documentation of 'add_terminal' of Grammar.
         '''
      self._grammar.add_terminal(terminal)
      return terminal

   def rule(self, symbols, production_name):
      '''Adds a rule 'production_name' -> 'symbols'. 
         See the documentation of 'add_production' of Grammar.
         '''
      self._grammar.add_rule(production_name, self._symbols(symbols))
      return production_name

   def choice(self, alternatives, production_name):
      '''Adds multiples rules, each rule assigned to the same 'production_name'
         symbol, making each one a possible alternative.
         See the documentation of 'add_rule' of Grammar.
         '''
      if len(alternatives) <= 1:
         raise ValueError('''Expected two or more alternatives, 
                             but received '%s'.''' % str(alternatives))
      
      for alternative in alternatives:
         self._grammar.add_rule(production_name, 
                                       self._symbols(alternative))
   
      return production_name
 
   
   def repeat(self, symbols, production_name):
      '''Adds a high level rule which derivates in "one or more" times
         the 'production_name' symbol.'''
      self._grammar.add_rule(production_name, 
                                    self._symbols(symbols, production_name))
      self._grammar.add_rule(production_name, 
                                    self._symbols(symbols))
      return production_name

   def optional(self, symbols, production_name):
      '''Adds a high level rule which derivates in "zero or one" times
         the 'production_name' symbol.'''
      self._grammar.add_rule(production_name, self._symbols(symbols))
      self._grammar.add_empty(production_name)
      return production_name
      
   
   def as_grammar(self):
      '''Returns a grammar.'''
      return self._grammar