Back to index

enigmail  1.4.3
Public Member Functions | Public Attributes
ply.yacc.LRParser Class Reference

List of all members.

Public Member Functions

def __init__
def errok
def restart
def parse
def parsedebug
def parseopt
def parseopt_notrack

Public Attributes

 productions
 action
 goto
 errorfunc
 errorok
 statestack
 symstack

Detailed Description

Definition at line 239 of file yacc.py.


Constructor & Destructor Documentation

def ply.yacc.LRParser.__init__ (   self,
  lrtab,
  errorf 
)

Definition at line 240 of file yacc.py.

00240 
00241     def __init__(self,lrtab,errorf):
00242         self.productions = lrtab.lr_productions
00243         self.action      = lrtab.lr_action
00244         self.goto        = lrtab.lr_goto
00245         self.errorfunc   = errorf


Member Function Documentation

def ply.yacc.LRParser.errok (   self)

Definition at line 246 of file yacc.py.

00246 
00247     def errok(self):
00248         self.errorok     = 1

Here is the caller graph for this function:

def ply.yacc.LRParser.parse (   self,
  input = None,
  lexer = None,
  debug = 0,
  tracking = 0,
  tokenfunc = None 
)

Definition at line 257 of file yacc.py.

00257 
00258     def parse(self,input=None,lexer=None,debug=0,tracking=0,tokenfunc=None):
00259         if debug or yaccdevel:
00260             if isinstance(debug,int):
00261                 debug = PlyLogger(sys.stderr)
00262             return self.parsedebug(input,lexer,debug,tracking,tokenfunc)
00263         elif tracking:
00264             return self.parseopt(input,lexer,debug,tracking,tokenfunc)
00265         else:
00266             return self.parseopt_notrack(input,lexer,debug,tracking,tokenfunc)
00267         

Here is the call graph for this function:

def ply.yacc.LRParser.parsedebug (   self,
  input = None,
  lexer = None,
  debug = None,
  tracking = 0,
  tokenfunc = None 
)

Definition at line 282 of file yacc.py.

