comparison python/pyyacc.py @ 395:3b0c495e3008

Speed improvements
author Windel Bouwman
date Fri, 23 May 2014 14:28:03 +0200
parents 173e20a47fda
children
comparison
equal deleted inserted replaced
394:988f3fb861e4 395:3b0c495e3008
300 self.production = production 300 self.production = production
301 self.dotpos = dotpos 301 self.dotpos = dotpos
302 assert self.dotpos <= len(self.production.symbols) 302 assert self.dotpos <= len(self.production.symbols)
303 self.look_ahead = look_ahead 303 self.look_ahead = look_ahead
304 self._is_shift = self.dotpos < len(self.production.symbols) 304 self._is_shift = self.dotpos < len(self.production.symbols)
305 305 self.IsShift = self._is_shift
306 def getdata(self): 306 if self.IsShift:
307 """ Gets the members as a tuple """ 307 self.Next = self.production.symbols[self.dotpos]
308 return (self.production, self.dotpos, self.look_ahead) 308 self._data = (self.production, self.dotpos, self.look_ahead)
309 self._hash = self._data.__hash__()
309 310
310 def __eq__(self, other): 311 def __eq__(self, other):
311 if type(other) is type(self): 312 if type(other) is type(self):
312 return self.getdata() == other.getdata() 313 return self._data == other._data
313 return False 314 return False
314 315
315 def __hash__(self): 316 def __hash__(self):
316 return self.getdata().__hash__() 317 return self._hash
317 318
318 @property 319 @property
319 def IsReduce(self): 320 def IsReduce(self):
320 """ Check if this item has the dot at the end """ 321 """ Check if this item has the dot at the end """
321 return not self._is_shift 322 return not self._is_shift
322 323
323 @property
324 def IsShift(self):
325 """ Check if this item is a shift item, i.e. the dot can proceed """
326 return self._is_shift
327
328 @property
329 def Next(self):
330 """ Returns the symbol after the dot """
331 return self.production.symbols[self.dotpos]
332
333 def can_shift_over(self, symbol): 324 def can_shift_over(self, symbol):
334 """ Determines if this item can shift over the given symbol """ 325 """ Determines if this item can shift over the given symbol """
335 return self.IsShift and self.Next == symbol 326 return self._is_shift and self.Next == symbol
336 327
337 def shifted(self): 328 def shifted(self):
338 """ Creates a new item that is shifted one position """ 329 """ Creates a new item that is shifted one position """
339 return Item(self.production, self.dotpos + 1, self.look_ahead) 330 return Item(self.production, self.dotpos + 1, self.look_ahead)
340 331
354 args = (name, predot, postdot, self.look_ahead) 345 args = (name, predot, postdot, self.look_ahead)
355 return '[{0} -> {1} . {2} -> {3}]'.format(*args) 346 return '[{0} -> {1} . {2} -> {3}]'.format(*args)
356 347
357 348
358 class LRParser: 349 class LRParser:
359 """ LR parser automata """ 350 """ LR parser automata. This class takes goto and action table
351 and can then process a sequence of tokens.
352 """
360 def __init__(self, action_table, goto_table, start_symbol): 353 def __init__(self, action_table, goto_table, start_symbol):
361 self.action_table = action_table 354 self.action_table = action_table
362 self.goto_table = goto_table 355 self.goto_table = goto_table
363 self.start_symbol = start_symbol 356 self.start_symbol = start_symbol
364 357