OLD | NEW |
(Empty) | |
| 1 # ----------------------------------------------------------------------------- |
| 2 # ply: yacc.py |
| 3 # |
| 4 # Copyright (C) 2001-2016 |
| 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 |
| 62 import re |
| 63 import types |
| 64 import sys |
| 65 import os.path |
| 66 import inspect |
| 67 import base64 |
| 68 import warnings |
| 69 |
| 70 __version__ = '3.9' |
| 71 __tabversion__ = '3.8' |
| 72 |
| 73 #----------------------------------------------------------------------------- |
| 74 # === User configurable parameters === |
| 75 # |
| 76 # Change these to modify the default behavior of yacc (if you wish) |
| 77 #----------------------------------------------------------------------------- |
| 78 |
| 79 yaccdebug = True # Debugging mode. If set, yacc generates a |
| 80 # a 'parser.out' file in the current directory |
| 81 |
| 82 debug_file = 'parser.out' # Default name of the debugging file |
| 83 tab_module = 'parsetab' # Default name of the table module |
| 84 default_lr = 'LALR' # Default LR table generation method |
| 85 |
| 86 error_count = 3 # Number of symbols that must be shifted to leave
recovery mode |
| 87 |
| 88 yaccdevel = False # Set to True if developing yacc. This turns off
optimized |
| 89 # implementations of certain functions. |
| 90 |
| 91 resultlimit = 40 # Size limit of results when running in debug mod
e. |
| 92 |
| 93 pickle_protocol = 0 # Protocol to use when writing pickle files |
| 94 |
| 95 # String type-checking compatibility |
| 96 if sys.version_info[0] < 3: |
| 97 string_types = basestring |
| 98 else: |
| 99 string_types = str |
| 100 |
| 101 MAXINT = sys.maxsize |
| 102 |
| 103 # This object is a stand-in for a logging object created by the |
| 104 # logging module. PLY will use this by default to create things |
| 105 # such as the parser.out file. If a user wants more detailed |
| 106 # information, they can create their own logging object and pass |
| 107 # it into PLY. |
| 108 |
| 109 class PlyLogger(object): |
| 110 def __init__(self, f): |
| 111 self.f = f |
| 112 |
| 113 def debug(self, msg, *args, **kwargs): |
| 114 self.f.write((msg % args) + '\n') |
| 115 |
| 116 info = debug |
| 117 |
| 118 def warning(self, msg, *args, **kwargs): |
| 119 self.f.write('WARNING: ' + (msg % args) + '\n') |
| 120 |
| 121 def error(self, msg, *args, **kwargs): |
| 122 self.f.write('ERROR: ' + (msg % args) + '\n') |
| 123 |
| 124 critical = debug |
| 125 |
| 126 # Null logger is used when no output is generated. Does nothing. |
| 127 class NullLogger(object): |
| 128 def __getattribute__(self, name): |
| 129 return self |
| 130 |
| 131 def __call__(self, *args, **kwargs): |
| 132 return self |
| 133 |
| 134 # Exception raised for yacc-related errors |
| 135 class YaccError(Exception): |
| 136 pass |
| 137 |
| 138 # Format the result message that the parser produces when running in debug mode. |
| 139 def format_result(r): |
| 140 repr_str = repr(r) |
| 141 if '\n' in repr_str: |
| 142 repr_str = repr(repr_str) |
| 143 if len(repr_str) > resultlimit: |
| 144 repr_str = repr_str[:resultlimit] + ' ...' |
| 145 result = '<%s @ 0x%x> (%s)' % (type(r).__name__, id(r), repr_str) |
| 146 return result |
| 147 |
| 148 # Format stack entries when the parser is running in debug mode |
| 149 def format_stack_entry(r): |
| 150 repr_str = repr(r) |
| 151 if '\n' in repr_str: |
| 152 repr_str = repr(repr_str) |
| 153 if len(repr_str) < 16: |
| 154 return repr_str |
| 155 else: |
| 156 return '<%s @ 0x%x>' % (type(r).__name__, id(r)) |
| 157 |
| 158 # Panic mode error recovery support. This feature is being reworked--much of t
he |
| 159 # code here is to offer a deprecation/backwards compatible transition |
| 160 |
| 161 _errok = None |
| 162 _token = None |
| 163 _restart = None |
| 164 _warnmsg = '''PLY: Don't use global functions errok(), token(), and restart() in
p_error(). |
| 165 Instead, invoke the methods on the associated parser instance: |
| 166 |
| 167 def p_error(p): |
| 168 ... |
| 169 # Use parser.errok(), parser.token(), parser.restart() |
| 170 ... |
| 171 |
| 172 parser = yacc.yacc() |
| 173 ''' |
| 174 |
| 175 def errok(): |
| 176 warnings.warn(_warnmsg) |
| 177 return _errok() |
| 178 |
| 179 def restart(): |
| 180 warnings.warn(_warnmsg) |
| 181 return _restart() |
| 182 |
| 183 def token(): |
| 184 warnings.warn(_warnmsg) |
| 185 return _token() |
| 186 |
| 187 # Utility function to call the p_error() function with some deprecation hacks |
| 188 def call_errorfunc(errorfunc, token, parser): |
| 189 global _errok, _token, _restart |
| 190 _errok = parser.errok |
| 191 _token = parser.token |
| 192 _restart = parser.restart |
| 193 r = errorfunc(token) |
| 194 try: |
| 195 del _errok, _token, _restart |
| 196 except NameError: |
| 197 pass |
| 198 return r |
| 199 |
| 200 #----------------------------------------------------------------------------- |
| 201 # === LR Parsing Engine === |
| 202 # |
| 203 # The following classes are used for the LR parser itself. These are not |
| 204 # used during table construction and are independent of the actual LR |
| 205 # table generation algorithm |
| 206 #----------------------------------------------------------------------------- |
| 207 |
| 208 # This class is used to hold non-terminal grammar symbols during parsing. |
| 209 # It normally has the following attributes set: |
| 210 # .type = Grammar symbol type |
| 211 # .value = Symbol value |
| 212 # .lineno = Starting line number |
| 213 # .endlineno = Ending line number (optional, set automatically) |
| 214 # .lexpos = Starting lex position |
| 215 # .endlexpos = Ending lex position (optional, set automatically) |
| 216 |
| 217 class YaccSymbol: |
| 218 def __str__(self): |
| 219 return self.type |
| 220 |
| 221 def __repr__(self): |
| 222 return str(self) |
| 223 |
| 224 # This class is a wrapper around the objects actually passed to each |
| 225 # grammar rule. Index lookup and assignment actually assign the |
| 226 # .value attribute of the underlying YaccSymbol object. |
| 227 # The lineno() method returns the line number of a given |
| 228 # item (or 0 if not defined). The linespan() method returns |
| 229 # a tuple of (startline,endline) representing the range of lines |
| 230 # for a symbol. The lexspan() method returns a tuple (lexpos,endlexpos) |
| 231 # representing the range of positional information for a symbol. |
| 232 |
| 233 class YaccProduction: |
| 234 def __init__(self, s, stack=None): |
| 235 self.slice = s |
| 236 self.stack = stack |
| 237 self.lexer = None |
| 238 self.parser = None |
| 239 |
| 240 def __getitem__(self, n): |
| 241 if isinstance(n, slice): |
| 242 return [s.value for s in self.slice[n]] |
| 243 elif n >= 0: |
| 244 return self.slice[n].value |
| 245 else: |
| 246 return self.stack[n].value |
| 247 |
| 248 def __setitem__(self, n, v): |
| 249 self.slice[n].value = v |
| 250 |
| 251 def __getslice__(self, i, j): |
| 252 return [s.value for s in self.slice[i:j]] |
| 253 |
| 254 def __len__(self): |
| 255 return len(self.slice) |
| 256 |
| 257 def lineno(self, n): |
| 258 return getattr(self.slice[n], 'lineno', 0) |
| 259 |
| 260 def set_lineno(self, n, lineno): |
| 261 self.slice[n].lineno = lineno |
| 262 |
| 263 def linespan(self, n): |
| 264 startline = getattr(self.slice[n], 'lineno', 0) |
| 265 endline = getattr(self.slice[n], 'endlineno', startline) |
| 266 return startline, endline |
| 267 |
| 268 def lexpos(self, n): |
| 269 return getattr(self.slice[n], 'lexpos', 0) |
| 270 |
| 271 def lexspan(self, n): |
| 272 startpos = getattr(self.slice[n], 'lexpos', 0) |
| 273 endpos = getattr(self.slice[n], 'endlexpos', startpos) |
| 274 return startpos, endpos |
| 275 |
| 276 def error(self): |
| 277 raise SyntaxError |
| 278 |
| 279 # ----------------------------------------------------------------------------- |
| 280 # == LRParser == |
| 281 # |
| 282 # The LR Parsing engine. |
| 283 # ----------------------------------------------------------------------------- |
| 284 |
| 285 class LRParser: |
| 286 def __init__(self, lrtab, errorf): |
| 287 self.productions = lrtab.lr_productions |
| 288 self.action = lrtab.lr_action |
| 289 self.goto = lrtab.lr_goto |
| 290 self.errorfunc = errorf |
| 291 self.set_defaulted_states() |
| 292 self.errorok = True |
| 293 |
| 294 def errok(self): |
| 295 self.errorok = True |
| 296 |
| 297 def restart(self): |
| 298 del self.statestack[:] |
| 299 del self.symstack[:] |
| 300 sym = YaccSymbol() |
| 301 sym.type = '$end' |
| 302 self.symstack.append(sym) |
| 303 self.statestack.append(0) |
| 304 |
| 305 # Defaulted state support. |
| 306 # This method identifies parser states where there is only one possible redu
ction action. |
| 307 # For such states, the parser can make a choose to make a rule reduction wit
hout consuming |
| 308 # the next look-ahead token. This delayed invocation of the tokenizer can b
e useful in |
| 309 # certain kinds of advanced parsing situations where the lexer and parser in
teract with |
| 310 # each other or change states (i.e., manipulation of scope, lexer states, et
c.). |
| 311 # |
| 312 # See: http://www.gnu.org/software/bison/manual/html_node/Default-Reduction
s.html#Default-Reductions |
| 313 def set_defaulted_states(self): |
| 314 self.defaulted_states = {} |
| 315 for state, actions in self.action.items(): |
| 316 rules = list(actions.values()) |
| 317 if len(rules) == 1 and rules[0] < 0: |
| 318 self.defaulted_states[state] = rules[0] |
| 319 |
| 320 def disable_defaulted_states(self): |
| 321 self.defaulted_states = {} |
| 322 |
| 323 def parse(self, input=None, lexer=None, debug=False, tracking=False, tokenfu
nc=None): |
| 324 if debug or yaccdevel: |
| 325 if isinstance(debug, int): |
| 326 debug = PlyLogger(sys.stderr) |
| 327 return self.parsedebug(input, lexer, debug, tracking, tokenfunc) |
| 328 elif tracking: |
| 329 return self.parseopt(input, lexer, debug, tracking, tokenfunc) |
| 330 else: |
| 331 return self.parseopt_notrack(input, lexer, debug, tracking, tokenfun
c) |
| 332 |
| 333 |
| 334 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! |
| 335 # parsedebug(). |
| 336 # |
| 337 # This is the debugging enabled version of parse(). All changes made to the |
| 338 # parsing engine should be made here. Optimized versions of this function |
| 339 # are automatically created by the ply/ygen.py script. This script cuts out |
| 340 # sections enclosed in markers such as this: |
| 341 # |
| 342 # #--! DEBUG |
| 343 # statements |
| 344 # #--! DEBUG |
| 345 # |
| 346 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! |
| 347 |
| 348 def parsedebug(self, input=None, lexer=None, debug=False, tracking=False, to
kenfunc=None): |
| 349 #--! parsedebug-start |
| 350 lookahead = None # Current lookahead symbol |
| 351 lookaheadstack = [] # Stack of lookahead symbols |
| 352 actions = self.action # Local reference to action tab
le (to avoid lookup on self.) |
| 353 goto = self.goto # Local reference to goto table
(to avoid lookup on self.) |
| 354 prod = self.productions # Local reference to production
list (to avoid lookup on self.) |
| 355 defaulted_states = self.defaulted_states # Local reference to defaulted
states |
| 356 pslice = YaccProduction(None) # Production object passed to g
rammar rules |
| 357 errorcount = 0 # Used during error recovery |
| 358 |
| 359 #--! DEBUG |
| 360 debug.info('PLY: PARSE DEBUG START') |
| 361 #--! DEBUG |
| 362 |
| 363 # If no lexer was given, we will try to use the lex module |
| 364 if not lexer: |
| 365 from . import lex |
| 366 lexer = lex.lexer |
| 367 |
| 368 # Set up the lexer and parser objects on pslice |
| 369 pslice.lexer = lexer |
| 370 pslice.parser = self |
| 371 |
| 372 # If input was supplied, pass to lexer |
| 373 if input is not None: |
| 374 lexer.input(input) |
| 375 |
| 376 if tokenfunc is None: |
| 377 # Tokenize function |
| 378 get_token = lexer.token |
| 379 else: |
| 380 get_token = tokenfunc |
| 381 |
| 382 # Set the parser() token method (sometimes used in error recovery) |
| 383 self.token = get_token |
| 384 |
| 385 # Set up the state and symbol stacks |
| 386 |
| 387 statestack = [] # Stack of parsing states |
| 388 self.statestack = statestack |
| 389 symstack = [] # Stack of grammar symbols |
| 390 self.symstack = symstack |
| 391 |
| 392 pslice.stack = symstack # Put in the production |
| 393 errtoken = None # Err token |
| 394 |
| 395 # The start state is assumed to be (0,$end) |
| 396 |
| 397 statestack.append(0) |
| 398 sym = YaccSymbol() |
| 399 sym.type = '$end' |
| 400 symstack.append(sym) |
| 401 state = 0 |
| 402 while True: |
| 403 # Get the next symbol on the input. If a lookahead symbol |
| 404 # is already set, we just use that. Otherwise, we'll pull |
| 405 # the next token off of the lookaheadstack or from the lexer |
| 406 |
| 407 #--! DEBUG |
| 408 debug.debug('') |
| 409 debug.debug('State : %s', state) |
| 410 #--! DEBUG |
| 411 |
| 412 if state not in defaulted_states: |
| 413 if not lookahead: |
| 414 if not lookaheadstack: |
| 415 lookahead = get_token() # Get the next token |
| 416 else: |
| 417 lookahead = lookaheadstack.pop() |
| 418 if not lookahead: |
| 419 lookahead = YaccSymbol() |
| 420 lookahead.type = '$end' |
| 421 |
| 422 # Check the action table |
| 423 ltype = lookahead.type |
| 424 t = actions[state].get(ltype) |
| 425 else: |
| 426 t = defaulted_states[state] |
| 427 #--! DEBUG |
| 428 debug.debug('Defaulted state %s: Reduce using %d', state, -t) |
| 429 #--! DEBUG |
| 430 |
| 431 #--! DEBUG |
| 432 debug.debug('Stack : %s', |
| 433 ('%s . %s' % (' '.join([xx.type for xx in symstack][1:])
, str(lookahead))).lstrip()) |
| 434 #--! DEBUG |
| 435 |
| 436 if t is not None: |
| 437 if t > 0: |
| 438 # shift a symbol on the stack |
| 439 statestack.append(t) |
| 440 state = t |
| 441 |
| 442 #--! DEBUG |
| 443 debug.debug('Action : Shift and goto state %s', t) |
| 444 #--! DEBUG |
| 445 |
| 446 symstack.append(lookahead) |
| 447 lookahead = None |
| 448 |
| 449 # Decrease error count on successful shift |
| 450 if errorcount: |
| 451 errorcount -= 1 |
| 452 continue |
| 453 |
| 454 if t < 0: |
| 455 # reduce a symbol on the stack, emit a production |
| 456 p = prod[-t] |
| 457 pname = p.name |
| 458 plen = p.len |
| 459 |
| 460 # Get production function |
| 461 sym = YaccSymbol() |
| 462 sym.type = pname # Production name |
| 463 sym.value = None |
| 464 |
| 465 #--! DEBUG |
| 466 if plen: |
| 467 debug.info('Action : Reduce rule [%s] with %s and goto s
tate %d', p.str, |
| 468 '['+','.join([format_stack_entry(_v.value) fo
r _v in symstack[-plen:]])+']', |
| 469 goto[statestack[-1-plen]][pname]) |
| 470 else: |
| 471 debug.info('Action : Reduce rule [%s] with %s and goto s
tate %d', p.str, [], |
| 472 goto[statestack[-1]][pname]) |
| 473 |
| 474 #--! DEBUG |
| 475 |
| 476 if plen: |
| 477 targ = symstack[-plen-1:] |
| 478 targ[0] = sym |
| 479 |
| 480 #--! TRACKING |
| 481 if tracking: |
| 482 t1 = targ[1] |
| 483 sym.lineno = t1.lineno |
| 484 sym.lexpos = t1.lexpos |
| 485 t1 = targ[-1] |
| 486 sym.endlineno = getattr(t1, 'endlineno', t1.lineno) |
| 487 sym.endlexpos = getattr(t1, 'endlexpos', t1.lexpos) |
| 488 #--! TRACKING |
| 489 |
| 490 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 491 # The code enclosed in this section is duplicated |
| 492 # below as a performance optimization. Make sure |
| 493 # changes get made in both locations. |
| 494 |
| 495 pslice.slice = targ |
| 496 |
| 497 try: |
| 498 # Call the grammar rule with our special slice objec
t |
| 499 del symstack[-plen:] |
| 500 self.state = state |
| 501 p.callable(pslice) |
| 502 del statestack[-plen:] |
| 503 #--! DEBUG |
| 504 debug.info('Result : %s', format_result(pslice[0])) |
| 505 #--! DEBUG |
| 506 symstack.append(sym) |
| 507 state = goto[statestack[-1]][pname] |
| 508 statestack.append(state) |
| 509 except SyntaxError: |
| 510 # If an error was set. Enter error recovery state |
| 511 lookaheadstack.append(lookahead) # Save the curre
nt lookahead token |
| 512 symstack.extend(targ[1:-1]) # Put the produc
tion slice back on the stack |
| 513 statestack.pop() # Pop back one s
tate (before the reduce) |
| 514 state = statestack[-1] |
| 515 sym.type = 'error' |
| 516 sym.value = 'error' |
| 517 lookahead = sym |
| 518 errorcount = error_count |
| 519 self.errorok = False |
| 520 |
| 521 continue |
| 522 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 523 |
| 524 else: |
| 525 |
| 526 #--! TRACKING |
| 527 if tracking: |
| 528 sym.lineno = lexer.lineno |
| 529 sym.lexpos = lexer.lexpos |
| 530 #--! TRACKING |
| 531 |
| 532 targ = [sym] |
| 533 |
| 534 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 535 # The code enclosed in this section is duplicated |
| 536 # above as a performance optimization. Make sure |
| 537 # changes get made in both locations. |
| 538 |
| 539 pslice.slice = targ |
| 540 |
| 541 try: |
| 542 # Call the grammar rule with our special slice objec
t |
| 543 self.state = state |
| 544 p.callable(pslice) |
| 545 #--! DEBUG |
| 546 debug.info('Result : %s', format_result(pslice[0])) |
| 547 #--! DEBUG |
| 548 symstack.append(sym) |
| 549 state = goto[statestack[-1]][pname] |
| 550 statestack.append(state) |
| 551 except SyntaxError: |
| 552 # If an error was set. Enter error recovery state |
| 553 lookaheadstack.append(lookahead) # Save the curre
nt lookahead token |
| 554 statestack.pop() # Pop back one s
tate (before the reduce) |
| 555 state = statestack[-1] |
| 556 sym.type = 'error' |
| 557 sym.value = 'error' |
| 558 lookahead = sym |
| 559 errorcount = error_count |
| 560 self.errorok = False |
| 561 |
| 562 continue |
| 563 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 564 |
| 565 if t == 0: |
| 566 n = symstack[-1] |
| 567 result = getattr(n, 'value', None) |
| 568 #--! DEBUG |
| 569 debug.info('Done : Returning %s', format_result(result)) |
| 570 debug.info('PLY: PARSE DEBUG END') |
| 571 #--! DEBUG |
| 572 return result |
| 573 |
| 574 if t is None: |
| 575 |
| 576 #--! DEBUG |
| 577 debug.error('Error : %s', |
| 578 ('%s . %s' % (' '.join([xx.type for xx in symstack][
1:]), str(lookahead))).lstrip()) |
| 579 #--! DEBUG |
| 580 |
| 581 # We have some kind of parsing error here. To handle |
| 582 # this, we are going to push the current token onto |
| 583 # the tokenstack and replace it with an 'error' token. |
| 584 # If there are any synchronization rules, they may |
| 585 # catch it. |
| 586 # |
| 587 # In addition to pushing the error token, we call call |
| 588 # the user defined p_error() function if this is the |
| 589 # first syntax error. This function is only called if |
| 590 # errorcount == 0. |
| 591 if errorcount == 0 or self.errorok: |
| 592 errorcount = error_count |
| 593 self.errorok = False |
| 594 errtoken = lookahead |
| 595 if errtoken.type == '$end': |
| 596 errtoken = None # End of file! |
| 597 if self.errorfunc: |
| 598 if errtoken and not hasattr(errtoken, 'lexer'): |
| 599 errtoken.lexer = lexer |
| 600 self.state = state |
| 601 tok = call_errorfunc(self.errorfunc, errtoken, self) |
| 602 if self.errorok: |
| 603 # User must have done some kind of panic |
| 604 # mode recovery on their own. The |
| 605 # returned token is the next lookahead |
| 606 lookahead = tok |
| 607 errtoken = None |
| 608 continue |
| 609 else: |
| 610 if errtoken: |
| 611 if hasattr(errtoken, 'lineno'): |
| 612 lineno = lookahead.lineno |
| 613 else: |
| 614 lineno = 0 |
| 615 if lineno: |
| 616 sys.stderr.write('yacc: Syntax error at line %d,
token=%s\n' % (lineno, errtoken.type)) |
| 617 else: |
| 618 sys.stderr.write('yacc: Syntax error, token=%s'
% errtoken.type) |
| 619 else: |
| 620 sys.stderr.write('yacc: Parse error in input. EOF\n'
) |
| 621 return |
| 622 |
| 623 else: |
| 624 errorcount = error_count |
| 625 |
| 626 # case 1: the statestack only has 1 entry on it. If we're in t
his state, the |
| 627 # entire parse has been rolled back and we're completely hosed.
The token is |
| 628 # discarded and we just keep going. |
| 629 |
| 630 if len(statestack) <= 1 and lookahead.type != '$end': |
| 631 lookahead = None |
| 632 errtoken = None |
| 633 state = 0 |
| 634 # Nuke the pushback stack |
| 635 del lookaheadstack[:] |
| 636 continue |
| 637 |
| 638 # case 2: the statestack has a couple of entries on it, but we'r
e |
| 639 # at the end of the file. nuke the top entry and generate an err
or token |
| 640 |
| 641 # Start nuking entries on the stack |
| 642 if lookahead.type == '$end': |
| 643 # Whoa. We're really hosed here. Bail out |
| 644 return |
| 645 |
| 646 if lookahead.type != 'error': |
| 647 sym = symstack[-1] |
| 648 if sym.type == 'error': |
| 649 # Hmmm. Error is on top of stack, we'll just nuke input |
| 650 # symbol and continue |
| 651 #--! TRACKING |
| 652 if tracking: |
| 653 sym.endlineno = getattr(lookahead, 'lineno', sym.lin
eno) |
| 654 sym.endlexpos = getattr(lookahead, 'lexpos', sym.lex
pos) |
| 655 #--! TRACKING |
| 656 lookahead = None |
| 657 continue |
| 658 |
| 659 # Create the error symbol for the first time and make it the
new lookahead symbol |
| 660 t = YaccSymbol() |
| 661 t.type = 'error' |
| 662 |
| 663 if hasattr(lookahead, 'lineno'): |
| 664 t.lineno = t.endlineno = lookahead.lineno |
| 665 if hasattr(lookahead, 'lexpos'): |
| 666 t.lexpos = t.endlexpos = lookahead.lexpos |
| 667 t.value = lookahead |
| 668 lookaheadstack.append(lookahead) |
| 669 lookahead = t |
| 670 else: |
| 671 sym = symstack.pop() |
| 672 #--! TRACKING |
| 673 if tracking: |
| 674 lookahead.lineno = sym.lineno |
| 675 lookahead.lexpos = sym.lexpos |
| 676 #--! TRACKING |
| 677 statestack.pop() |
| 678 state = statestack[-1] |
| 679 |
| 680 continue |
| 681 |
| 682 # Call an error function here |
| 683 raise RuntimeError('yacc: internal parser error!!!\n') |
| 684 |
| 685 #--! parsedebug-end |
| 686 |
| 687 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! |
| 688 # parseopt(). |
| 689 # |
| 690 # Optimized version of parse() method. DO NOT EDIT THIS CODE DIRECTLY! |
| 691 # This code is automatically generated by the ply/ygen.py script. Make |
| 692 # changes to the parsedebug() method instead. |
| 693 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! |
| 694 |
| 695 def parseopt(self, input=None, lexer=None, debug=False, tracking=False, toke
nfunc=None): |
| 696 #--! parseopt-start |
| 697 lookahead = None # Current lookahead symbol |
| 698 lookaheadstack = [] # Stack of lookahead symbols |
| 699 actions = self.action # Local reference to action tab
le (to avoid lookup on self.) |
| 700 goto = self.goto # Local reference to goto table
(to avoid lookup on self.) |
| 701 prod = self.productions # Local reference to production
list (to avoid lookup on self.) |
| 702 defaulted_states = self.defaulted_states # Local reference to defaulted
states |
| 703 pslice = YaccProduction(None) # Production object passed to g
rammar rules |
| 704 errorcount = 0 # Used during error recovery |
| 705 |
| 706 |
| 707 # If no lexer was given, we will try to use the lex module |
| 708 if not lexer: |
| 709 from . import lex |
| 710 lexer = lex.lexer |
| 711 |
| 712 # Set up the lexer and parser objects on pslice |
| 713 pslice.lexer = lexer |
| 714 pslice.parser = self |
| 715 |
| 716 # If input was supplied, pass to lexer |
| 717 if input is not None: |
| 718 lexer.input(input) |
| 719 |
| 720 if tokenfunc is None: |
| 721 # Tokenize function |
| 722 get_token = lexer.token |
| 723 else: |
| 724 get_token = tokenfunc |
| 725 |
| 726 # Set the parser() token method (sometimes used in error recovery) |
| 727 self.token = get_token |
| 728 |
| 729 # Set up the state and symbol stacks |
| 730 |
| 731 statestack = [] # Stack of parsing states |
| 732 self.statestack = statestack |
| 733 symstack = [] # Stack of grammar symbols |
| 734 self.symstack = symstack |
| 735 |
| 736 pslice.stack = symstack # Put in the production |
| 737 errtoken = None # Err token |
| 738 |
| 739 # The start state is assumed to be (0,$end) |
| 740 |
| 741 statestack.append(0) |
| 742 sym = YaccSymbol() |
| 743 sym.type = '$end' |
| 744 symstack.append(sym) |
| 745 state = 0 |
| 746 while True: |
| 747 # Get the next symbol on the input. If a lookahead symbol |
| 748 # is already set, we just use that. Otherwise, we'll pull |
| 749 # the next token off of the lookaheadstack or from the lexer |
| 750 |
| 751 |
| 752 if state not in defaulted_states: |
| 753 if not lookahead: |
| 754 if not lookaheadstack: |
| 755 lookahead = get_token() # Get the next token |
| 756 else: |
| 757 lookahead = lookaheadstack.pop() |
| 758 if not lookahead: |
| 759 lookahead = YaccSymbol() |
| 760 lookahead.type = '$end' |
| 761 |
| 762 # Check the action table |
| 763 ltype = lookahead.type |
| 764 t = actions[state].get(ltype) |
| 765 else: |
| 766 t = defaulted_states[state] |
| 767 |
| 768 |
| 769 if t is not None: |
| 770 if t > 0: |
| 771 # shift a symbol on the stack |
| 772 statestack.append(t) |
| 773 state = t |
| 774 |
| 775 |
| 776 symstack.append(lookahead) |
| 777 lookahead = None |
| 778 |
| 779 # Decrease error count on successful shift |
| 780 if errorcount: |
| 781 errorcount -= 1 |
| 782 continue |
| 783 |
| 784 if t < 0: |
| 785 # reduce a symbol on the stack, emit a production |
| 786 p = prod[-t] |
| 787 pname = p.name |
| 788 plen = p.len |
| 789 |
| 790 # Get production function |
| 791 sym = YaccSymbol() |
| 792 sym.type = pname # Production name |
| 793 sym.value = None |
| 794 |
| 795 |
| 796 if plen: |
| 797 targ = symstack[-plen-1:] |
| 798 targ[0] = sym |
| 799 |
| 800 #--! TRACKING |
| 801 if tracking: |
| 802 t1 = targ[1] |
| 803 sym.lineno = t1.lineno |
| 804 sym.lexpos = t1.lexpos |
| 805 t1 = targ[-1] |
| 806 sym.endlineno = getattr(t1, 'endlineno', t1.lineno) |
| 807 sym.endlexpos = getattr(t1, 'endlexpos', t1.lexpos) |
| 808 #--! TRACKING |
| 809 |
| 810 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 811 # The code enclosed in this section is duplicated |
| 812 # below as a performance optimization. Make sure |
| 813 # changes get made in both locations. |
| 814 |
| 815 pslice.slice = targ |
| 816 |
| 817 try: |
| 818 # Call the grammar rule with our special slice objec
t |
| 819 del symstack[-plen:] |
| 820 self.state = state |
| 821 p.callable(pslice) |
| 822 del statestack[-plen:] |
| 823 symstack.append(sym) |
| 824 state = goto[statestack[-1]][pname] |
| 825 statestack.append(state) |
| 826 except SyntaxError: |
| 827 # If an error was set. Enter error recovery state |
| 828 lookaheadstack.append(lookahead) # Save the curre
nt lookahead token |
| 829 symstack.extend(targ[1:-1]) # Put the produc
tion slice back on the stack |
| 830 statestack.pop() # Pop back one s
tate (before the reduce) |
| 831 state = statestack[-1] |
| 832 sym.type = 'error' |
| 833 sym.value = 'error' |
| 834 lookahead = sym |
| 835 errorcount = error_count |
| 836 self.errorok = False |
| 837 |
| 838 continue |
| 839 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 840 |
| 841 else: |
| 842 |
| 843 #--! TRACKING |
| 844 if tracking: |
| 845 sym.lineno = lexer.lineno |
| 846 sym.lexpos = lexer.lexpos |
| 847 #--! TRACKING |
| 848 |
| 849 targ = [sym] |
| 850 |
| 851 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 852 # The code enclosed in this section is duplicated |
| 853 # above as a performance optimization. Make sure |
| 854 # changes get made in both locations. |
| 855 |
| 856 pslice.slice = targ |
| 857 |
| 858 try: |
| 859 # Call the grammar rule with our special slice objec
t |
| 860 self.state = state |
| 861 p.callable(pslice) |
| 862 symstack.append(sym) |
| 863 state = goto[statestack[-1]][pname] |
| 864 statestack.append(state) |
| 865 except SyntaxError: |
| 866 # If an error was set. Enter error recovery state |
| 867 lookaheadstack.append(lookahead) # Save the curre
nt lookahead token |
| 868 statestack.pop() # Pop back one s
tate (before the reduce) |
| 869 state = statestack[-1] |
| 870 sym.type = 'error' |
| 871 sym.value = 'error' |
| 872 lookahead = sym |
| 873 errorcount = error_count |
| 874 self.errorok = False |
| 875 |
| 876 continue |
| 877 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 878 |
| 879 if t == 0: |
| 880 n = symstack[-1] |
| 881 result = getattr(n, 'value', None) |
| 882 return result |
| 883 |
| 884 if t is None: |
| 885 |
| 886 |
| 887 # We have some kind of parsing error here. To handle |
| 888 # this, we are going to push the current token onto |
| 889 # the tokenstack and replace it with an 'error' token. |
| 890 # If there are any synchronization rules, they may |
| 891 # catch it. |
| 892 # |
| 893 # In addition to pushing the error token, we call call |
| 894 # the user defined p_error() function if this is the |
| 895 # first syntax error. This function is only called if |
| 896 # errorcount == 0. |
| 897 if errorcount == 0 or self.errorok: |
| 898 errorcount = error_count |
| 899 self.errorok = False |
| 900 errtoken = lookahead |
| 901 if errtoken.type == '$end': |
| 902 errtoken = None # End of file! |
| 903 if self.errorfunc: |
| 904 if errtoken and not hasattr(errtoken, 'lexer'): |
| 905 errtoken.lexer = lexer |
| 906 self.state = state |
| 907 tok = call_errorfunc(self.errorfunc, errtoken, self) |
| 908 if self.errorok: |
| 909 # User must have done some kind of panic |
| 910 # mode recovery on their own. The |
| 911 # returned token is the next lookahead |
| 912 lookahead = tok |
| 913 errtoken = None |
| 914 continue |
| 915 else: |
| 916 if errtoken: |
| 917 if hasattr(errtoken, 'lineno'): |
| 918 lineno = lookahead.lineno |
| 919 else: |
| 920 lineno = 0 |
| 921 if lineno: |
| 922 sys.stderr.write('yacc: Syntax error at line %d,
token=%s\n' % (lineno, errtoken.type)) |
| 923 else: |
| 924 sys.stderr.write('yacc: Syntax error, token=%s'
% errtoken.type) |
| 925 else: |
| 926 sys.stderr.write('yacc: Parse error in input. EOF\n'
) |
| 927 return |
| 928 |
| 929 else: |
| 930 errorcount = error_count |
| 931 |
| 932 # case 1: the statestack only has 1 entry on it. If we're in t
his state, the |
| 933 # entire parse has been rolled back and we're completely hosed.
The token is |
| 934 # discarded and we just keep going. |
| 935 |
| 936 if len(statestack) <= 1 and lookahead.type != '$end': |
| 937 lookahead = None |
| 938 errtoken = None |
| 939 state = 0 |
| 940 # Nuke the pushback stack |
| 941 del lookaheadstack[:] |
| 942 continue |
| 943 |
| 944 # case 2: the statestack has a couple of entries on it, but we'r
e |
| 945 # at the end of the file. nuke the top entry and generate an err
or token |
| 946 |
| 947 # Start nuking entries on the stack |
| 948 if lookahead.type == '$end': |
| 949 # Whoa. We're really hosed here. Bail out |
| 950 return |
| 951 |
| 952 if lookahead.type != 'error': |
| 953 sym = symstack[-1] |
| 954 if sym.type == 'error': |
| 955 # Hmmm. Error is on top of stack, we'll just nuke input |
| 956 # symbol and continue |
| 957 #--! TRACKING |
| 958 if tracking: |
| 959 sym.endlineno = getattr(lookahead, 'lineno', sym.lin
eno) |
| 960 sym.endlexpos = getattr(lookahead, 'lexpos', sym.lex
pos) |
| 961 #--! TRACKING |
| 962 lookahead = None |
| 963 continue |
| 964 |
| 965 # Create the error symbol for the first time and make it the
new lookahead symbol |
| 966 t = YaccSymbol() |
| 967 t.type = 'error' |
| 968 |
| 969 if hasattr(lookahead, 'lineno'): |
| 970 t.lineno = t.endlineno = lookahead.lineno |
| 971 if hasattr(lookahead, 'lexpos'): |
| 972 t.lexpos = t.endlexpos = lookahead.lexpos |
| 973 t.value = lookahead |
| 974 lookaheadstack.append(lookahead) |
| 975 lookahead = t |
| 976 else: |
| 977 sym = symstack.pop() |
| 978 #--! TRACKING |
| 979 if tracking: |
| 980 lookahead.lineno = sym.lineno |
| 981 lookahead.lexpos = sym.lexpos |
| 982 #--! TRACKING |
| 983 statestack.pop() |
| 984 state = statestack[-1] |
| 985 |
| 986 continue |
| 987 |
| 988 # Call an error function here |
| 989 raise RuntimeError('yacc: internal parser error!!!\n') |
| 990 |
| 991 #--! parseopt-end |
| 992 |
| 993 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! |
| 994 # parseopt_notrack(). |
| 995 # |
| 996 # Optimized version of parseopt() with line number tracking removed. |
| 997 # DO NOT EDIT THIS CODE DIRECTLY. This code is automatically generated |
| 998 # by the ply/ygen.py script. Make changes to the parsedebug() method instead
. |
| 999 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! |
| 1000 |
| 1001 def parseopt_notrack(self, input=None, lexer=None, debug=False, tracking=Fal
se, tokenfunc=None): |
| 1002 #--! parseopt-notrack-start |
| 1003 lookahead = None # Current lookahead symbol |
| 1004 lookaheadstack = [] # Stack of lookahead symbols |
| 1005 actions = self.action # Local reference to action tab
le (to avoid lookup on self.) |
| 1006 goto = self.goto # Local reference to goto table
(to avoid lookup on self.) |
| 1007 prod = self.productions # Local reference to production
list (to avoid lookup on self.) |
| 1008 defaulted_states = self.defaulted_states # Local reference to defaulted
states |
| 1009 pslice = YaccProduction(None) # Production object passed to g
rammar rules |
| 1010 errorcount = 0 # Used during error recovery |
| 1011 |
| 1012 |
| 1013 # If no lexer was given, we will try to use the lex module |
| 1014 if not lexer: |
| 1015 from . import lex |
| 1016 lexer = lex.lexer |
| 1017 |
| 1018 # Set up the lexer and parser objects on pslice |
| 1019 pslice.lexer = lexer |
| 1020 pslice.parser = self |
| 1021 |
| 1022 # If input was supplied, pass to lexer |
| 1023 if input is not None: |
| 1024 lexer.input(input) |
| 1025 |
| 1026 if tokenfunc is None: |
| 1027 # Tokenize function |
| 1028 get_token = lexer.token |
| 1029 else: |
| 1030 get_token = tokenfunc |
| 1031 |
| 1032 # Set the parser() token method (sometimes used in error recovery) |
| 1033 self.token = get_token |
| 1034 |
| 1035 # Set up the state and symbol stacks |
| 1036 |
| 1037 statestack = [] # Stack of parsing states |
| 1038 self.statestack = statestack |
| 1039 symstack = [] # Stack of grammar symbols |
| 1040 self.symstack = symstack |
| 1041 |
| 1042 pslice.stack = symstack # Put in the production |
| 1043 errtoken = None # Err token |
| 1044 |
| 1045 # The start state is assumed to be (0,$end) |
| 1046 |
| 1047 statestack.append(0) |
| 1048 sym = YaccSymbol() |
| 1049 sym.type = '$end' |
| 1050 symstack.append(sym) |
| 1051 state = 0 |
| 1052 while True: |
| 1053 # Get the next symbol on the input. If a lookahead symbol |
| 1054 # is already set, we just use that. Otherwise, we'll pull |
| 1055 # the next token off of the lookaheadstack or from the lexer |
| 1056 |
| 1057 |
| 1058 if state not in defaulted_states: |
| 1059 if not lookahead: |
| 1060 if not lookaheadstack: |
| 1061 lookahead = get_token() # Get the next token |
| 1062 else: |
| 1063 lookahead = lookaheadstack.pop() |
| 1064 if not lookahead: |
| 1065 lookahead = YaccSymbol() |
| 1066 lookahead.type = '$end' |
| 1067 |
| 1068 # Check the action table |
| 1069 ltype = lookahead.type |
| 1070 t = actions[state].get(ltype) |
| 1071 else: |
| 1072 t = defaulted_states[state] |
| 1073 |
| 1074 |
| 1075 if t is not None: |
| 1076 if t > 0: |
| 1077 # shift a symbol on the stack |
| 1078 statestack.append(t) |
| 1079 state = t |
| 1080 |
| 1081 |
| 1082 symstack.append(lookahead) |
| 1083 lookahead = None |
| 1084 |
| 1085 # Decrease error count on successful shift |
| 1086 if errorcount: |
| 1087 errorcount -= 1 |
| 1088 continue |
| 1089 |
| 1090 if t < 0: |
| 1091 # reduce a symbol on the stack, emit a production |
| 1092 p = prod[-t] |
| 1093 pname = p.name |
| 1094 plen = p.len |
| 1095 |
| 1096 # Get production function |
| 1097 sym = YaccSymbol() |
| 1098 sym.type = pname # Production name |
| 1099 sym.value = None |
| 1100 |
| 1101 |
| 1102 if plen: |
| 1103 targ = symstack[-plen-1:] |
| 1104 targ[0] = sym |
| 1105 |
| 1106 |
| 1107 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 1108 # The code enclosed in this section is duplicated |
| 1109 # below as a performance optimization. Make sure |
| 1110 # changes get made in both locations. |
| 1111 |
| 1112 pslice.slice = targ |
| 1113 |
| 1114 try: |
| 1115 # Call the grammar rule with our special slice objec
t |
| 1116 del symstack[-plen:] |
| 1117 self.state = state |
| 1118 p.callable(pslice) |
| 1119 del statestack[-plen:] |
| 1120 symstack.append(sym) |
| 1121 state = goto[statestack[-1]][pname] |
| 1122 statestack.append(state) |
| 1123 except SyntaxError: |
| 1124 # If an error was set. Enter error recovery state |
| 1125 lookaheadstack.append(lookahead) # Save the curre
nt lookahead token |
| 1126 symstack.extend(targ[1:-1]) # Put the produc
tion slice back on the stack |
| 1127 statestack.pop() # Pop back one s
tate (before the reduce) |
| 1128 state = statestack[-1] |
| 1129 sym.type = 'error' |
| 1130 sym.value = 'error' |
| 1131 lookahead = sym |
| 1132 errorcount = error_count |
| 1133 self.errorok = False |
| 1134 |
| 1135 continue |
| 1136 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 1137 |
| 1138 else: |
| 1139 |
| 1140 |
| 1141 targ = [sym] |
| 1142 |
| 1143 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 1144 # The code enclosed in this section is duplicated |
| 1145 # above as a performance optimization. Make sure |
| 1146 # changes get made in both locations. |
| 1147 |
| 1148 pslice.slice = targ |
| 1149 |
| 1150 try: |
| 1151 # Call the grammar rule with our special slice objec
t |
| 1152 self.state = state |
| 1153 p.callable(pslice) |
| 1154 symstack.append(sym) |
| 1155 state = goto[statestack[-1]][pname] |
| 1156 statestack.append(state) |
| 1157 except SyntaxError: |
| 1158 # If an error was set. Enter error recovery state |
| 1159 lookaheadstack.append(lookahead) # Save the curre
nt lookahead token |
| 1160 statestack.pop() # Pop back one s
tate (before the reduce) |
| 1161 state = statestack[-1] |
| 1162 sym.type = 'error' |
| 1163 sym.value = 'error' |
| 1164 lookahead = sym |
| 1165 errorcount = error_count |
| 1166 self.errorok = False |
| 1167 |
| 1168 continue |
| 1169 # !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! |
| 1170 |
| 1171 if t == 0: |
| 1172 n = symstack[-1] |
| 1173 result = getattr(n, 'value', None) |
| 1174 return result |
| 1175 |
| 1176 if t is None: |
| 1177 |
| 1178 |
| 1179 # We have some kind of parsing error here. To handle |
| 1180 # this, we are going to push the current token onto |
| 1181 # the tokenstack and replace it with an 'error' token. |
| 1182 # If there are any synchronization rules, they may |
| 1183 # catch it. |
| 1184 # |
| 1185 # In addition to pushing the error token, we call call |
| 1186 # the user defined p_error() function if this is the |
| 1187 # first syntax error. This function is only called if |
| 1188 # errorcount == 0. |
| 1189 if errorcount == 0 or self.errorok: |
| 1190 errorcount = error_count |
| 1191 self.errorok = False |
| 1192 errtoken = lookahead |
| 1193 if errtoken.type == '$end': |
| 1194 errtoken = None # End of file! |
| 1195 if self.errorfunc: |
| 1196 if errtoken and not hasattr(errtoken, 'lexer'): |
| 1197 errtoken.lexer = lexer |
| 1198 self.state = state |
| 1199 tok = call_errorfunc(self.errorfunc, errtoken, self) |
| 1200 if self.errorok: |
| 1201 # User must have done some kind of panic |
| 1202 # mode recovery on their own. The |
| 1203 # returned token is the next lookahead |
| 1204 lookahead = tok |
| 1205 errtoken = None |
| 1206 continue |
| 1207 else: |
| 1208 if errtoken: |
| 1209 if hasattr(errtoken, 'lineno'): |
| 1210 lineno = lookahead.lineno |
| 1211 else: |
| 1212 lineno = 0 |
| 1213 if lineno: |
| 1214 sys.stderr.write('yacc: Syntax error at line %d,
token=%s\n' % (lineno, errtoken.type)) |
| 1215 else: |
| 1216 sys.stderr.write('yacc: Syntax error, token=%s'
% errtoken.type) |
| 1217 else: |
| 1218 sys.stderr.write('yacc: Parse error in input. EOF\n'
) |
| 1219 return |
| 1220 |
| 1221 else: |
| 1222 errorcount = error_count |
| 1223 |
| 1224 # case 1: the statestack only has 1 entry on it. If we're in t
his state, the |
| 1225 # entire parse has been rolled back and we're completely hosed.
The token is |
| 1226 # discarded and we just keep going. |
| 1227 |
| 1228 if len(statestack) <= 1 and lookahead.type != '$end': |
| 1229 lookahead = None |
| 1230 errtoken = None |
| 1231 state = 0 |
| 1232 # Nuke the pushback stack |
| 1233 del lookaheadstack[:] |
| 1234 continue |
| 1235 |
| 1236 # case 2: the statestack has a couple of entries on it, but we'r
e |
| 1237 # at the end of the file. nuke the top entry and generate an err
or token |
| 1238 |
| 1239 # Start nuking entries on the stack |
| 1240 if lookahead.type == '$end': |
| 1241 # Whoa. We're really hosed here. Bail out |
| 1242 return |
| 1243 |
| 1244 if lookahead.type != 'error': |
| 1245 sym = symstack[-1] |
| 1246 if sym.type == 'error': |
| 1247 # Hmmm. Error is on top of stack, we'll just nuke input |
| 1248 # symbol and continue |
| 1249 lookahead = None |
| 1250 continue |
| 1251 |
| 1252 # Create the error symbol for the first time and make it the
new lookahead symbol |
| 1253 t = YaccSymbol() |
| 1254 t.type = 'error' |
| 1255 |
| 1256 if hasattr(lookahead, 'lineno'): |
| 1257 t.lineno = t.endlineno = lookahead.lineno |
| 1258 if hasattr(lookahead, 'lexpos'): |
| 1259 t.lexpos = t.endlexpos = lookahead.lexpos |
| 1260 t.value = lookahead |
| 1261 lookaheadstack.append(lookahead) |
| 1262 lookahead = t |
| 1263 else: |
| 1264 sym = symstack.pop() |
| 1265 statestack.pop() |
| 1266 state = statestack[-1] |
| 1267 |
| 1268 continue |
| 1269 |
| 1270 # Call an error function here |
| 1271 raise RuntimeError('yacc: internal parser error!!!\n') |
| 1272 |
| 1273 #--! parseopt-notrack-end |
| 1274 |
| 1275 # ----------------------------------------------------------------------------- |
| 1276 # === Grammar Representation === |
| 1277 # |
| 1278 # The following functions, classes, and variables are used to represent and |
| 1279 # manipulate the rules that make up a grammar. |
| 1280 # ----------------------------------------------------------------------------- |
| 1281 |
| 1282 # regex matching identifiers |
| 1283 _is_identifier = re.compile(r'^[a-zA-Z0-9_-]+$') |
| 1284 |
| 1285 # ----------------------------------------------------------------------------- |
| 1286 # class Production: |
| 1287 # |
| 1288 # This class stores the raw information about a single production or grammar rul
e. |
| 1289 # A grammar rule refers to a specification such as this: |
| 1290 # |
| 1291 # expr : expr PLUS term |
| 1292 # |
| 1293 # Here are the basic attributes defined on all productions |
| 1294 # |
| 1295 # name - Name of the production. For example 'expr' |
| 1296 # prod - A list of symbols on the right side ['expr','PLUS','term'] |
| 1297 # prec - Production precedence level |
| 1298 # number - Production number. |
| 1299 # func - Function that executes on reduce |
| 1300 # file - File where production function is defined |
| 1301 # lineno - Line number where production function is defined |
| 1302 # |
| 1303 # The following attributes are defined or optional. |
| 1304 # |
| 1305 # len - Length of the production (number of symbols on right hand si
de) |
| 1306 # usyms - Set of unique symbols found in the production |
| 1307 # ----------------------------------------------------------------------------- |
| 1308 |
| 1309 class Production(object): |
| 1310 reduced = 0 |
| 1311 def __init__(self, number, name, prod, precedence=('right', 0), func=None, f
ile='', line=0): |
| 1312 self.name = name |
| 1313 self.prod = tuple(prod) |
| 1314 self.number = number |
| 1315 self.func = func |
| 1316 self.callable = None |
| 1317 self.file = file |
| 1318 self.line = line |
| 1319 self.prec = precedence |
| 1320 |
| 1321 # Internal settings used during table construction |
| 1322 |
| 1323 self.len = len(self.prod) # Length of the production |
| 1324 |
| 1325 # Create a list of unique production symbols used in the production |
| 1326 self.usyms = [] |
| 1327 for s in self.prod: |
| 1328 if s not in self.usyms: |
| 1329 self.usyms.append(s) |
| 1330 |
| 1331 # List of all LR items for the production |
| 1332 self.lr_items = [] |
| 1333 self.lr_next = None |
| 1334 |
| 1335 # Create a string representation |
| 1336 if self.prod: |
| 1337 self.str = '%s -> %s' % (self.name, ' '.join(self.prod)) |
| 1338 else: |
| 1339 self.str = '%s -> <empty>' % self.name |
| 1340 |
| 1341 def __str__(self): |
| 1342 return self.str |
| 1343 |
| 1344 def __repr__(self): |
| 1345 return 'Production(' + str(self) + ')' |
| 1346 |
| 1347 def __len__(self): |
| 1348 return len(self.prod) |
| 1349 |
| 1350 def __nonzero__(self): |
| 1351 return 1 |
| 1352 |
| 1353 def __getitem__(self, index): |
| 1354 return self.prod[index] |
| 1355 |
| 1356 # Return the nth lr_item from the production (or None if at the end) |
| 1357 def lr_item(self, n): |
| 1358 if n > len(self.prod): |
| 1359 return None |
| 1360 p = LRItem(self, n) |
| 1361 # Precompute the list of productions immediately following. |
| 1362 try: |
| 1363 p.lr_after = Prodnames[p.prod[n+1]] |
| 1364 except (IndexError, KeyError): |
| 1365 p.lr_after = [] |
| 1366 try: |
| 1367 p.lr_before = p.prod[n-1] |
| 1368 except IndexError: |
| 1369 p.lr_before = None |
| 1370 return p |
| 1371 |
| 1372 # Bind the production function name to a callable |
| 1373 def bind(self, pdict): |
| 1374 if self.func: |
| 1375 self.callable = pdict[self.func] |
| 1376 |
| 1377 # This class serves as a minimal standin for Production objects when |
| 1378 # reading table data from files. It only contains information |
| 1379 # actually used by the LR parsing engine, plus some additional |
| 1380 # debugging information. |
| 1381 class MiniProduction(object): |
| 1382 def __init__(self, str, name, len, func, file, line): |
| 1383 self.name = name |
| 1384 self.len = len |
| 1385 self.func = func |
| 1386 self.callable = None |
| 1387 self.file = file |
| 1388 self.line = line |
| 1389 self.str = str |
| 1390 |
| 1391 def __str__(self): |
| 1392 return self.str |
| 1393 |
| 1394 def __repr__(self): |
| 1395 return 'MiniProduction(%s)' % self.str |
| 1396 |
| 1397 # Bind the production function name to a callable |
| 1398 def bind(self, pdict): |
| 1399 if self.func: |
| 1400 self.callable = pdict[self.func] |
| 1401 |
| 1402 |
| 1403 # ----------------------------------------------------------------------------- |
| 1404 # class LRItem |
| 1405 # |
| 1406 # This class represents a specific stage of parsing a production rule. For |
| 1407 # example: |
| 1408 # |
| 1409 # expr : expr . PLUS term |
| 1410 # |
| 1411 # In the above, the "." represents the current location of the parse. Here |
| 1412 # basic attributes: |
| 1413 # |
| 1414 # name - Name of the production. For example 'expr' |
| 1415 # prod - A list of symbols on the right side ['expr','.', 'PLUS','te
rm'] |
| 1416 # number - Production number. |
| 1417 # |
| 1418 # lr_next Next LR item. Example, if we are ' expr -> expr . PLUS term
' |
| 1419 # then lr_next refers to 'expr -> expr PLUS . term' |
| 1420 # lr_index - LR item index (location of the ".") in the prod list. |
| 1421 # lookaheads - LALR lookahead symbols for this item |
| 1422 # len - Length of the production (number of symbols on right hand s
ide) |
| 1423 # lr_after - List of all productions that immediately follow |
| 1424 # lr_before - Grammar symbol immediately before |
| 1425 # ----------------------------------------------------------------------------- |
| 1426 |
| 1427 class LRItem(object): |
| 1428 def __init__(self, p, n): |
| 1429 self.name = p.name |
| 1430 self.prod = list(p.prod) |
| 1431 self.number = p.number |
| 1432 self.lr_index = n |
| 1433 self.lookaheads = {} |
| 1434 self.prod.insert(n, '.') |
| 1435 self.prod = tuple(self.prod) |
| 1436 self.len = len(self.prod) |
| 1437 self.usyms = p.usyms |
| 1438 |
| 1439 def __str__(self): |
| 1440 if self.prod: |
| 1441 s = '%s -> %s' % (self.name, ' '.join(self.prod)) |
| 1442 else: |
| 1443 s = '%s -> <empty>' % self.name |
| 1444 return s |
| 1445 |
| 1446 def __repr__(self): |
| 1447 return 'LRItem(' + str(self) + ')' |
| 1448 |
| 1449 # ----------------------------------------------------------------------------- |
| 1450 # rightmost_terminal() |
| 1451 # |
| 1452 # Return the rightmost terminal from a list of symbols. Used in add_production(
) |
| 1453 # ----------------------------------------------------------------------------- |
| 1454 def rightmost_terminal(symbols, terminals): |
| 1455 i = len(symbols) - 1 |
| 1456 while i >= 0: |
| 1457 if symbols[i] in terminals: |
| 1458 return symbols[i] |
| 1459 i -= 1 |
| 1460 return None |
| 1461 |
| 1462 # ----------------------------------------------------------------------------- |
| 1463 # === GRAMMAR CLASS === |
| 1464 # |
| 1465 # The following class represents the contents of the specified grammar along |
| 1466 # with various computed properties such as first sets, follow sets, LR items, et
c. |
| 1467 # This data is used for critical parts of the table generation process later. |
| 1468 # ----------------------------------------------------------------------------- |
| 1469 |
| 1470 class GrammarError(YaccError): |
| 1471 pass |
| 1472 |
| 1473 class Grammar(object): |
| 1474 def __init__(self, terminals): |
| 1475 self.Productions = [None] # A list of all of the productions. The fir
st |
| 1476 # entry is always reserved for the purpose o
f |
| 1477 # building an augmented grammar |
| 1478 |
| 1479 self.Prodnames = {} # A dictionary mapping the names of nontermi
nals to a list of all |
| 1480 # productions of that nonterminal. |
| 1481 |
| 1482 self.Prodmap = {} # A dictionary that is only used to detect d
uplicate |
| 1483 # productions. |
| 1484 |
| 1485 self.Terminals = {} # A dictionary mapping the names of terminal
symbols to a |
| 1486 # list of the rules where they are used. |
| 1487 |
| 1488 for term in terminals: |
| 1489 self.Terminals[term] = [] |
| 1490 |
| 1491 self.Terminals['error'] = [] |
| 1492 |
| 1493 self.Nonterminals = {} # A dictionary mapping names of nonterminals
to a list |
| 1494 # of rule numbers where they are used. |
| 1495 |
| 1496 self.First = {} # A dictionary of precomputed FIRST(x) symbo
ls |
| 1497 |
| 1498 self.Follow = {} # A dictionary of precomputed FOLLOW(x) symb
ols |
| 1499 |
| 1500 self.Precedence = {} # Precedence rules for each terminal. Contai
ns tuples of the |
| 1501 # form ('right',level) or ('nonassoc', level
) or ('left',level) |
| 1502 |
| 1503 self.UsedPrecedence = set() # Precedence rules that were actually used b
y the grammer. |
| 1504 # This is only used to provide error checkin
g and to generate |
| 1505 # a warning about unused precedence rules. |
| 1506 |
| 1507 self.Start = None # Starting symbol for the grammar |
| 1508 |
| 1509 |
| 1510 def __len__(self): |
| 1511 return len(self.Productions) |
| 1512 |
| 1513 def __getitem__(self, index): |
| 1514 return self.Productions[index] |
| 1515 |
| 1516 # --------------------------------------------------------------------------
--- |
| 1517 # set_precedence() |
| 1518 # |
| 1519 # Sets the precedence for a given terminal. assoc is the associativity such
as |
| 1520 # 'left','right', or 'nonassoc'. level is a numeric level. |
| 1521 # |
| 1522 # --------------------------------------------------------------------------
--- |
| 1523 |
| 1524 def set_precedence(self, term, assoc, level): |
| 1525 assert self.Productions == [None], 'Must call set_precedence() before ad
d_production()' |
| 1526 if term in self.Precedence: |
| 1527 raise GrammarError('Precedence already specified for terminal %r' %
term) |
| 1528 if assoc not in ['left', 'right', 'nonassoc']: |
| 1529 raise GrammarError("Associativity must be one of 'left','right', or
'nonassoc'") |
| 1530 self.Precedence[term] = (assoc, level) |
| 1531 |
| 1532 # --------------------------------------------------------------------------
--- |
| 1533 # add_production() |
| 1534 # |
| 1535 # Given an action function, this function assembles a production rule and |
| 1536 # computes its precedence level. |
| 1537 # |
| 1538 # The production rule is supplied as a list of symbols. For example, |
| 1539 # a rule such as 'expr : expr PLUS term' has a production name of 'expr' and |
| 1540 # symbols ['expr','PLUS','term']. |
| 1541 # |
| 1542 # Precedence is determined by the precedence of the right-most non-terminal |
| 1543 # or the precedence of a terminal specified by %prec. |
| 1544 # |
| 1545 # A variety of error checks are performed to make sure production symbols |
| 1546 # are valid and that %prec is used correctly. |
| 1547 # --------------------------------------------------------------------------
--- |
| 1548 |
| 1549 def add_production(self, prodname, syms, func=None, file='', line=0): |
| 1550 |
| 1551 if prodname in self.Terminals: |
| 1552 raise GrammarError('%s:%d: Illegal rule name %r. Already defined as
a token' % (file, line, prodname)) |
| 1553 if prodname == 'error': |
| 1554 raise GrammarError('%s:%d: Illegal rule name %r. error is a reserved
word' % (file, line, prodname)) |
| 1555 if not _is_identifier.match(prodname): |
| 1556 raise GrammarError('%s:%d: Illegal rule name %r' % (file, line, prod
name)) |
| 1557 |
| 1558 # Look for literal tokens |
| 1559 for n, s in enumerate(syms): |
| 1560 if s[0] in "'\"": |
| 1561 try: |
| 1562 c = eval(s) |
| 1563 if (len(c) > 1): |
| 1564 raise GrammarError('%s:%d: Literal token %s in rule %r m
ay only be a single character' % |
| 1565 (file, line, s, prodname)) |
| 1566 if c not in self.Terminals: |
| 1567 self.Terminals[c] = [] |
| 1568 syms[n] = c |
| 1569 continue |
| 1570 except SyntaxError: |
| 1571 pass |
| 1572 if not _is_identifier.match(s) and s != '%prec': |
| 1573 raise GrammarError('%s:%d: Illegal name %r in rule %r' % (file,
line, s, prodname)) |
| 1574 |
| 1575 # Determine the precedence level |
| 1576 if '%prec' in syms: |
| 1577 if syms[-1] == '%prec': |
| 1578 raise GrammarError('%s:%d: Syntax error. Nothing follows %%prec'
% (file, line)) |
| 1579 if syms[-2] != '%prec': |
| 1580 raise GrammarError('%s:%d: Syntax error. %%prec can only appear
at the end of a grammar rule' % |
| 1581 (file, line)) |
| 1582 precname = syms[-1] |
| 1583 prodprec = self.Precedence.get(precname) |
| 1584 if not prodprec: |
| 1585 raise GrammarError('%s:%d: Nothing known about the precedence of
%r' % (file, line, precname)) |
| 1586 else: |
| 1587 self.UsedPrecedence.add(precname) |
| 1588 del syms[-2:] # Drop %prec from the rule |
| 1589 else: |
| 1590 # If no %prec, precedence is determined by the rightmost terminal sy
mbol |
| 1591 precname = rightmost_terminal(syms, self.Terminals) |
| 1592 prodprec = self.Precedence.get(precname, ('right', 0)) |
| 1593 |
| 1594 # See if the rule is already in the rulemap |
| 1595 map = '%s -> %s' % (prodname, syms) |
| 1596 if map in self.Prodmap: |
| 1597 m = self.Prodmap[map] |
| 1598 raise GrammarError('%s:%d: Duplicate rule %s. ' % (file, line, m) + |
| 1599 'Previous definition at %s:%d' % (m.file, m.line)
) |
| 1600 |
| 1601 # From this point on, everything is valid. Create a new Production inst
ance |
| 1602 pnumber = len(self.Productions) |
| 1603 if prodname not in self.Nonterminals: |
| 1604 self.Nonterminals[prodname] = [] |
| 1605 |
| 1606 # Add the production number to Terminals and Nonterminals |
| 1607 for t in syms: |
| 1608 if t in self.Terminals: |
| 1609 self.Terminals[t].append(pnumber) |
| 1610 else: |
| 1611 if t not in self.Nonterminals: |
| 1612 self.Nonterminals[t] = [] |
| 1613 self.Nonterminals[t].append(pnumber) |
| 1614 |
| 1615 # Create a production and add it to the list of productions |
| 1616 p = Production(pnumber, prodname, syms, prodprec, func, file, line) |
| 1617 self.Productions.append(p) |
| 1618 self.Prodmap[map] = p |
| 1619 |
| 1620 # Add to the global productions list |
| 1621 try: |
| 1622 self.Prodnames[prodname].append(p) |
| 1623 except KeyError: |
| 1624 self.Prodnames[prodname] = [p] |
| 1625 |
| 1626 # --------------------------------------------------------------------------
--- |
| 1627 # set_start() |
| 1628 # |
| 1629 # Sets the starting symbol and creates the augmented grammar. Production |
| 1630 # rule 0 is S' -> start where start is the start symbol. |
| 1631 # --------------------------------------------------------------------------
--- |
| 1632 |
| 1633 def set_start(self, start=None): |
| 1634 if not start: |
| 1635 start = self.Productions[1].name |
| 1636 if start not in self.Nonterminals: |
| 1637 raise GrammarError('start symbol %s undefined' % start) |
| 1638 self.Productions[0] = Production(0, "S'", [start]) |
| 1639 self.Nonterminals[start].append(0) |
| 1640 self.Start = start |
| 1641 |
| 1642 # --------------------------------------------------------------------------
--- |
| 1643 # find_unreachable() |
| 1644 # |
| 1645 # Find all of the nonterminal symbols that can't be reached from the startin
g |
| 1646 # symbol. Returns a list of nonterminals that can't be reached. |
| 1647 # --------------------------------------------------------------------------
--- |
| 1648 |
| 1649 def find_unreachable(self): |
| 1650 |
| 1651 # Mark all symbols that are reachable from a symbol s |
| 1652 def mark_reachable_from(s): |
| 1653 if s in reachable: |
| 1654 return |
| 1655 reachable.add(s) |
| 1656 for p in self.Prodnames.get(s, []): |
| 1657 for r in p.prod: |
| 1658 mark_reachable_from(r) |
| 1659 |
| 1660 reachable = set() |
| 1661 mark_reachable_from(self.Productions[0].prod[0]) |
| 1662 return [s for s in self.Nonterminals if s not in reachable] |
| 1663 |
| 1664 # --------------------------------------------------------------------------
--- |
| 1665 # infinite_cycles() |
| 1666 # |
| 1667 # This function looks at the various parsing rules and tries to detect |
| 1668 # infinite recursion cycles (grammar rules where there is no possible way |
| 1669 # to derive a string of only terminals). |
| 1670 # --------------------------------------------------------------------------
--- |
| 1671 |
| 1672 def infinite_cycles(self): |
| 1673 terminates = {} |
| 1674 |
| 1675 # Terminals: |
| 1676 for t in self.Terminals: |
| 1677 terminates[t] = True |
| 1678 |
| 1679 terminates['$end'] = True |
| 1680 |
| 1681 # Nonterminals: |
| 1682 |
| 1683 # Initialize to false: |
| 1684 for n in self.Nonterminals: |
| 1685 terminates[n] = False |
| 1686 |
| 1687 # Then propagate termination until no change: |
| 1688 while True: |
| 1689 some_change = False |
| 1690 for (n, pl) in self.Prodnames.items(): |
| 1691 # Nonterminal n terminates iff any of its productions terminates
. |
| 1692 for p in pl: |
| 1693 # Production p terminates iff all of its rhs symbols termina
te. |
| 1694 for s in p.prod: |
| 1695 if not terminates[s]: |
| 1696 # The symbol s does not terminate, |
| 1697 # so production p does not terminate. |
| 1698 p_terminates = False |
| 1699 break |
| 1700 else: |
| 1701 # didn't break from the loop, |
| 1702 # so every symbol s terminates |
| 1703 # so production p terminates. |
| 1704 p_terminates = True |
| 1705 |
| 1706 if p_terminates: |
| 1707 # symbol n terminates! |
| 1708 if not terminates[n]: |
| 1709 terminates[n] = True |
| 1710 some_change = True |
| 1711 # Don't need to consider any more productions for this n
. |
| 1712 break |
| 1713 |
| 1714 if not some_change: |
| 1715 break |
| 1716 |
| 1717 infinite = [] |
| 1718 for (s, term) in terminates.items(): |
| 1719 if not term: |
| 1720 if s not in self.Prodnames and s not in self.Terminals and s !=
'error': |
| 1721 # s is used-but-not-defined, and we've already warned of tha
t, |
| 1722 # so it would be overkill to say that it's also non-terminat
ing. |
| 1723 pass |
| 1724 else: |
| 1725 infinite.append(s) |
| 1726 |
| 1727 return infinite |
| 1728 |
| 1729 # --------------------------------------------------------------------------
--- |
| 1730 # undefined_symbols() |
| 1731 # |
| 1732 # Find all symbols that were used the grammar, but not defined as tokens or |
| 1733 # grammar rules. Returns a list of tuples (sym, prod) where sym in the symb
ol |
| 1734 # and prod is the production where the symbol was used. |
| 1735 # --------------------------------------------------------------------------
--- |
| 1736 def undefined_symbols(self): |
| 1737 result = [] |
| 1738 for p in self.Productions: |
| 1739 if not p: |
| 1740 continue |
| 1741 |
| 1742 for s in p.prod: |
| 1743 if s not in self.Prodnames and s not in self.Terminals and s !=
'error': |
| 1744 result.append((s, p)) |
| 1745 return result |
| 1746 |
| 1747 # --------------------------------------------------------------------------
--- |
| 1748 # unused_terminals() |
| 1749 # |
| 1750 # Find all terminals that were defined, but not used by the grammar. Return
s |
| 1751 # a list of all symbols. |
| 1752 # --------------------------------------------------------------------------
--- |
| 1753 def unused_terminals(self): |
| 1754 unused_tok = [] |
| 1755 for s, v in self.Terminals.items(): |
| 1756 if s != 'error' and not v: |
| 1757 unused_tok.append(s) |
| 1758 |
| 1759 return unused_tok |
| 1760 |
| 1761 # --------------------------------------------------------------------------
---- |
| 1762 # unused_rules() |
| 1763 # |
| 1764 # Find all grammar rules that were defined, but not used (maybe not reachab
le) |
| 1765 # Returns a list of productions. |
| 1766 # --------------------------------------------------------------------------
---- |
| 1767 |
| 1768 def unused_rules(self): |
| 1769 unused_prod = [] |
| 1770 for s, v in self.Nonterminals.items(): |
| 1771 if not v: |
| 1772 p = self.Prodnames[s][0] |
| 1773 unused_prod.append(p) |
| 1774 return unused_prod |
| 1775 |
| 1776 # --------------------------------------------------------------------------
--- |
| 1777 # unused_precedence() |
| 1778 # |
| 1779 # Returns a list of tuples (term,precedence) corresponding to precedence |
| 1780 # rules that were never used by the grammar. term is the name of the termin
al |
| 1781 # on which precedence was applied and precedence is a string such as 'left'
or |
| 1782 # 'right' corresponding to the type of precedence. |
| 1783 # --------------------------------------------------------------------------
--- |
| 1784 |
| 1785 def unused_precedence(self): |
| 1786 unused = [] |
| 1787 for termname in self.Precedence: |
| 1788 if not (termname in self.Terminals or termname in self.UsedPrecedenc
e): |
| 1789 unused.append((termname, self.Precedence[termname][0])) |
| 1790 |
| 1791 return unused |
| 1792 |
| 1793 # ------------------------------------------------------------------------- |
| 1794 # _first() |
| 1795 # |
| 1796 # Compute the value of FIRST1(beta) where beta is a tuple of symbols. |
| 1797 # |
| 1798 # During execution of compute_first1, the result may be incomplete. |
| 1799 # Afterward (e.g., when called from compute_follow()), it will be complete. |
| 1800 # ------------------------------------------------------------------------- |
| 1801 def _first(self, beta): |
| 1802 |
| 1803 # We are computing First(x1,x2,x3,...,xn) |
| 1804 result = [] |
| 1805 for x in beta: |
| 1806 x_produces_empty = False |
| 1807 |
| 1808 # Add all the non-<empty> symbols of First[x] to the result. |
| 1809 for f in self.First[x]: |
| 1810 if f == '<empty>': |
| 1811 x_produces_empty = True |
| 1812 else: |
| 1813 if f not in result: |
| 1814 result.append(f) |
| 1815 |
| 1816 if x_produces_empty: |
| 1817 # We have to consider the next x in beta, |
| 1818 # i.e. stay in the loop. |
| 1819 pass |
| 1820 else: |
| 1821 # We don't have to consider any further symbols in beta. |
| 1822 break |
| 1823 else: |
| 1824 # There was no 'break' from the loop, |
| 1825 # so x_produces_empty was true for all x in beta, |
| 1826 # so beta produces empty as well. |
| 1827 result.append('<empty>') |
| 1828 |
| 1829 return result |
| 1830 |
| 1831 # ------------------------------------------------------------------------- |
| 1832 # compute_first() |
| 1833 # |
| 1834 # Compute the value of FIRST1(X) for all symbols |
| 1835 # ------------------------------------------------------------------------- |
| 1836 def compute_first(self): |
| 1837 if self.First: |
| 1838 return self.First |
| 1839 |
| 1840 # Terminals: |
| 1841 for t in self.Terminals: |
| 1842 self.First[t] = [t] |
| 1843 |
| 1844 self.First['$end'] = ['$end'] |
| 1845 |
| 1846 # Nonterminals: |
| 1847 |
| 1848 # Initialize to the empty set: |
| 1849 for n in self.Nonterminals: |
| 1850 self.First[n] = [] |
| 1851 |
| 1852 # Then propagate symbols until no change: |
| 1853 while True: |
| 1854 some_change = False |
| 1855 for n in self.Nonterminals: |
| 1856 for p in self.Prodnames[n]: |
| 1857 for f in self._first(p.prod): |
| 1858 if f not in self.First[n]: |
| 1859 self.First[n].append(f) |
| 1860 some_change = True |
| 1861 if not some_change: |
| 1862 break |
| 1863 |
| 1864 return self.First |
| 1865 |
| 1866 # --------------------------------------------------------------------- |
| 1867 # compute_follow() |
| 1868 # |
| 1869 # Computes all of the follow sets for every non-terminal symbol. The |
| 1870 # follow set is the set of all symbols that might follow a given |
| 1871 # non-terminal. See the Dragon book, 2nd Ed. p. 189. |
| 1872 # --------------------------------------------------------------------- |
| 1873 def compute_follow(self, start=None): |
| 1874 # If already computed, return the result |
| 1875 if self.Follow: |
| 1876 return self.Follow |
| 1877 |
| 1878 # If first sets not computed yet, do that first. |
| 1879 if not self.First: |
| 1880 self.compute_first() |
| 1881 |
| 1882 # Add '$end' to the follow list of the start symbol |
| 1883 for k in self.Nonterminals: |
| 1884 self.Follow[k] = [] |
| 1885 |
| 1886 if not start: |
| 1887 start = self.Productions[1].name |
| 1888 |
| 1889 self.Follow[start] = ['$end'] |
| 1890 |
| 1891 while True: |
| 1892 didadd = False |
| 1893 for p in self.Productions[1:]: |
| 1894 # Here is the production set |
| 1895 for i, B in enumerate(p.prod): |
| 1896 if B in self.Nonterminals: |
| 1897 # Okay. We got a non-terminal in a production |
| 1898 fst = self._first(p.prod[i+1:]) |
| 1899 hasempty = False |
| 1900 for f in fst: |
| 1901 if f != '<empty>' and f not in self.Follow[B]: |
| 1902 self.Follow[B].append(f) |
| 1903 didadd = True |
| 1904 if f == '<empty>': |
| 1905 hasempty = True |
| 1906 if hasempty or i == (len(p.prod)-1): |
| 1907 # Add elements of follow(a) to follow(b) |
| 1908 for f in self.Follow[p.name]: |
| 1909 if f not in self.Follow[B]: |
| 1910 self.Follow[B].append(f) |
| 1911 didadd = True |
| 1912 if not didadd: |
| 1913 break |
| 1914 return self.Follow |
| 1915 |
| 1916 |
| 1917 # --------------------------------------------------------------------------
--- |
| 1918 # build_lritems() |
| 1919 # |
| 1920 # This function walks the list of productions and builds a complete set of t
he |
| 1921 # LR items. The LR items are stored in two ways: First, they are uniquely |
| 1922 # numbered and placed in the list _lritems. Second, a linked list of LR ite
ms |
| 1923 # is built for each production. For example: |
| 1924 # |
| 1925 # E -> E PLUS E |
| 1926 # |
| 1927 # Creates the list |
| 1928 # |
| 1929 # [E -> . E PLUS E, E -> E . PLUS E, E -> E PLUS . E, E -> E PLUS E . ] |
| 1930 # --------------------------------------------------------------------------
--- |
| 1931 |
| 1932 def build_lritems(self): |
| 1933 for p in self.Productions: |
| 1934 lastlri = p |
| 1935 i = 0 |
| 1936 lr_items = [] |
| 1937 while True: |
| 1938 if i > len(p): |
| 1939 lri = None |
| 1940 else: |
| 1941 lri = LRItem(p, i) |
| 1942 # Precompute the list of productions immediately following |
| 1943 try: |
| 1944 lri.lr_after = self.Prodnames[lri.prod[i+1]] |
| 1945 except (IndexError, KeyError): |
| 1946 lri.lr_after = [] |
| 1947 try: |
| 1948 lri.lr_before = lri.prod[i-1] |
| 1949 except IndexError: |
| 1950 lri.lr_before = None |
| 1951 |
| 1952 lastlri.lr_next = lri |
| 1953 if not lri: |
| 1954 break |
| 1955 lr_items.append(lri) |
| 1956 lastlri = lri |
| 1957 i += 1 |
| 1958 p.lr_items = lr_items |
| 1959 |
| 1960 # ----------------------------------------------------------------------------- |
| 1961 # == Class LRTable == |
| 1962 # |
| 1963 # This basic class represents a basic table of LR parsing information. |
| 1964 # Methods for generating the tables are not defined here. They are defined |
| 1965 # in the derived class LRGeneratedTable. |
| 1966 # ----------------------------------------------------------------------------- |
| 1967 |
| 1968 class VersionError(YaccError): |
| 1969 pass |
| 1970 |
| 1971 class LRTable(object): |
| 1972 def __init__(self): |
| 1973 self.lr_action = None |
| 1974 self.lr_goto = None |
| 1975 self.lr_productions = None |
| 1976 self.lr_method = None |
| 1977 |
| 1978 def read_table(self, module): |
| 1979 if isinstance(module, types.ModuleType): |
| 1980 parsetab = module |
| 1981 else: |
| 1982 exec('import %s' % module) |
| 1983 parsetab = sys.modules[module] |
| 1984 |
| 1985 if parsetab._tabversion != __tabversion__: |
| 1986 raise VersionError('yacc table file version is out of date') |
| 1987 |
| 1988 self.lr_action = parsetab._lr_action |
| 1989 self.lr_goto = parsetab._lr_goto |
| 1990 |
| 1991 self.lr_productions = [] |
| 1992 for p in parsetab._lr_productions: |
| 1993 self.lr_productions.append(MiniProduction(*p)) |
| 1994 |
| 1995 self.lr_method = parsetab._lr_method |
| 1996 return parsetab._lr_signature |
| 1997 |
| 1998 def read_pickle(self, filename): |
| 1999 try: |
| 2000 import cPickle as pickle |
| 2001 except ImportError: |
| 2002 import pickle |
| 2003 |
| 2004 if not os.path.exists(filename): |
| 2005 raise ImportError |
| 2006 |
| 2007 in_f = open(filename, 'rb') |
| 2008 |
| 2009 tabversion = pickle.load(in_f) |
| 2010 if tabversion != __tabversion__: |
| 2011 raise VersionError('yacc table file version is out of date') |
| 2012 self.lr_method = pickle.load(in_f) |
| 2013 signature = pickle.load(in_f) |
| 2014 self.lr_action = pickle.load(in_f) |
| 2015 self.lr_goto = pickle.load(in_f) |
| 2016 productions = pickle.load(in_f) |
| 2017 |
| 2018 self.lr_productions = [] |
| 2019 for p in productions: |
| 2020 self.lr_productions.append(MiniProduction(*p)) |
| 2021 |
| 2022 in_f.close() |
| 2023 return signature |
| 2024 |
| 2025 # Bind all production function names to callable objects in pdict |
| 2026 def bind_callables(self, pdict): |
| 2027 for p in self.lr_productions: |
| 2028 p.bind(pdict) |
| 2029 |
| 2030 |
| 2031 # ----------------------------------------------------------------------------- |
| 2032 # === LR Generator === |
| 2033 # |
| 2034 # The following classes and functions are used to generate LR parsing tables on |
| 2035 # a grammar. |
| 2036 # ----------------------------------------------------------------------------- |
| 2037 |
| 2038 # ----------------------------------------------------------------------------- |
| 2039 # digraph() |
| 2040 # traverse() |
| 2041 # |
| 2042 # The following two functions are used to compute set valued functions |
| 2043 # of the form: |
| 2044 # |
| 2045 # F(x) = F'(x) U U{F(y) | x R y} |
| 2046 # |
| 2047 # This is used to compute the values of Read() sets as well as FOLLOW sets |
| 2048 # in LALR(1) generation. |
| 2049 # |
| 2050 # Inputs: X - An input set |
| 2051 # R - A relation |
| 2052 # FP - Set-valued function |
| 2053 # ------------------------------------------------------------------------------ |
| 2054 |
| 2055 def digraph(X, R, FP): |
| 2056 N = {} |
| 2057 for x in X: |
| 2058 N[x] = 0 |
| 2059 stack = [] |
| 2060 F = {} |
| 2061 for x in X: |
| 2062 if N[x] == 0: |
| 2063 traverse(x, N, stack, F, X, R, FP) |
| 2064 return F |
| 2065 |
| 2066 def traverse(x, N, stack, F, X, R, FP): |
| 2067 stack.append(x) |
| 2068 d = len(stack) |
| 2069 N[x] = d |
| 2070 F[x] = FP(x) # F(X) <- F'(x) |
| 2071 |
| 2072 rel = R(x) # Get y's related to x |
| 2073 for y in rel: |
| 2074 if N[y] == 0: |
| 2075 traverse(y, N, stack, F, X, R, FP) |
| 2076 N[x] = min(N[x], N[y]) |
| 2077 for a in F.get(y, []): |
| 2078 if a not in F[x]: |
| 2079 F[x].append(a) |
| 2080 if N[x] == d: |
| 2081 N[stack[-1]] = MAXINT |
| 2082 F[stack[-1]] = F[x] |
| 2083 element = stack.pop() |
| 2084 while element != x: |
| 2085 N[stack[-1]] = MAXINT |
| 2086 F[stack[-1]] = F[x] |
| 2087 element = stack.pop() |
| 2088 |
| 2089 class LALRError(YaccError): |
| 2090 pass |
| 2091 |
| 2092 # ----------------------------------------------------------------------------- |
| 2093 # == LRGeneratedTable == |
| 2094 # |
| 2095 # This class implements the LR table generation algorithm. There are no |
| 2096 # public methods except for write() |
| 2097 # ----------------------------------------------------------------------------- |
| 2098 |
| 2099 class LRGeneratedTable(LRTable): |
| 2100 def __init__(self, grammar, method='LALR', log=None): |
| 2101 if method not in ['SLR', 'LALR']: |
| 2102 raise LALRError('Unsupported method %s' % method) |
| 2103 |
| 2104 self.grammar = grammar |
| 2105 self.lr_method = method |
| 2106 |
| 2107 # Set up the logger |
| 2108 if not log: |
| 2109 log = NullLogger() |
| 2110 self.log = log |
| 2111 |
| 2112 # Internal attributes |
| 2113 self.lr_action = {} # Action table |
| 2114 self.lr_goto = {} # Goto table |
| 2115 self.lr_productions = grammar.Productions # Copy of grammar Producti
on array |
| 2116 self.lr_goto_cache = {} # Cache of computed gotos |
| 2117 self.lr0_cidhash = {} # Cache of closures |
| 2118 |
| 2119 self._add_count = 0 # Internal counter used to detect cycles |
| 2120 |
| 2121 # Diagonistic information filled in by the table generator |
| 2122 self.sr_conflict = 0 |
| 2123 self.rr_conflict = 0 |
| 2124 self.conflicts = [] # List of conflicts |
| 2125 |
| 2126 self.sr_conflicts = [] |
| 2127 self.rr_conflicts = [] |
| 2128 |
| 2129 # Build the tables |
| 2130 self.grammar.build_lritems() |
| 2131 self.grammar.compute_first() |
| 2132 self.grammar.compute_follow() |
| 2133 self.lr_parse_table() |
| 2134 |
| 2135 # Compute the LR(0) closure operation on I, where I is a set of LR(0) items. |
| 2136 |
| 2137 def lr0_closure(self, I): |
| 2138 self._add_count += 1 |
| 2139 |
| 2140 # Add everything in I to J |
| 2141 J = I[:] |
| 2142 didadd = True |
| 2143 while didadd: |
| 2144 didadd = False |
| 2145 for j in J: |
| 2146 for x in j.lr_after: |
| 2147 if getattr(x, 'lr0_added', 0) == self._add_count: |
| 2148 continue |
| 2149 # Add B --> .G to J |
| 2150 J.append(x.lr_next) |
| 2151 x.lr0_added = self._add_count |
| 2152 didadd = True |
| 2153 |
| 2154 return J |
| 2155 |
| 2156 # Compute the LR(0) goto function goto(I,X) where I is a set |
| 2157 # of LR(0) items and X is a grammar symbol. This function is written |
| 2158 # in a way that guarantees uniqueness of the generated goto sets |
| 2159 # (i.e. the same goto set will never be returned as two different Python |
| 2160 # objects). With uniqueness, we can later do fast set comparisons using |
| 2161 # id(obj) instead of element-wise comparison. |
| 2162 |
| 2163 def lr0_goto(self, I, x): |
| 2164 # First we look for a previously cached entry |
| 2165 g = self.lr_goto_cache.get((id(I), x)) |
| 2166 if g: |
| 2167 return g |
| 2168 |
| 2169 # Now we generate the goto set in a way that guarantees uniqueness |
| 2170 # of the result |
| 2171 |
| 2172 s = self.lr_goto_cache.get(x) |
| 2173 if not s: |
| 2174 s = {} |
| 2175 self.lr_goto_cache[x] = s |
| 2176 |
| 2177 gs = [] |
| 2178 for p in I: |
| 2179 n = p.lr_next |
| 2180 if n and n.lr_before == x: |
| 2181 s1 = s.get(id(n)) |
| 2182 if not s1: |
| 2183 s1 = {} |
| 2184 s[id(n)] = s1 |
| 2185 gs.append(n) |
| 2186 s = s1 |
| 2187 g = s.get('$end') |
| 2188 if not g: |
| 2189 if gs: |
| 2190 g = self.lr0_closure(gs) |
| 2191 s['$end'] = g |
| 2192 else: |
| 2193 s['$end'] = gs |
| 2194 self.lr_goto_cache[(id(I), x)] = g |
| 2195 return g |
| 2196 |
| 2197 # Compute the LR(0) sets of item function |
| 2198 def lr0_items(self): |
| 2199 C = [self.lr0_closure([self.grammar.Productions[0].lr_next])] |
| 2200 i = 0 |
| 2201 for I in C: |
| 2202 self.lr0_cidhash[id(I)] = i |
| 2203 i += 1 |
| 2204 |
| 2205 # Loop over the items in C and each grammar symbols |
| 2206 i = 0 |
| 2207 while i < len(C): |
| 2208 I = C[i] |
| 2209 i += 1 |
| 2210 |
| 2211 # Collect all of the symbols that could possibly be in the goto(I,X)
sets |
| 2212 asyms = {} |
| 2213 for ii in I: |
| 2214 for s in ii.usyms: |
| 2215 asyms[s] = None |
| 2216 |
| 2217 for x in asyms: |
| 2218 g = self.lr0_goto(I, x) |
| 2219 if not g or id(g) in self.lr0_cidhash: |
| 2220 continue |
| 2221 self.lr0_cidhash[id(g)] = len(C) |
| 2222 C.append(g) |
| 2223 |
| 2224 return C |
| 2225 |
| 2226 # --------------------------------------------------------------------------
--- |
| 2227 # ==== LALR(1) Parsing ==== |
| 2228 # |
| 2229 # LALR(1) parsing is almost exactly the same as SLR except that instead of |
| 2230 # relying upon Follow() sets when performing reductions, a more selective |
| 2231 # lookahead set that incorporates the state of the LR(0) machine is utilized
. |
| 2232 # Thus, we mainly just have to focus on calculating the lookahead sets. |
| 2233 # |
| 2234 # The method used here is due to DeRemer and Pennelo (1982). |
| 2235 # |
| 2236 # DeRemer, F. L., and T. J. Pennelo: "Efficient Computation of LALR(1) |
| 2237 # Lookahead Sets", ACM Transactions on Programming Languages and Systems
, |
| 2238 # Vol. 4, No. 4, Oct. 1982, pp. 615-649 |
| 2239 # |
| 2240 # Further details can also be found in: |
| 2241 # |
| 2242 # J. Tremblay and P. Sorenson, "The Theory and Practice of Compiler Writing
", |
| 2243 # McGraw-Hill Book Company, (1985). |
| 2244 # |
| 2245 # --------------------------------------------------------------------------
--- |
| 2246 |
| 2247 # --------------------------------------------------------------------------
--- |
| 2248 # compute_nullable_nonterminals() |
| 2249 # |
| 2250 # Creates a dictionary containing all of the non-terminals that might produc
e |
| 2251 # an empty production. |
| 2252 # --------------------------------------------------------------------------
--- |
| 2253 |
| 2254 def compute_nullable_nonterminals(self): |
| 2255 nullable = set() |
| 2256 num_nullable = 0 |
| 2257 while True: |
| 2258 for p in self.grammar.Productions[1:]: |
| 2259 if p.len == 0: |
| 2260 nullable.add(p.name) |
| 2261 continue |
| 2262 for t in p.prod: |
| 2263 if t not in nullable: |
| 2264 break |
| 2265 else: |
| 2266 nullable.add(p.name) |
| 2267 if len(nullable) == num_nullable: |
| 2268 break |
| 2269 num_nullable = len(nullable) |
| 2270 return nullable |
| 2271 |
| 2272 # --------------------------------------------------------------------------
--- |
| 2273 # find_nonterminal_trans(C) |
| 2274 # |
| 2275 # Given a set of LR(0) items, this functions finds all of the non-terminal |
| 2276 # transitions. These are transitions in which a dot appears immediately b
efore |
| 2277 # a non-terminal. Returns a list of tuples of the form (state,N) where sta
te |
| 2278 # is the state number and N is the nonterminal symbol. |
| 2279 # |
| 2280 # The input C is the set of LR(0) items. |
| 2281 # --------------------------------------------------------------------------
--- |
| 2282 |
| 2283 def find_nonterminal_transitions(self, C): |
| 2284 trans = [] |
| 2285 for stateno, state in enumerate(C): |
| 2286 for p in state: |
| 2287 if p.lr_index < p.len - 1: |
| 2288 t = (stateno, p.prod[p.lr_index+1]) |
| 2289 if t[1] in self.grammar.Nonterminals: |
| 2290 if t not in trans: |
| 2291 trans.append(t) |
| 2292 return trans |
| 2293 |
| 2294 # --------------------------------------------------------------------------
--- |
| 2295 # dr_relation() |
| 2296 # |
| 2297 # Computes the DR(p,A) relationships for non-terminal transitions. The inpu
t |
| 2298 # is a tuple (state,N) where state is a number and N is a nonterminal symbol
. |
| 2299 # |
| 2300 # Returns a list of terminals. |
| 2301 # --------------------------------------------------------------------------
--- |
| 2302 |
| 2303 def dr_relation(self, C, trans, nullable): |
| 2304 dr_set = {} |
| 2305 state, N = trans |
| 2306 terms = [] |
| 2307 |
| 2308 g = self.lr0_goto(C[state], N) |
| 2309 for p in g: |
| 2310 if p.lr_index < p.len - 1: |
| 2311 a = p.prod[p.lr_index+1] |
| 2312 if a in self.grammar.Terminals: |
| 2313 if a not in terms: |
| 2314 terms.append(a) |
| 2315 |
| 2316 # This extra bit is to handle the start state |
| 2317 if state == 0 and N == self.grammar.Productions[0].prod[0]: |
| 2318 terms.append('$end') |
| 2319 |
| 2320 return terms |
| 2321 |
| 2322 # --------------------------------------------------------------------------
--- |
| 2323 # reads_relation() |
| 2324 # |
| 2325 # Computes the READS() relation (p,A) READS (t,C). |
| 2326 # --------------------------------------------------------------------------
--- |
| 2327 |
| 2328 def reads_relation(self, C, trans, empty): |
| 2329 # Look for empty transitions |
| 2330 rel = [] |
| 2331 state, N = trans |
| 2332 |
| 2333 g = self.lr0_goto(C[state], N) |
| 2334 j = self.lr0_cidhash.get(id(g), -1) |
| 2335 for p in g: |
| 2336 if p.lr_index < p.len - 1: |
| 2337 a = p.prod[p.lr_index + 1] |
| 2338 if a in empty: |
| 2339 rel.append((j, a)) |
| 2340 |
| 2341 return rel |
| 2342 |
| 2343 # --------------------------------------------------------------------------
--- |
| 2344 # compute_lookback_includes() |
| 2345 # |
| 2346 # Determines the lookback and includes relations |
| 2347 # |
| 2348 # LOOKBACK: |
| 2349 # |
| 2350 # This relation is determined by running the LR(0) state machine forward. |
| 2351 # For example, starting with a production "N : . A B C", we run it forward |
| 2352 # to obtain "N : A B C ." We then build a relationship between this final |
| 2353 # state and the starting state. These relationships are stored in a dictio
nary |
| 2354 # lookdict. |
| 2355 # |
| 2356 # INCLUDES: |
| 2357 # |
| 2358 # Computes the INCLUDE() relation (p,A) INCLUDES (p',B). |
| 2359 # |
| 2360 # This relation is used to determine non-terminal transitions that occur |
| 2361 # inside of other non-terminal transition states. (p,A) INCLUDES (p', B) |
| 2362 # if the following holds: |
| 2363 # |
| 2364 # B -> LAT, where T -> epsilon and p' -L-> p |
| 2365 # |
| 2366 # L is essentially a prefix (which may be empty), T is a suffix that must be |
| 2367 # able to derive an empty string. State p' must lead to state p with the st
ring L. |
| 2368 # |
| 2369 # --------------------------------------------------------------------------
--- |
| 2370 |
| 2371 def compute_lookback_includes(self, C, trans, nullable): |
| 2372 lookdict = {} # Dictionary of lookback relations |
| 2373 includedict = {} # Dictionary of include relations |
| 2374 |
| 2375 # Make a dictionary of non-terminal transitions |
| 2376 dtrans = {} |
| 2377 for t in trans: |
| 2378 dtrans[t] = 1 |
| 2379 |
| 2380 # Loop over all transitions and compute lookbacks and includes |
| 2381 for state, N in trans: |
| 2382 lookb = [] |
| 2383 includes = [] |
| 2384 for p in C[state]: |
| 2385 if p.name != N: |
| 2386 continue |
| 2387 |
| 2388 # Okay, we have a name match. We now follow the production all
the way |
| 2389 # through the state machine until we get the . on the right hand
side |
| 2390 |
| 2391 lr_index = p.lr_index |
| 2392 j = state |
| 2393 while lr_index < p.len - 1: |
| 2394 lr_index = lr_index + 1 |
| 2395 t = p.prod[lr_index] |
| 2396 |
| 2397 # Check to see if this symbol and state are a non-terminal t
ransition |
| 2398 if (j, t) in dtrans: |
| 2399 # Yes. Okay, there is some chance that this is an inclu
des relation |
| 2400 # the only way to know for certain is whether the rest o
f the |
| 2401 # production derives empty |
| 2402 |
| 2403 li = lr_index + 1 |
| 2404 while li < p.len: |
| 2405 if p.prod[li] in self.grammar.Terminals: |
| 2406 break # No forget it |
| 2407 if p.prod[li] not in nullable: |
| 2408 break |
| 2409 li = li + 1 |
| 2410 else: |
| 2411 # Appears to be a relation between (j,t) and (state,
N) |
| 2412 includes.append((j, t)) |
| 2413 |
| 2414 g = self.lr0_goto(C[j], t) # Go to next set |
| 2415 j = self.lr0_cidhash.get(id(g), -1) # Go to next state |
| 2416 |
| 2417 # When we get here, j is the final state, now we have to locate
the production |
| 2418 for r in C[j]: |
| 2419 if r.name != p.name: |
| 2420 continue |
| 2421 if r.len != p.len: |
| 2422 continue |
| 2423 i = 0 |
| 2424 # This look is comparing a production ". A B C" with "A B C
." |
| 2425 while i < r.lr_index: |
| 2426 if r.prod[i] != p.prod[i+1]: |
| 2427 break |
| 2428 i = i + 1 |
| 2429 else: |
| 2430 lookb.append((j, r)) |
| 2431 for i in includes: |
| 2432 if i not in includedict: |
| 2433 includedict[i] = [] |
| 2434 includedict[i].append((state, N)) |
| 2435 lookdict[(state, N)] = lookb |
| 2436 |
| 2437 return lookdict, includedict |
| 2438 |
| 2439 # --------------------------------------------------------------------------
--- |
| 2440 # compute_read_sets() |
| 2441 # |
| 2442 # Given a set of LR(0) items, this function computes the read sets. |
| 2443 # |
| 2444 # Inputs: C = Set of LR(0) items |
| 2445 # ntrans = Set of nonterminal transitions |
| 2446 # nullable = Set of empty transitions |
| 2447 # |
| 2448 # Returns a set containing the read sets |
| 2449 # --------------------------------------------------------------------------
--- |
| 2450 |
| 2451 def compute_read_sets(self, C, ntrans, nullable): |
| 2452 FP = lambda x: self.dr_relation(C, x, nullable) |
| 2453 R = lambda x: self.reads_relation(C, x, nullable) |
| 2454 F = digraph(ntrans, R, FP) |
| 2455 return F |
| 2456 |
| 2457 # --------------------------------------------------------------------------
--- |
| 2458 # compute_follow_sets() |
| 2459 # |
| 2460 # Given a set of LR(0) items, a set of non-terminal transitions, a readset, |
| 2461 # and an include set, this function computes the follow sets |
| 2462 # |
| 2463 # Follow(p,A) = Read(p,A) U U {Follow(p',B) | (p,A) INCLUDES (p',B)} |
| 2464 # |
| 2465 # Inputs: |
| 2466 # ntrans = Set of nonterminal transitions |
| 2467 # readsets = Readset (previously computed) |
| 2468 # inclsets = Include sets (previously computed) |
| 2469 # |
| 2470 # Returns a set containing the follow sets |
| 2471 # --------------------------------------------------------------------------
--- |
| 2472 |
| 2473 def compute_follow_sets(self, ntrans, readsets, inclsets): |
| 2474 FP = lambda x: readsets[x] |
| 2475 R = lambda x: inclsets.get(x, []) |
| 2476 F = digraph(ntrans, R, FP) |
| 2477 return F |
| 2478 |
| 2479 # --------------------------------------------------------------------------
--- |
| 2480 # add_lookaheads() |
| 2481 # |
| 2482 # Attaches the lookahead symbols to grammar rules. |
| 2483 # |
| 2484 # Inputs: lookbacks - Set of lookback relations |
| 2485 # followset - Computed follow set |
| 2486 # |
| 2487 # This function directly attaches the lookaheads to productions contained |
| 2488 # in the lookbacks set |
| 2489 # --------------------------------------------------------------------------
--- |
| 2490 |
| 2491 def add_lookaheads(self, lookbacks, followset): |
| 2492 for trans, lb in lookbacks.items(): |
| 2493 # Loop over productions in lookback |
| 2494 for state, p in lb: |
| 2495 if state not in p.lookaheads: |
| 2496 p.lookaheads[state] = [] |
| 2497 f = followset.get(trans, []) |
| 2498 for a in f: |
| 2499 if a not in p.lookaheads[state]: |
| 2500 p.lookaheads[state].append(a) |
| 2501 |
| 2502 # --------------------------------------------------------------------------
--- |
| 2503 # add_lalr_lookaheads() |
| 2504 # |
| 2505 # This function does all of the work of adding lookahead information for use |
| 2506 # with LALR parsing |
| 2507 # --------------------------------------------------------------------------
--- |
| 2508 |
| 2509 def add_lalr_lookaheads(self, C): |
| 2510 # Determine all of the nullable nonterminals |
| 2511 nullable = self.compute_nullable_nonterminals() |
| 2512 |
| 2513 # Find all non-terminal transitions |
| 2514 trans = self.find_nonterminal_transitions(C) |
| 2515 |
| 2516 # Compute read sets |
| 2517 readsets = self.compute_read_sets(C, trans, nullable) |
| 2518 |
| 2519 # Compute lookback/includes relations |
| 2520 lookd, included = self.compute_lookback_includes(C, trans, nullable) |
| 2521 |
| 2522 # Compute LALR FOLLOW sets |
| 2523 followsets = self.compute_follow_sets(trans, readsets, included) |
| 2524 |
| 2525 # Add all of the lookaheads |
| 2526 self.add_lookaheads(lookd, followsets) |
| 2527 |
| 2528 # --------------------------------------------------------------------------
--- |
| 2529 # lr_parse_table() |
| 2530 # |
| 2531 # This function constructs the parse tables for SLR or LALR |
| 2532 # --------------------------------------------------------------------------
--- |
| 2533 def lr_parse_table(self): |
| 2534 Productions = self.grammar.Productions |
| 2535 Precedence = self.grammar.Precedence |
| 2536 goto = self.lr_goto # Goto array |
| 2537 action = self.lr_action # Action array |
| 2538 log = self.log # Logger for output |
| 2539 |
| 2540 actionp = {} # Action production array (temporary) |
| 2541 |
| 2542 log.info('Parsing method: %s', self.lr_method) |
| 2543 |
| 2544 # Step 1: Construct C = { I0, I1, ... IN}, collection of LR(0) items |
| 2545 # This determines the number of states |
| 2546 |
| 2547 C = self.lr0_items() |
| 2548 |
| 2549 if self.lr_method == 'LALR': |
| 2550 self.add_lalr_lookaheads(C) |
| 2551 |
| 2552 # Build the parser table, state by state |
| 2553 st = 0 |
| 2554 for I in C: |
| 2555 # Loop over each production in I |
| 2556 actlist = [] # List of actions |
| 2557 st_action = {} |
| 2558 st_actionp = {} |
| 2559 st_goto = {} |
| 2560 log.info('') |
| 2561 log.info('state %d', st) |
| 2562 log.info('') |
| 2563 for p in I: |
| 2564 log.info(' (%d) %s', p.number, p) |
| 2565 log.info('') |
| 2566 |
| 2567 for p in I: |
| 2568 if p.len == p.lr_index + 1: |
| 2569 if p.name == "S'": |
| 2570 # Start symbol. Accept! |
| 2571 st_action['$end'] = 0 |
| 2572 st_actionp['$end'] = p |
| 2573 else: |
| 2574 # We are at the end of a production. Reduce! |
| 2575 if self.lr_method == 'LALR': |
| 2576 laheads = p.lookaheads[st] |
| 2577 else: |
| 2578 laheads = self.grammar.Follow[p.name] |
| 2579 for a in laheads: |
| 2580 actlist.append((a, p, 'reduce using rule %d (%s)
' % (p.number, p))) |
| 2581 r = st_action.get(a) |
| 2582 if r is not None: |
| 2583 # Whoa. Have a shift/reduce or reduce/reduce
conflict |
| 2584 if r > 0: |
| 2585 # Need to decide on shift or reduce here |
| 2586 # By default we favor shifting. Need to
add |
| 2587 # some precedence rules here. |
| 2588 sprec, slevel = Productions[st_actionp[a
].number].prec |
| 2589 rprec, rlevel = Precedence.get(a, ('righ
t', 0)) |
| 2590 if (slevel < rlevel) or ((slevel == rlev
el) and (rprec == 'left')): |
| 2591 # We really need to reduce here. |
| 2592 st_action[a] = -p.number |
| 2593 st_actionp[a] = p |
| 2594 if not slevel and not rlevel: |
| 2595 log.info(' ! shift/reduce confl
ict for %s resolved as reduce', a) |
| 2596 self.sr_conflicts.append((st, a,
'reduce')) |
| 2597 Productions[p.number].reduced += 1 |
| 2598 elif (slevel == rlevel) and (rprec == 'n
onassoc'): |
| 2599 st_action[a] = None |
| 2600 else: |
| 2601 # Hmmm. Guess we'll keep the shift |
| 2602 if not rlevel: |
| 2603 log.info(' ! shift/reduce confl
ict for %s resolved as shift', a) |
| 2604 self.sr_conflicts.append((st, a,
'shift')) |
| 2605 elif r < 0: |
| 2606 # Reduce/reduce conflict. In this case
, we favor the rule |
| 2607 # that was defined first in the grammar
file |
| 2608 oldp = Productions[-r] |
| 2609 pp = Productions[p.number] |
| 2610 if oldp.line > pp.line: |
| 2611 st_action[a] = -p.number |
| 2612 st_actionp[a] = p |
| 2613 chosenp, rejectp = pp, oldp |
| 2614 Productions[p.number].reduced += 1 |
| 2615 Productions[oldp.number].reduced -=
1 |
| 2616 else: |
| 2617 chosenp, rejectp = oldp, pp |
| 2618 self.rr_conflicts.append((st, chosenp, r
ejectp)) |
| 2619 log.info(' ! reduce/reduce conflict for
%s resolved using rule %d (%s)', |
| 2620 a, st_actionp[a].number, st_act
ionp[a]) |
| 2621 else: |
| 2622 raise LALRError('Unknown conflict in sta
te %d' % st) |
| 2623 else: |
| 2624 st_action[a] = -p.number |
| 2625 st_actionp[a] = p |
| 2626 Productions[p.number].reduced += 1 |
| 2627 else: |
| 2628 i = p.lr_index |
| 2629 a = p.prod[i+1] # Get symbol right after the "." |
| 2630 if a in self.grammar.Terminals: |
| 2631 g = self.lr0_goto(I, a) |
| 2632 j = self.lr0_cidhash.get(id(g), -1) |
| 2633 if j >= 0: |
| 2634 # We are in a shift state |
| 2635 actlist.append((a, p, 'shift and go to state %d'
% j)) |
| 2636 r = st_action.get(a) |
| 2637 if r is not None: |
| 2638 # Whoa have a shift/reduce or shift/shift co
nflict |
| 2639 if r > 0: |
| 2640 if r != j: |
| 2641 raise LALRError('Shift/shift conflic
t in state %d' % st) |
| 2642 elif r < 0: |
| 2643 # Do a precedence check. |
| 2644 # - if precedence of reduce rule is h
igher, we reduce. |
| 2645 # - if precedence of reduce is same a
nd left assoc, we reduce. |
| 2646 # - otherwise we shift |
| 2647 rprec, rlevel = Productions[st_actionp[a
].number].prec |
| 2648 sprec, slevel = Precedence.get(a, ('righ
t', 0)) |
| 2649 if (slevel > rlevel) or ((slevel == rlev
el) and (rprec == 'right')): |
| 2650 # We decide to shift here... highest
precedence to shift |
| 2651 Productions[st_actionp[a].number].re
duced -= 1 |
| 2652 st_action[a] = j |
| 2653 st_actionp[a] = p |
| 2654 if not rlevel: |
| 2655 log.info(' ! shift/reduce confl
ict for %s resolved as shift', a) |
| 2656 self.sr_conflicts.append((st, a,
'shift')) |
| 2657 elif (slevel == rlevel) and (rprec == 'n
onassoc'): |
| 2658 st_action[a] = None |
| 2659 else: |
| 2660 # Hmmm. Guess we'll keep the reduce |
| 2661 if not slevel and not rlevel: |
| 2662 log.info(' ! shift/reduce confl
ict for %s resolved as reduce', a) |
| 2663 self.sr_conflicts.append((st, a,
'reduce')) |
| 2664 |
| 2665 else: |
| 2666 raise LALRError('Unknown conflict in sta
te %d' % st) |
| 2667 else: |
| 2668 st_action[a] = j |
| 2669 st_actionp[a] = p |
| 2670 |
| 2671 # Print the actions associated with each terminal |
| 2672 _actprint = {} |
| 2673 for a, p, m in actlist: |
| 2674 if a in st_action: |
| 2675 if p is st_actionp[a]: |
| 2676 log.info(' %-15s %s', a, m) |
| 2677 _actprint[(a, m)] = 1 |
| 2678 log.info('') |
| 2679 # Print the actions that were not used. (debugging) |
| 2680 not_used = 0 |
| 2681 for a, p, m in actlist: |
| 2682 if a in st_action: |
| 2683 if p is not st_actionp[a]: |
| 2684 if not (a, m) in _actprint: |
| 2685 log.debug(' ! %-15s [ %s ]', a, m) |
| 2686 not_used = 1 |
| 2687 _actprint[(a, m)] = 1 |
| 2688 if not_used: |
| 2689 log.debug('') |
| 2690 |
| 2691 # Construct the goto table for this state |
| 2692 |
| 2693 nkeys = {} |
| 2694 for ii in I: |
| 2695 for s in ii.usyms: |
| 2696 if s in self.grammar.Nonterminals: |
| 2697 nkeys[s] = None |
| 2698 for n in nkeys: |
| 2699 g = self.lr0_goto(I, n) |
| 2700 j = self.lr0_cidhash.get(id(g), -1) |
| 2701 if j >= 0: |
| 2702 st_goto[n] = j |
| 2703 log.info(' %-30s shift and go to state %d', n, j) |
| 2704 |
| 2705 action[st] = st_action |
| 2706 actionp[st] = st_actionp |
| 2707 goto[st] = st_goto |
| 2708 st += 1 |
| 2709 |
| 2710 # --------------------------------------------------------------------------
--- |
| 2711 # write() |
| 2712 # |
| 2713 # This function writes the LR parsing tables to a file |
| 2714 # --------------------------------------------------------------------------
--- |
| 2715 |
| 2716 def write_table(self, tabmodule, outputdir='', signature=''): |
| 2717 if isinstance(tabmodule, types.ModuleType): |
| 2718 raise IOError("Won't overwrite existing tabmodule") |
| 2719 |
| 2720 basemodulename = tabmodule.split('.')[-1] |
| 2721 filename = os.path.join(outputdir, basemodulename) + '.py' |
| 2722 try: |
| 2723 f = open(filename, 'w') |
| 2724 |
| 2725 f.write(''' |
| 2726 # %s |
| 2727 # This file is automatically generated. Do not edit. |
| 2728 _tabversion = %r |
| 2729 |
| 2730 _lr_method = %r |
| 2731 |
| 2732 _lr_signature = %r |
| 2733 ''' % (os.path.basename(filename), __tabversion__, self.lr_method, signature
)) |
| 2734 |
| 2735 # Change smaller to 0 to go back to original tables |
| 2736 smaller = 1 |
| 2737 |
| 2738 # Factor out names to try and make smaller |
| 2739 if smaller: |
| 2740 items = {} |
| 2741 |
| 2742 for s, nd in self.lr_action.items(): |
| 2743 for name, v in nd.items(): |
| 2744 i = items.get(name) |
| 2745 if not i: |
| 2746 i = ([], []) |
| 2747 items[name] = i |
| 2748 i[0].append(s) |
| 2749 i[1].append(v) |
| 2750 |
| 2751 f.write('\n_lr_action_items = {') |
| 2752 for k, v in items.items(): |
| 2753 f.write('%r:([' % k) |
| 2754 for i in v[0]: |
| 2755 f.write('%r,' % i) |
| 2756 f.write('],[') |
| 2757 for i in v[1]: |
| 2758 f.write('%r,' % i) |
| 2759 |
| 2760 f.write(']),') |
| 2761 f.write('}\n') |
| 2762 |
| 2763 f.write(''' |
| 2764 _lr_action = {} |
| 2765 for _k, _v in _lr_action_items.items(): |
| 2766 for _x,_y in zip(_v[0],_v[1]): |
| 2767 if not _x in _lr_action: _lr_action[_x] = {} |
| 2768 _lr_action[_x][_k] = _y |
| 2769 del _lr_action_items |
| 2770 ''') |
| 2771 |
| 2772 else: |
| 2773 f.write('\n_lr_action = { ') |
| 2774 for k, v in self.lr_action.items(): |
| 2775 f.write('(%r,%r):%r,' % (k[0], k[1], v)) |
| 2776 f.write('}\n') |
| 2777 |
| 2778 if smaller: |
| 2779 # Factor out names to try and make smaller |
| 2780 items = {} |
| 2781 |
| 2782 for s, nd in self.lr_goto.items(): |
| 2783 for name, v in nd.items(): |
| 2784 i = items.get(name) |
| 2785 if not i: |
| 2786 i = ([], []) |
| 2787 items[name] = i |
| 2788 i[0].append(s) |
| 2789 i[1].append(v) |
| 2790 |
| 2791 f.write('\n_lr_goto_items = {') |
| 2792 for k, v in items.items(): |
| 2793 f.write('%r:([' % k) |
| 2794 for i in v[0]: |
| 2795 f.write('%r,' % i) |
| 2796 f.write('],[') |
| 2797 for i in v[1]: |
| 2798 f.write('%r,' % i) |
| 2799 |
| 2800 f.write(']),') |
| 2801 f.write('}\n') |
| 2802 |
| 2803 f.write(''' |
| 2804 _lr_goto = {} |
| 2805 for _k, _v in _lr_goto_items.items(): |
| 2806 for _x, _y in zip(_v[0], _v[1]): |
| 2807 if not _x in _lr_goto: _lr_goto[_x] = {} |
| 2808 _lr_goto[_x][_k] = _y |
| 2809 del _lr_goto_items |
| 2810 ''') |
| 2811 else: |
| 2812 f.write('\n_lr_goto = { ') |
| 2813 for k, v in self.lr_goto.items(): |
| 2814 f.write('(%r,%r):%r,' % (k[0], k[1], v)) |
| 2815 f.write('}\n') |
| 2816 |
| 2817 # Write production table |
| 2818 f.write('_lr_productions = [\n') |
| 2819 for p in self.lr_productions: |
| 2820 if p.func: |
| 2821 f.write(' (%r,%r,%d,%r,%r,%d),\n' % (p.str, p.name, p.len, |
| 2822 p.func, os.path.basena
me(p.file), p.line)) |
| 2823 else: |
| 2824 f.write(' (%r,%r,%d,None,None,None),\n' % (str(p), p.name,
p.len)) |
| 2825 f.write(']\n') |
| 2826 f.close() |
| 2827 |
| 2828 except IOError as e: |
| 2829 raise |
| 2830 |
| 2831 |
| 2832 # --------------------------------------------------------------------------
--- |
| 2833 # pickle_table() |
| 2834 # |
| 2835 # This function pickles the LR parsing tables to a supplied file object |
| 2836 # --------------------------------------------------------------------------
--- |
| 2837 |
| 2838 def pickle_table(self, filename, signature=''): |
| 2839 try: |
| 2840 import cPickle as pickle |
| 2841 except ImportError: |
| 2842 import pickle |
| 2843 with open(filename, 'wb') as outf: |
| 2844 pickle.dump(__tabversion__, outf, pickle_protocol) |
| 2845 pickle.dump(self.lr_method, outf, pickle_protocol) |
| 2846 pickle.dump(signature, outf, pickle_protocol) |
| 2847 pickle.dump(self.lr_action, outf, pickle_protocol) |
| 2848 pickle.dump(self.lr_goto, outf, pickle_protocol) |
| 2849 |
| 2850 outp = [] |
| 2851 for p in self.lr_productions: |
| 2852 if p.func: |
| 2853 outp.append((p.str, p.name, p.len, p.func, os.path.basename(
p.file), p.line)) |
| 2854 else: |
| 2855 outp.append((str(p), p.name, p.len, None, None, None)) |
| 2856 pickle.dump(outp, outf, pickle_protocol) |
| 2857 |
| 2858 # ----------------------------------------------------------------------------- |
| 2859 # === INTROSPECTION === |
| 2860 # |
| 2861 # The following functions and classes are used to implement the PLY |
| 2862 # introspection features followed by the yacc() function itself. |
| 2863 # ----------------------------------------------------------------------------- |
| 2864 |
| 2865 # ----------------------------------------------------------------------------- |
| 2866 # get_caller_module_dict() |
| 2867 # |
| 2868 # This function returns a dictionary containing all of the symbols defined withi
n |
| 2869 # a caller further down the call stack. This is used to get the environment |
| 2870 # associated with the yacc() call if none was provided. |
| 2871 # ----------------------------------------------------------------------------- |
| 2872 |
| 2873 def get_caller_module_dict(levels): |
| 2874 f = sys._getframe(levels) |
| 2875 ldict = f.f_globals.copy() |
| 2876 if f.f_globals != f.f_locals: |
| 2877 ldict.update(f.f_locals) |
| 2878 return ldict |
| 2879 |
| 2880 # ----------------------------------------------------------------------------- |
| 2881 # parse_grammar() |
| 2882 # |
| 2883 # This takes a raw grammar rule string and parses it into production data |
| 2884 # ----------------------------------------------------------------------------- |
| 2885 def parse_grammar(doc, file, line): |
| 2886 grammar = [] |
| 2887 # Split the doc string into lines |
| 2888 pstrings = doc.splitlines() |
| 2889 lastp = None |
| 2890 dline = line |
| 2891 for ps in pstrings: |
| 2892 dline += 1 |
| 2893 p = ps.split() |
| 2894 if not p: |
| 2895 continue |
| 2896 try: |
| 2897 if p[0] == '|': |
| 2898 # This is a continuation of a previous rule |
| 2899 if not lastp: |
| 2900 raise SyntaxError("%s:%d: Misplaced '|'" % (file, dline)) |
| 2901 prodname = lastp |
| 2902 syms = p[1:] |
| 2903 else: |
| 2904 prodname = p[0] |
| 2905 lastp = prodname |
| 2906 syms = p[2:] |
| 2907 assign = p[1] |
| 2908 if assign != ':' and assign != '::=': |
| 2909 raise SyntaxError("%s:%d: Syntax error. Expected ':'" % (fil
e, dline)) |
| 2910 |
| 2911 grammar.append((file, dline, prodname, syms)) |
| 2912 except SyntaxError: |
| 2913 raise |
| 2914 except Exception: |
| 2915 raise SyntaxError('%s:%d: Syntax error in rule %r' % (file, dline, p
s.strip())) |
| 2916 |
| 2917 return grammar |
| 2918 |
| 2919 # ----------------------------------------------------------------------------- |
| 2920 # ParserReflect() |
| 2921 # |
| 2922 # This class represents information extracted for building a parser including |
| 2923 # start symbol, error function, tokens, precedence list, action functions, |
| 2924 # etc. |
| 2925 # ----------------------------------------------------------------------------- |
| 2926 class ParserReflect(object): |
| 2927 def __init__(self, pdict, log=None): |
| 2928 self.pdict = pdict |
| 2929 self.start = None |
| 2930 self.error_func = None |
| 2931 self.tokens = None |
| 2932 self.modules = set() |
| 2933 self.grammar = [] |
| 2934 self.error = False |
| 2935 |
| 2936 if log is None: |
| 2937 self.log = PlyLogger(sys.stderr) |
| 2938 else: |
| 2939 self.log = log |
| 2940 |
| 2941 # Get all of the basic information |
| 2942 def get_all(self): |
| 2943 self.get_start() |
| 2944 self.get_error_func() |
| 2945 self.get_tokens() |
| 2946 self.get_precedence() |
| 2947 self.get_pfunctions() |
| 2948 |
| 2949 # Validate all of the information |
| 2950 def validate_all(self): |
| 2951 self.validate_start() |
| 2952 self.validate_error_func() |
| 2953 self.validate_tokens() |
| 2954 self.validate_precedence() |
| 2955 self.validate_pfunctions() |
| 2956 self.validate_modules() |
| 2957 return self.error |
| 2958 |
| 2959 # Compute a signature over the grammar |
| 2960 def signature(self): |
| 2961 try: |
| 2962 from hashlib import md5 |
| 2963 except ImportError: |
| 2964 from md5 import md5 |
| 2965 try: |
| 2966 sig = md5() |
| 2967 if self.start: |
| 2968 sig.update(self.start.encode('latin-1')) |
| 2969 if self.prec: |
| 2970 sig.update(''.join([''.join(p) for p in self.prec]).encode('lati
n-1')) |
| 2971 if self.tokens: |
| 2972 sig.update(' '.join(self.tokens).encode('latin-1')) |
| 2973 for f in self.pfuncs: |
| 2974 if f[3]: |
| 2975 sig.update(f[3].encode('latin-1')) |
| 2976 except (TypeError, ValueError): |
| 2977 pass |
| 2978 |
| 2979 digest = base64.b16encode(sig.digest()) |
| 2980 if sys.version_info[0] >= 3: |
| 2981 digest = digest.decode('latin-1') |
| 2982 return digest |
| 2983 |
| 2984 # --------------------------------------------------------------------------
--- |
| 2985 # validate_modules() |
| 2986 # |
| 2987 # This method checks to see if there are duplicated p_rulename() functions |
| 2988 # in the parser module file. Without this function, it is really easy for |
| 2989 # users to make mistakes by cutting and pasting code fragments (and it's a r
eal |
| 2990 # bugger to try and figure out why the resulting parser doesn't work). Ther
efore, |
| 2991 # we just do a little regular expression pattern matching of def statements |
| 2992 # to try and detect duplicates. |
| 2993 # --------------------------------------------------------------------------
--- |
| 2994 |
| 2995 def validate_modules(self): |
| 2996 # Match def p_funcname( |
| 2997 fre = re.compile(r'\s*def\s+(p_[a-zA-Z_0-9]*)\(') |
| 2998 |
| 2999 for module in self.modules: |
| 3000 try: |
| 3001 lines, linen = inspect.getsourcelines(module) |
| 3002 except IOError: |
| 3003 continue |
| 3004 |
| 3005 counthash = {} |
| 3006 for linen, line in enumerate(lines): |
| 3007 linen += 1 |
| 3008 m = fre.match(line) |
| 3009 if m: |
| 3010 name = m.group(1) |
| 3011 prev = counthash.get(name) |
| 3012 if not prev: |
| 3013 counthash[name] = linen |
| 3014 else: |
| 3015 filename = inspect.getsourcefile(module) |
| 3016 self.log.warning('%s:%d: Function %s redefined. Previous
ly defined on line %d', |
| 3017 filename, linen, name, prev) |
| 3018 |
| 3019 # Get the start symbol |
| 3020 def get_start(self): |
| 3021 self.start = self.pdict.get('start') |
| 3022 |
| 3023 # Validate the start symbol |
| 3024 def validate_start(self): |
| 3025 if self.start is not None: |
| 3026 if not isinstance(self.start, string_types): |
| 3027 self.log.error("'start' must be a string") |
| 3028 |
| 3029 # Look for error handler |
| 3030 def get_error_func(self): |
| 3031 self.error_func = self.pdict.get('p_error') |
| 3032 |
| 3033 # Validate the error function |
| 3034 def validate_error_func(self): |
| 3035 if self.error_func: |
| 3036 if isinstance(self.error_func, types.FunctionType): |
| 3037 ismethod = 0 |
| 3038 elif isinstance(self.error_func, types.MethodType): |
| 3039 ismethod = 1 |
| 3040 else: |
| 3041 self.log.error("'p_error' defined, but is not a function or meth
od") |
| 3042 self.error = True |
| 3043 return |
| 3044 |
| 3045 eline = self.error_func.__code__.co_firstlineno |
| 3046 efile = self.error_func.__code__.co_filename |
| 3047 module = inspect.getmodule(self.error_func) |
| 3048 self.modules.add(module) |
| 3049 |
| 3050 argcount = self.error_func.__code__.co_argcount - ismethod |
| 3051 if argcount != 1: |
| 3052 self.log.error('%s:%d: p_error() requires 1 argument', efile, el
ine) |
| 3053 self.error = True |
| 3054 |
| 3055 # Get the tokens map |
| 3056 def get_tokens(self): |
| 3057 tokens = self.pdict.get('tokens') |
| 3058 if not tokens: |
| 3059 self.log.error('No token list is defined') |
| 3060 self.error = True |
| 3061 return |
| 3062 |
| 3063 if not isinstance(tokens, (list, tuple)): |
| 3064 self.log.error('tokens must be a list or tuple') |
| 3065 self.error = True |
| 3066 return |
| 3067 |
| 3068 if not tokens: |
| 3069 self.log.error('tokens is empty') |
| 3070 self.error = True |
| 3071 return |
| 3072 |
| 3073 self.tokens = tokens |
| 3074 |
| 3075 # Validate the tokens |
| 3076 def validate_tokens(self): |
| 3077 # Validate the tokens. |
| 3078 if 'error' in self.tokens: |
| 3079 self.log.error("Illegal token name 'error'. Is a reserved word") |
| 3080 self.error = True |
| 3081 return |
| 3082 |
| 3083 terminals = set() |
| 3084 for n in self.tokens: |
| 3085 if n in terminals: |
| 3086 self.log.warning('Token %r multiply defined', n) |
| 3087 terminals.add(n) |
| 3088 |
| 3089 # Get the precedence map (if any) |
| 3090 def get_precedence(self): |
| 3091 self.prec = self.pdict.get('precedence') |
| 3092 |
| 3093 # Validate and parse the precedence map |
| 3094 def validate_precedence(self): |
| 3095 preclist = [] |
| 3096 if self.prec: |
| 3097 if not isinstance(self.prec, (list, tuple)): |
| 3098 self.log.error('precedence must be a list or tuple') |
| 3099 self.error = True |
| 3100 return |
| 3101 for level, p in enumerate(self.prec): |
| 3102 if not isinstance(p, (list, tuple)): |
| 3103 self.log.error('Bad precedence table') |
| 3104 self.error = True |
| 3105 return |
| 3106 |
| 3107 if len(p) < 2: |
| 3108 self.log.error('Malformed precedence entry %s. Must be (asso
c, term, ..., term)', p) |
| 3109 self.error = True |
| 3110 return |
| 3111 assoc = p[0] |
| 3112 if not isinstance(assoc, string_types): |
| 3113 self.log.error('precedence associativity must be a string') |
| 3114 self.error = True |
| 3115 return |
| 3116 for term in p[1:]: |
| 3117 if not isinstance(term, string_types): |
| 3118 self.log.error('precedence items must be strings') |
| 3119 self.error = True |
| 3120 return |
| 3121 preclist.append((term, assoc, level+1)) |
| 3122 self.preclist = preclist |
| 3123 |
| 3124 # Get all p_functions from the grammar |
| 3125 def get_pfunctions(self): |
| 3126 p_functions = [] |
| 3127 for name, item in self.pdict.items(): |
| 3128 if not name.startswith('p_') or name == 'p_error': |
| 3129 continue |
| 3130 if isinstance(item, (types.FunctionType, types.MethodType)): |
| 3131 line = getattr(item, 'co_firstlineno', item.__code__.co_firstlin
eno) |
| 3132 module = inspect.getmodule(item) |
| 3133 p_functions.append((line, module, name, item.__doc__)) |
| 3134 |
| 3135 # Sort all of the actions by line number; make sure to stringify |
| 3136 # modules to make them sortable, since `line` may not uniquely sort all |
| 3137 # p functions |
| 3138 p_functions.sort(key=lambda p_function: ( |
| 3139 p_function[0], |
| 3140 str(p_function[1]), |
| 3141 p_function[2], |
| 3142 p_function[3])) |
| 3143 self.pfuncs = p_functions |
| 3144 |
| 3145 # Validate all of the p_functions |
| 3146 def validate_pfunctions(self): |
| 3147 grammar = [] |
| 3148 # Check for non-empty symbols |
| 3149 if len(self.pfuncs) == 0: |
| 3150 self.log.error('no rules of the form p_rulename are defined') |
| 3151 self.error = True |
| 3152 return |
| 3153 |
| 3154 for line, module, name, doc in self.pfuncs: |
| 3155 file = inspect.getsourcefile(module) |
| 3156 func = self.pdict[name] |
| 3157 if isinstance(func, types.MethodType): |
| 3158 reqargs = 2 |
| 3159 else: |
| 3160 reqargs = 1 |
| 3161 if func.__code__.co_argcount > reqargs: |
| 3162 self.log.error('%s:%d: Rule %r has too many arguments', file, li
ne, func.__name__) |
| 3163 self.error = True |
| 3164 elif func.__code__.co_argcount < reqargs: |
| 3165 self.log.error('%s:%d: Rule %r requires an argument', file, line
, func.__name__) |
| 3166 self.error = True |
| 3167 elif not func.__doc__: |
| 3168 self.log.warning('%s:%d: No documentation string specified in fu
nction %r (ignored)', |
| 3169 file, line, func.__name__) |
| 3170 else: |
| 3171 try: |
| 3172 parsed_g = parse_grammar(doc, file, line) |
| 3173 for g in parsed_g: |
| 3174 grammar.append((name, g)) |
| 3175 except SyntaxError as e: |
| 3176 self.log.error(str(e)) |
| 3177 self.error = True |
| 3178 |
| 3179 # Looks like a valid grammar rule |
| 3180 # Mark the file in which defined. |
| 3181 self.modules.add(module) |
| 3182 |
| 3183 # Secondary validation step that looks for p_ definitions that are not f
unctions |
| 3184 # or functions that look like they might be grammar rules. |
| 3185 |
| 3186 for n, v in self.pdict.items(): |
| 3187 if n.startswith('p_') and isinstance(v, (types.FunctionType, types.M
ethodType)): |
| 3188 continue |
| 3189 if n.startswith('t_'): |
| 3190 continue |
| 3191 if n.startswith('p_') and n != 'p_error': |
| 3192 self.log.warning('%r not defined as a function', n) |
| 3193 if ((isinstance(v, types.FunctionType) and v.__code__.co_argcount ==
1) or |
| 3194 (isinstance(v, types.MethodType) and v.__func__.__code__.co_a
rgcount == 2)): |
| 3195 if v.__doc__: |
| 3196 try: |
| 3197 doc = v.__doc__.split(' ') |
| 3198 if doc[1] == ':': |
| 3199 self.log.warning('%s:%d: Possible grammar rule %r de
fined without p_ prefix', |
| 3200 v.__code__.co_filename, v.__code__.
co_firstlineno, n) |
| 3201 except IndexError: |
| 3202 pass |
| 3203 |
| 3204 self.grammar = grammar |
| 3205 |
| 3206 # ----------------------------------------------------------------------------- |
| 3207 # yacc(module) |
| 3208 # |
| 3209 # Build a parser |
| 3210 # ----------------------------------------------------------------------------- |
| 3211 |
| 3212 def yacc(method='LALR', debug=yaccdebug, module=None, tabmodule=tab_module, star
t=None, |
| 3213 check_recursion=True, optimize=False, write_tables=True, debugfile=debu
g_file, |
| 3214 outputdir=None, debuglog=None, errorlog=None, picklefile=None): |
| 3215 |
| 3216 if tabmodule is None: |
| 3217 tabmodule = tab_module |
| 3218 |
| 3219 # Reference to the parsing method of the last built parser |
| 3220 global parse |
| 3221 |
| 3222 # If pickling is enabled, table files are not created |
| 3223 if picklefile: |
| 3224 write_tables = 0 |
| 3225 |
| 3226 if errorlog is None: |
| 3227 errorlog = PlyLogger(sys.stderr) |
| 3228 |
| 3229 # Get the module dictionary used for the parser |
| 3230 if module: |
| 3231 _items = [(k, getattr(module, k)) for k in dir(module)] |
| 3232 pdict = dict(_items) |
| 3233 # If no __file__ attribute is available, try to obtain it from the __mod
ule__ instead |
| 3234 if '__file__' not in pdict: |
| 3235 pdict['__file__'] = sys.modules[pdict['__module__']].__file__ |
| 3236 else: |
| 3237 pdict = get_caller_module_dict(2) |
| 3238 |
| 3239 if outputdir is None: |
| 3240 # If no output directory is set, the location of the output files |
| 3241 # is determined according to the following rules: |
| 3242 # - If tabmodule specifies a package, files go into that package dir
ectory |
| 3243 # - Otherwise, files go in the same directory as the specifying modu
le |
| 3244 if isinstance(tabmodule, types.ModuleType): |
| 3245 srcfile = tabmodule.__file__ |
| 3246 else: |
| 3247 if '.' not in tabmodule: |
| 3248 srcfile = pdict['__file__'] |
| 3249 else: |
| 3250 parts = tabmodule.split('.') |
| 3251 pkgname = '.'.join(parts[:-1]) |
| 3252 exec('import %s' % pkgname) |
| 3253 srcfile = getattr(sys.modules[pkgname], '__file__', '') |
| 3254 outputdir = os.path.dirname(srcfile) |
| 3255 |
| 3256 # Determine if the module is package of a package or not. |
| 3257 # If so, fix the tabmodule setting so that tables load correctly |
| 3258 pkg = pdict.get('__package__') |
| 3259 if pkg and isinstance(tabmodule, str): |
| 3260 if '.' not in tabmodule: |
| 3261 tabmodule = pkg + '.' + tabmodule |
| 3262 |
| 3263 |
| 3264 |
| 3265 # Set start symbol if it's specified directly using an argument |
| 3266 if start is not None: |
| 3267 pdict['start'] = start |
| 3268 |
| 3269 # Collect parser information from the dictionary |
| 3270 pinfo = ParserReflect(pdict, log=errorlog) |
| 3271 pinfo.get_all() |
| 3272 |
| 3273 if pinfo.error: |
| 3274 raise YaccError('Unable to build parser') |
| 3275 |
| 3276 # Check signature against table files (if any) |
| 3277 signature = pinfo.signature() |
| 3278 |
| 3279 # Read the tables |
| 3280 try: |
| 3281 lr = LRTable() |
| 3282 if picklefile: |
| 3283 read_signature = lr.read_pickle(picklefile) |
| 3284 else: |
| 3285 read_signature = lr.read_table(tabmodule) |
| 3286 if optimize or (read_signature == signature): |
| 3287 try: |
| 3288 lr.bind_callables(pinfo.pdict) |
| 3289 parser = LRParser(lr, pinfo.error_func) |
| 3290 parse = parser.parse |
| 3291 return parser |
| 3292 except Exception as e: |
| 3293 errorlog.warning('There was a problem loading the table file: %r
', e) |
| 3294 except VersionError as e: |
| 3295 errorlog.warning(str(e)) |
| 3296 except ImportError: |
| 3297 pass |
| 3298 |
| 3299 if debuglog is None: |
| 3300 if debug: |
| 3301 try: |
| 3302 debuglog = PlyLogger(open(os.path.join(outputdir, debugfile), 'w
')) |
| 3303 except IOError as e: |
| 3304 errorlog.warning("Couldn't open %r. %s" % (debugfile, e)) |
| 3305 debuglog = NullLogger() |
| 3306 else: |
| 3307 debuglog = NullLogger() |
| 3308 |
| 3309 debuglog.info('Created by PLY version %s (http://www.dabeaz.com/ply)', __ver
sion__) |
| 3310 |
| 3311 errors = False |
| 3312 |
| 3313 # Validate the parser information |
| 3314 if pinfo.validate_all(): |
| 3315 raise YaccError('Unable to build parser') |
| 3316 |
| 3317 if not pinfo.error_func: |
| 3318 errorlog.warning('no p_error() function is defined') |
| 3319 |
| 3320 # Create a grammar object |
| 3321 grammar = Grammar(pinfo.tokens) |
| 3322 |
| 3323 # Set precedence level for terminals |
| 3324 for term, assoc, level in pinfo.preclist: |
| 3325 try: |
| 3326 grammar.set_precedence(term, assoc, level) |
| 3327 except GrammarError as e: |
| 3328 errorlog.warning('%s', e) |
| 3329 |
| 3330 # Add productions to the grammar |
| 3331 for funcname, gram in pinfo.grammar: |
| 3332 file, line, prodname, syms = gram |
| 3333 try: |
| 3334 grammar.add_production(prodname, syms, funcname, file, line) |
| 3335 except GrammarError as e: |
| 3336 errorlog.error('%s', e) |
| 3337 errors = True |
| 3338 |
| 3339 # Set the grammar start symbols |
| 3340 try: |
| 3341 if start is None: |
| 3342 grammar.set_start(pinfo.start) |
| 3343 else: |
| 3344 grammar.set_start(start) |
| 3345 except GrammarError as e: |
| 3346 errorlog.error(str(e)) |
| 3347 errors = True |
| 3348 |
| 3349 if errors: |
| 3350 raise YaccError('Unable to build parser') |
| 3351 |
| 3352 # Verify the grammar structure |
| 3353 undefined_symbols = grammar.undefined_symbols() |
| 3354 for sym, prod in undefined_symbols: |
| 3355 errorlog.error('%s:%d: Symbol %r used, but not defined as a token or a r
ule', prod.file, prod.line, sym) |
| 3356 errors = True |
| 3357 |
| 3358 unused_terminals = grammar.unused_terminals() |
| 3359 if unused_terminals: |
| 3360 debuglog.info('') |
| 3361 debuglog.info('Unused terminals:') |
| 3362 debuglog.info('') |
| 3363 for term in unused_terminals: |
| 3364 errorlog.warning('Token %r defined, but not used', term) |
| 3365 debuglog.info(' %s', term) |
| 3366 |
| 3367 # Print out all productions to the debug log |
| 3368 if debug: |
| 3369 debuglog.info('') |
| 3370 debuglog.info('Grammar') |
| 3371 debuglog.info('') |
| 3372 for n, p in enumerate(grammar.Productions): |
| 3373 debuglog.info('Rule %-5d %s', n, p) |
| 3374 |
| 3375 # Find unused non-terminals |
| 3376 unused_rules = grammar.unused_rules() |
| 3377 for prod in unused_rules: |
| 3378 errorlog.warning('%s:%d: Rule %r defined, but not used', prod.file, prod
.line, prod.name) |
| 3379 |
| 3380 if len(unused_terminals) == 1: |
| 3381 errorlog.warning('There is 1 unused token') |
| 3382 if len(unused_terminals) > 1: |
| 3383 errorlog.warning('There are %d unused tokens', len(unused_terminals)) |
| 3384 |
| 3385 if len(unused_rules) == 1: |
| 3386 errorlog.warning('There is 1 unused rule') |
| 3387 if len(unused_rules) > 1: |
| 3388 errorlog.warning('There are %d unused rules', len(unused_rules)) |
| 3389 |
| 3390 if debug: |
| 3391 debuglog.info('') |
| 3392 debuglog.info('Terminals, with rules where they appear') |
| 3393 debuglog.info('') |
| 3394 terms = list(grammar.Terminals) |
| 3395 terms.sort() |
| 3396 for term in terms: |
| 3397 debuglog.info('%-20s : %s', term, ' '.join([str(s) for s in grammar.
Terminals[term]])) |
| 3398 |
| 3399 debuglog.info('') |
| 3400 debuglog.info('Nonterminals, with rules where they appear') |
| 3401 debuglog.info('') |
| 3402 nonterms = list(grammar.Nonterminals) |
| 3403 nonterms.sort() |
| 3404 for nonterm in nonterms: |
| 3405 debuglog.info('%-20s : %s', nonterm, ' '.join([str(s) for s in gramm
ar.Nonterminals[nonterm]])) |
| 3406 debuglog.info('') |
| 3407 |
| 3408 if check_recursion: |
| 3409 unreachable = grammar.find_unreachable() |
| 3410 for u in unreachable: |
| 3411 errorlog.warning('Symbol %r is unreachable', u) |
| 3412 |
| 3413 infinite = grammar.infinite_cycles() |
| 3414 for inf in infinite: |
| 3415 errorlog.error('Infinite recursion detected for symbol %r', inf) |
| 3416 errors = True |
| 3417 |
| 3418 unused_prec = grammar.unused_precedence() |
| 3419 for term, assoc in unused_prec: |
| 3420 errorlog.error('Precedence rule %r defined for unknown symbol %r', assoc
, term) |
| 3421 errors = True |
| 3422 |
| 3423 if errors: |
| 3424 raise YaccError('Unable to build parser') |
| 3425 |
| 3426 # Run the LRGeneratedTable on the grammar |
| 3427 if debug: |
| 3428 errorlog.debug('Generating %s tables', method) |
| 3429 |
| 3430 lr = LRGeneratedTable(grammar, method, debuglog) |
| 3431 |
| 3432 if debug: |
| 3433 num_sr = len(lr.sr_conflicts) |
| 3434 |
| 3435 # Report shift/reduce and reduce/reduce conflicts |
| 3436 if num_sr == 1: |
| 3437 errorlog.warning('1 shift/reduce conflict') |
| 3438 elif num_sr > 1: |
| 3439 errorlog.warning('%d shift/reduce conflicts', num_sr) |
| 3440 |
| 3441 num_rr = len(lr.rr_conflicts) |
| 3442 if num_rr == 1: |
| 3443 errorlog.warning('1 reduce/reduce conflict') |
| 3444 elif num_rr > 1: |
| 3445 errorlog.warning('%d reduce/reduce conflicts', num_rr) |
| 3446 |
| 3447 # Write out conflicts to the output file |
| 3448 if debug and (lr.sr_conflicts or lr.rr_conflicts): |
| 3449 debuglog.warning('') |
| 3450 debuglog.warning('Conflicts:') |
| 3451 debuglog.warning('') |
| 3452 |
| 3453 for state, tok, resolution in lr.sr_conflicts: |
| 3454 debuglog.warning('shift/reduce conflict for %s in state %d resolved
as %s', tok, state, resolution) |
| 3455 |
| 3456 already_reported = set() |
| 3457 for state, rule, rejected in lr.rr_conflicts: |
| 3458 if (state, id(rule), id(rejected)) in already_reported: |
| 3459 continue |
| 3460 debuglog.warning('reduce/reduce conflict in state %d resolved using
rule (%s)', state, rule) |
| 3461 debuglog.warning('rejected rule (%s) in state %d', rejected, state) |
| 3462 errorlog.warning('reduce/reduce conflict in state %d resolved using
rule (%s)', state, rule) |
| 3463 errorlog.warning('rejected rule (%s) in state %d', rejected, state) |
| 3464 already_reported.add((state, id(rule), id(rejected))) |
| 3465 |
| 3466 warned_never = [] |
| 3467 for state, rule, rejected in lr.rr_conflicts: |
| 3468 if not rejected.reduced and (rejected not in warned_never): |
| 3469 debuglog.warning('Rule (%s) is never reduced', rejected) |
| 3470 errorlog.warning('Rule (%s) is never reduced', rejected) |
| 3471 warned_never.append(rejected) |
| 3472 |
| 3473 # Write the table file if requested |
| 3474 if write_tables: |
| 3475 try: |
| 3476 lr.write_table(tabmodule, outputdir, signature) |
| 3477 except IOError as e: |
| 3478 errorlog.warning("Couldn't create %r. %s" % (tabmodule, e)) |
| 3479 |
| 3480 # Write a pickled version of the tables |
| 3481 if picklefile: |
| 3482 try: |
| 3483 lr.pickle_table(picklefile, signature) |
| 3484 except IOError as e: |
| 3485 errorlog.warning("Couldn't create %r. %s" % (picklefile, e)) |
| 3486 |
| 3487 # Build the parser |
| 3488 lr.bind_callables(pinfo.pdict) |
| 3489 parser = LRParser(lr, pinfo.error_func) |
| 3490 |
| 3491 parse = parser.parse |
| 3492 return parser |
OLD | NEW |