def setUp(self): self.arith = grammar.Grammar('E', ('+', '*', '(', ')', 'id')) semantic_action = lambda args: args self.arith.add_rule('E', ['E', '+', 'T', semantic_action]) self.arith.add_rule('E', ['T']) self.arith.add_rule('T', ['T', '*', 'F', semantic_action]) self.arith.add_rule('T', ['F']) self.arith.add_rule('F', ['(', 'E', ')', semantic_action]) self.arith.add_rule('F', ['id']) self.lrvalue = grammar.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.reduce_reduce = grammar.Grammar('S', ('=', '?', '#')) self.reduce_reduce.add_rule('S', ['A', '#', '=']) self.reduce_reduce.add_rule('S', ['B', '#', '?']) self.reduce_reduce.add_rule('A', ['=', '?']) self.reduce_reduce.add_rule('B', ['=', '?']) self.StartExtendedSymbol = grammar.Grammar.START
def setUp(self): self.arith = grammar.Grammar('S', ('+', '*', '(', ')', 'id')) self.result = None def get_result(v): self.result = v return v def add(x, y): t = x + y return t def mul(x, y): t = x * y return t self.arith.add_rule('S', ['E', get_result]) self.arith.add_rule('E', ['E', '+', 'T', add]) self.arith.add_rule('E', ['T', lambda v: v]) self.arith.add_rule('T', ['T', '*', 'F', mul]) self.arith.add_rule('T', ['F', lambda v: v]) self.arith.add_rule('F', ['(', 'E', ')', lambda v: v]) self.arith.add_rule('F', ['id', lambda v: v]) self.action_table, self.goto_table, self.start_state = build_parsing_table( self.arith, LR0(self.arith.START, 0, 0)) self.driver = Driver(self.action_table, self.goto_table, self.start_state)
def setUp(self): self.lrvalue = grammar.Grammar(None, ('=', '*', ';', 'id', 'num')) self.symbol_table = dict() self.last_value = None def set_var(lv, rv): self.symbol_table[lv] = rv; return self.symbol_table[lv] def get_var(rv): if rv in self.symbol_table: return self.symbol_table[rv] raise KeyError(rv) def grab_last_value(v): self.last_value = v if isinstance(v, int) else self.symbol_table[v] return v self.lrvalue.augment('S') self.lrvalue.add_rule('S', ['E', ';', 'S', lambda e, s: s]) self.lrvalue.add_rule('S', ['E', ';', lambda v: v]) self.lrvalue.add_rule('E', ['L', '=', 'R', set_var]) self.lrvalue.add_rule('E', ['R', grab_last_value]) self.lrvalue.add_rule('L', ['*', 'R', get_var]) self.lrvalue.add_rule('L', ['id', lambda v: v]) self.lrvalue.add_rule('R', ['L', lambda v: v]) self.lrvalue.add_rule('R', ['num', lambda v: v]) self.action_table, self.goto_table, self.start_state = build_parsing_table_lalr(self.lrvalue, LR0(self.lrvalue.START, 0, 0), False) self.driver = Driver(self.action_table, self.goto_table, self.start_state)
def setUp(self): self.arith = grammar.Grammar('S', ('c', 'd')) self.arith.add_rule('S', ['C', 'C']) self.arith.add_rule('C', ['c', 'C']) self.arith.add_rule('C', ['d']) self.StartExtendedSymbol = grammar.Grammar.START
def setUp(self): self.lrvalue = grammar.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', ')'])
def setUp(self): self.arith = grammar.Grammar('E', ('+', '*', '(', ')', 'id')) self.arith.add_rule('E', ['E', '+', 'T']) self.arith.add_rule('E', ['T']) self.arith.add_rule('T', ['T', '*', 'F']) self.arith.add_rule('T', ['F']) self.arith.add_rule('F', ['(', 'E', ')']) self.arith.add_rule('F', ['id']) self.lrvalue = grammar.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.StartExtendedSymbol = grammar.Grammar.START
def setUp(self): self.arith = grammar.Grammar('E', ('+', '*', '(', ')', 'id')) self.arith.add_rule('E', ['E', '+', 'T']) self.arith.add_rule('E', ['T']) self.arith.add_rule('T', ['T', '*', 'F']) self.arith.add_rule('T', ['F']) self.arith.add_rule('F', ['(', 'E', ')']) self.arith.add_rule('F', ['id']) self.lrvalue = grammar.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.some = grammar.Grammar('S', ('c', 'd')) self.some.add_rule('S', ['C', 'C']) self.some.add_rule('C', ['c', 'C']) self.some.add_rule('C', ['d']) self.lrvalue_with_actions = grammar.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 ]) self.StartExtendedSymbol = grammar.Grammar.START
def setUp(self): self.lrvalue = grammar.Grammar('S', ('c', 'd')) self.lrvalue.add_rule('S', ['C', 'C']) self.lrvalue.add_rule('C', ['c', 'C']) self.lrvalue.add_rule('C', ['d']) self.StartExtendedSymbol = grammar.Grammar.START self.kernels_lalr, self.goto_table = generate_spontaneously_lookaheads( self.lrvalue, LR0(self.lrvalue.START, 0, 0), UserFriendlyMapping(False))
def setUp(self): self.arith = grammar.Grammar( 'S', ('+', '*', '(', ')', 'id', 'var', '=', 'let')) self.symbol_table = [dict()] self.result = None def get_result(x): self.result = x return x def add(x, y): t = x + y return t def mul(x, y): t = x * y return t def set_var(lv, rv): self.symbol_table[-1][lv] = rv return rv def get_var(rv): for table in reversed(self.symbol_table): if rv in table: return table[rv] raise KeyError(rv) def push(): self.symbol_table.append(dict()) def pop(*others): self.symbol_table.pop() self.arith.add_rule('S', ['E', get_result]) self.arith.add_rule('E', ['E', '+', 'T', add]) self.arith.add_rule('E', ['T', lambda v: v]) self.arith.add_rule('T', ['T', '*', 'F', mul]) self.arith.add_rule('T', ['F', lambda v: v]) self.arith.add_rule('F', ['(', 'E', ')', lambda v: v]) self.arith.add_rule('F', ['id', lambda v: v]) self.arith.add_rule('F', ['var', '=', 'E', set_var]) self.arith.add_rule('F', ['var', get_var]) self.arith.add_rule('F', ['let', push, '(', 'E', ')', pop, lambda v: v]) self.action_table, self.goto_table, self.start_state = build_parsing_table( self.arith, LR0(self.arith.START, 0, 0)) self.driver = Driver(self.action_table, self.goto_table, self.start_state)
def setUp(self): self.arith = grammar.Grammar('S', ('(', ')', 'id', 'let')) self.symbol_table = [dict()] def push(*args): pass def pop(*args): pass self.arith.add_rule('S', ['E']) self.arith.add_rule('E', ['id']) self.arith.add_rule( 'E', ['let', push, '(', 'E', ')', pop, lambda *args: args]) self.action_table, self.goto_table, self.start_state = build_parsing_table( self.arith, LR0(self.arith.START, 0, 0), disable_mapping=True) self.driver = Driver(self.action_table, dict(self.goto_table), self.start_state)