00282 
00283     def parsedebug(self,input=None,lexer=None,debug=None,tracking=0,tokenfunc=None):
00284         lookahead = None                 # Current lookahead symbol
00285         lookaheadstack = [ ]             # Stack of lookahead symbols
00286         actions = self.action            # Local reference to action table (to avoid lookup on self.)
00287         goto    = self.goto              # Local reference to goto table (to avoid lookup on self.)
00288         prod    = self.productions       # Local reference to production list (to avoid lookup on self.)
00289         pslice  = YaccProduction(None)   # Production object passed to grammar rules
00290         errorcount = 0                   # Used during error recovery 
00291 
00292         # --! DEBUG
00293         debug.info("PLY: PARSE DEBUG START")
00294         # --! DEBUG
00295 
00296         # If no lexer was given, we will try to use the lex module
00297         if not lexer:
00298             lex = load_ply_lex()
00299             lexer = lex.lexer
00300 
00301         # Set up the lexer and parser objects on pslice
00302         pslice.lexer = lexer
00303         pslice.parser = self
00304 
00305         # If input was supplied, pass to lexer
00306         if input is not None:
00307             lexer.input(input)
00308 
00309         if tokenfunc is None:
00310            # Tokenize function
00311            get_token = lexer.token
00312         else:
00313            get_token = tokenfunc
00314 
00315         # Set up the state and symbol stacks
00316 
00317         statestack = [ ]                # Stack of parsing states
00318         self.statestack = statestack
00319         symstack   = [ ]                # Stack of grammar symbols
00320         self.symstack = symstack
00321 
00322         pslice.stack = symstack         # Put in the production
00323         errtoken   = None               # Err token
00324 
00325         # The start state is assumed to be (0,$end)
00326 
00327         statestack.append(0)
00328         sym = YaccSymbol()
00329         sym.type = "$end"
00330         symstack.append(sym)
00331         state = 0
00332         while 1:
00333             # Get the next symbol on the input.  If a lookahead symbol
00334             # is already set, we just use that. Otherwise, we'll pull
00335             # the next token off of the lookaheadstack or from the lexer
00336 
00337             # --! DEBUG
00338             debug.debug('')
00339             debug.debug('State  : %s', state)
00340             # --! DEBUG
00341 
00342             if not lookahead:
00343                 if not lookaheadstack:
00344                     lookahead = get_token()     # Get the next token
00345                 else:
00346                     lookahead = lookaheadstack.pop()
00347                 if not lookahead:
00348                     lookahead = YaccSymbol()
00349                     lookahead.type = "$end"
00350 
00351             # --! DEBUG
00352             debug.debug('Stack  : %s',
00353                         ("%s . %s" % (" ".join([xx.type for xx in symstack][1:]), str(lookahead))).lstrip())
00354             # --! DEBUG
00355 
00356             # Check the action table
00357             ltype = lookahead.type
00358             t = actions[state].get(ltype)
00359 
00360             if t is not None:
00361                 if t > 0:
00362                     # shift a symbol on the stack
00363                     statestack.append(t)
00364                     state = t
00365                     
00366                     # --! DEBUG
00367                     debug.debug("Action : Shift and goto state %s", t)
00368                     # --! DEBUG
00369 
00370                     symstack.append(lookahead)
00371                     lookahead = None
00372 
00373                     # Decrease error count on successful shift
00374                     if errorcount: errorcount -=1
00375                     continue
00376 
00377                 if t < 0:
00378                     # reduce a symbol on the stack, emit a production
00379                     p = prod[-t]
00380                     pname = p.name
00381                     plen  = p.len
00382 
00383                     # Get production function
00384                     sym = YaccSymbol()
00385                     sym.type = pname       # Production name
00386                     sym.value = None
00387 
00388                     # --! DEBUG
00389                     if plen:
00390                         debug.info("Action : Reduce rule [%s] with %s and goto state %d", p.str, "["+",".join([format_stack_entry(_v.value) for _v in symstack[-plen:]])+"]",-t)
00391                     else:
00392                         debug.info("Action : Reduce rule [%s] with %s and goto state %d", p.str, [],-t)
00393                         
00394                     # --! DEBUG
00395 
00396                     if plen:
00397                         targ = symstack[-plen-1:]
00398                         targ[0] = sym
00399 
00400                         # --! TRACKING
00401                         if tracking:
00402                            t1 = targ[1]
00403                            sym.lineno = t1.lineno
00404                            sym.lexpos = t1.lexpos
00405                            t1 = targ[-1]
00406                            sym.endlineno = getattr(t1,"endlineno",t1.lineno)
00407                            sym.endlexpos = getattr(t1,"endlexpos",t1.lexpos)
00408 
00409                         # --! TRACKING
00410 
00411                         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00412                         # The code enclosed in this section is duplicated 
00413                         # below as a performance optimization.  Make sure
00414                         # changes get made in both locations.
00415 
00416                         pslice.slice = targ
00417                         
00418                         try:
00419                             # Call the grammar rule with our special slice object
00420                             del symstack[-plen:]
00421                             del statestack[-plen:]
00422                             p.callable(pslice)
00423                             # --! DEBUG
00424                             debug.info("Result : %s", format_result(pslice[0]))
00425                             # --! DEBUG
00426                             symstack.append(sym)
00427                             state = goto[statestack[-1]][pname]
00428                             statestack.append(state)
00429                         except SyntaxError:
00430                             # If an error was set. Enter error recovery state
00431                             lookaheadstack.append(lookahead)
00432                             symstack.pop()
00433                             statestack.pop()
00434                             state = statestack[-1]
00435                             sym.type = 'error'
00436                             lookahead = sym
00437                             errorcount = error_count
00438                             self.errorok = 0
00439                         continue
00440                         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00441     
00442                     else:
00443 
00444                         # --! TRACKING
00445                         if tracking:
00446                            sym.lineno = lexer.lineno
00447                            sym.lexpos = lexer.lexpos
00448                         # --! TRACKING
00449 
00450                         targ = [ sym ]
00451 
00452                         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00453                         # The code enclosed in this section is duplicated 
00454                         # above as a performance optimization.  Make sure
00455                         # changes get made in both locations.
00456 
00457                         pslice.slice = targ
00458 
00459                         try:
00460                             # Call the grammar rule with our special slice object
00461                             p.callable(pslice)
00462                             # --! DEBUG
00463                             debug.info("Result : %s", format_result(pslice[0]))
00464                             # --! DEBUG
00465                             symstack.append(sym)
00466                             state = goto[statestack[-1]][pname]
00467                             statestack.append(state)
00468                         except SyntaxError:
00469                             # If an error was set. Enter error recovery state
00470                             lookaheadstack.append(lookahead)
00471                             symstack.pop()
00472                             statestack.pop()
00473                             state = statestack[-1]
00474                             sym.type = 'error'
00475                             lookahead = sym
00476                             errorcount = error_count
00477                             self.errorok = 0
00478                         continue
00479                         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00480 
00481                 if t == 0:
00482                     n = symstack[-1]
00483                     result = getattr(n,"value",None)
00484                     # --! DEBUG
00485                     debug.info("Done   : Returning %s", format_result(result))
00486                     debug.info("PLY: PARSE DEBUG END")
00487                     # --! DEBUG
00488                     return result
00489 
00490             if t == None:
00491 
00492                 # --! DEBUG
00493                 debug.error('Error  : %s',
00494                             ("%s . %s" % (" ".join([xx.type for xx in symstack][1:]), str(lookahead))).lstrip())
00495                 # --! DEBUG
00496 
00497                 # We have some kind of parsing error here.  To handle
00498                 # this, we are going to push the current token onto
00499                 # the tokenstack and replace it with an 'error' token.
00500                 # If there are any synchronization rules, they may
00501                 # catch it.
00502                 #
00503                 # In addition to pushing the error token, we call call
00504                 # the user defined p_error() function if this is the
00505                 # first syntax error.  This function is only called if
00506                 # errorcount == 0.
00507                 if errorcount == 0 or self.errorok:
00508                     errorcount = error_count
00509                     self.errorok = 0
00510                     errtoken = lookahead
00511                     if errtoken.type == "$end":
00512                         errtoken = None               # End of file!
00513                     if self.errorfunc:
00514                         global errok,token,restart
00515                         errok = self.errok        # Set some special functions available in error recovery
00516                         token = get_token
00517                         restart = self.restart
00518                         if errtoken and not hasattr(errtoken,'lexer'):
00519                             errtoken.lexer = lexer
00520                         tok = self.errorfunc(errtoken)
00521                         del errok, token, restart   # Delete special functions
00522 
00523                         if self.errorok:
00524                             # User must have done some kind of panic
00525                             # mode recovery on their own.  The
00526                             # returned token is the next lookahead
00527                             lookahead = tok
00528                             errtoken = None
00529                             continue
00530                     else:
00531                         if errtoken:
00532                             if hasattr(errtoken,"lineno"): lineno = lookahead.lineno
00533                             else: lineno = 0
00534                             if lineno:
00535                                 sys.stderr.write("yacc: Syntax error at line %d, token=%s\n" % (lineno, errtoken.type))
00536                             else:
00537                                 sys.stderr.write("yacc: Syntax error, token=%s" % errtoken.type)
00538                         else:
00539                             sys.stderr.write("yacc: Parse error in input. EOF\n")
00540                             return
00541 
00542                 else:
00543                     errorcount = error_count
00544 
00545                 # case 1:  the statestack only has 1 entry on it.  If we're in this state, the
00546                 # entire parse has been rolled back and we're completely hosed.   The token is
00547                 # discarded and we just keep going.
00548 
00549                 if len(statestack) <= 1 and lookahead.type != "$end":
00550                     lookahead = None
00551                     errtoken = None
00552                     state = 0
00553                     # Nuke the pushback stack
00554                     del lookaheadstack[:]
00555                     continue
00556 
00557                 # case 2: the statestack has a couple of entries on it, but we're
00558                 # at the end of the file. nuke the top entry and generate an error token
00559 
00560                 # Start nuking entries on the stack
00561                 if lookahead.type == "$end":
00562                     # Whoa. We're really hosed here. Bail out
00563                     return
00564 
00565                 if lookahead.type != 'error':
00566                     sym = symstack[-1]
00567                     if sym.type == 'error':
00568                         # Hmmm. Error is on top of stack, we'll just nuke input
00569                         # symbol and continue
00570                         lookahead = None
00571                         continue
00572                     t = YaccSymbol()
00573                     t.type = 'error'
00574                     if hasattr(lookahead,"lineno"):
00575                         t.lineno = lookahead.lineno
00576                     t.value = lookahead
00577                     lookaheadstack.append(lookahead)
00578                     lookahead = t
00579                 else:
00580                     symstack.pop()
00581                     statestack.pop()
00582                     state = statestack[-1]       # Potential bug fix
00583 
00584                 continue
00585 
00586             # Call an error function here
00587             raise RuntimeError("yacc: internal parser error!!!\n")

