PageRenderTime 60ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 1ms

/ext/ply/ply/yacc.py

https://bitbucket.org/musleh123/ece565
Python | 3276 lines | 2373 code | 289 blank | 614 comment | 324 complexity | 9154ae6bd469cb728532dc7bcca1d1a0 MD5 | raw file
Possible License(s): BSD-3-Clause, LGPL-2.1, WTFPL

Large files files are truncated, but you can click here to view the full file

  1. # -----------------------------------------------------------------------------
  2. # ply: yacc.py
  3. #
  4. # Copyright (C) 2001-2009,
  5. # David M. Beazley (Dabeaz LLC)
  6. # All rights reserved.
  7. #
  8. # Redistribution and use in source and binary forms, with or without
  9. # modification, are permitted provided that the following conditions are
  10. # met:
  11. #
  12. # * Redistributions of source code must retain the above copyright notice,
  13. # this list of conditions and the following disclaimer.
  14. # * Redistributions in binary form must reproduce the above copyright notice,
  15. # this list of conditions and the following disclaimer in the documentation
  16. # and/or other materials provided with the distribution.
  17. # * Neither the name of the David Beazley or Dabeaz LLC may be used to
  18. # endorse or promote products derived from this software without
  19. # specific prior written permission.
  20. #
  21. # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  24. # A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  25. # OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  26. # SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  27. # LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  28. # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  29. # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  30. # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  31. # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  32. # -----------------------------------------------------------------------------
  33. #
  34. # This implements an LR parser that is constructed from grammar rules defined
  35. # as Python functions. The grammer is specified by supplying the BNF inside
  36. # Python documentation strings. The inspiration for this technique was borrowed
  37. # from John Aycock's Spark parsing system. PLY might be viewed as cross between
  38. # Spark and the GNU bison utility.
  39. #
  40. # The current implementation is only somewhat object-oriented. The
  41. # LR parser itself is defined in terms of an object (which allows multiple
  42. # parsers to co-exist). However, most of the variables used during table
  43. # construction are defined in terms of global variables. Users shouldn't
  44. # notice unless they are trying to define multiple parsers at the same
  45. # time using threads (in which case they should have their head examined).
  46. #
  47. # This implementation supports both SLR and LALR(1) parsing. LALR(1)
  48. # support was originally implemented by Elias Ioup (ezioup@alumni.uchicago.edu),
  49. # using the algorithm found in Aho, Sethi, and Ullman "Compilers: Principles,
  50. # Techniques, and Tools" (The Dragon Book). LALR(1) has since been replaced
  51. # by the more efficient DeRemer and Pennello algorithm.
  52. #
  53. # :::::::: WARNING :::::::
  54. #
  55. # Construction of LR parsing tables is fairly complicated and expensive.
  56. # To make this module run fast, a *LOT* of work has been put into
  57. # optimization---often at the expensive of readability and what might
  58. # consider to be good Python "coding style." Modify the code at your
  59. # own risk!
  60. # ----------------------------------------------------------------------------
  61. __version__ = "3.2"
  62. __tabversion__ = "3.2" # Table version
  63. #-----------------------------------------------------------------------------
  64. # === User configurable parameters ===
  65. #
  66. # Change these to modify the default behavior of yacc (if you wish)
  67. #-----------------------------------------------------------------------------
  68. yaccdebug = 1 # Debugging mode. If set, yacc generates a
  69. # a 'parser.out' file in the current directory
  70. debug_file = 'parser.out' # Default name of the debugging file
  71. tab_module = 'parsetab' # Default name of the table module
  72. default_lr = 'LALR' # Default LR table generation method
  73. error_count = 3 # Number of symbols that must be shifted to leave recovery mode
  74. yaccdevel = 0 # Set to True if developing yacc. This turns off optimized
  75. # implementations of certain functions.
  76. resultlimit = 40 # Size limit of results when running in debug mode.
  77. pickle_protocol = 0 # Protocol to use when writing pickle files
  78. import re, types, sys, os.path
  79. # Compatibility function for python 2.6/3.0
  80. if sys.version_info[0] < 3:
  81. def func_code(f):
  82. return f.func_code
  83. else:
  84. def func_code(f):
  85. return f.__code__
  86. # Compatibility
  87. try:
  88. MAXINT = sys.maxint
  89. except AttributeError:
  90. MAXINT = sys.maxsize
  91. # Python 2.x/3.0 compatibility.
  92. def load_ply_lex():
  93. if sys.version_info[0] < 3:
  94. import lex
  95. else:
  96. import ply.lex as lex
  97. return lex
  98. # This object is a stand-in for a logging object created by the
  99. # logging module. PLY will use this by default to create things
  100. # such as the parser.out file. If a user wants more detailed
  101. # information, they can create their own logging object and pass
  102. # it into PLY.
  103. class PlyLogger(object):
  104. def __init__(self,f):
  105. self.f = f
  106. def debug(self,msg,*args,**kwargs):
  107. self.f.write((msg % args) + "\n")
  108. info = debug
  109. def warning(self,msg,*args,**kwargs):
  110. self.f.write("WARNING: "+ (msg % args) + "\n")
  111. def error(self,msg,*args,**kwargs):
  112. self.f.write("ERROR: " + (msg % args) + "\n")
  113. critical = debug
  114. # Null logger is used when no output is generated. Does nothing.
  115. class NullLogger(object):
  116. def __getattribute__(self,name):
  117. return self
  118. def __call__(self,*args,**kwargs):
  119. return self
  120. # Exception raised for yacc-related errors
  121. class YaccError(Exception): pass
  122. # Format the result message that the parser produces when running in debug mode.
  123. def format_result(r):
  124. repr_str = repr(r)
  125. if '\n' in repr_str: repr_str = repr(repr_str)
  126. if len(repr_str) > resultlimit:
  127. repr_str = repr_str[:resultlimit]+" ..."
  128. result = "<%s @ 0x%x> (%s)" % (type(r).__name__,id(r),repr_str)
  129. return result
  130. # Format stack entries when the parser is running in debug mode
  131. def format_stack_entry(r):
  132. repr_str = repr(r)
  133. if '\n' in repr_str: repr_str = repr(repr_str)
  134. if len(repr_str) < 16:
  135. return repr_str
  136. else:
  137. return "<%s @ 0x%x>" % (type(r).__name__,id(r))
  138. #-----------------------------------------------------------------------------
  139. # === LR Parsing Engine ===
  140. #
  141. # The following classes are used for the LR parser itself. These are not
  142. # used during table construction and are independent of the actual LR
  143. # table generation algorithm
  144. #-----------------------------------------------------------------------------
  145. # This class is used to hold non-terminal grammar symbols during parsing.
  146. # It normally has the following attributes set:
  147. # .type = Grammar symbol type
  148. # .value = Symbol value
  149. # .lineno = Starting line number
  150. # .endlineno = Ending line number (optional, set automatically)
  151. # .lexpos = Starting lex position
  152. # .endlexpos = Ending lex position (optional, set automatically)
  153. class YaccSymbol:
  154. def __str__(self): return self.type
  155. def __repr__(self): return str(self)
  156. # This class is a wrapper around the objects actually passed to each
  157. # grammar rule. Index lookup and assignment actually assign the
  158. # .value attribute of the underlying YaccSymbol object.
  159. # The lineno() method returns the line number of a given
  160. # item (or 0 if not defined). The linespan() method returns
  161. # a tuple of (startline,endline) representing the range of lines
  162. # for a symbol. The lexspan() method returns a tuple (lexpos,endlexpos)
  163. # representing the range of positional information for a symbol.
  164. class YaccProduction:
  165. def __init__(self,s,stack=None):
  166. self.slice = s
  167. self.stack = stack
  168. self.lexer = None
  169. self.parser= None
  170. def __getitem__(self,n):
  171. if n >= 0: return self.slice[n].value
  172. else: return self.stack[n].value
  173. def __setitem__(self,n,v):
  174. self.slice[n].value = v
  175. def __getslice__(self,i,j):
  176. return [s.value for s in self.slice[i:j]]
  177. def __len__(self):
  178. return len(self.slice)
  179. def lineno(self,n):
  180. return getattr(self.slice[n],"lineno",0)
  181. def set_lineno(self,n,lineno):
  182. self.slice[n].lineno = n
  183. def linespan(self,n):
  184. startline = getattr(self.slice[n],"lineno",0)
  185. endline = getattr(self.slice[n],"endlineno",startline)
  186. return startline,endline
  187. def lexpos(self,n):
  188. return getattr(self.slice[n],"lexpos",0)
  189. def lexspan(self,n):
  190. startpos = getattr(self.slice[n],"lexpos",0)
  191. endpos = getattr(self.slice[n],"endlexpos",startpos)
  192. return startpos,endpos
  193. def error(self):
  194. raise SyntaxError
  195. # -----------------------------------------------------------------------------
  196. # == LRParser ==
  197. #
  198. # The LR Parsing engine.
  199. # -----------------------------------------------------------------------------
  200. class LRParser:
  201. def __init__(self,lrtab,errorf):
  202. self.productions = lrtab.lr_productions
  203. self.action = lrtab.lr_action
  204. self.goto = lrtab.lr_goto
  205. self.errorfunc = errorf
  206. def errok(self):
  207. self.errorok = 1
  208. def restart(self):
  209. del self.statestack[:]
  210. del self.symstack[:]
  211. sym = YaccSymbol()
  212. sym.type = '$end'
  213. self.symstack.append(sym)
  214. self.statestack.append(0)
  215. def parse(self,input=None,lexer=None,debug=0,tracking=0,tokenfunc=None):
  216. if debug or yaccdevel:
  217. if isinstance(debug,int):
  218. debug = PlyLogger(sys.stderr)
  219. return self.parsedebug(input,lexer,debug,tracking,tokenfunc)
  220. elif tracking:
  221. return self.parseopt(input,lexer,debug,tracking,tokenfunc)
  222. else:
  223. return self.parseopt_notrack(input,lexer,debug,tracking,tokenfunc)
  224. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  225. # parsedebug().
  226. #
  227. # This is the debugging enabled version of parse(). All changes made to the
  228. # parsing engine should be made here. For the non-debugging version,
  229. # copy this code to a method parseopt() and delete all of the sections
  230. # enclosed in:
  231. #
  232. # #--! DEBUG
  233. # statements
  234. # #--! DEBUG
  235. #
  236. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  237. def parsedebug(self,input=None,lexer=None,debug=None,tracking=0,tokenfunc=None):
  238. lookahead = None # Current lookahead symbol
  239. lookaheadstack = [ ] # Stack of lookahead symbols
  240. actions = self.action # Local reference to action table (to avoid lookup on self.)
  241. goto = self.goto # Local reference to goto table (to avoid lookup on self.)
  242. prod = self.productions # Local reference to production list (to avoid lookup on self.)
  243. pslice = YaccProduction(None) # Production object passed to grammar rules
  244. errorcount = 0 # Used during error recovery
  245. # --! DEBUG
  246. debug.info("PLY: PARSE DEBUG START")
  247. # --! DEBUG
  248. # If no lexer was given, we will try to use the lex module
  249. if not lexer:
  250. lex = load_ply_lex()
  251. lexer = lex.lexer
  252. # Set up the lexer and parser objects on pslice
  253. pslice.lexer = lexer
  254. pslice.parser = self
  255. # If input was supplied, pass to lexer
  256. if input is not None:
  257. lexer.input(input)
  258. if tokenfunc is None:
  259. # Tokenize function
  260. get_token = lexer.token
  261. else:
  262. get_token = tokenfunc
  263. # Set up the state and symbol stacks
  264. statestack = [ ] # Stack of parsing states
  265. self.statestack = statestack
  266. symstack = [ ] # Stack of grammar symbols
  267. self.symstack = symstack
  268. pslice.stack = symstack # Put in the production
  269. errtoken = None # Err token
  270. # The start state is assumed to be (0,$end)
  271. statestack.append(0)
  272. sym = YaccSymbol()
  273. sym.type = "$end"
  274. symstack.append(sym)
  275. state = 0
  276. while 1:
  277. # Get the next symbol on the input. If a lookahead symbol
  278. # is already set, we just use that. Otherwise, we'll pull
  279. # the next token off of the lookaheadstack or from the lexer
  280. # --! DEBUG
  281. debug.debug('')
  282. debug.debug('State : %s', state)
  283. # --! DEBUG
  284. if not lookahead:
  285. if not lookaheadstack:
  286. lookahead = get_token() # Get the next token
  287. else:
  288. lookahead = lookaheadstack.pop()
  289. if not lookahead:
  290. lookahead = YaccSymbol()
  291. lookahead.type = "$end"
  292. # --! DEBUG
  293. debug.debug('Stack : %s',
  294. ("%s . %s" % (" ".join([xx.type for xx in symstack][1:]), str(lookahead))).lstrip())
  295. # --! DEBUG
  296. # Check the action table
  297. ltype = lookahead.type
  298. t = actions[state].get(ltype)
  299. if t is not None:
  300. if t > 0:
  301. # shift a symbol on the stack
  302. statestack.append(t)
  303. state = t
  304. # --! DEBUG
  305. debug.debug("Action : Shift and goto state %s", t)
  306. # --! DEBUG
  307. symstack.append(lookahead)
  308. lookahead = None
  309. # Decrease error count on successful shift
  310. if errorcount: errorcount -=1
  311. continue
  312. if t < 0:
  313. # reduce a symbol on the stack, emit a production
  314. p = prod[-t]
  315. pname = p.name
  316. plen = p.len
  317. # Get production function
  318. sym = YaccSymbol()
  319. sym.type = pname # Production name
  320. sym.value = None
  321. # --! DEBUG
  322. if plen:
  323. 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)
  324. else:
  325. debug.info("Action : Reduce rule [%s] with %s and goto state %d", p.str, [],-t)
  326. # --! DEBUG
  327. if plen:
  328. targ = symstack[-plen-1:]
  329. targ[0] = sym
  330. # --! TRACKING
  331. if tracking:
  332. t1 = targ[1]
  333. sym.lineno = t1.lineno
  334. sym.lexpos = t1.lexpos
  335. t1 = targ[-1]
  336. sym.endlineno = getattr(t1,"endlineno",t1.lineno)
  337. sym.endlexpos = getattr(t1,"endlexpos",t1.lexpos)
  338. # --! TRACKING
  339. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  340. # The code enclosed in this section is duplicated
  341. # below as a performance optimization. Make sure
  342. # changes get made in both locations.
  343. pslice.slice = targ
  344. try:
  345. # Call the grammar rule with our special slice object
  346. del symstack[-plen:]
  347. del statestack[-plen:]
  348. p.callable(pslice)
  349. # --! DEBUG
  350. debug.info("Result : %s", format_result(pslice[0]))
  351. # --! DEBUG
  352. symstack.append(sym)
  353. state = goto[statestack[-1]][pname]
  354. statestack.append(state)
  355. except SyntaxError:
  356. # If an error was set. Enter error recovery state
  357. lookaheadstack.append(lookahead)
  358. symstack.pop()
  359. statestack.pop()
  360. state = statestack[-1]
  361. sym.type = 'error'
  362. lookahead = sym
  363. errorcount = error_count
  364. self.errorok = 0
  365. continue
  366. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  367. else:
  368. # --! TRACKING
  369. if tracking:
  370. sym.lineno = lexer.lineno
  371. sym.lexpos = lexer.lexpos
  372. # --! TRACKING
  373. targ = [ sym ]
  374. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  375. # The code enclosed in this section is duplicated
  376. # above as a performance optimization. Make sure
  377. # changes get made in both locations.
  378. pslice.slice = targ
  379. try:
  380. # Call the grammar rule with our special slice object
  381. p.callable(pslice)
  382. # --! DEBUG
  383. debug.info("Result : %s", format_result(pslice[0]))
  384. # --! DEBUG
  385. symstack.append(sym)
  386. state = goto[statestack[-1]][pname]
  387. statestack.append(state)
  388. except SyntaxError:
  389. # If an error was set. Enter error recovery state
  390. lookaheadstack.append(lookahead)
  391. symstack.pop()
  392. statestack.pop()
  393. state = statestack[-1]
  394. sym.type = 'error'
  395. lookahead = sym
  396. errorcount = error_count
  397. self.errorok = 0
  398. continue
  399. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  400. if t == 0:
  401. n = symstack[-1]
  402. result = getattr(n,"value",None)
  403. # --! DEBUG
  404. debug.info("Done : Returning %s", format_result(result))
  405. debug.info("PLY: PARSE DEBUG END")
  406. # --! DEBUG
  407. return result
  408. if t == None:
  409. # --! DEBUG
  410. debug.error('Error : %s',
  411. ("%s . %s" % (" ".join([xx.type for xx in symstack][1:]), str(lookahead))).lstrip())
  412. # --! DEBUG
  413. # We have some kind of parsing error here. To handle
  414. # this, we are going to push the current token onto
  415. # the tokenstack and replace it with an 'error' token.
  416. # If there are any synchronization rules, they may
  417. # catch it.
  418. #
  419. # In addition to pushing the error token, we call call
  420. # the user defined p_error() function if this is the
  421. # first syntax error. This function is only called if
  422. # errorcount == 0.
  423. if errorcount == 0 or self.errorok:
  424. errorcount = error_count
  425. self.errorok = 0
  426. errtoken = lookahead
  427. if errtoken.type == "$end":
  428. errtoken = None # End of file!
  429. if self.errorfunc:
  430. global errok,token,restart
  431. errok = self.errok # Set some special functions available in error recovery
  432. token = get_token
  433. restart = self.restart
  434. if errtoken and not hasattr(errtoken,'lexer'):
  435. errtoken.lexer = lexer
  436. tok = self.errorfunc(errtoken)
  437. del errok, token, restart # Delete special functions
  438. if self.errorok:
  439. # User must have done some kind of panic
  440. # mode recovery on their own. The
  441. # returned token is the next lookahead
  442. lookahead = tok
  443. errtoken = None
  444. continue
  445. else:
  446. if errtoken:
  447. if hasattr(errtoken,"lineno"): lineno = lookahead.lineno
  448. else: lineno = 0
  449. if lineno:
  450. sys.stderr.write("yacc: Syntax error at line %d, token=%s\n" % (lineno, errtoken.type))
  451. else:
  452. sys.stderr.write("yacc: Syntax error, token=%s" % errtoken.type)
  453. else:
  454. sys.stderr.write("yacc: Parse error in input. EOF\n")
  455. return
  456. else:
  457. errorcount = error_count
  458. # case 1: the statestack only has 1 entry on it. If we're in this state, the
  459. # entire parse has been rolled back and we're completely hosed. The token is
  460. # discarded and we just keep going.
  461. if len(statestack) <= 1 and lookahead.type != "$end":
  462. lookahead = None
  463. errtoken = None
  464. state = 0
  465. # Nuke the pushback stack
  466. del lookaheadstack[:]
  467. continue
  468. # case 2: the statestack has a couple of entries on it, but we're
  469. # at the end of the file. nuke the top entry and generate an error token
  470. # Start nuking entries on the stack
  471. if lookahead.type == "$end":
  472. # Whoa. We're really hosed here. Bail out
  473. return
  474. if lookahead.type != 'error':
  475. sym = symstack[-1]
  476. if sym.type == 'error':
  477. # Hmmm. Error is on top of stack, we'll just nuke input
  478. # symbol and continue
  479. lookahead = None
  480. continue
  481. t = YaccSymbol()
  482. t.type = 'error'
  483. if hasattr(lookahead,"lineno"):
  484. t.lineno = lookahead.lineno
  485. t.value = lookahead
  486. lookaheadstack.append(lookahead)
  487. lookahead = t
  488. else:
  489. symstack.pop()
  490. statestack.pop()
  491. state = statestack[-1] # Potential bug fix
  492. continue
  493. # Call an error function here
  494. raise RuntimeError("yacc: internal parser error!!!\n")
  495. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  496. # parseopt().
  497. #
  498. # Optimized version of parse() method. DO NOT EDIT THIS CODE DIRECTLY.
  499. # Edit the debug version above, then copy any modifications to the method
  500. # below while removing #--! DEBUG sections.
  501. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  502. def parseopt(self,input=None,lexer=None,debug=0,tracking=0,tokenfunc=None):
  503. lookahead = None # Current lookahead symbol
  504. lookaheadstack = [ ] # Stack of lookahead symbols
  505. actions = self.action # Local reference to action table (to avoid lookup on self.)
  506. goto = self.goto # Local reference to goto table (to avoid lookup on self.)
  507. prod = self.productions # Local reference to production list (to avoid lookup on self.)
  508. pslice = YaccProduction(None) # Production object passed to grammar rules
  509. errorcount = 0 # Used during error recovery
  510. # If no lexer was given, we will try to use the lex module
  511. if not lexer:
  512. lex = load_ply_lex()
  513. lexer = lex.lexer
  514. # Set up the lexer and parser objects on pslice
  515. pslice.lexer = lexer
  516. pslice.parser = self
  517. # If input was supplied, pass to lexer
  518. if input is not None:
  519. lexer.input(input)
  520. if tokenfunc is None:
  521. # Tokenize function
  522. get_token = lexer.token
  523. else:
  524. get_token = tokenfunc
  525. # Set up the state and symbol stacks
  526. statestack = [ ] # Stack of parsing states
  527. self.statestack = statestack
  528. symstack = [ ] # Stack of grammar symbols
  529. self.symstack = symstack
  530. pslice.stack = symstack # Put in the production
  531. errtoken = None # Err token
  532. # The start state is assumed to be (0,$end)
  533. statestack.append(0)
  534. sym = YaccSymbol()
  535. sym.type = '$end'
  536. symstack.append(sym)
  537. state = 0
  538. while 1:
  539. # Get the next symbol on the input. If a lookahead symbol
  540. # is already set, we just use that. Otherwise, we'll pull
  541. # the next token off of the lookaheadstack or from the lexer
  542. if not lookahead:
  543. if not lookaheadstack:
  544. lookahead = get_token() # Get the next token
  545. else:
  546. lookahead = lookaheadstack.pop()
  547. if not lookahead:
  548. lookahead = YaccSymbol()
  549. lookahead.type = '$end'
  550. # Check the action table
  551. ltype = lookahead.type
  552. t = actions[state].get(ltype)
  553. if t is not None:
  554. if t > 0:
  555. # shift a symbol on the stack
  556. statestack.append(t)
  557. state = t
  558. symstack.append(lookahead)
  559. lookahead = None
  560. # Decrease error count on successful shift
  561. if errorcount: errorcount -=1
  562. continue
  563. if t < 0:
  564. # reduce a symbol on the stack, emit a production
  565. p = prod[-t]
  566. pname = p.name
  567. plen = p.len
  568. # Get production function
  569. sym = YaccSymbol()
  570. sym.type = pname # Production name
  571. sym.value = None
  572. if plen:
  573. targ = symstack[-plen-1:]
  574. targ[0] = sym
  575. # --! TRACKING
  576. if tracking:
  577. t1 = targ[1]
  578. sym.lineno = t1.lineno
  579. sym.lexpos = t1.lexpos
  580. t1 = targ[-1]
  581. sym.endlineno = getattr(t1,"endlineno",t1.lineno)
  582. sym.endlexpos = getattr(t1,"endlexpos",t1.lexpos)
  583. # --! TRACKING
  584. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  585. # The code enclosed in this section is duplicated
  586. # below as a performance optimization. Make sure
  587. # changes get made in both locations.
  588. pslice.slice = targ
  589. try:
  590. # Call the grammar rule with our special slice object
  591. del symstack[-plen:]
  592. del statestack[-plen:]
  593. p.callable(pslice)
  594. symstack.append(sym)
  595. state = goto[statestack[-1]][pname]
  596. statestack.append(state)
  597. except SyntaxError:
  598. # If an error was set. Enter error recovery state
  599. lookaheadstack.append(lookahead)
  600. symstack.pop()
  601. statestack.pop()
  602. state = statestack[-1]
  603. sym.type = 'error'
  604. lookahead = sym
  605. errorcount = error_count
  606. self.errorok = 0
  607. continue
  608. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  609. else:
  610. # --! TRACKING
  611. if tracking:
  612. sym.lineno = lexer.lineno
  613. sym.lexpos = lexer.lexpos
  614. # --! TRACKING
  615. targ = [ sym ]
  616. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  617. # The code enclosed in this section is duplicated
  618. # above as a performance optimization. Make sure
  619. # changes get made in both locations.
  620. pslice.slice = targ
  621. try:
  622. # Call the grammar rule with our special slice object
  623. p.callable(pslice)
  624. symstack.append(sym)
  625. state = goto[statestack[-1]][pname]
  626. statestack.append(state)
  627. except SyntaxError:
  628. # If an error was set. Enter error recovery state
  629. lookaheadstack.append(lookahead)
  630. symstack.pop()
  631. statestack.pop()
  632. state = statestack[-1]
  633. sym.type = 'error'
  634. lookahead = sym
  635. errorcount = error_count
  636. self.errorok = 0
  637. continue
  638. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  639. if t == 0:
  640. n = symstack[-1]
  641. return getattr(n,"value",None)
  642. if t == None:
  643. # We have some kind of parsing error here. To handle
  644. # this, we are going to push the current token onto
  645. # the tokenstack and replace it with an 'error' token.
  646. # If there are any synchronization rules, they may
  647. # catch it.
  648. #
  649. # In addition to pushing the error token, we call call
  650. # the user defined p_error() function if this is the
  651. # first syntax error. This function is only called if
  652. # errorcount == 0.
  653. if errorcount == 0 or self.errorok:
  654. errorcount = error_count
  655. self.errorok = 0
  656. errtoken = lookahead
  657. if errtoken.type == '$end':
  658. errtoken = None # End of file!
  659. if self.errorfunc:
  660. global errok,token,restart
  661. errok = self.errok # Set some special functions available in error recovery
  662. token = get_token
  663. restart = self.restart
  664. if errtoken and not hasattr(errtoken,'lexer'):
  665. errtoken.lexer = lexer
  666. tok = self.errorfunc(errtoken)
  667. del errok, token, restart # Delete special functions
  668. if self.errorok:
  669. # User must have done some kind of panic
  670. # mode recovery on their own. The
  671. # returned token is the next lookahead
  672. lookahead = tok
  673. errtoken = None
  674. continue
  675. else:
  676. if errtoken:
  677. if hasattr(errtoken,"lineno"): lineno = lookahead.lineno
  678. else: lineno = 0
  679. if lineno:
  680. sys.stderr.write("yacc: Syntax error at line %d, token=%s\n" % (lineno, errtoken.type))
  681. else:
  682. sys.stderr.write("yacc: Syntax error, token=%s" % errtoken.type)
  683. else:
  684. sys.stderr.write("yacc: Parse error in input. EOF\n")
  685. return
  686. else:
  687. errorcount = error_count
  688. # case 1: the statestack only has 1 entry on it. If we're in this state, the
  689. # entire parse has been rolled back and we're completely hosed. The token is
  690. # discarded and we just keep going.
  691. if len(statestack) <= 1 and lookahead.type != '$end':
  692. lookahead = None
  693. errtoken = None
  694. state = 0
  695. # Nuke the pushback stack
  696. del lookaheadstack[:]
  697. continue
  698. # case 2: the statestack has a couple of entries on it, but we're
  699. # at the end of the file. nuke the top entry and generate an error token
  700. # Start nuking entries on the stack
  701. if lookahead.type == '$end':
  702. # Whoa. We're really hosed here. Bail out
  703. return
  704. if lookahead.type != 'error':
  705. sym = symstack[-1]
  706. if sym.type == 'error':
  707. # Hmmm. Error is on top of stack, we'll just nuke input
  708. # symbol and continue
  709. lookahead = None
  710. continue
  711. t = YaccSymbol()
  712. t.type = 'error'
  713. if hasattr(lookahead,"lineno"):
  714. t.lineno = lookahead.lineno
  715. t.value = lookahead
  716. lookaheadstack.append(lookahead)
  717. lookahead = t
  718. else:
  719. symstack.pop()
  720. statestack.pop()
  721. state = statestack[-1] # Potential bug fix
  722. continue
  723. # Call an error function here
  724. raise RuntimeError("yacc: internal parser error!!!\n")
  725. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  726. # parseopt_notrack().
  727. #
  728. # Optimized version of parseopt() with line number tracking removed.
  729. # DO NOT EDIT THIS CODE DIRECTLY. Copy the optimized version and remove
  730. # code in the #--! TRACKING sections
  731. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  732. def parseopt_notrack(self,input=None,lexer=None,debug=0,tracking=0,tokenfunc=None):
  733. lookahead = None # Current lookahead symbol
  734. lookaheadstack = [ ] # Stack of lookahead symbols
  735. actions = self.action # Local reference to action table (to avoid lookup on self.)
  736. goto = self.goto # Local reference to goto table (to avoid lookup on self.)
  737. prod = self.productions # Local reference to production list (to avoid lookup on self.)
  738. pslice = YaccProduction(None) # Production object passed to grammar rules
  739. errorcount = 0 # Used during error recovery
  740. # If no lexer was given, we will try to use the lex module
  741. if not lexer:
  742. lex = load_ply_lex()
  743. lexer = lex.lexer
  744. # Set up the lexer and parser objects on pslice
  745. pslice.lexer = lexer
  746. pslice.parser = self
  747. # If input was supplied, pass to lexer
  748. if input is not None:
  749. lexer.input(input)
  750. if tokenfunc is None:
  751. # Tokenize function
  752. get_token = lexer.token
  753. else:
  754. get_token = tokenfunc
  755. # Set up the state and symbol stacks
  756. statestack = [ ] # Stack of parsing states
  757. self.statestack = statestack
  758. symstack = [ ] # Stack of grammar symbols
  759. self.symstack = symstack
  760. pslice.stack = symstack # Put in the production
  761. errtoken = None # Err token
  762. # The start state is assumed to be (0,$end)
  763. statestack.append(0)
  764. sym = YaccSymbol()
  765. sym.type = '$end'
  766. symstack.append(sym)
  767. state = 0
  768. while 1:
  769. # Get the next symbol on the input. If a lookahead symbol
  770. # is already set, we just use that. Otherwise, we'll pull
  771. # the next token off of the lookaheadstack or from the lexer
  772. if not lookahead:
  773. if not lookaheadstack:
  774. lookahead = get_token() # Get the next token
  775. else:
  776. lookahead = lookaheadstack.pop()
  777. if not lookahead:
  778. lookahead = YaccSymbol()
  779. lookahead.type = '$end'
  780. # Check the action table
  781. ltype = lookahead.type
  782. t = actions[state].get(ltype)
  783. if t is not None:
  784. if t > 0:
  785. # shift a symbol on the stack
  786. statestack.append(t)
  787. state = t
  788. symstack.append(lookahead)
  789. lookahead = None
  790. # Decrease error count on successful shift
  791. if errorcount: errorcount -=1
  792. continue
  793. if t < 0:
  794. # reduce a symbol on the stack, emit a production
  795. p = prod[-t]
  796. pname = p.name
  797. plen = p.len
  798. # Get production function
  799. sym = YaccSymbol()
  800. sym.type = pname # Production name
  801. sym.value = None
  802. if plen:
  803. targ = symstack[-plen-1:]
  804. targ[0] = sym
  805. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  806. # The code enclosed in this section is duplicated
  807. # below as a performance optimization. Make sure
  808. # changes get made in both locations.
  809. pslice.slice = targ
  810. try:
  811. # Call the grammar rule with our special slice object
  812. del symstack[-plen:]
  813. del statestack[-plen:]
  814. p.callable(pslice)
  815. symstack.append(sym)
  816. state = goto[statestack[-1]][pname]
  817. statestack.append(state)
  818. except SyntaxError:
  819. # If an error was set. Enter error recovery state
  820. lookaheadstack.append(lookahead)
  821. symstack.pop()
  822. statestack.pop()
  823. state = statestack[-1]
  824. sym.type = 'error'
  825. lookahead = sym
  826. errorcount = error_count
  827. self.errorok = 0
  828. continue
  829. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  830. else:
  831. targ = [ sym ]
  832. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  833. # The code enclosed in this section is duplicated
  834. # above as a performance optimization. Make sure
  835. # changes get made in both locations.
  836. pslice.slice = targ
  837. try:
  838. # Call the grammar rule with our special slice object
  839. p.callable(pslice)
  840. symstack.append(sym)
  841. state = goto[statestack[-1]][pname]
  842. statestack.append(state)
  843. except SyntaxError:
  844. # If an error was set. Enter error recovery state
  845. lookaheadstack.append(lookahead)
  846. symstack.pop()
  847. statestack.pop()
  848. state = statestack[-1]
  849. sym.type = 'error'
  850. lookahead = sym
  851. errorcount = error_count
  852. self.errorok = 0
  853. continue
  854. # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
  855. if t == 0:
  856. n = symstack[-1]
  857. return getattr(n,"value",None)
  858. if t == None:
  859. # We have some kind of parsing error here. To handle
  860. # this, we are going to push the current token onto
  861. # the tokenstack and replace it with an 'error' token.
  862. # If there are any synchronization rules, they may
  863. # catch it.
  864. #
  865. # In addition to pushing the error token, we call call
  866. # the user defined p_error() function if this is the
  867. # first syntax error. This function is only called if
  868. # errorcount == 0.
  869. if errorcount == 0 or self.errorok:
  870. errorcount = error_count
  871. self.errorok = 0
  872. errtoken = lookahead
  873. if errtoken.type == '$end':
  874. errtoken = None # End of file!
  875. if self.errorfunc:
  876. global errok,token,restart
  877. errok = self.errok # Set some special functions available in error recovery
  878. token = get_token
  879. restart = self.restart
  880. if errtoken and not hasattr(errtoken,'lexer'):
  881. errtoken.lexer = lexer
  882. tok = self.errorfunc(errtoken)
  883. del errok, token, restart # Delete special functions
  884. if self.errorok:
  885. # User must have done some kind of panic
  886. # mode recovery on their own. The
  887. # returned token is the next lookahead
  888. lookahead = tok
  889. errtoken = None
  890. continue
  891. else:
  892. if errtoken:
  893. if hasattr(errtoken,"lineno"): lineno = lookahead.lineno
  894. else: lineno = 0
  895. if lineno:
  896. sys.stderr.write("yacc: Syntax error at line %d, token=%s\n" % (lineno, errtoken.type))
  897. else:
  898. sys.stderr.write("yacc: Syntax error, token=%s" % errtoken.type)
  899. else:
  900. sys.stderr.write("yacc: Parse error in input. EOF\n")
  901. return
  902. else:
  903. errorcount = error_count
  904. # case 1: the statestack only has 1 entry on it. If we're in this state, the
  905. # entire parse has been rolled back and we're completely hosed. The token is
  906. # discarded and we just keep going.
  907. if len(statestack) <= 1 and lookahead.type != '$end':
  908. lookahead = None
  909. errtoken = None
  910. state = 0
  911. # Nuke the pushback stack
  912. del lookaheadstack[:]
  913. continue
  914. # case 2: the statestack has a couple of entries on it, but we're
  915. # at the end of the file. nuke the top entry and generate an error token
  916. # Start nuking entries on the stack
  917. if lookahead.type == '$end':
  918. # Whoa. We're really hosed here. Bail out
  919. return
  920. if lookahead.type != 'error':
  921. sym = symstack[-1]
  922. if sym.type == 'error':
  923. # Hmmm. Error is on top of stack, we'll just nuke input
  924. # symbol and continue
  925. lookahead = None
  926. continue
  927. t = YaccSymbol()
  928. t.type = 'error'
  929. if hasattr(lookahead,"lineno"):
  930. t.lineno = lookahead.lineno
  931. t.value = lookahead
  932. lookaheadstack.append(lookahead)
  933. lookahead = t
  934. else:
  935. symstack.pop()
  936. statestack.pop()
  937. state = statestack[-1] # Potential bug fix
  938. continue
  939. # Call an error function here
  940. raise RuntimeError("yacc: internal parser error!!!\n")
  941. # -----------------------------------------------------------------------------
  942. # === Grammar Representation ===
  943. #
  944. # The following functions, classes, and variables are used to represent and
  945. # manipulate the rules that make up a grammar.
  946. # -----------------------------------------------------------------------------
  947. import re
  948. # regex matching identifiers
  949. _is_identifier = re.compile(r'^[a-zA-Z0-9_-]+$')
  950. # -----------------------------------------------------------------------------
  951. # class Production:
  952. #
  953. # This class stores the raw information about a single production or grammar rule.
  954. # A grammar rule refers to a specification such as this:
  955. #
  956. # expr : expr PLUS term
  957. #
  958. # Here are the basic attributes defined on all productions
  959. #
  960. # name - Name of the production. For example 'expr'
  961. # prod - A list of symbols on the right side ['expr','PLUS','term']
  962. # prec - Production precedence level
  963. # number - Production number.
  964. # func - Function that executes on reduce
  965. # file - File where production function is defined
  966. # lineno - Line number where production function is defined
  967. #
  968. # The following attributes are defined or optional.
  969. #
  970. # len - Length of the production (number of symbols on right hand side)
  971. # usyms - Set of unique symbols found in the production
  972. # -----------------------------------------------------------------------------
  973. class Production(object):
  974. reduced = 0
  975. def __init__(self,number,name,prod,precedence=('right',0),func=None,file='',line=0):
  976. self.name = name
  977. self.prod = tuple(prod)
  978. self.number = number
  979. self.func = func
  980. self.callable = None
  981. self.file = file
  982. self.line = line
  983. self.prec = precedence
  984. # Internal settings used during table construction
  985. self.len = len(self.prod) # Length of the production
  986. # Create a list of unique production symbols used in the production
  987. self.usyms = [ ]
  988. for s in self.prod:
  989. if s not in self.usyms:
  990. self.usyms.append(s)
  991. # List of all LR items for the production
  992. self.lr_items = []
  993. self.lr_next = None
  994. # Create a string representation
  995. if self.prod:
  996. self.str = "%s -> %s" % (self.name," ".join(self.prod))
  997. else:
  998. self.str = "%s -> <empty>" % self.name
  999. def __str__(self):
  1000. return self.str
  1001. def __repr__(self):
  1002. return "Production("+str(self)+")"
  1003. def __len__(self):
  1004. return len(self.prod)
  1005. def __nonzero__(self):
  1006. return 1
  1007. def __getitem__(self,index):
  1008. return self.prod[index]
  1009. # Return the nth lr_item from the production (or None if at the end)
  1010. def lr_item(self,n):
  1011. if n > len(self.prod): return None
  1012. p = LRItem(self,n)
  1013. # Precompute the list of productions immediately following. Hack. Remove later
  1014. try:
  1015. p.lr_after = Prodnames[p.prod[n+1]]
  1016. except (IndexError,KeyError):
  1017. p.lr_after = []
  1018. try:
  1019. p.lr_before = p.prod[n-1]
  1020. except IndexError:
  1021. p.lr_before = None
  1022. return p
  1023. # Bind the production function name to a callable
  1024. def bind(self,pdict):
  1025. if self.func:
  1026. self.callable = pdict[self.func]
  1027. # This class serves as a minimal standin for Production objects when
  1028. # reading table data from files. It only contains information
  1029. # actually used by the LR parsing engine, plus some additional
  1030. # debugging information.
  1031. class MiniProduction(object):
  1032. def __init__(self,str,name,len,func,file,line):
  1033. self.name

Large files files are truncated, but you can click here to view the full file