Package libxyz :: Package parser :: Module lr
[hide private]
[frames] | no frames]

Source Code for Module libxyz.parser.lr

  1  #-*- coding: utf8 -* 
  2  # 
  3  # Max E. Kuznecov <syhpoon@syhpoon.name> 2008 
  4  # 
  5  # This file is part of XYZCommander. 
  6  # XYZCommander is free software: you can redistribute it and/or modify 
  7  # it under the terms of the GNU Lesser Public License as published by 
  8  # the Free Software Foundation, either version 3 of the License, or 
  9  # (at your option) any later version. 
 10  # XYZCommander is distributed in the hope that it will be useful, 
 11  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
 12  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 
 13  # GNU Lesser Public License for more details. 
 14  # You should have received a copy of the GNU Lesser Public License 
 15  # along with XYZCommander. If not, see <http://www.gnu.org/licenses/>. 
 16   
 17  ''' 
 18  LR parser stuff 
 19  ''' 
 20   
21 -class ActionTable(object):
22 """ 23 Action table for LR parsing 24 """ 25
26 - def __init__(self):
27 self._table = {}
28 29 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 30
31 - def add(self, state, token, args):
32 """ 33 Add table entry 34 """ 35 36 if state not in self._table: 37 self._table[state] = {} 38 39 self._table[state][token] = args
40 41 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 42
43 - def get(self, state, token):
44 """ 45 Get action for state and token or raise KeyError 46 """ 47 48 return self._table[state][token]
49 50 #++++++++++++++++++++++++++++++++++++++++++++++++ 51
52 -class GotoTable(object):
53 """ 54 Goto table 55 """ 56
57 - def __init__(self):
58 self._table = {}
59 60 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 61
62 - def add(self, state, ntoken, newstate):
63 """ 64 Add table entry 65 """ 66 67 if state not in self._table: 68 self._table[state] = {} 69 70 self._table[state][ntoken] = newstate
71 72 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 73
74 - def get(self, state, ntoken):
75 return self._table[state][ntoken]
76 77 #++++++++++++++++++++++++++++++++++++++++++++++++ 78
79 -class Rules(object):
80 """ 81 Parsing rules 82 """ 83
84 - def __init__(self):
85 self._rules = {}
86 87 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 88
89 - def add(self, ruleno, ntoken, size):
90 """ 91 Add rule 92 93 @param ruleno: Rule number, used by REDUCE action 94 @param ntoken: Left side non-terminal of the rule 95 @param size: RHS size 96 """ 97 98 self._rules[ruleno] = (ntoken, size)
99 100 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 101
102 - def get(self, ruleno):
103 """ 104 Return tuple: ntoken, size or raise KeyError 105 """ 106 107 return self._rules[ruleno]
108 109 #++++++++++++++++++++++++++++++++++++++++++++++++ 110
111 -class Tree(list):
112 """ 113 Tree class 114 """ 115
116 - def __init__(self):
117 super(Tree, self).__init__()
118 119 #~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ 120
121 - def add(self, obj):
122 self.append(obj)
123