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 __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)
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 ])