Example #1
0
   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)
Example #2
0
   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(rv):
         self.last_value = rv if isinstance(rv, int) else self.symbol_table[rv]
         return rv

      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(self.lrvalue, LR1(self.lrvalue.START, 0, 0, self.lrvalue.EOF), False)
      self.driver = Driver(self.action_table, self.goto_table, self.start_state)
   def setUp(self):
      syn = syntax.Syntax('input')
      self.result = []

      syn.terminal('NL', None)
      syn.terminal('NUM', None)
      syn.terminal('+', None)
      syn.terminal('-', None)

      syn.repeat(('line',), 'input')
      syn.choice((('NL',), ('expr','NL', lambda x: self.result.append(x))),'line')
      syn.choice((('NUM',), ('expr','expr', '+', lambda x, y: x+y), ('expr','expr', '-', lambda x, y: x-y)),'expr')

      self.grammar = syn.as_grammar()
      
      self.start_item = LR0(self.grammar.START, 0, 0)
      self.action_table, self.goto_table, self.start_state = build_parsing_table(self.grammar, self.start_item, disable_mapping=True)
      self.driver = Driver(self.action_table, self.goto_table, self.start_state)
      
      self.kernel_states = [
         frozenset([
            LR0(self.grammar.START, 0, 0),
            ]),
         frozenset([
            LR0(self.grammar.START, 0, 1),
            ]),
         frozenset([
            LR0('input', 0, 1),
            LR0('input', 1, 1),
            ]),
         frozenset([
            LR0('line', 0, 1),
            ]),
         frozenset([
            LR0('line', 1, 1),
            LR0('expr', 1, 1),
            LR0('expr', 2, 1),
            ]),
         frozenset([
            LR0('expr', 0, 1),
            ]),
         frozenset([
            LR0('input', 0, 2),
            ]),
         frozenset([
            LR0('line', 1, 2),
            ]),
         frozenset([
            LR0('expr', 1, 1),
            LR0('expr', 1, 2),
            LR0('expr', 2, 1),
            LR0('expr', 2, 2),
            ]),
         frozenset([
            LR0('expr', 1, 3),
            ]),
         frozenset([
            LR0('expr', 2, 3),
            ])
         ]
Example #4
0
    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)
Example #5
0
   def test_build_tables_of_arithmetic_grammar(self):
      action_table, goto_table, start_set = build_parsing_table(self.arith, LR0(self.StartExtendedSymbol, 0, 0))
      states = action_table.keys()


      s5 = None      #Shift to state number 5
      s4 = None      #Shift to state number 4
      count = 0
      for state in states:
         if 'id' in action_table[state]:
            s5 = action_table[state]['id'] if s5 == None else s5
            s4 = action_table[state]['('] if s4 == None else s4
            self.assertTrue(s5 == action_table[state]['id'])
            self.assertTrue(s4 == action_table[state]['('])
            count += 1
      
      self.assertTrue(count == 4)
      state_5, state_4 = s5._state_to_shift, s4._state_to_shift

      self.assertTrue(action_table[state_5]['+'] == \
            action_table[state_5]['*'] == \
            action_table[state_5][')'] == \
            action_table[state_5][self.arith.EOF])
      self.assertTrue(len(action_table[state_5]) == 4)

      
      count = 0
      for state in states:
         s = action_table[state]
         try:
            if s['+'] == s['*'] == s[')'] == s[self.arith.EOF]:
               self.assertTrue(hasattr(s['+'], '_semantic_action'))
               count += 1
         except:
            pass

      self.assertTrue(count == 4)

      count = 0
      s7 = None
      for state in states:
         s = action_table[state]
         try:
            if s['+'] == s[')'] == s[self.arith.EOF]:
               count += 1
               if self.assertTrue(hasattr(s['*'], '_state_to_shift')):
                  s7 = s['*'] if s7 == None else s7
                  self.assertTrue(s7 == s['*'])

         except:
            pass

      self.assertTrue(count == 6)
Example #6
0
    def test_build_tables_of_arithmetic_grammar(self):
        action_table, goto_table, start_set = build_parsing_table(
            self.arith, LR0(self.StartExtendedSymbol, 0, 0))
        states = action_table.keys()

        s5 = None  #Shift to state number 5
        s4 = None  #Shift to state number 4
        count = 0
        for state in states:
            if 'id' in action_table[state]:
                s5 = action_table[state]['id'] if s5 == None else s5
                s4 = action_table[state]['('] if s4 == None else s4
                self.assertTrue(s5 == action_table[state]['id'])
                self.assertTrue(s4 == action_table[state]['('])
                count += 1

        self.assertTrue(count == 4)
        state_5, state_4 = s5._state_to_shift, s4._state_to_shift

        self.assertTrue(action_table[state_5]['+'] == \
              action_table[state_5]['*'] == \
              action_table[state_5][')'] == \
              action_table[state_5][self.arith.EOF])
        self.assertTrue(len(action_table[state_5]) == 4)

        count = 0
        for state in states:
            s = action_table[state]
            try:
                if s['+'] == s['*'] == s[')'] == s[self.arith.EOF]:
                    self.assertTrue(hasattr(s['+'], '_semantic_action'))
                    count += 1
            except:
                pass

        self.assertTrue(count == 4)

        count = 0
        s7 = None
        for state in states:
            s = action_table[state]
            try:
                if s['+'] == s[')'] == s[self.arith.EOF]:
                    count += 1
                    if self.assertTrue(hasattr(s['*'], '_state_to_shift')):
                        s7 = s['*'] if s7 == None else s7
                        self.assertTrue(s7 == s['*'])

            except:
                pass

        self.assertTrue(count == 6)