Here is the call graph for this function:

Here is the caller graph for this function:

def ply.yacc.LRParser.parseopt (   self,
  input = None,
  lexer = None,
  debug = 0,
  tracking = 0,
  tokenfunc = None 
)

Definition at line 597 of file yacc.py.

00597 
00598     def parseopt(self,input=None,lexer=None,debug=0,tracking=0,tokenfunc=None):
00599         lookahead = None                 # Current lookahead symbol
00600         lookaheadstack = [ ]             # Stack of lookahead symbols
00601         actions = self.action            # Local reference to action table (to avoid lookup on self.)
00602         goto    = self.goto              # Local reference to goto table (to avoid lookup on self.)
00603         prod    = self.productions       # Local reference to production list (to avoid lookup on self.)
00604         pslice  = YaccProduction(None)   # Production object passed to grammar rules
00605         errorcount = 0                   # Used during error recovery 
00606 
00607         # If no lexer was given, we will try to use the lex module
00608         if not lexer:
00609             lex = load_ply_lex()
00610             lexer = lex.lexer
00611         
00612         # Set up the lexer and parser objects on pslice
00613         pslice.lexer = lexer
00614         pslice.parser = self
00615 
00616         # If input was supplied, pass to lexer
00617         if input is not None:
00618             lexer.input(input)
00619 
00620         if tokenfunc is None:
00621            # Tokenize function
00622            get_token = lexer.token
00623         else:
00624            get_token = tokenfunc
00625 
00626         # Set up the state and symbol stacks
00627 
00628         statestack = [ ]                # Stack of parsing states
00629         self.statestack = statestack
00630         symstack   = [ ]                # Stack of grammar symbols
00631         self.symstack = symstack
00632 
00633         pslice.stack = symstack         # Put in the production
00634         errtoken   = None               # Err token
00635 
00636         # The start state is assumed to be (0,$end)
00637 
00638         statestack.append(0)
00639         sym = YaccSymbol()
00640         sym.type = '$end'
00641         symstack.append(sym)
00642         state = 0
00643         while 1:
00644             # Get the next symbol on the input.  If a lookahead symbol
00645             # is already set, we just use that. Otherwise, we'll pull
00646             # the next token off of the lookaheadstack or from the lexer
00647 
00648             if not lookahead:
00649                 if not lookaheadstack:
00650                     lookahead = get_token()     # Get the next token
00651                 else:
00652                     lookahead = lookaheadstack.pop()
00653                 if not lookahead:
00654                     lookahead = YaccSymbol()
00655                     lookahead.type = '$end'
00656 
00657             # Check the action table
00658             ltype = lookahead.type
00659             t = actions[state].get(ltype)
00660 
00661             if t is not None:
00662                 if t > 0:
00663                     # shift a symbol on the stack
00664                     statestack.append(t)
00665                     state = t
00666 
00667                     symstack.append(lookahead)
00668                     lookahead = None
00669 
00670                     # Decrease error count on successful shift
00671                     if errorcount: errorcount -=1
00672                     continue
00673 
00674                 if t < 0:
00675                     # reduce a symbol on the stack, emit a production
00676                     p = prod[-t]
00677                     pname = p.name
00678                     plen  = p.len
00679 
00680                     # Get production function
00681                     sym = YaccSymbol()
00682                     sym.type = pname       # Production name
00683                     sym.value = None
00684 
00685                     if plen:
00686                         targ = symstack[-plen-1:]
00687                         targ[0] = sym
00688 
00689                         # --! TRACKING
00690                         if tracking:
00691                            t1 = targ[1]
00692                            sym.lineno = t1.lineno
00693                            sym.lexpos = t1.lexpos
00694                            t1 = targ[-1]
00695                            sym.endlineno = getattr(t1,"endlineno",t1.lineno)
00696                            sym.endlexpos = getattr(t1,"endlexpos",t1.lexpos)
00697 
00698                         # --! TRACKING
00699 
00700                         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00701                         # The code enclosed in this section is duplicated 
00702                         # below as a performance optimization.  Make sure
00703                         # changes get made in both locations.
00704 
00705                         pslice.slice = targ
00706                         
00707                         try:
00708                             # Call the grammar rule with our special slice object
00709                             del symstack[-plen:]
00710                             del statestack[-plen:]
00711                             p.callable(pslice)
00712                             symstack.append(sym)
00713                             state = goto[statestack[-1]][pname]
00714                             statestack.append(state)
00715                         except SyntaxError:
00716                             # If an error was set. Enter error recovery state
00717                             lookaheadstack.append(lookahead)
00718                             symstack.pop()
00719                             statestack.pop()
00720                             state = statestack[-1]
00721                             sym.type = 'error'
00722                             lookahead = sym
00723                             errorcount = error_count
00724                             self.errorok = 0
00725                         continue
00726                         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00727     
00728                     else:
00729 
00730                         # --! TRACKING
00731                         if tracking:
00732                            sym.lineno = lexer.lineno
00733                            sym.lexpos = lexer.lexpos
00734                         # --! TRACKING
00735 
00736                         targ = [ sym ]
00737 
00738                         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00739                         # The code enclosed in this section is duplicated 
00740                         # above as a performance optimization.  Make sure
00741                         # changes get made in both locations.
00742 
00743                         pslice.slice = targ
00744 
00745                         try:
00746                             # Call the grammar rule with our special slice object
00747                             p.callable(pslice)
00748                             symstack.append(sym)
00749                             state = goto[statestack[-1]][pname]
00750                             statestack.append(state)
00751                         except SyntaxError:
00752                             # If an error was set. Enter error recovery state
00753                             lookaheadstack.append(lookahead)
00754                             symstack.pop()
00755                             statestack.pop()
00756                             state = statestack[-1]
00757                             sym.type = 'error'
00758                             lookahead = sym
00759                             errorcount = error_count
00760                             self.errorok = 0
00761                         continue
00762                         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00763 
00764                 if t == 0:
00765                     n = symstack[-1]
00766                     return getattr(n,"value",None)
00767 
00768             if t == None:
00769 
00770                 # We have some kind of parsing error here.  To handle
00771                 # this, we are going to push the current token onto
00772                 # the tokenstack and replace it with an 'error' token.
00773                 # If there are any synchronization rules, they may
00774                 # catch it.
00775                 #
00776                 # In addition to pushing the error token, we call call
00777                 # the user defined p_error() function if this is the
00778                 # first syntax error.  This function is only called if
00779                 # errorcount == 0.
00780                 if errorcount == 0 or self.errorok:
00781                     errorcount = error_count
00782                     self.errorok = 0
00783                     errtoken = lookahead
00784                     if errtoken.type == '$end':
00785                         errtoken = None               # End of file!
00786                     if self.errorfunc:
00787                         global errok,token,restart
00788                         errok = self.errok        # Set some special functions available in error recovery
00789                         token = get_token
00790                         restart = self.restart
00791                         if errtoken and not hasattr(errtoken,'lexer'):
00792                             errtoken.lexer = lexer
00793                         tok = self.errorfunc(errtoken)
00794                         del errok, token, restart   # Delete special functions
00795 
00796                         if self.errorok:
00797                             # User must have done some kind of panic
00798                             # mode recovery on their own.  The
00799                             # returned token is the next lookahead
00800                             lookahead = tok
00801                             errtoken = None
00802                             continue
00803                     else:
00804                         if errtoken:
00805                             if hasattr(errtoken,"lineno"): lineno = lookahead.lineno
00806                             else: lineno = 0
00807                             if lineno:
00808                                 sys.stderr.write("yacc: Syntax error at line %d, token=%s\n" % (lineno, errtoken.type))
00809                             else:
00810                                 sys.stderr.write("yacc: Syntax error, token=%s" % errtoken.type)
00811                         else:
00812                             sys.stderr.write("yacc: Parse error in input. EOF\n")
00813                             return
00814 
00815                 else:
00816                     errorcount = error_count
00817 
00818                 # case 1:  the statestack only has 1 entry on it.  If we're in this state, the
00819                 # entire parse has been rolled back and we're completely hosed.   The token is
00820                 # discarded and we just keep going.
00821 
00822                 if len(statestack) <= 1 and lookahead.type != '$end':
00823                     lookahead = None
00824                     errtoken = None
00825                     state = 0
00826                     # Nuke the pushback stack
00827                     del lookaheadstack[:]
00828                     continue
00829 
00830                 # case 2: the statestack has a couple of entries on it, but we're
00831                 # at the end of the file. nuke the top entry and generate an error token
00832 
00833                 # Start nuking entries on the stack
00834                 if lookahead.type == '$end':
00835                     # Whoa. We're really hosed here. Bail out
00836                     return
00837 
00838                 if lookahead.type != 'error':
00839                     sym = symstack[-1]
00840                     if sym.type == 'error':
00841                         # Hmmm. Error is on top of stack, we'll just nuke input
00842                         # symbol and continue
00843                         lookahead = None
00844                         continue
00845                     t = YaccSymbol()
00846                     t.type = 'error'
00847                     if hasattr(lookahead,"lineno"):
00848                         t.lineno = lookahead.lineno
00849                     t.value = lookahead
00850                     lookaheadstack.append(lookahead)
00851                     lookahead = t
00852                 else:
00853                     symstack.pop()
00854                     statestack.pop()
00855                     state = statestack[-1]       # Potential bug fix
00856 
00857                 continue
00858 
00859             # Call an error function here
00860             raise RuntimeError("yacc: internal parser error!!!\n")

