| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 |
|
| 24 |
|
| 25 |
|
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 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.10' |
| 71 |
__tabversion__ = '3.10' |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
yaccdebug = True |
| 80 |
|
| 81 |
|
| 82 |
debug_file = 'parser.out' |
| 83 |
tab_module = 'parsetab' |
| 84 |
default_lr = 'LALR' |
| 85 |
|
| 86 |
error_count = 3 |
| 87 |
|
| 88 |
yaccdevel = False |
| 89 |
|
| 90 |
|
| 91 |
resultlimit = 40 |
| 92 |
|
| 93 |
pickle_protocol = 0 |
| 94 |
|
| 95 |
|
| 96 |
if sys.version_info[0] < 3: |
| 97 |
string_types = basestring |
| 98 |
else: |
| 99 |
string_types = str |
| 100 |
|
| 101 |
MAXINT = sys.maxsize |
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 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 |
|
| 127 |
class NullLogger(object): |
| 128 |
def __getattribute__(self, name): |
| 129 |
return self |
| 130 |
|
| 131 |
def __call__(self, *args, **kwargs): |
| 132 |
return self |
| 133 |
|
| 134 |
|
| 135 |
class YaccError(Exception): |
| 136 |
pass |
| 137 |
|
| 138 |
|
| 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 |
|
| 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 |
|
| 159 |
|
| 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 |
|
| 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 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
class YaccSymbol: |
| 218 |
def __str__(self): |
| 219 |
return self.type |
| 220 |
|
| 221 |
def __repr__(self): |
| 222 |
return str(self) |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
|
| 230 |
|
| 231 |
|
| 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 |
|
| 281 |
|
| 282 |
|
| 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 |
|
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
|
| 312 |
|
| 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, tokenfunc=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, tokenfunc) |
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
|
| 340 |
|
| 341 |
|
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
def parsedebug(self, input=None, lexer=None, debug=False, tracking=False, tokenfunc=None): |
| 349 |
|
| 350 |
lookahead = None |
| 351 |
lookaheadstack = [] |
| 352 |
actions = self.action |
| 353 |
goto = self.goto |
| 354 |
prod = self.productions |
| 355 |
defaulted_states = self.defaulted_states |
| 356 |
pslice = YaccProduction(None) |
| 357 |
errorcount = 0 |
| 358 |
|
| 359 |
|
| 360 |
debug.info('PLY: PARSE DEBUG START') |
| 361 |
|
| 362 |
|
| 363 |
|
| 364 |
if not lexer: |
| 365 |
from . import lex |
| 366 |
lexer = lex.lexer |
| 367 |
|
| 368 |
|
| 369 |
pslice.lexer = lexer |
| 370 |
pslice.parser = self |
| 371 |
|
| 372 |
|
| 373 |
if input is not None: |
| 374 |
lexer.input(input) |
| 375 |
|
| 376 |
if tokenfunc is None: |
| 377 |
|
| 378 |
get_token = lexer.token |
| 379 |
else: |
| 380 |
get_token = tokenfunc |
| 381 |
|
| 382 |
|
| 383 |
self.token = get_token |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
statestack = [] |
| 388 |
self.statestack = statestack |
| 389 |
symstack = [] |
| 390 |
self.symstack = symstack |
| 391 |
|
| 392 |
pslice.stack = symstack |
| 393 |
errtoken = None |
| 394 |
|
| 395 |
|
| 396 |
|
| 397 |
statestack.append(0) |
| 398 |
sym = YaccSymbol() |
| 399 |
sym.type = '$end' |
| 400 |
symstack.append(sym) |
| 401 |
state = 0 |
| 402 |
while True: |
| 403 |
|
| 404 |
|
| 405 |
|
| 406 |
|
| 407 |
|
| 408 |
debug.debug('') |
| 409 |
debug.debug('State : %s', state) |
| 410 |
|
| 411 |
|
| 412 |
if state not in defaulted_states: |
| 413 |
if not lookahead: |
| 414 |
if not lookaheadstack: |
| 415 |
lookahead = get_token() |
| 416 |
else: |
| 417 |
lookahead = lookaheadstack.pop() |
| 418 |
if not lookahead: |
| 419 |
lookahead = YaccSymbol() |
| 420 |
lookahead.type = '$end' |
| 421 |
|
| 422 |
|
| 423 |
ltype = lookahead.type |
| 424 |
t = actions[state].get(ltype) |
| 425 |
else: |
| 426 |
t = defaulted_states[state] |
| 427 |
|
| 428 |
debug.debug('Defaulted state %s: Reduce using %d', state, -t) |
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
debug.debug('Stack : %s', |
| 433 |
('%s . %s' % (' '.join([xx.type for xx in symstack][1:]), str(lookahead))).lstrip()) |
| 434 |
|
| 435 |
|
| 436 |
if t is not None: |
| 437 |
if t > 0: |
| 438 |
|
| 439 |
statestack.append(t) |
| 440 |
state = t |
| 441 |
|
| 442 |
|
| 443 |
debug.debug('Action : Shift and goto state %s', t) |
| 444 |
|
| 445 |
|
| 446 |
symstack.append(lookahead) |
| 447 |
lookahead = None |
| 448 |
|
| 449 |
|
| 450 |
if errorcount: |
| 451 |
errorcount -= 1 |
| 452 |
continue |
| 453 |
|
| 454 |
if t < 0: |
| 455 |
|
| 456 |
p = prod[-t] |
| 457 |
pname = p.name |
| 458 |
plen = p.len |
| 459 |
|
| 460 |
|
| 461 |
sym = YaccSymbol() |
| 462 |
sym.type = pname |
| 463 |
sym.value = None |
| 464 |
|
| 465 |
|
| 466 |
if plen: |
| 467 |
debug.info('Action : Reduce rule [%s] with %s and goto state %d', p.str, |
| 468 |
'['+','.join([format_stack_entry(_v.value) for _v in symstack[-plen:]])+']', |
| 469 |
goto[statestack[-1-plen]][pname]) |
| 470 |
else: |
| 471 |
debug.info('Action : Reduce rule [%s] with %s and goto state %d', p.str, [], |
| 472 |
goto[statestack[-1]][pname]) |
| 473 |
|
| 474 |
|
| 475 |
|
| 476 |
if plen: |
| 477 |
targ = symstack[-plen-1:] |
| 478 |
targ[0] = sym |
| 479 |
|
| 480 |
|
| 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 |
|
| 489 |
|
| 490 |
|
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
pslice.slice = targ |
| 496 |
|
| 497 |
try: |
| 498 |
|
| 499 |
del symstack[-plen:] |
| 500 |
self.state = state |
| 501 |
p.callable(pslice) |
| 502 |
del statestack[-plen:] |
| 503 |
|
| 504 |
debug.info('Result : %s', format_result(pslice[0])) |
| 505 |
|
| 506 |
symstack.append(sym) |
| 507 |
state = goto[statestack[-1]][pname] |
| 508 |
statestack.append(state) |
| 509 |
except SyntaxError: |
| 510 |
|
| 511 |
lookaheadstack.append(lookahead) |
| 512 |
symstack.extend(targ[1:-1]) |
| 513 |
statestack.pop() |
| 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 |
|
| 527 |
if tracking: |
| 528 |
sym.lineno = lexer.lineno |
| 529 |
sym.lexpos = lexer.lexpos |
| 530 |
|
| 531 |
|
| 532 |
targ = [sym] |
| 533 |
|
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
pslice.slice = targ |
| 540 |
|
| 541 |
try: |
| 542 |
|
| 543 |
self.state = state |
| 544 |
p.callable(pslice) |
| 545 |
|
| 546 |
debug.info('Result : %s', format_result(pslice[0])) |
| 547 |
|
| 548 |
symstack.append(sym) |
| 549 |
state = goto[statestack[-1]][pname] |
| 550 |
statestack.append(state) |
| 551 |
except SyntaxError: |
| 552 |
|
| 553 |
lookaheadstack.append(lookahead) |
| 554 |
statestack.pop() |
| 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 |
|
| 569 |
debug.info('Done : Returning %s', format_result(result)) |
| 570 |
debug.info('PLY: PARSE DEBUG END') |
| 571 |
|
| 572 |
return result |
| 573 |
|
| 574 |
if t is None: |
| 575 |
|
| 576 |
|
| 577 |
debug.error('Error : %s', |
| 578 |
('%s . %s' % (' '.join([xx.type for xx in symstack][1:]), str(lookahead))).lstrip()) |
| 579 |
|
| 580 |
|
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
|
| 588 |
|
| 589 |
|
| 590 |
|
| 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 |
| 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 |
|
| 604 |
|
| 605 |
|
| 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 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
if len(statestack) <= 1 and lookahead.type != '$end': |
| 631 |
lookahead = None |
| 632 |
errtoken = None |
| 633 |
state = 0 |
| 634 |
|
| 635 |
del lookaheadstack[:] |
| 636 |
continue |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
|
| 642 |
if lookahead.type == '$end': |
| 643 |
|
| 644 |
return |
| 645 |
|
| 646 |
if lookahead.type != 'error': |
| 647 |
sym = symstack[-1] |
| 648 |
if sym.type == 'error': |
| 649 |
|
| 650 |
|
| 651 |
|
| 652 |
if tracking: |
| 653 |
sym.endlineno = getattr(lookahead, 'lineno', sym.lineno) |
| 654 |
sym.endlexpos = getattr(lookahead, 'lexpos', sym.lexpos) |
| 655 |
|
| 656 |
lookahead = None |
| 657 |
continue |
| 658 |
|
| 659 |
|
| 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 |
|
| 673 |
if tracking: |
| 674 |
lookahead.lineno = sym.lineno |
| 675 |
lookahead.lexpos = sym.lexpos |
| 676 |
|
| 677 |
statestack.pop() |
| 678 |
state = statestack[-1] |
| 679 |
|
| 680 |
continue |
| 681 |
|
| 682 |
|
| 683 |
raise RuntimeError('yacc: internal parser error!!!\n') |
| 684 |
|
| 685 |
|
| 686 |
|
| 687 |
|
| 688 |
|
| 689 |
|
| 690 |
|
| 691 |
|
| 692 |
|
| 693 |
|
| 694 |
|
| 695 |
def parseopt(self, input=None, lexer=None, debug=False, tracking=False, tokenfunc=None): |
| 696 |
|
| 697 |
lookahead = None |
| 698 |
lookaheadstack = [] |
| 699 |
actions = self.action |
| 700 |
goto = self.goto |
| 701 |
prod = self.productions |
| 702 |
defaulted_states = self.defaulted_states |
| 703 |
pslice = YaccProduction(None) |
| 704 |
errorcount = 0 |
| 705 |
|
| 706 |
|
| 707 |
|
| 708 |
if not lexer: |
| 709 |
from . import lex |
| 710 |
lexer = lex.lexer |
| 711 |
|
| 712 |
|
| 713 |
pslice.lexer = lexer |
| 714 |
pslice.parser = self |
| 715 |
|
| 716 |
|
| 717 |
if input is not None: |
| 718 |
lexer.input(input) |
| 719 |
|
| 720 |
if tokenfunc is None: |
| 721 |
|
| 722 |
get_token = lexer.token |
| 723 |
else: |
| 724 |
get_token = tokenfunc |
| 725 |
|
| 726 |
|
| 727 |
self.token = get_token |
| 728 |
|
| 729 |
|
| 730 |
|
| 731 |
statestack = [] |
| 732 |
self.statestack = statestack |
| 733 |
symstack = [] |
| 734 |
self.symstack = symstack |
| 735 |
|
| 736 |
pslice.stack = symstack |
| 737 |
errtoken = None |
| 738 |
|
| 739 |
|
| 740 |
|
| 741 |
statestack.append(0) |
| 742 |
sym = YaccSymbol() |
| 743 |
sym.type = '$end' |
| 744 |
symstack.append(sym) |
| 745 |
state = 0 |
| 746 |
while True: |
| 747 |
|
| 748 |
|
| 749 |
|
| 750 |
|
| 751 |
|
| 752 |
if state not in defaulted_states: |
| 753 |
if not lookahead: |
| 754 |
if not lookaheadstack: |
| 755 |
lookahead = get_token() |
| 756 |
else: |
| 757 |
lookahead = lookaheadstack.pop() |
| 758 |
if not lookahead: |
| 759 |
lookahead = YaccSymbol() |
| 760 |
lookahead.type = '$end' |
| 761 |
|
| 762 |
|
| 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 |
|
| 772 |
statestack.append(t) |
| 773 |
state = t |
| 774 |
|
| 775 |
|
| 776 |
symstack.append(lookahead) |
| 777 |
lookahead = None |
| 778 |
|
| 779 |
|
| 780 |
if errorcount: |
| 781 |
errorcount -= 1 |
| 782 |
continue |
| 783 |
|
| 784 |
if t < 0: |
| 785 |
|
| 786 |
p = prod[-t] |
| 787 |
pname = p.name |
| 788 |
plen = p.len |
| 789 |
|
| 790 |
|
| 791 |
sym = YaccSymbol() |
| 792 |
sym.type = pname |
| 793 |
sym.value = None |
| 794 |
|
| 795 |
|
| 796 |
if plen: |
| 797 |
targ = symstack[-plen-1:] |
| 798 |
targ[0] = sym |
| 799 |
|
| 800 |
|
| 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 |
|
| 809 |
|
| 810 |
|
| 811 |
|
| 812 |
|
| 813 |
|
| 814 |
|
| 815 |
pslice.slice = targ |
| 816 |
|
| 817 |
try: |
| 818 |
|
| 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 |
|
| 828 |
lookaheadstack.append(lookahead) |
| 829 |
symstack.extend(targ[1:-1]) |
| 830 |
statestack.pop() |
| 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 |
|
| 844 |
if tracking: |
| 845 |
sym.lineno = lexer.lineno |
| 846 |
sym.lexpos = lexer.lexpos |
| 847 |
|
| 848 |
|
| 849 |
targ = [sym] |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
|
| 855 |
|
| 856 |
pslice.slice = targ |
| 857 |
|
| 858 |
try: |
| 859 |
|
| 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 |
|
| 867 |
lookaheadstack.append(lookahead) |
| 868 |
statestack.pop() |
| 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 |
|
| 888 |
|
| 889 |
|
| 890 |
|
| 891 |
|
| 892 |
|
| 893 |
|
| 894 |
|
| 895 |
|
| 896 |
|
| 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 |
| 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 |
|
| 910 |
|
| 911 |
|
| 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 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
if len(statestack) <= 1 and lookahead.type != '$end': |
| 937 |
lookahead = None |
| 938 |
errtoken = None |
| 939 |
state = 0 |
| 940 |
|
| 941 |
del lookaheadstack[:] |
| 942 |
continue |
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
if lookahead.type == '$end': |
| 949 |
|
| 950 |
return |
| 951 |
|
| 952 |
if lookahead.type != 'error': |
| 953 |
sym = symstack[-1] |
| 954 |
if sym.type == 'error': |
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
if tracking: |
| 959 |
sym.endlineno = getattr(lookahead, 'lineno', sym.lineno) |
| 960 |
sym.endlexpos = getattr(lookahead, 'lexpos', sym.lexpos) |
| 961 |
|
| 962 |
lookahead = None |
| 963 |
continue |
| 964 |
|
| 965 |
|
| 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 |
|
| 979 |
if tracking: |
| 980 |
lookahead.lineno = sym.lineno |
| 981 |
lookahead.lexpos = sym.lexpos |
| 982 |
|
| 983 |
statestack.pop() |
| 984 |
state = statestack[-1] |
| 985 |
|
| 986 |
continue |
| 987 |
|
| 988 |
|
| 989 |
raise RuntimeError('yacc: internal parser error!!!\n') |
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
def parseopt_notrack(self, input=None, lexer=None, debug=False, tracking=False, tokenfunc=None): |
| 1002 |
|
| 1003 |
lookahead = None |
| 1004 |
lookaheadstack = [] |
| 1005 |
actions = self.action |
| 1006 |
goto = self.goto |
| 1007 |
prod = self.productions |
| 1008 |
defaulted_states = self.defaulted_states |
| 1009 |
pslice = YaccProduction(None) |
| 1010 |
errorcount = 0 |
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
if not lexer: |
| 1015 |
from . import lex |
| 1016 |
lexer = lex.lexer |
| 1017 |
|
| 1018 |
|
| 1019 |
pslice.lexer = lexer |
| 1020 |
pslice.parser = self |
| 1021 |
|
| 1022 |
|
| 1023 |
if input is not None: |
| 1024 |
lexer.input(input) |
| 1025 |
|
| 1026 |
if tokenfunc is None: |
| 1027 |
|
| 1028 |
get_token = lexer.token |
| 1029 |
else: |
| 1030 |
get_token = tokenfunc |
| 1031 |
|
| 1032 |
|
| 1033 |
self.token = get_token |
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
statestack = [] |
| 1038 |
self.statestack = statestack |
| 1039 |
symstack = [] |
| 1040 |
self.symstack = symstack |
| 1041 |
|
| 1042 |
pslice.stack = symstack |
| 1043 |
errtoken = None |
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
statestack.append(0) |
| 1048 |
sym = YaccSymbol() |
| 1049 |
sym.type = '$end' |
| 1050 |
symstack.append(sym) |
| 1051 |
state = 0 |
| 1052 |
while True: |
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
if state not in defaulted_states: |
| 1059 |
if not lookahead: |
| 1060 |
if not lookaheadstack: |
| 1061 |
lookahead = get_token() |
| 1062 |
else: |
| 1063 |
lookahead = lookaheadstack.pop() |
| 1064 |
if not lookahead: |
| 1065 |
lookahead = YaccSymbol() |
| 1066 |
lookahead.type = '$end' |
| 1067 |
|
| 1068 |
|
| 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 |
|
| 1078 |
statestack.append(t) |
| 1079 |
state = t |
| 1080 |
|
| 1081 |
|
| 1082 |
symstack.append(lookahead) |
| 1083 |
lookahead = None |
| 1084 |
|
| 1085 |
|
| 1086 |
if errorcount: |
| 1087 |
errorcount -= 1 |
| 1088 |
continue |
| 1089 |
|
| 1090 |
if t < 0: |
| 1091 |
|
| 1092 |
p = prod[-t] |
| 1093 |
pname = p.name |
| 1094 |
plen = p.len |
| 1095 |
|
| 1096 |
|
| 1097 |
sym = YaccSymbol() |
| 1098 |
sym.type = pname |
| 1099 |
sym.value = None |
| 1100 |
|
| 1101 |
|
| 1102 |
if plen: |
| 1103 |
targ = symstack[-plen-1:] |
| 1104 |
targ[0] = sym |
| 1105 |
|
| 1106 |
|
| 1107 |
|
| 1108 |
|
| 1109 |
|
| 1110 |
|
| 1111 |
|
| 1112 |
pslice.slice = targ |
| 1113 |
|
| 1114 |
try: |
| 1115 |
|
| 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 |
|
| 1125 |
lookaheadstack.append(lookahead) |
| 1126 |
symstack.extend(targ[1:-1]) |
| 1127 |
statestack.pop() |
| 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 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
pslice.slice = targ |
| 1149 |
|
| 1150 |
try: |
| 1151 |
|
| 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 |
|
| 1159 |
lookaheadstack.append(lookahead) |
| 1160 |
statestack.pop() |
| 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 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
|
| 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 |
| 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 |
|
| 1202 |
|
| 1203 |
|
| 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 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
if len(statestack) <= 1 and lookahead.type != '$end': |
| 1229 |
lookahead = None |
| 1230 |
errtoken = None |
| 1231 |
state = 0 |
| 1232 |
|
| 1233 |
del lookaheadstack[:] |
| 1234 |
continue |
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
if lookahead.type == '$end': |
| 1241 |
|
| 1242 |
return |
| 1243 |
|
| 1244 |
if lookahead.type != 'error': |
| 1245 |
sym = symstack[-1] |
| 1246 |
if sym.type == 'error': |
| 1247 |
|
| 1248 |
|
| 1249 |
lookahead = None |
| 1250 |
continue |
| 1251 |
|
| 1252 |
|
| 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 |
|
| 1271 |
raise RuntimeError('yacc: internal parser error!!!\n') |
| 1272 |
|
| 1273 |
|
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
|
| 1283 |
_is_identifier = re.compile(r'^[a-zA-Z0-9_-]+$') |
| 1284 |
|
| 1285 |
|
| 1286 |
|
| 1287 |
|
| 1288 |
|
| 1289 |
|
| 1290 |
|
| 1291 |
|
| 1292 |
|
| 1293 |
|
| 1294 |
|
| 1295 |
|
| 1296 |
|
| 1297 |
|
| 1298 |
|
| 1299 |
|
| 1300 |
|
| 1301 |
|
| 1302 |
|
| 1303 |
|
| 1304 |
|
| 1305 |
|
| 1306 |
|
| 1307 |
|
| 1308 |
|
| 1309 |
class Production(object): |
| 1310 |
reduced = 0 |
| 1311 |
def __init__(self, number, name, prod, precedence=('right', 0), func=None, file='', 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 |
|
| 1322 |
|
| 1323 |
self.len = len(self.prod) |
| 1324 |
|
| 1325 |
|
| 1326 |
self.usyms = [] |
| 1327 |
for s in self.prod: |
| 1328 |
if s not in self.usyms: |
| 1329 |
self.usyms.append(s) |
| 1330 |
|
| 1331 |
|
| 1332 |
self.lr_items = [] |
| 1333 |
self.lr_next = None |
| 1334 |
|
| 1335 |
|
| 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 |
|
| 1357 |
def lr_item(self, n): |
| 1358 |
if n > len(self.prod): |
| 1359 |
return None |
| 1360 |
p = LRItem(self, n) |
| 1361 |
|
| 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 |
|
| 1373 |
def bind(self, pdict): |
| 1374 |
if self.func: |
| 1375 |
self.callable = pdict[self.func] |
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 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 |
|
| 1398 |
def bind(self, pdict): |
| 1399 |
if self.func: |
| 1400 |
self.callable = pdict[self.func] |
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
|
| 1421 |
|
| 1422 |
|
| 1423 |
|
| 1424 |
|
| 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 |
|
| 1451 |
|
| 1452 |
|
| 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 |
|
| 1464 |
|
| 1465 |
|
| 1466 |
|
| 1467 |
|
| 1468 |
|
| 1469 |
|
| 1470 |
class GrammarError(YaccError): |
| 1471 |
pass |
| 1472 |
|
| 1473 |
class Grammar(object): |
| 1474 |
def __init__(self, terminals): |
| 1475 |
self.Productions = [None] |
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
self.Prodnames = {} |
| 1480 |
|
| 1481 |
|
| 1482 |
self.Prodmap = {} |
| 1483 |
|
| 1484 |
|
| 1485 |
self.Terminals = {} |
| 1486 |
|
| 1487 |
|
| 1488 |
for term in terminals: |
| 1489 |
self.Terminals[term] = [] |
| 1490 |
|
| 1491 |
self.Terminals['error'] = [] |
| 1492 |
|
| 1493 |
self.Nonterminals = {} |
| 1494 |
|
| 1495 |
|
| 1496 |
self.First = {} |
| 1497 |
|
| 1498 |
self.Follow = {} |
| 1499 |
|
| 1500 |
self.Precedence = {} |
| 1501 |
|
| 1502 |
|
| 1503 |
self.UsedPrecedence = set() |
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
self.Start = None |
| 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 |
|
| 1518 |
|
| 1519 |
|
| 1520 |
|
| 1521 |
|
| 1522 |
|
| 1523 |
|
| 1524 |
def set_precedence(self, term, assoc, level): |
| 1525 |
assert self.Productions == [None], 'Must call set_precedence() before add_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 |
|
| 1534 |
|
| 1535 |
|
| 1536 |
|
| 1537 |
|
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 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, prodname)) |
| 1557 |
|
| 1558 |
|
| 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 may 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 |
|
| 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:] |
| 1589 |
else: |
| 1590 |
|
| 1591 |
precname = rightmost_terminal(syms, self.Terminals) |
| 1592 |
prodprec = self.Precedence.get(precname, ('right', 0)) |
| 1593 |
|
| 1594 |
|
| 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 |
|
| 1602 |
pnumber = len(self.Productions) |
| 1603 |
if prodname not in self.Nonterminals: |
| 1604 |
self.Nonterminals[prodname] = [] |
| 1605 |
|
| 1606 |
|
| 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 |
|
| 1616 |
p = Production(pnumber, prodname, syms, prodprec, func, file, line) |
| 1617 |
self.Productions.append(p) |
| 1618 |
self.Prodmap[map] = p |
| 1619 |
|
| 1620 |
|
| 1621 |
try: |
| 1622 |
self.Prodnames[prodname].append(p) |
| 1623 |
except KeyError: |
| 1624 |
self.Prodnames[prodname] = [p] |
| 1625 |
|
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 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 |
|
| 1644 |
|
| 1645 |
|
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
def find_unreachable(self): |
| 1650 |
|
| 1651 |
|
| 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 |
|
| 1666 |
|
| 1667 |
|
| 1668 |
|
| 1669 |
|
| 1670 |
|
| 1671 |
|
| 1672 |
def infinite_cycles(self): |
| 1673 |
terminates = {} |
| 1674 |
|
| 1675 |
|
| 1676 |
for t in self.Terminals: |
| 1677 |
terminates[t] = True |
| 1678 |
|
| 1679 |
terminates['$end'] = True |
| 1680 |
|
| 1681 |
|
| 1682 |
|
| 1683 |
|
| 1684 |
for n in self.Nonterminals: |
| 1685 |
terminates[n] = False |
| 1686 |
|
| 1687 |
|
| 1688 |
while True: |
| 1689 |
some_change = False |
| 1690 |
for (n, pl) in self.Prodnames.items(): |
| 1691 |
|
| 1692 |
for p in pl: |
| 1693 |
|
| 1694 |
for s in p.prod: |
| 1695 |
if not terminates[s]: |
| 1696 |
|
| 1697 |
|
| 1698 |
p_terminates = False |
| 1699 |
break |
| 1700 |
else: |
| 1701 |
|
| 1702 |
|
| 1703 |
|
| 1704 |
p_terminates = True |
| 1705 |
|
| 1706 |
if p_terminates: |
| 1707 |
|
| 1708 |
if not terminates[n]: |
| 1709 |
terminates[n] = True |
| 1710 |
some_change = True |
| 1711 |
|
| 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 |
|
| 1722 |
|
| 1723 |
pass |
| 1724 |
else: |
| 1725 |
infinite.append(s) |
| 1726 |
|
| 1727 |
return infinite |
| 1728 |
|
| 1729 |
|
| 1730 |
|
| 1731 |
|
| 1732 |
|
| 1733 |
|
| 1734 |
|
| 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 |
|
| 1749 |
|
| 1750 |
|
| 1751 |
|
| 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 |
|
| 1763 |
|
| 1764 |
|
| 1765 |
|
| 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 |
|
| 1778 |
|
| 1779 |
|
| 1780 |
|
| 1781 |
|
| 1782 |
|
| 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.UsedPrecedence): |
| 1789 |
unused.append((termname, self.Precedence[termname][0])) |
| 1790 |
|
| 1791 |
return unused |
| 1792 |
|
| 1793 |
|
| 1794 |
|
| 1795 |
|
| 1796 |
|
| 1797 |
|
| 1798 |
|
| 1799 |
|
| 1800 |
|
| 1801 |
def _first(self, beta): |
| 1802 |
|
| 1803 |
|
| 1804 |
result = [] |
| 1805 |
for x in beta: |
| 1806 |
x_produces_empty = False |
| 1807 |
|
| 1808 |
|
| 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 |
|
| 1818 |
|
| 1819 |
pass |
| 1820 |
else: |
| 1821 |
|
| 1822 |
break |
| 1823 |
else: |
| 1824 |
|
| 1825 |
|
| 1826 |
|
| 1827 |
result.append('<empty>') |
| 1828 |
|
| 1829 |
return result |
| 1830 |
|
| 1831 |
|
| 1832 |
|
| 1833 |
|
| 1834 |
|
| 1835 |
|
| 1836 |
def compute_first(self): |
| 1837 |
if self.First: |
| 1838 |
return self.First |
| 1839 |
|
| 1840 |
|
| 1841 |
for t in self.Terminals: |
| 1842 |
self.First[t] = [t] |
| 1843 |
|
| 1844 |
self.First['$end'] = ['$end'] |
| 1845 |
|
| 1846 |
|
| 1847 |
|
| 1848 |
|
| 1849 |
for n in self.Nonterminals: |
| 1850 |
self.First[n] = [] |
| 1851 |
|
| 1852 |
|
| 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 |
|
| 1868 |
|
| 1869 |
|
| 1870 |
|
| 1871 |
|
| 1872 |
|
| 1873 |
def compute_follow(self, start=None): |
| 1874 |
|
| 1875 |
if self.Follow: |
| 1876 |
return self.Follow |
| 1877 |
|
| 1878 |
|
| 1879 |
if not self.First: |
| 1880 |
self.compute_first() |
| 1881 |
|
| 1882 |
|
| 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 |
|
| 1895 |
for i, B in enumerate(p.prod): |
| 1896 |
if B in self.Nonterminals: |
| 1897 |
|
| 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 |
|
| 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 |
|
| 1919 |
|
| 1920 |
|
| 1921 |
|
| 1922 |
|
| 1923 |
|
| 1924 |
|
| 1925 |
|
| 1926 |
|
| 1927 |
|
| 1928 |
|
| 1929 |
|
| 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 |
|
| 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 |
|
| 1962 |
|
| 1963 |
|
| 1964 |
|
| 1965 |
|
| 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 |
|
| 2026 |
def bind_callables(self, pdict): |
| 2027 |
for p in self.lr_productions: |
| 2028 |
p.bind(pdict) |
| 2029 |
|
| 2030 |
|
| 2031 |
|
| 2032 |
|
| 2033 |
|
| 2034 |
|
| 2035 |
|
| 2036 |
|
| 2037 |
|
| 2038 |
|
| 2039 |
|
| 2040 |
|
| 2041 |
|
| 2042 |
|
| 2043 |
|
| 2044 |
|
| 2045 |
|
| 2046 |
|
| 2047 |
|
| 2048 |
|
| 2049 |
|
| 2050 |
|
| 2051 |
|
| 2052 |
|
| 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) |
| 2071 |
|
| 2072 |
rel = R(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 |
|
| 2094 |
|
| 2095 |
|
| 2096 |
|
| 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 |
|
| 2108 |
if not log: |
| 2109 |
log = NullLogger() |
| 2110 |
self.log = log |
| 2111 |
|
| 2112 |
|
| 2113 |
self.lr_action = {} |
| 2114 |
self.lr_goto = {} |
| 2115 |
self.lr_productions = grammar.Productions |
| 2116 |
self.lr_goto_cache = {} |
| 2117 |
self.lr0_cidhash = {} |
| 2118 |
|
| 2119 |
self._add_count = 0 |
| 2120 |
|
| 2121 |
|
| 2122 |
self.sr_conflict = 0 |
| 2123 |
self.rr_conflict = 0 |
| 2124 |
self.conflicts = [] |
| 2125 |
|
| 2126 |
self.sr_conflicts = [] |
| 2127 |
self.rr_conflicts = [] |
| 2128 |
|
| 2129 |
|
| 2130 |
self.grammar.build_lritems() |
| 2131 |
self.grammar.compute_first() |
| 2132 |
self.grammar.compute_follow() |
| 2133 |
self.lr_parse_table() |
| 2134 |
|
| 2135 |
|
| 2136 |
|
| 2137 |
def lr0_closure(self, I): |
| 2138 |
self._add_count += 1 |
| 2139 |
|
| 2140 |
|
| 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 |
|
| 2150 |
J.append(x.lr_next) |
| 2151 |
x.lr0_added = self._add_count |
| 2152 |
didadd = True |
| 2153 |
|
| 2154 |
return J |
| 2155 |
|
| 2156 |
|
| 2157 |
|
| 2158 |
|
| 2159 |
|
| 2160 |
|
| 2161 |
|
| 2162 |
|
| 2163 |
def lr0_goto(self, I, x): |
| 2164 |
|
| 2165 |
g = self.lr_goto_cache.get((id(I), x)) |
| 2166 |
if g: |
| 2167 |
return g |
| 2168 |
|
| 2169 |
|
| 2170 |
|
| 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 |
|
| 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 |
|
| 2206 |
i = 0 |
| 2207 |
while i < len(C): |
| 2208 |
I = C[i] |
| 2209 |
i += 1 |
| 2210 |
|
| 2211 |
|
| 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 |
|
| 2228 |
|
| 2229 |
|
| 2230 |
|
| 2231 |
|
| 2232 |
|
| 2233 |
|
| 2234 |
|
| 2235 |
|
| 2236 |
|
| 2237 |
|
| 2238 |
|
| 2239 |
|
| 2240 |
|
| 2241 |
|
| 2242 |
|
| 2243 |
|
| 2244 |
|
| 2245 |
|
| 2246 |
|
| 2247 |
|
| 2248 |
|
| 2249 |
|
| 2250 |
|
| 2251 |
|
| 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 |
|
| 2274 |
|
| 2275 |
|
| 2276 |
|
| 2277 |
|
| 2278 |
|
| 2279 |
|
| 2280 |
|
| 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 |
|
| 2296 |
|
| 2297 |
|
| 2298 |
|
| 2299 |
|
| 2300 |
|
| 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 |
|
| 2317 |
if state == 0 and N == self.grammar.Productions[0].prod[0]: |
| 2318 |
terms.append('$end') |
| 2319 |
|
| 2320 |
return terms |
| 2321 |
|
| 2322 |
|
| 2323 |
|
| 2324 |
|
| 2325 |
|
| 2326 |
|
| 2327 |
|
| 2328 |
def reads_relation(self, C, trans, empty): |
| 2329 |
|
| 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 |
|
| 2345 |
|
| 2346 |
|
| 2347 |
|
| 2348 |
|
| 2349 |
|
| 2350 |
|
| 2351 |
|
| 2352 |
|
| 2353 |
|
| 2354 |
|
| 2355 |
|
| 2356 |
|
| 2357 |
|
| 2358 |
|
| 2359 |
|
| 2360 |
|
| 2361 |
|
| 2362 |
|
| 2363 |
|
| 2364 |
|
| 2365 |
|
| 2366 |
|
| 2367 |
|
| 2368 |
|
| 2369 |
|
| 2370 |
|
| 2371 |
def compute_lookback_includes(self, C, trans, nullable): |
| 2372 |
lookdict = {} |
| 2373 |
includedict = {} |
| 2374 |
|
| 2375 |
|
| 2376 |
dtrans = {} |
| 2377 |
for t in trans: |
| 2378 |
dtrans[t] = 1 |
| 2379 |
|
| 2380 |
|
| 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 |
|
| 2389 |
|
| 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 |
|
| 2398 |
if (j, t) in dtrans: |
| 2399 |
|
| 2400 |
|
| 2401 |
|
| 2402 |
|
| 2403 |
li = lr_index + 1 |
| 2404 |
while li < p.len: |
| 2405 |
if p.prod[li] in self.grammar.Terminals: |
| 2406 |
break |
| 2407 |
if p.prod[li] not in nullable: |
| 2408 |
break |
| 2409 |
li = li + 1 |
| 2410 |
else: |
| 2411 |
|
| 2412 |
includes.append((j, t)) |
| 2413 |
|
| 2414 |
g = self.lr0_goto(C[j], t) |
| 2415 |
j = self.lr0_cidhash.get(id(g), -1) |
| 2416 |
|
| 2417 |
|
| 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 |
|
| 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 |
|
| 2441 |
|
| 2442 |
|
| 2443 |
|
| 2444 |
|
| 2445 |
|
| 2446 |
|
| 2447 |
|
| 2448 |
|
| 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 |
|
| 2459 |
|
| 2460 |
|
| 2461 |
|
| 2462 |
|
| 2463 |
|
| 2464 |
|
| 2465 |
|
| 2466 |
|
| 2467 |
|
| 2468 |
|
| 2469 |
|
| 2470 |
|
| 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 |
|
| 2481 |
|
| 2482 |
|
| 2483 |
|
| 2484 |
|
| 2485 |
|
| 2486 |
|
| 2487 |
|
| 2488 |
|
| 2489 |
|
| 2490 |
|
| 2491 |
def add_lookaheads(self, lookbacks, followset): |
| 2492 |
for trans, lb in lookbacks.items(): |
| 2493 |
|
| 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 |
|
| 2504 |
|
| 2505 |
|
| 2506 |
|
| 2507 |
|
| 2508 |
|
| 2509 |
def add_lalr_lookaheads(self, C): |
| 2510 |
|
| 2511 |
nullable = self.compute_nullable_nonterminals() |
| 2512 |
|
| 2513 |
|
| 2514 |
trans = self.find_nonterminal_transitions(C) |
| 2515 |
|
| 2516 |
|
| 2517 |
readsets = self.compute_read_sets(C, trans, nullable) |
| 2518 |
|
| 2519 |
|
| 2520 |
lookd, included = self.compute_lookback_includes(C, trans, nullable) |
| 2521 |
|
| 2522 |
|
| 2523 |
followsets = self.compute_follow_sets(trans, readsets, included) |
| 2524 |
|
| 2525 |
|
| 2526 |
self.add_lookaheads(lookd, followsets) |
| 2527 |
|
| 2528 |
|
| 2529 |
|
| 2530 |
|
| 2531 |
|
| 2532 |
|
| 2533 |
def lr_parse_table(self): |
| 2534 |
Productions = self.grammar.Productions |
| 2535 |
Precedence = self.grammar.Precedence |
| 2536 |
goto = self.lr_goto |
| 2537 |
action = self.lr_action |
| 2538 |
log = self.log |
| 2539 |
|
| 2540 |
actionp = {} |
| 2541 |
|
| 2542 |
log.info('Parsing method: %s', self.lr_method) |
| 2543 |
|
| 2544 |
|
| 2545 |
|
| 2546 |
|
| 2547 |
C = self.lr0_items() |
| 2548 |
|
| 2549 |
if self.lr_method == 'LALR': |
| 2550 |
self.add_lalr_lookaheads(C) |
| 2551 |
|
| 2552 |
|
| 2553 |
st = 0 |
| 2554 |
for I in C: |
| 2555 |
|
| 2556 |
actlist = [] |
| 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 |
|
| 2571 |
st_action['$end'] = 0 |
| 2572 |
st_actionp['$end'] = p |
| 2573 |
else: |
| 2574 |
|
| 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 |
|
| 2584 |
if r > 0: |
| 2585 |
|
| 2586 |
|
| 2587 |
|
| 2588 |
|
| 2589 |
|
| 2590 |
sprec, slevel = Precedence.get(a, ('right', 0)) |
| 2591 |
|
| 2592 |
|
| 2593 |
rprec, rlevel = Productions[p.number].prec |
| 2594 |
|
| 2595 |
if (slevel < rlevel) or ((slevel == rlevel) and (rprec == 'left')): |
| 2596 |
|
| 2597 |
st_action[a] = -p.number |
| 2598 |
st_actionp[a] = p |
| 2599 |
if not slevel and not rlevel: |
| 2600 |
log.info(' ! shift/reduce conflict for %s resolved as reduce', a) |
| 2601 |
self.sr_conflicts.append((st, a, 'reduce')) |
| 2602 |
Productions[p.number].reduced += 1 |
| 2603 |
elif (slevel == rlevel) and (rprec == 'nonassoc'): |
| 2604 |
st_action[a] = None |
| 2605 |
else: |
| 2606 |
|
| 2607 |
if not rlevel: |
| 2608 |
log.info(' ! shift/reduce conflict for %s resolved as shift', a) |
| 2609 |
self.sr_conflicts.append((st, a, 'shift')) |
| 2610 |
elif r < 0: |
| 2611 |
|
| 2612 |
|
| 2613 |
oldp = Productions[-r] |
| 2614 |
pp = Productions[p.number] |
| 2615 |
if oldp.line > pp.line: |
| 2616 |
st_action[a] = -p.number |
| 2617 |
st_actionp[a] = p |
| 2618 |
chosenp, rejectp = pp, oldp |
| 2619 |
Productions[p.number].reduced += 1 |
| 2620 |
Productions[oldp.number].reduced -= 1 |
| 2621 |
else: |
| 2622 |
chosenp, rejectp = oldp, pp |
| 2623 |
self.rr_conflicts.append((st, chosenp, rejectp)) |
| 2624 |
log.info(' ! reduce/reduce conflict for %s resolved using rule %d (%s)', |
| 2625 |
a, st_actionp[a].number, st_actionp[a]) |
| 2626 |
else: |
| 2627 |
raise LALRError('Unknown conflict in state %d' % st) |
| 2628 |
else: |
| 2629 |
st_action[a] = -p.number |
| 2630 |
st_actionp[a] = p |
| 2631 |
Productions[p.number].reduced += 1 |
| 2632 |
else: |
| 2633 |
i = p.lr_index |
| 2634 |
a = p.prod[i+1] |
| 2635 |
if a in self.grammar.Terminals: |
| 2636 |
g = self.lr0_goto(I, a) |
| 2637 |
j = self.lr0_cidhash.get(id(g), -1) |
| 2638 |
if j >= 0: |
| 2639 |
|
| 2640 |
actlist.append((a, p, 'shift and go to state %d' % j)) |
| 2641 |
r = st_action.get(a) |
| 2642 |
if r is not None: |
| 2643 |
|
| 2644 |
if r > 0: |
| 2645 |
if r != j: |
| 2646 |
raise LALRError('Shift/shift conflict in state %d' % st) |
| 2647 |
elif r < 0: |
| 2648 |
|
| 2649 |
|
| 2650 |
|
| 2651 |
|
| 2652 |
|
| 2653 |
|
| 2654 |
sprec, slevel = Precedence.get(a, ('right', 0)) |
| 2655 |
|
| 2656 |
|
| 2657 |
rprec, rlevel = Productions[st_actionp[a].number].prec |
| 2658 |
|
| 2659 |
if (slevel > rlevel) or ((slevel == rlevel) and (rprec == 'right')): |
| 2660 |
|
| 2661 |
Productions[st_actionp[a].number].reduced -= 1 |
| 2662 |
st_action[a] = j |
| 2663 |
st_actionp[a] = p |
| 2664 |
if not rlevel: |
| 2665 |
log.info(' ! shift/reduce conflict for %s resolved as shift', a) |
| 2666 |
self.sr_conflicts.append((st, a, 'shift')) |
| 2667 |
elif (slevel == rlevel) and (rprec == 'nonassoc'): |
| 2668 |
st_action[a] = None |
| 2669 |
else: |
| 2670 |
|
| 2671 |
if not slevel and not rlevel: |
| 2672 |
log.info(' ! shift/reduce conflict for %s resolved as reduce', a) |
| 2673 |
self.sr_conflicts.append((st, a, 'reduce')) |
| 2674 |
|
| 2675 |
else: |
| 2676 |
raise LALRError('Unknown conflict in state %d' % st) |
| 2677 |
else: |
| 2678 |
st_action[a] = j |
| 2679 |
st_actionp[a] = p |
| 2680 |
|
| 2681 |
|
| 2682 |
_actprint = {} |
| 2683 |
for a, p, m in actlist: |
| 2684 |
if a in st_action: |
| 2685 |
if p is st_actionp[a]: |
| 2686 |
log.info(' %-15s %s', a, m) |
| 2687 |
_actprint[(a, m)] = 1 |
| 2688 |
log.info('') |
| 2689 |
|
| 2690 |
not_used = 0 |
| 2691 |
for a, p, m in actlist: |
| 2692 |
if a in st_action: |
| 2693 |
if p is not st_actionp[a]: |
| 2694 |
if not (a, m) in _actprint: |
| 2695 |
log.debug(' ! %-15s [ %s ]', a, m) |
| 2696 |
not_used = 1 |
| 2697 |
_actprint[(a, m)] = 1 |
| 2698 |
if not_used: |
| 2699 |
log.debug('') |
| 2700 |
|
| 2701 |
|
| 2702 |
|
| 2703 |
nkeys = {} |
| 2704 |
for ii in I: |
| 2705 |
for s in ii.usyms: |
| 2706 |
if s in self.grammar.Nonterminals: |
| 2707 |
nkeys[s] = None |
| 2708 |
for n in nkeys: |
| 2709 |
g = self.lr0_goto(I, n) |
| 2710 |
j = self.lr0_cidhash.get(id(g), -1) |
| 2711 |
if j >= 0: |
| 2712 |
st_goto[n] = j |
| 2713 |
log.info(' %-30s shift and go to state %d', n, j) |
| 2714 |
|
| 2715 |
action[st] = st_action |
| 2716 |
actionp[st] = st_actionp |
| 2717 |
goto[st] = st_goto |
| 2718 |
st += 1 |
| 2719 |
|
| 2720 |
|
| 2721 |
|
| 2722 |
|
| 2723 |
|
| 2724 |
|
| 2725 |
|
| 2726 |
def write_table(self, tabmodule, outputdir='', signature=''): |
| 2727 |
if isinstance(tabmodule, types.ModuleType): |
| 2728 |
raise IOError("Won't overwrite existing tabmodule") |
| 2729 |
|
| 2730 |
basemodulename = tabmodule.split('.')[-1] |
| 2731 |
filename = os.path.join(outputdir, basemodulename) + '.py' |
| 2732 |
try: |
| 2733 |
f = open(filename, 'w') |
| 2734 |
|
| 2735 |
f.write(''' |
| 2736 |
# %s |
| 2737 |
# This file is automatically generated. Do not edit. |
| 2738 |
_tabversion = %r |
| 2739 |
|
| 2740 |
_lr_method = %r |
| 2741 |
|
| 2742 |
_lr_signature = %r |
| 2743 |
''' % (os.path.basename(filename), __tabversion__, self.lr_method, signature)) |
| 2744 |
|
| 2745 |
|
| 2746 |
smaller = 1 |
| 2747 |
|
| 2748 |
|
| 2749 |
if smaller: |
| 2750 |
items = {} |
| 2751 |
|
| 2752 |
for s, nd in self.lr_action.items(): |
| 2753 |
for name, v in nd.items(): |
| 2754 |
i = items.get(name) |
| 2755 |
if not i: |
| 2756 |
i = ([], []) |
| 2757 |
items[name] = i |
| 2758 |
i[0].append(s) |
| 2759 |
i[1].append(v) |
| 2760 |
|
| 2761 |
f.write('\n_lr_action_items = {') |
| 2762 |
for k, v in items.items(): |
| 2763 |
f.write('%r:([' % k) |
| 2764 |
for i in v[0]: |
| 2765 |
f.write('%r,' % i) |
| 2766 |
f.write('],[') |
| 2767 |
for i in v[1]: |
| 2768 |
f.write('%r,' % i) |
| 2769 |
|
| 2770 |
f.write(']),') |
| 2771 |
f.write('}\n') |
| 2772 |
|
| 2773 |
f.write(''' |
| 2774 |
_lr_action = {} |
| 2775 |
for _k, _v in _lr_action_items.items(): |
| 2776 |
for _x,_y in zip(_v[0],_v[1]): |
| 2777 |
if not _x in _lr_action: _lr_action[_x] = {} |
| 2778 |
_lr_action[_x][_k] = _y |
| 2779 |
del _lr_action_items |
| 2780 |
''') |
| 2781 |
|
| 2782 |
else: |
| 2783 |
f.write('\n_lr_action = { ') |
| 2784 |
for k, v in self.lr_action.items(): |
| 2785 |
f.write('(%r,%r):%r,' % (k[0], k[1], v)) |
| 2786 |
f.write('}\n') |
| 2787 |
|
| 2788 |
if smaller: |
| 2789 |
|
| 2790 |
items = {} |
| 2791 |
|
| 2792 |
for s, nd in self.lr_goto.items(): |
| 2793 |
for name, v in nd.items(): |
| 2794 |
i = items.get(name) |
| 2795 |
if not i: |
| 2796 |
i = ([], []) |
| 2797 |
items[name] = i |
| 2798 |
i[0].append(s) |
| 2799 |
i[1].append(v) |
| 2800 |
|
| 2801 |
f.write('\n_lr_goto_items = {') |
| 2802 |
for k, v in items.items(): |
| 2803 |
f.write('%r:([' % k) |
| 2804 |
for i in v[0]: |
| 2805 |
f.write('%r,' % i) |
| 2806 |
f.write('],[') |
| 2807 |
for i in v[1]: |
| 2808 |
f.write('%r,' % i) |
| 2809 |
|
| 2810 |
f.write(']),') |
| 2811 |
f.write('}\n') |
| 2812 |
|
| 2813 |
f.write(''' |
| 2814 |
_lr_goto = {} |
| 2815 |
for _k, _v in _lr_goto_items.items(): |
| 2816 |
for _x, _y in zip(_v[0], _v[1]): |
| 2817 |
if not _x in _lr_goto: _lr_goto[_x] = {} |
| 2818 |
_lr_goto[_x][_k] = _y |
| 2819 |
del _lr_goto_items |
| 2820 |
''') |
| 2821 |
else: |
| 2822 |
f.write('\n_lr_goto = { ') |
| 2823 |
for k, v in self.lr_goto.items(): |
| 2824 |
f.write('(%r,%r):%r,' % (k[0], k[1], v)) |
| 2825 |
f.write('}\n') |
| 2826 |
|
| 2827 |
|
| 2828 |
f.write('_lr_productions = [\n') |
| 2829 |
for p in self.lr_productions: |
| 2830 |
if p.func: |
| 2831 |
f.write(' (%r,%r,%d,%r,%r,%d),\n' % (p.str, p.name, p.len, |
| 2832 |
p.func, os.path.basename(p.file), p.line)) |
| 2833 |
else: |
| 2834 |
f.write(' (%r,%r,%d,None,None,None),\n' % (str(p), p.name, p.len)) |
| 2835 |
f.write(']\n') |
| 2836 |
f.close() |
| 2837 |
|
| 2838 |
except IOError as e: |
| 2839 |
raise |
| 2840 |
|
| 2841 |
|
| 2842 |
|
| 2843 |
|
| 2844 |
|
| 2845 |
|
| 2846 |
|
| 2847 |
|
| 2848 |
def pickle_table(self, filename, signature=''): |
| 2849 |
try: |
| 2850 |
import cPickle as pickle |
| 2851 |
except ImportError: |
| 2852 |
import pickle |
| 2853 |
with open(filename, 'wb') as outf: |
| 2854 |
pickle.dump(__tabversion__, outf, pickle_protocol) |
| 2855 |
pickle.dump(self.lr_method, outf, pickle_protocol) |
| 2856 |
pickle.dump(signature, outf, pickle_protocol) |
| 2857 |
pickle.dump(self.lr_action, outf, pickle_protocol) |
| 2858 |
pickle.dump(self.lr_goto, outf, pickle_protocol) |
| 2859 |
|
| 2860 |
outp = [] |
| 2861 |
for p in self.lr_productions: |
| 2862 |
if p.func: |
| 2863 |
outp.append((p.str, p.name, p.len, p.func, os.path.basename(p.file), p.line)) |
| 2864 |
else: |
| 2865 |
outp.append((str(p), p.name, p.len, None, None, None)) |
| 2866 |
pickle.dump(outp, outf, pickle_protocol) |
| 2867 |
|
| 2868 |
|
| 2869 |
|
| 2870 |
|
| 2871 |
|
| 2872 |
|
| 2873 |
|
| 2874 |
|
| 2875 |
|
| 2876 |
|
| 2877 |
|
| 2878 |
|
| 2879 |
|
| 2880 |
|
| 2881 |
|
| 2882 |
|
| 2883 |
def get_caller_module_dict(levels): |
| 2884 |
f = sys._getframe(levels) |
| 2885 |
ldict = f.f_globals.copy() |
| 2886 |
if f.f_globals != f.f_locals: |
| 2887 |
ldict.update(f.f_locals) |
| 2888 |
return ldict |
| 2889 |
|
| 2890 |
|
| 2891 |
|
| 2892 |
|
| 2893 |
|
| 2894 |
|
| 2895 |
def parse_grammar(doc, file, line): |
| 2896 |
grammar = [] |
| 2897 |
|
| 2898 |
pstrings = doc.splitlines() |
| 2899 |
lastp = None |
| 2900 |
dline = line |
| 2901 |
for ps in pstrings: |
| 2902 |
dline += 1 |
| 2903 |
p = ps.split() |
| 2904 |
if not p: |
| 2905 |
continue |
| 2906 |
try: |
| 2907 |
if p[0] == '|': |
| 2908 |
|
| 2909 |
if not lastp: |
| 2910 |
raise SyntaxError("%s:%d: Misplaced '|'" % (file, dline)) |
| 2911 |
prodname = lastp |
| 2912 |
syms = p[1:] |
| 2913 |
else: |
| 2914 |
prodname = p[0] |
| 2915 |
lastp = prodname |
| 2916 |
syms = p[2:] |
| 2917 |
assign = p[1] |
| 2918 |
if assign != ':' and assign != '::=': |
| 2919 |
raise SyntaxError("%s:%d: Syntax error. Expected ':'" % (file, dline)) |
| 2920 |
|
| 2921 |
grammar.append((file, dline, prodname, syms)) |
| 2922 |
except SyntaxError: |
| 2923 |
raise |
| 2924 |
except Exception: |
| 2925 |
raise SyntaxError('%s:%d: Syntax error in rule %r' % (file, dline, ps.strip())) |
| 2926 |
|
| 2927 |
return grammar |
| 2928 |
|
| 2929 |
|
| 2930 |
|
| 2931 |
|
| 2932 |
|
| 2933 |
|
| 2934 |
|
| 2935 |
|
| 2936 |
class ParserReflect(object): |
| 2937 |
def __init__(self, pdict, log=None): |
| 2938 |
self.pdict = pdict |
| 2939 |
self.start = None |
| 2940 |
self.error_func = None |
| 2941 |
self.tokens = None |
| 2942 |
self.modules = set() |
| 2943 |
self.grammar = [] |
| 2944 |
self.error = False |
| 2945 |
|
| 2946 |
if log is None: |
| 2947 |
self.log = PlyLogger(sys.stderr) |
| 2948 |
else: |
| 2949 |
self.log = log |
| 2950 |
|
| 2951 |
|
| 2952 |
def get_all(self): |
| 2953 |
self.get_start() |
| 2954 |
self.get_error_func() |
| 2955 |
self.get_tokens() |
| 2956 |
self.get_precedence() |
| 2957 |
self.get_pfunctions() |
| 2958 |
|
| 2959 |
|
| 2960 |
def validate_all(self): |
| 2961 |
self.validate_start() |
| 2962 |
self.validate_error_func() |
| 2963 |
self.validate_tokens() |
| 2964 |
self.validate_precedence() |
| 2965 |
self.validate_pfunctions() |
| 2966 |
self.validate_modules() |
| 2967 |
return self.error |
| 2968 |
|
| 2969 |
|
| 2970 |
def signature(self): |
| 2971 |
parts = [] |
| 2972 |
try: |
| 2973 |
if self.start: |
| 2974 |
parts.append(self.start) |
| 2975 |
if self.prec: |
| 2976 |
parts.append(''.join([''.join(p) for p in self.prec])) |
| 2977 |
if self.tokens: |
| 2978 |
parts.append(' '.join(self.tokens)) |
| 2979 |
for f in self.pfuncs: |
| 2980 |
if f[3]: |
| 2981 |
parts.append(f[3]) |
| 2982 |
except (TypeError, ValueError): |
| 2983 |
pass |
| 2984 |
return ''.join(parts) |
| 2985 |
|
| 2986 |
|
| 2987 |
|
| 2988 |
|
| 2989 |
|
| 2990 |
|
| 2991 |
|
| 2992 |
|
| 2993 |
|
| 2994 |
|
| 2995 |
|
| 2996 |
|
| 2997 |
def validate_modules(self): |
| 2998 |
|
| 2999 |
fre = re.compile(r'\s*def\s+(p_[a-zA-Z_0-9]*)\(') |
| 3000 |
|
| 3001 |
for module in self.modules: |
| 3002 |
try: |
| 3003 |
lines, linen = inspect.getsourcelines(module) |
| 3004 |
except IOError: |
| 3005 |
continue |
| 3006 |
|
| 3007 |
counthash = {} |
| 3008 |
for linen, line in enumerate(lines): |
| 3009 |
linen += 1 |
| 3010 |
m = fre.match(line) |
| 3011 |
if m: |
| 3012 |
name = m.group(1) |
| 3013 |
prev = counthash.get(name) |
| 3014 |
if not prev: |
| 3015 |
counthash[name] = linen |
| 3016 |
else: |
| 3017 |
filename = inspect.getsourcefile(module) |
| 3018 |
self.log.warning('%s:%d: Function %s redefined. Previously defined on line %d', |
| 3019 |
filename, linen, name, prev) |
| 3020 |
|
| 3021 |
|
| 3022 |
def get_start(self): |
| 3023 |
self.start = self.pdict.get('start') |
| 3024 |
|
| 3025 |
|
| 3026 |
def validate_start(self): |
| 3027 |
if self.start is not None: |
| 3028 |
if not isinstance(self.start, string_types): |
| 3029 |
self.log.error("'start' must be a string") |
| 3030 |
|
| 3031 |
|
| 3032 |
def get_error_func(self): |
| 3033 |
self.error_func = self.pdict.get('p_error') |
| 3034 |
|
| 3035 |
|
| 3036 |
def validate_error_func(self): |
| 3037 |
if self.error_func: |
| 3038 |
if isinstance(self.error_func, types.FunctionType): |
| 3039 |
ismethod = 0 |
| 3040 |
elif isinstance(self.error_func, types.MethodType): |
| 3041 |
ismethod = 1 |
| 3042 |
else: |
| 3043 |
self.log.error("'p_error' defined, but is not a function or method") |
| 3044 |
self.error = True |
| 3045 |
return |
| 3046 |
|
| 3047 |
eline = self.error_func.__code__.co_firstlineno |
| 3048 |
efile = self.error_func.__code__.co_filename |
| 3049 |
module = inspect.getmodule(self.error_func) |
| 3050 |
self.modules.add(module) |
| 3051 |
|
| 3052 |
argcount = self.error_func.__code__.co_argcount - ismethod |
| 3053 |
if argcount != 1: |
| 3054 |
self.log.error('%s:%d: p_error() requires 1 argument', efile, eline) |
| 3055 |
self.error = True |
| 3056 |
|
| 3057 |
|
| 3058 |
def get_tokens(self): |
| 3059 |
tokens = self.pdict.get('tokens') |
| 3060 |
if not tokens: |
| 3061 |
self.log.error('No token list is defined') |
| 3062 |
self.error = True |
| 3063 |
return |
| 3064 |
|
| 3065 |
if not isinstance(tokens, (list, tuple)): |
| 3066 |
self.log.error('tokens must be a list or tuple') |
| 3067 |
self.error = True |
| 3068 |
return |
| 3069 |
|
| 3070 |
if not tokens: |
| 3071 |
self.log.error('tokens is empty') |
| 3072 |
self.error = True |
| 3073 |
return |
| 3074 |
|
| 3075 |
self.tokens = tokens |
| 3076 |
|
| 3077 |
|
| 3078 |
def validate_tokens(self): |
| 3079 |
|
| 3080 |
if 'error' in self.tokens: |
| 3081 |
self.log.error("Illegal token name 'error'. Is a reserved word") |
| 3082 |
self.error = True |
| 3083 |
return |
| 3084 |
|
| 3085 |
terminals = set() |
| 3086 |
for n in self.tokens: |
| 3087 |
if n in terminals: |
| 3088 |
self.log.warning('Token %r multiply defined', n) |
| 3089 |
terminals.add(n) |
| 3090 |
|
| 3091 |
|
| 3092 |
def get_precedence(self): |
| 3093 |
self.prec = self.pdict.get('precedence') |
| 3094 |
|
| 3095 |
|
| 3096 |
def validate_precedence(self): |
| 3097 |
preclist = [] |
| 3098 |
if self.prec: |
| 3099 |
if not isinstance(self.prec, (list, tuple)): |
| 3100 |
self.log.error('precedence must be a list or tuple') |
| 3101 |
self.error = True |
| 3102 |
return |
| 3103 |
for level, p in enumerate(self.prec): |
| 3104 |
if not isinstance(p, (list, tuple)): |
| 3105 |
self.log.error('Bad precedence table') |
| 3106 |
self.error = True |
| 3107 |
return |
| 3108 |
|
| 3109 |
if len(p) < 2: |
| 3110 |
self.log.error('Malformed precedence entry %s. Must be (assoc, term, ..., term)', p) |
| 3111 |
self.error = True |
| 3112 |
return |
| 3113 |
assoc = p[0] |
| 3114 |
if not isinstance(assoc, string_types): |
| 3115 |
self.log.error('precedence associativity must be a string') |
| 3116 |
self.error = True |
| 3117 |
return |
| 3118 |
for term in p[1:]: |
| 3119 |
if not isinstance(term, string_types): |
| 3120 |
self.log.error('precedence items must be strings') |
| 3121 |
self.error = True |
| 3122 |
return |
| 3123 |
preclist.append((term, assoc, level+1)) |
| 3124 |
self.preclist = preclist |
| 3125 |
|
| 3126 |
|
| 3127 |
def get_pfunctions(self): |
| 3128 |
p_functions = [] |
| 3129 |
for name, item in self.pdict.items(): |
| 3130 |
if not name.startswith('p_') or name == 'p_error': |
| 3131 |
continue |
| 3132 |
if isinstance(item, (types.FunctionType, types.MethodType)): |
| 3133 |
line = getattr(item, 'co_firstlineno', item.__code__.co_firstlineno) |
| 3134 |
module = inspect.getmodule(item) |
| 3135 |
p_functions.append((line, module, name, item.__doc__)) |
| 3136 |
|
| 3137 |
|
| 3138 |
|
| 3139 |
|
| 3140 |
p_functions.sort(key=lambda p_function: ( |
| 3141 |
p_function[0], |
| 3142 |
str(p_function[1]), |
| 3143 |
p_function[2], |
| 3144 |
p_function[3])) |
| 3145 |
self.pfuncs = p_functions |
| 3146 |
|
| 3147 |
|
| 3148 |
def validate_pfunctions(self): |
| 3149 |
grammar = [] |
| 3150 |
|
| 3151 |
if len(self.pfuncs) == 0: |
| 3152 |
self.log.error('no rules of the form p_rulename are defined') |
| 3153 |
self.error = True |
| 3154 |
return |
| 3155 |
|
| 3156 |
for line, module, name, doc in self.pfuncs: |
| 3157 |
file = inspect.getsourcefile(module) |
| 3158 |
func = self.pdict[name] |
| 3159 |
if isinstance(func, types.MethodType): |
| 3160 |
reqargs = 2 |
| 3161 |
else: |
| 3162 |
reqargs = 1 |
| 3163 |
if func.__code__.co_argcount > reqargs: |
| 3164 |
self.log.error('%s:%d: Rule %r has too many arguments', file, line, func.__name__) |
| 3165 |
self.error = True |
| 3166 |
elif func.__code__.co_argcount < reqargs: |
| 3167 |
self.log.error('%s:%d: Rule %r requires an argument', file, line, func.__name__) |
| 3168 |
self.error = True |
| 3169 |
elif not func.__doc__: |
| 3170 |
self.log.warning('%s:%d: No documentation string specified in function %r (ignored)', |
| 3171 |
file, line, func.__name__) |
| 3172 |
else: |
| 3173 |
try: |
| 3174 |
parsed_g = parse_grammar(doc, file, line) |
| 3175 |
for g in parsed_g: |
| 3176 |
grammar.append((name, g)) |
| 3177 |
except SyntaxError as e: |
| 3178 |
self.log.error(str(e)) |
| 3179 |
self.error = True |
| 3180 |
|
| 3181 |
|
| 3182 |
|
| 3183 |
self.modules.add(module) |
| 3184 |
|
| 3185 |
|
| 3186 |
|
| 3187 |
|
| 3188 |
for n, v in self.pdict.items(): |
| 3189 |
if n.startswith('p_') and isinstance(v, (types.FunctionType, types.MethodType)): |
| 3190 |
continue |
| 3191 |
if n.startswith('t_'): |
| 3192 |
continue |
| 3193 |
if n.startswith('p_') and n != 'p_error': |
| 3194 |
self.log.warning('%r not defined as a function', n) |
| 3195 |
if ((isinstance(v, types.FunctionType) and v.__code__.co_argcount == 1) or |
| 3196 |
(isinstance(v, types.MethodType) and v.__func__.__code__.co_argcount == 2)): |
| 3197 |
if v.__doc__: |
| 3198 |
try: |
| 3199 |
doc = v.__doc__.split(' ') |
| 3200 |
if doc[1] == ':': |
| 3201 |
self.log.warning('%s:%d: Possible grammar rule %r defined without p_ prefix', |
| 3202 |
v.__code__.co_filename, v.__code__.co_firstlineno, n) |
| 3203 |
except IndexError: |
| 3204 |
pass |
| 3205 |
|
| 3206 |
self.grammar = grammar |
| 3207 |
|
| 3208 |
|
| 3209 |
|
| 3210 |
|
| 3211 |
|
| 3212 |
|
| 3213 |
|
| 3214 |
def yacc(method='LALR', debug=yaccdebug, module=None, tabmodule=tab_module, start=None, |
| 3215 |
check_recursion=True, optimize=False, write_tables=True, debugfile=debug_file, |
| 3216 |
outputdir=None, debuglog=None, errorlog=None, picklefile=None): |
| 3217 |
|
| 3218 |
if tabmodule is None: |
| 3219 |
tabmodule = tab_module |
| 3220 |
|
| 3221 |
|
| 3222 |
global parse |
| 3223 |
|
| 3224 |
|
| 3225 |
if picklefile: |
| 3226 |
write_tables = 0 |
| 3227 |
|
| 3228 |
if errorlog is None: |
| 3229 |
errorlog = PlyLogger(sys.stderr) |
| 3230 |
|
| 3231 |
|
| 3232 |
if module: |
| 3233 |
_items = [(k, getattr(module, k)) for k in dir(module)] |
| 3234 |
pdict = dict(_items) |
| 3235 |
|
| 3236 |
if '__file__' not in pdict: |
| 3237 |
pdict['__file__'] = sys.modules[pdict['__module__']].__file__ |
| 3238 |
else: |
| 3239 |
pdict = get_caller_module_dict(2) |
| 3240 |
|
| 3241 |
if outputdir is None: |
| 3242 |
|
| 3243 |
|
| 3244 |
|
| 3245 |
|
| 3246 |
if isinstance(tabmodule, types.ModuleType): |
| 3247 |
srcfile = tabmodule.__file__ |
| 3248 |
else: |
| 3249 |
if '.' not in tabmodule: |
| 3250 |
srcfile = pdict['__file__'] |
| 3251 |
else: |
| 3252 |
parts = tabmodule.split('.') |
| 3253 |
pkgname = '.'.join(parts[:-1]) |
| 3254 |
exec('import %s' % pkgname) |
| 3255 |
srcfile = getattr(sys.modules[pkgname], '__file__', '') |
| 3256 |
outputdir = os.path.dirname(srcfile) |
| 3257 |
|
| 3258 |
|
| 3259 |
|
| 3260 |
pkg = pdict.get('__package__') |
| 3261 |
if pkg and isinstance(tabmodule, str): |
| 3262 |
if '.' not in tabmodule: |
| 3263 |
tabmodule = pkg + '.' + tabmodule |
| 3264 |
|
| 3265 |
|
| 3266 |
|
| 3267 |
|
| 3268 |
if start is not None: |
| 3269 |
pdict['start'] = start |
| 3270 |
|
| 3271 |
|
| 3272 |
pinfo = ParserReflect(pdict, log=errorlog) |
| 3273 |
pinfo.get_all() |
| 3274 |
|
| 3275 |
if pinfo.error: |
| 3276 |
raise YaccError('Unable to build parser') |
| 3277 |
|
| 3278 |
|
| 3279 |
signature = pinfo.signature() |
| 3280 |
|
| 3281 |
|
| 3282 |
try: |
| 3283 |
lr = LRTable() |
| 3284 |
if picklefile: |
| 3285 |
read_signature = lr.read_pickle(picklefile) |
| 3286 |
else: |
| 3287 |
read_signature = lr.read_table(tabmodule) |
| 3288 |
if optimize or (read_signature == signature): |
| 3289 |
try: |
| 3290 |
lr.bind_callables(pinfo.pdict) |
| 3291 |
parser = LRParser(lr, pinfo.error_func) |
| 3292 |
parse = parser.parse |
| 3293 |
return parser |
| 3294 |
except Exception as e: |
| 3295 |
errorlog.warning('There was a problem loading the table file: %r', e) |
| 3296 |
except VersionError as e: |
| 3297 |
errorlog.warning(str(e)) |
| 3298 |
except ImportError: |
| 3299 |
pass |
| 3300 |
|
| 3301 |
if debuglog is None: |
| 3302 |
if debug: |
| 3303 |
try: |
| 3304 |
debuglog = PlyLogger(open(os.path.join(outputdir, debugfile), 'w')) |
| 3305 |
except IOError as e: |
| 3306 |
errorlog.warning("Couldn't open %r. %s" % (debugfile, e)) |
| 3307 |
debuglog = NullLogger() |
| 3308 |
else: |
| 3309 |
debuglog = NullLogger() |
| 3310 |
|
| 3311 |
debuglog.info('Created by PLY version %s (http://www.dabeaz.com/ply)', __version__) |
| 3312 |
|
| 3313 |
errors = False |
| 3314 |
|
| 3315 |
|
| 3316 |
if pinfo.validate_all(): |
| 3317 |
raise YaccError('Unable to build parser') |
| 3318 |
|
| 3319 |
if not pinfo.error_func: |
| 3320 |
errorlog.warning('no p_error() function is defined') |
| 3321 |
|
| 3322 |
|
| 3323 |
grammar = Grammar(pinfo.tokens) |
| 3324 |
|
| 3325 |
|
| 3326 |
for term, assoc, level in pinfo.preclist: |
| 3327 |
try: |
| 3328 |
grammar.set_precedence(term, assoc, level) |
| 3329 |
except GrammarError as e: |
| 3330 |
errorlog.warning('%s', e) |
| 3331 |
|
| 3332 |
|
| 3333 |
for funcname, gram in pinfo.grammar: |
| 3334 |
file, line, prodname, syms = gram |
| 3335 |
try: |
| 3336 |
grammar.add_production(prodname, syms, funcname, file, line) |
| 3337 |
except GrammarError as e: |
| 3338 |
errorlog.error('%s', e) |
| 3339 |
errors = True |
| 3340 |
|
| 3341 |
|
| 3342 |
try: |
| 3343 |
if start is None: |
| 3344 |
grammar.set_start(pinfo.start) |
| 3345 |
else: |
| 3346 |
grammar.set_start(start) |
| 3347 |
except GrammarError as e: |
| 3348 |
errorlog.error(str(e)) |
| 3349 |
errors = True |
| 3350 |
|
| 3351 |
if errors: |
| 3352 |
raise YaccError('Unable to build parser') |
| 3353 |
|
| 3354 |
|
| 3355 |
undefined_symbols = grammar.undefined_symbols() |
| 3356 |
for sym, prod in undefined_symbols: |
| 3357 |
errorlog.error('%s:%d: Symbol %r used, but not defined as a token or a rule', prod.file, prod.line, sym) |
| 3358 |
errors = True |
| 3359 |
|
| 3360 |
unused_terminals = grammar.unused_terminals() |
| 3361 |
if unused_terminals: |
| 3362 |
debuglog.info('') |
| 3363 |
debuglog.info('Unused terminals:') |
| 3364 |
debuglog.info('') |
| 3365 |
for term in unused_terminals: |
| 3366 |
errorlog.warning('Token %r defined, but not used', term) |
| 3367 |
debuglog.info(' %s', term) |
| 3368 |
|
| 3369 |
|
| 3370 |
if debug: |
| 3371 |
debuglog.info('') |
| 3372 |
debuglog.info('Grammar') |
| 3373 |
debuglog.info('') |
| 3374 |
for n, p in enumerate(grammar.Productions): |
| 3375 |
debuglog.info('Rule %-5d %s', n, p) |
| 3376 |
|
| 3377 |
|
| 3378 |
unused_rules = grammar.unused_rules() |
| 3379 |
for prod in unused_rules: |
| 3380 |
errorlog.warning('%s:%d: Rule %r defined, but not used', prod.file, prod.line, prod.name) |
| 3381 |
|
| 3382 |
if len(unused_terminals) == 1: |
| 3383 |
errorlog.warning('There is 1 unused token') |
| 3384 |
if len(unused_terminals) > 1: |
| 3385 |
errorlog.warning('There are %d unused tokens', len(unused_terminals)) |
| 3386 |
|
| 3387 |
if len(unused_rules) == 1: |
| 3388 |
errorlog.warning('There is 1 unused rule') |
| 3389 |
if len(unused_rules) > 1: |
| 3390 |
errorlog.warning('There are %d unused rules', len(unused_rules)) |
| 3391 |
|
| 3392 |
if debug: |
| 3393 |
debuglog.info('') |
| 3394 |
debuglog.info('Terminals, with rules where they appear') |
| 3395 |
debuglog.info('') |
| 3396 |
terms = list(grammar.Terminals) |
| 3397 |
terms.sort() |
| 3398 |
for term in terms: |
| 3399 |
debuglog.info('%-20s : %s', term, ' '.join([str(s) for s in grammar.Terminals[term]])) |
| 3400 |
|
| 3401 |
debuglog.info('') |
| 3402 |
debuglog.info('Nonterminals, with rules where they appear') |
| 3403 |
debuglog.info('') |
| 3404 |
nonterms = list(grammar.Nonterminals) |
| 3405 |
nonterms.sort() |
| 3406 |
for nonterm in nonterms: |
| 3407 |
debuglog.info('%-20s : %s', nonterm, ' '.join([str(s) for s in grammar.Nonterminals[nonterm]])) |
| 3408 |
debuglog.info('') |
| 3409 |
|
| 3410 |
if check_recursion: |
| 3411 |
unreachable = grammar.find_unreachable() |
| 3412 |
for u in unreachable: |
| 3413 |
errorlog.warning('Symbol %r is unreachable', u) |
| 3414 |
|
| 3415 |
infinite = grammar.infinite_cycles() |
| 3416 |
for inf in infinite: |
| 3417 |
errorlog.error('Infinite recursion detected for symbol %r', inf) |
| 3418 |
errors = True |
| 3419 |
|
| 3420 |
unused_prec = grammar.unused_precedence() |
| 3421 |
for term, assoc in unused_prec: |
| 3422 |
errorlog.error('Precedence rule %r defined for unknown symbol %r', assoc, term) |
| 3423 |
errors = True |
| 3424 |
|
| 3425 |
if errors: |
| 3426 |
raise YaccError('Unable to build parser') |
| 3427 |
|
| 3428 |
|
| 3429 |
if debug: |
| 3430 |
errorlog.debug('Generating %s tables', method) |
| 3431 |
|
| 3432 |
lr = LRGeneratedTable(grammar, method, debuglog) |
| 3433 |
|
| 3434 |
if debug: |
| 3435 |
num_sr = len(lr.sr_conflicts) |
| 3436 |
|
| 3437 |
|
| 3438 |
if num_sr == 1: |
| 3439 |
errorlog.warning('1 shift/reduce conflict') |
| 3440 |
elif num_sr > 1: |
| 3441 |
errorlog.warning('%d shift/reduce conflicts', num_sr) |
| 3442 |
|
| 3443 |
num_rr = len(lr.rr_conflicts) |
| 3444 |
if num_rr == 1: |
| 3445 |
errorlog.warning('1 reduce/reduce conflict') |
| 3446 |
elif num_rr > 1: |
| 3447 |
errorlog.warning('%d reduce/reduce conflicts', num_rr) |
| 3448 |
|
| 3449 |
|
| 3450 |
if debug and (lr.sr_conflicts or lr.rr_conflicts): |
| 3451 |
debuglog.warning('') |
| 3452 |
debuglog.warning('Conflicts:') |
| 3453 |
debuglog.warning('') |
| 3454 |
|
| 3455 |
for state, tok, resolution in lr.sr_conflicts: |
| 3456 |
debuglog.warning('shift/reduce conflict for %s in state %d resolved as %s', tok, state, resolution) |
| 3457 |
|
| 3458 |
already_reported = set() |
| 3459 |
for state, rule, rejected in lr.rr_conflicts: |
| 3460 |
if (state, id(rule), id(rejected)) in already_reported: |
| 3461 |
continue |
| 3462 |
debuglog.warning('reduce/reduce conflict in state %d resolved using rule (%s)', state, rule) |
| 3463 |
debuglog.warning('rejected rule (%s) in state %d', rejected, state) |
| 3464 |
errorlog.warning('reduce/reduce conflict in state %d resolved using rule (%s)', state, rule) |
| 3465 |
errorlog.warning('rejected rule (%s) in state %d', rejected, state) |
| 3466 |
already_reported.add((state, id(rule), id(rejected))) |
| 3467 |
|
| 3468 |
warned_never = [] |
| 3469 |
for state, rule, rejected in lr.rr_conflicts: |
| 3470 |
if not rejected.reduced and (rejected not in warned_never): |
| 3471 |
debuglog.warning('Rule (%s) is never reduced', rejected) |
| 3472 |
errorlog.warning('Rule (%s) is never reduced', rejected) |
| 3473 |
warned_never.append(rejected) |
| 3474 |
|
| 3475 |
|
| 3476 |
if write_tables: |
| 3477 |
try: |
| 3478 |
lr.write_table(tabmodule, outputdir, signature) |
| 3479 |
except IOError as e: |
| 3480 |
errorlog.warning("Couldn't create %r. %s" % (tabmodule, e)) |
| 3481 |
|
| 3482 |
|
| 3483 |
if picklefile: |
| 3484 |
try: |
| 3485 |
lr.pickle_table(picklefile, signature) |
| 3486 |
except IOError as e: |
| 3487 |
errorlog.warning("Couldn't create %r. %s" % (picklefile, e)) |
| 3488 |
|
| 3489 |
|
| 3490 |
lr.bind_callables(pinfo.pdict) |
| 3491 |
parser = LRParser(lr, pinfo.error_func) |
| 3492 |
|
| 3493 |
parse = parser.parse |
| 3494 |
return parser |