Example #7
0
   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)
Example #8
0
    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)
Example #9
0
   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)
Example #10
0
    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)
Example #11
0
    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(rv):
            self.last_value = rv if isinstance(rv,
                                               int) else self.symbol_table[rv]
            return rv

        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(
            self.lrvalue, LR1(self.lrvalue.START, 0, 0, self.lrvalue.EOF),
            False)
        self.driver = Driver(self.action_table, self.goto_table,
                             self.start_state)
Example #12
0
 def test_shift_reduce_conflict_from_LR0_solved_with_LR1(self):
     action_table, goto_table, start_set = build_parsing_table(
         self.lrvalue, LR1(self.lrvalue.START, 0, 0, self.lrvalue.EOF),
         False)
     self.assertTrue(True)  #No raised any exception
Example #13
0

def prompt(*args):
    print ">>> ",


grammar = from_string(
    rpcalc_grammar,
    "input",
    print_result=print_result,
    probe=probe,
    prompt=prompt,
    up=lambda x: x,
    add=lambda x, y: x + y,
    sub=lambda x, y: x - y,
    mul=lambda x, y: x * y,
    div=lambda x, y: x / y,
    mod=lambda x, y: x % y,
    neg=lambda x: -x,
)

states, gotos, first = build_parsing_table(grammar, LR0(grammar.START, 0, 0))
driver = Driver(states, gotos, first)

print "Reverse polish calculator. Write expressions like "
print " >>> 2 + 3       (result in 2 + 3 = 5)  or "
print " >>> 2 + 4 * 5   (result in 2 + (4 * 5) = 22)"
print
prompt()
driver.parse(CalcLexer(sys.stdin))
Example #14
0
def probe(*args):
    print "--", len(args), "-", " ".join(map(str, args))


def prompt(*args):
    print ">>> ",


grammar = from_string(rpcalc_grammar,
                      'input',
                      print_result=print_result,
                      probe=probe,
                      prompt=prompt,
                      up=lambda x: x,
                      add=lambda x, y: x + y,
                      sub=lambda x, y: x - y,
                      mul=lambda x, y: x * y,
                      div=lambda x, y: x / y,
                      mod=lambda x, y: x % y,
                      neg=lambda x: -x)

states, gotos, first = build_parsing_table(grammar, LR0(grammar.START, 0, 0))
driver = Driver(states, gotos, first)

print "Reverse polish calculator. Write expressions like "
print " >>> 2 + 3       (result in 2 + 3 = 5)  or "
print " >>> 2 + 4 * 5   (result in 2 + (4 * 5) = 22)"
print
prompt()
driver.parse(CalcLexer(sys.stdin))
Example #15
0
    def setUp(self):
        syn = syntax.Syntax('input')
        self.result = []

        syn.terminal('NL', None)
        syn.terminal('NUM', None)
        syn.terminal('+', None)
        syn.terminal('-', None)

        syn.repeat(('line', ), 'input')
        syn.choice((('NL', ), ('expr', 'NL', lambda x: self.result.append(x))),
                   'line')
        syn.choice((('NUM', ), ('expr', 'expr', '+', lambda x, y: x + y),
                    ('expr', 'expr', '-', lambda x, y: x - y)), 'expr')

        self.grammar = syn.as_grammar()

        self.start_item = LR0(self.grammar.START, 0, 0)
        self.action_table, self.goto_table, self.start_state = build_parsing_table(
            self.grammar, self.start_item, disable_mapping=True)
        self.driver = Driver(self.action_table, self.goto_table,
                             self.start_state)

        self.kernel_states = [
            frozenset([
                LR0(self.grammar.START, 0, 0),
            ]),
            frozenset([
                LR0(self.grammar.START, 0, 1),
            ]),
            frozenset([
                LR0('input', 0, 1),
                LR0('input', 1, 1),
            ]),
            frozenset([
                LR0('line', 0, 1),
            ]),
            frozenset([
                LR0('line', 1, 1),
                LR0('expr', 1, 1),
                LR0('expr', 2, 1),
            ]),
            frozenset([
                LR0('expr', 0, 1),
            ]),
            frozenset([
                LR0('input', 0, 2),
            ]),
            frozenset([
                LR0('line', 1, 2),
            ]),
            frozenset([
                LR0('expr', 1, 1),
                LR0('expr', 1, 2),
                LR0('expr', 2, 1),
                LR0('expr', 2, 2),
            ]),
            frozenset([
                LR0('expr', 1, 3),
            ]),
            frozenset([
                LR0('expr', 2, 3),
            ])
        ]
Example #16
0
 def test_build_tables(self):
     action_table, goto_table, start_set = build_parsing_table(
         self.arith, LR0(self.StartExtendedSymbol, 0, 0))
     self.assertTrue(len(action_table) == 12)
Example #17
0
 def test_shift_reduce_conflict_from_LR0_solved_with_LR1(self):
    action_table, goto_table, start_set = build_parsing_table(self.lrvalue, LR1(self.lrvalue.START, 0, 0, self.lrvalue.EOF), False)
    self.assertTrue(True) #No raised any exception
Example #18
0
 def test_build_tables(self):
    action_table, goto_table, start_set = build_parsing_table(self.arith, LR0(self.StartExtendedSymbol, 0, 0))
    self.assertTrue(len(action_table) == 12)