Here is the call graph for this function:

Here is the caller graph for this function:

def ply.yacc.LRParser.parseopt_notrack (   self,
  input = None,
  lexer = None,
  debug = 0,
  tracking = 0,
  tokenfunc = None 
)

Definition at line 869 of file yacc.py.

00869 
00870     def parseopt_notrack(self,input=None,lexer=None,debug=0,tracking=0,tokenfunc=None):
00871         lookahead = None                 # Current lookahead symbol
00872         lookaheadstack = [ ]             # Stack of lookahead symbols
00873         actions = self.action            # Local reference to action table (to avoid lookup on self.)
00874         goto    = self.goto              # Local reference to goto table (to avoid lookup on self.)
00875         prod    = self.productions       # Local reference to production list (to avoid lookup on self.)
00876         pslice  = YaccProduction(None)   # Production object passed to grammar rules
00877         errorcount = 0                   # Used during error recovery 
00878 
00879         # If no lexer was given, we will try to use the lex module
00880         if not lexer:
00881             lex = load_ply_lex()
00882             lexer = lex.lexer
00883         
00884         # Set up the lexer and parser objects on pslice
00885         pslice.lexer = lexer
00886         pslice.parser = self
00887 
00888         # If input was supplied, pass to lexer
00889         if input is not None:
00890             lexer.input(input)
00891 
00892         if tokenfunc is None:
00893            # Tokenize function
00894            get_token = lexer.token
00895         else:
00896            get_token = tokenfunc
00897 
00898         # Set up the state and symbol stacks
00899 
00900         statestack = [ ]                # Stack of parsing states
00901         self.statestack = statestack
00902         symstack   = [ ]                # Stack of grammar symbols
00903         self.symstack = symstack
00904 
00905         pslice.stack = symstack         # Put in the production
00906         errtoken   = None               # Err token
00907 
00908         # The start state is assumed to be (0,$end)
00909 
00910         statestack.append(0)
00911         sym = YaccSymbol()
00912         sym.type = '$end'
00913         symstack.append(sym)
00914         state = 0
00915         while 1:
00916             # Get the next symbol on the input.  If a lookahead symbol
00917             # is already set, we just use that. Otherwise, we'll pull
00918             # the next token off of the lookaheadstack or from the lexer
00919 
00920             if not lookahead:
00921                 if not lookaheadstack:
00922                     lookahead = get_token()     # Get the next token
00923                 else:
00924                     lookahead = lookaheadstack.pop()
00925                 if not lookahead:
00926                     lookahead = YaccSymbol()
00927                     lookahead.type = '$end'
00928 
00929             # Check the action table
00930             ltype = lookahead.type
00931             t = actions[state].get(ltype)
00932 
00933             if t is not None:
00934                 if t > 0:
00935                     # shift a symbol on the stack
00936                     statestack.append(t)
00937                     state = t
00938 
00939                     symstack.append(lookahead)
00940                     lookahead = None
00941 
00942                     # Decrease error count on successful shift
00943                     if errorcount: errorcount -=1
00944                     continue
00945 
00946                 if t < 0:
00947                     # reduce a symbol on the stack, emit a production
00948                     p = prod[-t]
00949                     pname = p.name
00950                     plen  = p.len
00951 
00952                     # Get production function
00953                     sym = YaccSymbol()
00954                     sym.type = pname       # Production name
00955                     sym.value = None
00956 
00957                     if plen:
00958                         targ = symstack[-plen-1:]
00959                         targ[0] = sym
00960 
00961                         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00962                         # The code enclosed in this section is duplicated 
00963                         # below as a performance optimization.  Make sure
00964                         # changes get made in both locations.
00965 
00966                         pslice.slice = targ
00967                         
00968                         try:
00969                             # Call the grammar rule with our special slice object
00970                             del symstack[-plen:]
00971                             del statestack[-plen:]
00972                             p.callable(pslice)
00973                             symstack.append(sym)
00974                             state = goto[statestack[-1]][pname]
00975                             statestack.append(state)
00976                         except SyntaxError:
00977                             # If an error was set. Enter error recovery state
00978                             lookaheadstack.append(lookahead)
00979                             symstack.pop()
00980                             statestack.pop()
00981                             state = statestack[-1]
00982                             sym.type = 'error'
00983                             lookahead = sym
00984                             errorcount = error_count
00985                             self.errorok = 0
00986                         continue
00987                         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00988     
00989                     else:
00990 
00991                         targ = [ sym ]
00992 
00993                         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
00994                         # The code enclosed in this section is duplicated 
00995                         # above as a performance optimization.  Make sure
00996                         # changes get made in both locations.
00997 
00998                         pslice.slice = targ
00999 
01000                         try:
01001                             # Call the grammar rule with our special slice object
01002                             p.callable(pslice)
01003                             symstack.append(sym)
01004                             state = goto[statestack[-1]][pname]
01005                             statestack.append(state)
01006                         except SyntaxError:
01007                             # If an error was set. Enter error recovery state
01008                             lookaheadstack.append(lookahead)
01009                             symstack.pop()
01010                             statestack.pop()
01011                             state = statestack[-1]
01012                             sym.type = 'error'
01013                             lookahead = sym
01014                             errorcount = error_count
01015                             self.errorok = 0
01016                         continue
01017                         # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
01018 
01019                 if t == 0:
01020                     n = symstack[-1]
01021                     return getattr(n,"value",None)
01022 
01023             if t == None:
01024 
01025                 # We have some kind of parsing error here.  To handle
01026                 # this, we are going to push the current token onto
01027                 # the tokenstack and replace it with an 'error' token.
01028                 # If there are any synchronization rules, they may
01029                 # catch it.
01030                 #
01031                 # In addition to pushing the error token, we call call
01032                 # the user defined p_error() function if this is the
01033                 # first syntax error.  This function is only called if
01034                 # errorcount == 0.
01035                 if errorcount == 0 or self.errorok:
01036                     errorcount = error_count
01037                     self.errorok = 0
01038                     errtoken = lookahead
01039                     if errtoken.type == '$end':
01040                         errtoken = None               # End of file!
01041                     if self.errorfunc:
01042                         global errok,token,restart
01043                         errok = self.errok        # Set some special functions available in error recovery
01044                         token = get_token
01045                         restart = self.restart
01046                         if errtoken and not hasattr(errtoken,'lexer'):
01047                             errtoken.lexer = lexer
01048                         tok = self.errorfunc(errtoken)
01049                         del errok, token, restart   # Delete special functions
01050 
01051                         if self.errorok:
01052                             # User must have done some kind of panic
01053                             # mode recovery on their own.  The
01054                             # returned token is the next lookahead
01055                             lookahead = tok
01056                             errtoken = None
01057                             continue
01058                     else:
01059                         if errtoken:
01060                             if hasattr(errtoken,"lineno"): lineno = lookahead.lineno
01061                             else: lineno = 0
01062                             if lineno:
01063                                 sys.stderr.write("yacc: Syntax error at line %d, token=%s\n" % (lineno, errtoken.type))
01064                             else:
01065                                 sys.stderr.write("yacc: Syntax error, token=%s" % errtoken.type)
01066                         else:
01067                             sys.stderr.write("yacc: Parse error in input. EOF\n")
01068                             return
01069 
01070                 else:
01071                     errorcount = error_count
01072 
01073                 # case 1:  the statestack only has 1 entry on it.  If we're in this state, the
01074                 # entire parse has been rolled back and we're completely hosed.   The token is
01075                 # discarded and we just keep going.
01076 
01077                 if len(statestack) <= 1 and lookahead.type != '$end':
01078                     lookahead = None
01079                     errtoken = None
01080                     state = 0
01081                     # Nuke the pushback stack
01082                     del lookaheadstack[:]
01083                     continue
01084 
01085                 # case 2: the statestack has a couple of entries on it, but we're
01086                 # at the end of the file. nuke the top entry and generate an error token
01087 
01088                 # Start nuking entries on the stack
01089                 if lookahead.type == '$end':
01090                     # Whoa. We're really hosed here. Bail out
01091                     return
01092 
01093                 if lookahead.type != 'error':
01094                     sym = symstack[-1]
01095                     if sym.type == 'error':
01096                         # Hmmm. Error is on top of stack, we'll just nuke input
01097                         # symbol and continue
01098                         lookahead = None
01099                         continue
01100                     t = YaccSymbol()
01101                     t.type = 'error'
01102                     if hasattr(lookahead,"lineno"):
01103                         t.lineno = lookahead.lineno
01104                     t.value = lookahead
01105                     lookaheadstack.append(lookahead)
01106                     lookahead = t
01107                 else:
01108                     symstack.pop()
01109                     statestack.pop()
01110                     state = statestack[-1]       # Potential bug fix
01111 
01112                 continue
01113 
01114             # Call an error function here
01115             raise RuntimeError("yacc: internal parser error!!!\n")
01116 
01117 # -----------------------------------------------------------------------------
01118 #                          === Grammar Representation ===
01119 #
01120 # The following functions, classes, and variables are used to represent and
01121 # manipulate the rules that make up a grammar. 
01122 # -----------------------------------------------------------------------------

Here is the call graph for this function:

Here is the caller graph for this function:

def ply.yacc.LRParser.restart (   self)

Definition at line 249 of file yacc.py.

00249 
00250     def restart(self):
00251         del self.statestack[:]
00252         del self.symstack[:]
00253         sym = YaccSymbol()
00254         sym.type = '$end'
00255         self.symstack.append(sym)
00256         self.statestack.append(0)

Here is the caller graph for this function:


Member Data Documentation

Definition at line 242 of file yacc.py.

Definition at line 244 of file yacc.py.

Definition at line 247 of file yacc.py.

Definition at line 243 of file yacc.py.

Definition at line 241 of file yacc.py.

Definition at line 317 of file yacc.py.

Definition at line 319 of file yacc.py.


The documentation for this class was generated from the following file: