| 1 |
from __future__ import print_function |
| 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 |
__version__ = "2.2" |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
yaccdebug = 1 |
| 64 |
|
| 65 |
|
| 66 |
debug_file = 'parser.out' |
| 67 |
tab_module = 'parsetab' |
| 68 |
default_lr = 'LALR' |
| 69 |
|
| 70 |
error_count = 3 |
| 71 |
|
| 72 |
import re, types, sys, cStringIO, md5, os.path |
| 73 |
|
| 74 |
|
| 75 |
class YaccError(Exception): pass |
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
class YaccSymbol: |
| 95 |
filename = '' |
| 96 |
def __str__(self): return self.type |
| 97 |
def __repr__(self): return str(self) |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
class YaccProduction: |
| 109 |
def __init__(self,s,stack=None): |
| 110 |
self.slice = s |
| 111 |
self.pbstack = [] |
| 112 |
self.stack = stack |
| 113 |
|
| 114 |
def __getitem__(self,n): |
| 115 |
if type(n) == types.IntType: |
| 116 |
if n >= 0: return self.slice[n].value |
| 117 |
else: return self.stack[n].value |
| 118 |
else: |
| 119 |
return [s.value for s in self.slice[n.start:n.stop:n.step]] |
| 120 |
|
| 121 |
def __setitem__(self,n,v): |
| 122 |
self.slice[n].value = v |
| 123 |
|
| 124 |
def __len__(self): |
| 125 |
return len(self.slice) |
| 126 |
|
| 127 |
def lineno(self,n): |
| 128 |
return getattr(self.slice[n],"lineno",0) |
| 129 |
|
| 130 |
def linespan(self,n): |
| 131 |
startline = getattr(self.slice[n],"lineno",0) |
| 132 |
endline = getattr(self.slice[n],"endlineno",startline) |
| 133 |
return startline,endline |
| 134 |
|
| 135 |
def lexpos(self,n): |
| 136 |
return getattr(self.slice[n],"lexpos",0) |
| 137 |
|
| 138 |
def lexspan(self,n): |
| 139 |
startpos = getattr(self.slice[n],"lexpos",0) |
| 140 |
endpos = getattr(self.slice[n],"endlexpos",startpos) |
| 141 |
return startpos,endpos |
| 142 |
|
| 143 |
def pushback(self,n): |
| 144 |
if n <= 0: |
| 145 |
raise ValueError("Expected a positive value") |
| 146 |
if n > (len(self.slice)-1): |
| 147 |
raise ValueError("Can't push %d tokens. Only %d are available." % (n,len(self.slice)-1)) |
| 148 |
for i in range(0,n): |
| 149 |
self.pbstack.append(self.slice[-i-1]) |
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
class Parser: |
| 157 |
|
| 158 |
def __init__(self): |
| 159 |
|
| 160 |
self.productions = None |
| 161 |
self.errorfunc = None |
| 162 |
self.action = { } |
| 163 |
self.goto = { } |
| 164 |
self.require = { } |
| 165 |
self.method = "Unknown LR" |
| 166 |
|
| 167 |
|
| 168 |
self.statestackstack = [] |
| 169 |
self.symstackstack = [] |
| 170 |
|
| 171 |
def errok(self): |
| 172 |
self.errorcount = 0 |
| 173 |
|
| 174 |
def restart(self): |
| 175 |
del self.statestack[:] |
| 176 |
del self.symstack[:] |
| 177 |
sym = YaccSymbol() |
| 178 |
sym.type = '$end' |
| 179 |
self.symstack.append(sym) |
| 180 |
self.statestack.append(0) |
| 181 |
|
| 182 |
def push_state(self): |
| 183 |
'''Save parser state and restart it.''' |
| 184 |
|
| 185 |
self.statestackstack.append(self.statestack[:]) |
| 186 |
self.symstackstack.append(self.symstack[:]) |
| 187 |
self.restart() |
| 188 |
|
| 189 |
def pop_state(self): |
| 190 |
'''Restore saved parser state.''' |
| 191 |
|
| 192 |
self.statestack[:] = self.statestackstack.pop() |
| 193 |
self.symstack[:] = self.symstackstack.pop() |
| 194 |
|
| 195 |
def parse(self,input=None,lexer=None,debug=0): |
| 196 |
lookahead = None |
| 197 |
lookaheadstack = [ ] |
| 198 |
actions = self.action |
| 199 |
goto = self.goto |
| 200 |
prod = self.productions |
| 201 |
pslice = YaccProduction(None) |
| 202 |
pslice.parser = self |
| 203 |
self.errorcount = 0 |
| 204 |
|
| 205 |
|
| 206 |
if not lexer: |
| 207 |
import lex |
| 208 |
lexer = lex.lexer |
| 209 |
|
| 210 |
pslice.lexer = lexer |
| 211 |
|
| 212 |
|
| 213 |
if input: |
| 214 |
lexer.input(input) |
| 215 |
|
| 216 |
|
| 217 |
get_token = lexer.token |
| 218 |
|
| 219 |
statestack = [ ] |
| 220 |
self.statestack = statestack |
| 221 |
symstack = [ ] |
| 222 |
self.symstack = symstack |
| 223 |
|
| 224 |
pslice.stack = symstack |
| 225 |
errtoken = None |
| 226 |
|
| 227 |
|
| 228 |
statestack.append(0) |
| 229 |
sym = YaccSymbol() |
| 230 |
sym.type = '$end' |
| 231 |
symstack.append(sym) |
| 232 |
|
| 233 |
while 1: |
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
if debug > 1: |
| 238 |
print('state', statestack[-1]) |
| 239 |
if not lookahead: |
| 240 |
if not lookaheadstack: |
| 241 |
lookahead = get_token() |
| 242 |
else: |
| 243 |
lookahead = lookaheadstack.pop() |
| 244 |
if not lookahead: |
| 245 |
lookahead = YaccSymbol() |
| 246 |
lookahead.type = '$end' |
| 247 |
if debug: |
| 248 |
errorlead = ("%s . %s" % (" ".join([xx.type for xx in symstack][1:]), str(lookahead))).lstrip() |
| 249 |
|
| 250 |
|
| 251 |
s = statestack[-1] |
| 252 |
ltype = lookahead.type |
| 253 |
t = actions.get((s,ltype),None) |
| 254 |
|
| 255 |
if debug > 1: |
| 256 |
print('action', t) |
| 257 |
if t is not None: |
| 258 |
if t > 0: |
| 259 |
|
| 260 |
if ltype == '$end': |
| 261 |
|
| 262 |
sys.stderr.write("yacc: Parse error. EOF\n") |
| 263 |
return |
| 264 |
statestack.append(t) |
| 265 |
if debug > 1: |
| 266 |
sys.stderr.write("%-60s shift state %s\n" % (errorlead, t)) |
| 267 |
symstack.append(lookahead) |
| 268 |
lookahead = None |
| 269 |
|
| 270 |
|
| 271 |
if self.errorcount > 0: |
| 272 |
self.errorcount -= 1 |
| 273 |
|
| 274 |
continue |
| 275 |
|
| 276 |
if t < 0: |
| 277 |
|
| 278 |
p = prod[-t] |
| 279 |
pname = p.name |
| 280 |
plen = p.len |
| 281 |
|
| 282 |
|
| 283 |
sym = YaccSymbol() |
| 284 |
sym.type = pname |
| 285 |
sym.value = None |
| 286 |
if debug > 1: |
| 287 |
sys.stderr.write("%-60s reduce %d\n" % (errorlead, -t)) |
| 288 |
|
| 289 |
if plen: |
| 290 |
targ = symstack[-plen-1:] |
| 291 |
targ[0] = sym |
| 292 |
try: |
| 293 |
sym.lineno = targ[1].lineno |
| 294 |
sym.filename = targ[1].filename |
| 295 |
sym.endlineno = getattr(targ[-1],"endlineno",targ[-1].lineno) |
| 296 |
sym.lexpos = targ[1].lexpos |
| 297 |
sym.endlexpos = getattr(targ[-1],"endlexpos",targ[-1].lexpos) |
| 298 |
except AttributeError: |
| 299 |
sym.lineno = 0 |
| 300 |
del symstack[-plen:] |
| 301 |
del statestack[-plen:] |
| 302 |
else: |
| 303 |
sym.lineno = 0 |
| 304 |
targ = [ sym ] |
| 305 |
pslice.slice = targ |
| 306 |
pslice.pbstack = [] |
| 307 |
|
| 308 |
p.func(pslice) |
| 309 |
|
| 310 |
|
| 311 |
if pslice.pbstack: |
| 312 |
lookaheadstack.append(lookahead) |
| 313 |
for _t in pslice.pbstack: |
| 314 |
lookaheadstack.append(_t) |
| 315 |
lookahead = None |
| 316 |
|
| 317 |
symstack.append(sym) |
| 318 |
statestack.append(goto[statestack[-1],pname]) |
| 319 |
continue |
| 320 |
|
| 321 |
if t == 0: |
| 322 |
n = symstack[-1] |
| 323 |
return getattr(n,"value",None) |
| 324 |
sys.stderr.write(errorlead, "\n") |
| 325 |
|
| 326 |
if t == None: |
| 327 |
if debug: |
| 328 |
sys.stderr.write(errorlead + "\n") |
| 329 |
|
| 330 |
|
| 331 |
|
| 332 |
|
| 333 |
|
| 334 |
|
| 335 |
|
| 336 |
|
| 337 |
|
| 338 |
|
| 339 |
if not self.errorcount: |
| 340 |
self.errorcount = error_count |
| 341 |
errtoken = lookahead |
| 342 |
if errtoken.type == '$end': |
| 343 |
errtoken = None |
| 344 |
if self.errorfunc: |
| 345 |
global errok,token,restart |
| 346 |
errok = self.errok |
| 347 |
token = get_token |
| 348 |
restart = self.restart |
| 349 |
tok = self.errorfunc(errtoken) |
| 350 |
del errok, token, restart |
| 351 |
|
| 352 |
if not self.errorcount: |
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
lookahead = tok |
| 357 |
errtoken = None |
| 358 |
continue |
| 359 |
else: |
| 360 |
if errtoken: |
| 361 |
if hasattr(errtoken,"lineno"): lineno = lookahead.lineno |
| 362 |
else: lineno = 0 |
| 363 |
if lineno: |
| 364 |
sys.stderr.write("yacc: Syntax error at line %d, token=%s\n" % (lineno, errtoken.type)) |
| 365 |
else: |
| 366 |
sys.stderr.write("yacc: Syntax error, token=%s" % errtoken.type) |
| 367 |
else: |
| 368 |
sys.stderr.write("yacc: Parse error in input. EOF\n") |
| 369 |
return |
| 370 |
|
| 371 |
else: |
| 372 |
self.errorcount = error_count |
| 373 |
|
| 374 |
|
| 375 |
|
| 376 |
|
| 377 |
|
| 378 |
if len(statestack) <= 1 and lookahead.type != '$end': |
| 379 |
lookahead = None |
| 380 |
errtoken = None |
| 381 |
|
| 382 |
del lookaheadstack[:] |
| 383 |
continue |
| 384 |
|
| 385 |
|
| 386 |
|
| 387 |
|
| 388 |
|
| 389 |
if lookahead.type == '$end': |
| 390 |
|
| 391 |
return |
| 392 |
|
| 393 |
if lookahead.type != 'error': |
| 394 |
sym = symstack[-1] |
| 395 |
if sym.type == 'error': |
| 396 |
|
| 397 |
|
| 398 |
lookahead = None |
| 399 |
continue |
| 400 |
t = YaccSymbol() |
| 401 |
t.type = 'error' |
| 402 |
if hasattr(lookahead,"lineno"): |
| 403 |
t.lineno = lookahead.lineno |
| 404 |
t.value = lookahead |
| 405 |
lookaheadstack.append(lookahead) |
| 406 |
lookahead = t |
| 407 |
else: |
| 408 |
symstack.pop() |
| 409 |
statestack.pop() |
| 410 |
|
| 411 |
continue |
| 412 |
|
| 413 |
|
| 414 |
raise RuntimeError("yacc: internal parser error!!!\n") |
| 415 |
|
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
|
| 432 |
|
| 433 |
|
| 434 |
|
| 435 |
|
| 436 |
|
| 437 |
|
| 438 |
def validate_file(filename): |
| 439 |
base,ext = os.path.splitext(filename) |
| 440 |
if ext != '.py': return 1 |
| 441 |
|
| 442 |
try: |
| 443 |
f = open(filename) |
| 444 |
lines = f.readlines() |
| 445 |
f.close() |
| 446 |
except IOError: |
| 447 |
return 1 |
| 448 |
|
| 449 |
|
| 450 |
fre = re.compile(r'\s*def\s+(p_[a-zA-Z_0-9]*)\(') |
| 451 |
counthash = { } |
| 452 |
linen = 1 |
| 453 |
noerror = 1 |
| 454 |
for l in lines: |
| 455 |
m = fre.match(l) |
| 456 |
if m: |
| 457 |
name = m.group(1) |
| 458 |
prev = counthash.get(name) |
| 459 |
if not prev: |
| 460 |
counthash[name] = linen |
| 461 |
else: |
| 462 |
sys.stderr.write("%s:%d: Function %s redefined. Previously defined on line %d\n" % (filename,linen,name,prev)) |
| 463 |
noerror = 0 |
| 464 |
linen += 1 |
| 465 |
return noerror |
| 466 |
|
| 467 |
|
| 468 |
def validate_dict(d): |
| 469 |
for n,v in d.items(): |
| 470 |
if n[0:2] == 'p_' and type(v) in (types.FunctionType, types.MethodType): continue |
| 471 |
if n[0:2] == 't_': continue |
| 472 |
|
| 473 |
if n[0:2] == 'p_': |
| 474 |
sys.stderr.write("yacc: Warning. '%s' not defined as a function\n" % n) |
| 475 |
if 1 and isinstance(v,types.FunctionType) and v.func_code.co_argcount == 1: |
| 476 |
try: |
| 477 |
doc = v.__doc__.split(" ") |
| 478 |
if doc[1] == ':': |
| 479 |
sys.stderr.write("%s:%d: Warning. Possible grammar rule '%s' defined without p_ prefix.\n" % (v.func_code.co_filename, v.func_code.co_firstlineno,n)) |
| 480 |
except Exception: |
| 481 |
pass |
| 482 |
|
| 483 |
|
| 484 |
|
| 485 |
|
| 486 |
|
| 487 |
|
| 488 |
|
| 489 |
|
| 490 |
|
| 491 |
def initialize_vars(): |
| 492 |
global Productions, Prodnames, Prodmap, Terminals |
| 493 |
global Nonterminals, First, Follow, Precedence, LRitems |
| 494 |
global Errorfunc, Signature, Requires |
| 495 |
|
| 496 |
Productions = [None] |
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
Prodnames = { } |
| 501 |
|
| 502 |
|
| 503 |
Prodmap = { } |
| 504 |
|
| 505 |
|
| 506 |
Terminals = { } |
| 507 |
|
| 508 |
|
| 509 |
Nonterminals = { } |
| 510 |
|
| 511 |
|
| 512 |
First = { } |
| 513 |
|
| 514 |
Follow = { } |
| 515 |
|
| 516 |
Precedence = { } |
| 517 |
|
| 518 |
|
| 519 |
LRitems = [ ] |
| 520 |
|
| 521 |
|
| 522 |
Errorfunc = None |
| 523 |
|
| 524 |
Signature = md5.new() |
| 525 |
|
| 526 |
|
| 527 |
|
| 528 |
Requires = { } |
| 529 |
|
| 530 |
|
| 531 |
global _vf, _vfc |
| 532 |
_vf = cStringIO.StringIO() |
| 533 |
_vfc = cStringIO.StringIO() |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
|
| 542 |
|
| 543 |
|
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
class Production: |
| 560 |
def __init__(self,**kw): |
| 561 |
for k,v in kw.items(): |
| 562 |
setattr(self,k,v) |
| 563 |
self.lr_index = -1 |
| 564 |
self.lr0_added = 0 |
| 565 |
self.lr1_added = 0 |
| 566 |
self.usyms = [ ] |
| 567 |
self.lookaheads = { } |
| 568 |
self.lk_added = { } |
| 569 |
self.setnumbers = [ ] |
| 570 |
|
| 571 |
def __str__(self): |
| 572 |
if self.prod: |
| 573 |
s = "%s -> %s" % (self.name," ".join(self.prod)) |
| 574 |
else: |
| 575 |
s = "%s -> <empty>" % self.name |
| 576 |
return s |
| 577 |
|
| 578 |
def __repr__(self): |
| 579 |
return str(self) |
| 580 |
|
| 581 |
|
| 582 |
def lr_item(self,n): |
| 583 |
if n > len(self.prod): return None |
| 584 |
p = Production() |
| 585 |
p.name = self.name |
| 586 |
p.prod = list(self.prod) |
| 587 |
p.number = self.number |
| 588 |
p.lr_index = n |
| 589 |
p.lookaheads = { } |
| 590 |
p.setnumbers = self.setnumbers |
| 591 |
p.prod.insert(n,".") |
| 592 |
p.prod = tuple(p.prod) |
| 593 |
p.len = len(p.prod) |
| 594 |
p.usyms = self.usyms |
| 595 |
|
| 596 |
|
| 597 |
try: |
| 598 |
p.lrafter = Prodnames[p.prod[n+1]] |
| 599 |
except (IndexError,KeyError) as e: |
| 600 |
p.lrafter = [] |
| 601 |
try: |
| 602 |
p.lrbefore = p.prod[n-1] |
| 603 |
except IndexError: |
| 604 |
p.lrbefore = None |
| 605 |
|
| 606 |
return p |
| 607 |
|
| 608 |
class MiniProduction: |
| 609 |
pass |
| 610 |
|
| 611 |
|
| 612 |
_is_identifier = re.compile(r'^[a-zA-Z0-9_-]+$') |
| 613 |
|
| 614 |
|
| 615 |
|
| 616 |
|
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
|
| 622 |
|
| 623 |
|
| 624 |
|
| 625 |
|
| 626 |
|
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
def add_production(f,file,line,prodname,syms): |
| 632 |
|
| 633 |
if Terminals.has_key(prodname): |
| 634 |
sys.stderr.write("%s:%d: Illegal rule name '%s'. Already defined as a token.\n" % (file,line,prodname)) |
| 635 |
return -1 |
| 636 |
if prodname == 'error': |
| 637 |
sys.stderr.write("%s:%d: Illegal rule name '%s'. error is a reserved word.\n" % (file,line,prodname)) |
| 638 |
return -1 |
| 639 |
|
| 640 |
if not _is_identifier.match(prodname): |
| 641 |
sys.stderr.write("%s:%d: Illegal rule name '%s'\n" % (file,line,prodname)) |
| 642 |
return -1 |
| 643 |
|
| 644 |
for x in range(len(syms)): |
| 645 |
s = syms[x] |
| 646 |
if s[0] in "'\"": |
| 647 |
try: |
| 648 |
c = eval(s) |
| 649 |
if (len(c) > 1): |
| 650 |
sys.stderr.write("%s:%d: Literal token %s in rule '%s' may only be a single character\n" % (file,line,s, prodname)) |
| 651 |
return -1 |
| 652 |
if not Terminals.has_key(c): |
| 653 |
Terminals[c] = [] |
| 654 |
syms[x] = c |
| 655 |
continue |
| 656 |
except SyntaxError: |
| 657 |
pass |
| 658 |
if not _is_identifier.match(s) and s != '%prec': |
| 659 |
sys.stderr.write("%s:%d: Illegal name '%s' in rule '%s'\n" % (file,line,s, prodname)) |
| 660 |
return -1 |
| 661 |
|
| 662 |
|
| 663 |
map = "%s -> %s" % (prodname,syms) |
| 664 |
if Prodmap.has_key(map): |
| 665 |
m = Prodmap[map] |
| 666 |
sys.stderr.write("%s:%d: Duplicate rule %s.\n" % (file,line, m)) |
| 667 |
sys.stderr.write("%s:%d: Previous definition at %s:%d\n" % (file,line, m.file, m.line)) |
| 668 |
return -1 |
| 669 |
|
| 670 |
p = Production() |
| 671 |
p.name = prodname |
| 672 |
p.prod = syms |
| 673 |
p.file = file |
| 674 |
p.line = line |
| 675 |
p.func = f |
| 676 |
p.number = len(Productions) |
| 677 |
|
| 678 |
|
| 679 |
Productions.append(p) |
| 680 |
Prodmap[map] = p |
| 681 |
if not Nonterminals.has_key(prodname): |
| 682 |
Nonterminals[prodname] = [ ] |
| 683 |
|
| 684 |
|
| 685 |
i = 0 |
| 686 |
while i < len(p.prod): |
| 687 |
t = p.prod[i] |
| 688 |
if t == '%prec': |
| 689 |
try: |
| 690 |
precname = p.prod[i+1] |
| 691 |
except IndexError: |
| 692 |
sys.stderr.write("%s:%d: Syntax error. Nothing follows %%prec.\n" % (p.file,p.line)) |
| 693 |
return -1 |
| 694 |
|
| 695 |
prec = Precedence.get(precname,None) |
| 696 |
if not prec: |
| 697 |
sys.stderr.write("%s:%d: Nothing known about the precedence of '%s'\n" % (p.file,p.line,precname)) |
| 698 |
return -1 |
| 699 |
else: |
| 700 |
p.prec = prec |
| 701 |
del p.prod[i] |
| 702 |
del p.prod[i] |
| 703 |
continue |
| 704 |
|
| 705 |
if Terminals.has_key(t): |
| 706 |
Terminals[t].append(p.number) |
| 707 |
|
| 708 |
if not hasattr(p,"prec"): |
| 709 |
p.prec = Precedence.get(t,('right',0)) |
| 710 |
else: |
| 711 |
if not Nonterminals.has_key(t): |
| 712 |
Nonterminals[t] = [ ] |
| 713 |
Nonterminals[t].append(p.number) |
| 714 |
i += 1 |
| 715 |
|
| 716 |
if not hasattr(p,"prec"): |
| 717 |
p.prec = ('right',0) |
| 718 |
|
| 719 |
|
| 720 |
p.len = len(p.prod) |
| 721 |
p.prod = tuple(p.prod) |
| 722 |
|
| 723 |
|
| 724 |
p.usyms = [ ] |
| 725 |
for s in p.prod: |
| 726 |
if s not in p.usyms: |
| 727 |
p.usyms.append(s) |
| 728 |
|
| 729 |
|
| 730 |
try: |
| 731 |
Prodnames[p.name].append(p) |
| 732 |
except KeyError: |
| 733 |
Prodnames[p.name] = [ p ] |
| 734 |
return 0 |
| 735 |
|
| 736 |
|
| 737 |
|
| 738 |
|
| 739 |
def add_function(f): |
| 740 |
line = f.func_code.co_firstlineno |
| 741 |
file = f.func_code.co_filename |
| 742 |
error = 0 |
| 743 |
|
| 744 |
if isinstance(f,types.MethodType): |
| 745 |
reqdargs = 2 |
| 746 |
else: |
| 747 |
reqdargs = 1 |
| 748 |
|
| 749 |
if f.func_code.co_argcount > reqdargs: |
| 750 |
sys.stderr.write("%s:%d: Rule '%s' has too many arguments.\n" % (file,line,f.__name__)) |
| 751 |
return -1 |
| 752 |
|
| 753 |
if f.func_code.co_argcount < reqdargs: |
| 754 |
sys.stderr.write("%s:%d: Rule '%s' requires an argument.\n" % (file,line,f.__name__)) |
| 755 |
return -1 |
| 756 |
|
| 757 |
if f.__doc__: |
| 758 |
|
| 759 |
pstrings = f.__doc__.splitlines() |
| 760 |
lastp = None |
| 761 |
dline = line |
| 762 |
for ps in pstrings: |
| 763 |
dline += 1 |
| 764 |
p = ps.split() |
| 765 |
if not p: continue |
| 766 |
try: |
| 767 |
if p[0] == '|': |
| 768 |
|
| 769 |
if not lastp: |
| 770 |
sys.stderr.write("%s:%d: Misplaced '|'.\n" % (file,dline)) |
| 771 |
return -1 |
| 772 |
prodname = lastp |
| 773 |
if len(p) > 1: |
| 774 |
syms = p[1:] |
| 775 |
else: |
| 776 |
syms = [ ] |
| 777 |
else: |
| 778 |
prodname = p[0] |
| 779 |
lastp = prodname |
| 780 |
assign = p[1] |
| 781 |
if len(p) > 2: |
| 782 |
syms = p[2:] |
| 783 |
else: |
| 784 |
syms = [ ] |
| 785 |
if assign != ':' and assign != '::=': |
| 786 |
sys.stderr.write("%s:%d: Syntax error. Expected ':'\n" % (file,dline)) |
| 787 |
return -1 |
| 788 |
|
| 789 |
|
| 790 |
e = add_production(f,file,dline,prodname,syms) |
| 791 |
error += e |
| 792 |
|
| 793 |
|
| 794 |
except Exception: |
| 795 |
sys.stderr.write("%s:%d: Syntax error in rule '%s'\n" % (file,dline,ps)) |
| 796 |
error -= 1 |
| 797 |
else: |
| 798 |
sys.stderr.write("%s:%d: No documentation string specified in function '%s'\n" % (file,line,f.__name__)) |
| 799 |
return error |
| 800 |
|
| 801 |
|
| 802 |
|
| 803 |
|
| 804 |
def compute_reachable(): |
| 805 |
''' |
| 806 |
Find each symbol that can be reached from the start symbol. |
| 807 |
Print a warning for any nonterminals that can't be reached. |
| 808 |
(Unused terminals have already had their warning.) |
| 809 |
''' |
| 810 |
Reachable = { } |
| 811 |
for s in Terminals.keys() + Nonterminals.keys(): |
| 812 |
Reachable[s] = 0 |
| 813 |
|
| 814 |
mark_reachable_from( Productions[0].prod[0], Reachable ) |
| 815 |
|
| 816 |
for s in Nonterminals.keys(): |
| 817 |
if not Reachable[s]: |
| 818 |
sys.stderr.write("yacc: Symbol '%s' is unreachable.\n" % s) |
| 819 |
|
| 820 |
def mark_reachable_from(s, Reachable): |
| 821 |
''' |
| 822 |
Mark all symbols that are reachable from symbol s. |
| 823 |
''' |
| 824 |
if Reachable[s]: |
| 825 |
|
| 826 |
return |
| 827 |
Reachable[s] = 1 |
| 828 |
for p in Prodnames.get(s,[]): |
| 829 |
for r in p.prod: |
| 830 |
mark_reachable_from(r, Reachable) |
| 831 |
|
| 832 |
|
| 833 |
|
| 834 |
|
| 835 |
|
| 836 |
|
| 837 |
|
| 838 |
|
| 839 |
def compute_terminates(): |
| 840 |
''' |
| 841 |
Raise an error for any symbols that don't terminate. |
| 842 |
''' |
| 843 |
Terminates = {} |
| 844 |
|
| 845 |
|
| 846 |
for t in Terminals.keys(): |
| 847 |
Terminates[t] = 1 |
| 848 |
|
| 849 |
Terminates['$end'] = 1 |
| 850 |
|
| 851 |
|
| 852 |
|
| 853 |
|
| 854 |
for n in Nonterminals.keys(): |
| 855 |
Terminates[n] = 0 |
| 856 |
|
| 857 |
|
| 858 |
while 1: |
| 859 |
some_change = 0 |
| 860 |
for (n,pl) in Prodnames.items(): |
| 861 |
|
| 862 |
for p in pl: |
| 863 |
|
| 864 |
for s in p.prod: |
| 865 |
if not Terminates[s]: |
| 866 |
|
| 867 |
|
| 868 |
p_terminates = 0 |
| 869 |
break |
| 870 |
else: |
| 871 |
|
| 872 |
|
| 873 |
|
| 874 |
p_terminates = 1 |
| 875 |
|
| 876 |
if p_terminates: |
| 877 |
|
| 878 |
if not Terminates[n]: |
| 879 |
Terminates[n] = 1 |
| 880 |
some_change = 1 |
| 881 |
|
| 882 |
break |
| 883 |
|
| 884 |
if not some_change: |
| 885 |
break |
| 886 |
|
| 887 |
some_error = 0 |
| 888 |
for (s,terminates) in Terminates.items(): |
| 889 |
if not terminates: |
| 890 |
if not Prodnames.has_key(s) and not Terminals.has_key(s) and s != 'error': |
| 891 |
|
| 892 |
|
| 893 |
pass |
| 894 |
else: |
| 895 |
sys.stderr.write("yacc: Infinite recursion detected for symbol '%s'.\n" % s) |
| 896 |
some_error = 1 |
| 897 |
|
| 898 |
return some_error |
| 899 |
|
| 900 |
|
| 901 |
|
| 902 |
|
| 903 |
|
| 904 |
|
| 905 |
def verify_productions(cycle_check=1): |
| 906 |
error = 0 |
| 907 |
for p in Productions: |
| 908 |
if not p: continue |
| 909 |
|
| 910 |
for s in p.prod: |
| 911 |
if not Prodnames.has_key(s) and not Terminals.has_key(s) and s != 'error': |
| 912 |
sys.stderr.write("%s:%d: Symbol '%s' used, but not defined as a token or a rule.\n" % (p.file,p.line,s)) |
| 913 |
error = 1 |
| 914 |
continue |
| 915 |
|
| 916 |
unused_tok = 0 |
| 917 |
|
| 918 |
if yaccdebug: |
| 919 |
_vf.write("Unused terminals:\n\n") |
| 920 |
for s,v in Terminals.items(): |
| 921 |
if s != 'error' and not v: |
| 922 |
sys.stderr.write("yacc: Warning. Token '%s' defined, but not used.\n" % s) |
| 923 |
if yaccdebug: _vf.write(" %s\n"% s) |
| 924 |
unused_tok += 1 |
| 925 |
|
| 926 |
|
| 927 |
if yaccdebug: |
| 928 |
_vf.write("\nGrammar\n\n") |
| 929 |
for i in range(1,len(Productions)): |
| 930 |
_vf.write("Rule %-5d %s\n" % (i, Productions[i])) |
| 931 |
|
| 932 |
unused_prod = 0 |
| 933 |
|
| 934 |
for s,v in Nonterminals.items(): |
| 935 |
if not v: |
| 936 |
p = Prodnames[s][0] |
| 937 |
sys.stderr.write("%s:%d: Warning. Rule '%s' defined, but not used.\n" % (p.file,p.line, s)) |
| 938 |
unused_prod += 1 |
| 939 |
|
| 940 |
|
| 941 |
if unused_tok == 1: |
| 942 |
sys.stderr.write("yacc: Warning. There is 1 unused token.\n") |
| 943 |
if unused_tok > 1: |
| 944 |
sys.stderr.write("yacc: Warning. There are %d unused tokens.\n" % unused_tok) |
| 945 |
|
| 946 |
if unused_prod == 1: |
| 947 |
sys.stderr.write("yacc: Warning. There is 1 unused rule.\n") |
| 948 |
if unused_prod > 1: |
| 949 |
sys.stderr.write("yacc: Warning. There are %d unused rules.\n" % unused_prod) |
| 950 |
|
| 951 |
if yaccdebug: |
| 952 |
_vf.write("\nTerminals, with rules where they appear\n\n") |
| 953 |
ks = Terminals.keys() |
| 954 |
ks.sort() |
| 955 |
for k in ks: |
| 956 |
_vf.write("%-20s : %s\n" % (k, " ".join([str(s) for s in Terminals[k]]))) |
| 957 |
_vf.write("\nNonterminals, with rules where they appear\n\n") |
| 958 |
ks = Nonterminals.keys() |
| 959 |
ks.sort() |
| 960 |
for k in ks: |
| 961 |
_vf.write("%-20s : %s\n" % (k, " ".join([str(s) for s in Nonterminals[k]]))) |
| 962 |
|
| 963 |
if (cycle_check): |
| 964 |
compute_reachable() |
| 965 |
error += compute_terminates() |
| 966 |
|
| 967 |
return error |
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
|
| 981 |
|
| 982 |
|
| 983 |
|
| 984 |
def build_lritems(): |
| 985 |
for p in Productions: |
| 986 |
lastlri = p |
| 987 |
lri = p.lr_item(0) |
| 988 |
i = 0 |
| 989 |
while 1: |
| 990 |
lri = p.lr_item(i) |
| 991 |
lastlri.lr_next = lri |
| 992 |
if not lri: break |
| 993 |
lri.lr_num = len(LRitems) |
| 994 |
LRitems.append(lri) |
| 995 |
lastlri = lri |
| 996 |
i += 1 |
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
|
| 1007 |
|
| 1008 |
|
| 1009 |
def add_precedence(plist): |
| 1010 |
plevel = 0 |
| 1011 |
error = 0 |
| 1012 |
for p in plist: |
| 1013 |
plevel += 1 |
| 1014 |
try: |
| 1015 |
prec = p[0] |
| 1016 |
terms = p[1:] |
| 1017 |
if prec != 'left' and prec != 'right' and prec != 'nonassoc': |
| 1018 |
sys.stderr.write("yacc: Invalid precedence '%s'\n" % prec) |
| 1019 |
return -1 |
| 1020 |
for t in terms: |
| 1021 |
if Precedence.has_key(t): |
| 1022 |
sys.stderr.write("yacc: Precedence already specified for terminal '%s'\n" % t) |
| 1023 |
error += 1 |
| 1024 |
continue |
| 1025 |
Precedence[t] = (prec,plevel) |
| 1026 |
except: |
| 1027 |
sys.stderr.write("yacc: Invalid precedence table.\n") |
| 1028 |
error += 1 |
| 1029 |
|
| 1030 |
return error |
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
def augment_grammar(start=None): |
| 1040 |
if not start: |
| 1041 |
start = Productions[1].name |
| 1042 |
Productions[0] = Production(name="S'",prod=[start],number=0,len=1,prec=('right',0),func=None) |
| 1043 |
Productions[0].usyms = [ start ] |
| 1044 |
Nonterminals[start].append(0) |
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
def first(beta): |
| 1056 |
|
| 1057 |
|
| 1058 |
result = [ ] |
| 1059 |
for x in beta: |
| 1060 |
x_produces_empty = 0 |
| 1061 |
|
| 1062 |
|
| 1063 |
for f in First[x]: |
| 1064 |
if f == '<empty>': |
| 1065 |
x_produces_empty = 1 |
| 1066 |
else: |
| 1067 |
if f not in result: result.append(f) |
| 1068 |
|
| 1069 |
if x_produces_empty: |
| 1070 |
|
| 1071 |
|
| 1072 |
pass |
| 1073 |
else: |
| 1074 |
|
| 1075 |
break |
| 1076 |
else: |
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
result.append('<empty>') |
| 1081 |
|
| 1082 |
return result |
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
|
| 1087 |
|
| 1088 |
|
| 1089 |
def compute_follow(start=None): |
| 1090 |
|
| 1091 |
for k in Nonterminals.keys(): |
| 1092 |
Follow[k] = [ ] |
| 1093 |
|
| 1094 |
if not start: |
| 1095 |
start = Productions[1].name |
| 1096 |
|
| 1097 |
Follow[start] = [ '$end' ] |
| 1098 |
|
| 1099 |
while 1: |
| 1100 |
didadd = 0 |
| 1101 |
for p in Productions[1:]: |
| 1102 |
|
| 1103 |
for i in range(len(p.prod)): |
| 1104 |
B = p.prod[i] |
| 1105 |
if Nonterminals.has_key(B): |
| 1106 |
|
| 1107 |
fst = first(p.prod[i+1:]) |
| 1108 |
hasempty = 0 |
| 1109 |
for f in fst: |
| 1110 |
if f != '<empty>' and f not in Follow[B]: |
| 1111 |
Follow[B].append(f) |
| 1112 |
didadd = 1 |
| 1113 |
if f == '<empty>': |
| 1114 |
hasempty = 1 |
| 1115 |
if hasempty or i == (len(p.prod)-1): |
| 1116 |
|
| 1117 |
for f in Follow[p.name]: |
| 1118 |
if f not in Follow[B]: |
| 1119 |
Follow[B].append(f) |
| 1120 |
didadd = 1 |
| 1121 |
if not didadd: break |
| 1122 |
|
| 1123 |
if 0 and yaccdebug: |
| 1124 |
_vf.write('\nFollow:\n') |
| 1125 |
for k in Nonterminals.keys(): |
| 1126 |
_vf.write("%-20s : %s\n" % (k, " ".join([str(s) for s in Follow[k]]))) |
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
def compute_first1(): |
| 1134 |
|
| 1135 |
|
| 1136 |
for t in Terminals.keys(): |
| 1137 |
First[t] = [t] |
| 1138 |
|
| 1139 |
First['$end'] = ['$end'] |
| 1140 |
First['#'] = ['#'] |
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
for n in Nonterminals.keys(): |
| 1146 |
First[n] = [] |
| 1147 |
|
| 1148 |
|
| 1149 |
while 1: |
| 1150 |
some_change = 0 |
| 1151 |
for n in Nonterminals.keys(): |
| 1152 |
for p in Prodnames[n]: |
| 1153 |
for f in first(p.prod): |
| 1154 |
if f not in First[n]: |
| 1155 |
First[n].append( f ) |
| 1156 |
some_change = 1 |
| 1157 |
if not some_change: |
| 1158 |
break |
| 1159 |
|
| 1160 |
if 0 and yaccdebug: |
| 1161 |
_vf.write('\nFirst:\n') |
| 1162 |
for k in Nonterminals.keys(): |
| 1163 |
_vf.write("%-20s : %s\n" % |
| 1164 |
(k, " ".join([str(s) for s in First[k]]))) |
| 1165 |
|
| 1166 |
|
| 1167 |
|
| 1168 |
|
| 1169 |
|
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
def lr_init_vars(): |
| 1175 |
global _lr_action, _lr_goto, _lr_method |
| 1176 |
global _lr_goto_cache, _lr0_cidhash |
| 1177 |
|
| 1178 |
_lr_action = { } |
| 1179 |
_lr_goto = { } |
| 1180 |
_lr_method = "Unknown" |
| 1181 |
_lr_goto_cache = { } |
| 1182 |
_lr0_cidhash = { } |
| 1183 |
|
| 1184 |
|
| 1185 |
|
| 1186 |
|
| 1187 |
|
| 1188 |
_add_count = 0 |
| 1189 |
|
| 1190 |
def lr0_closure(I): |
| 1191 |
global _add_count |
| 1192 |
|
| 1193 |
_add_count += 1 |
| 1194 |
prodlist = Productions |
| 1195 |
|
| 1196 |
|
| 1197 |
J = I[:] |
| 1198 |
didadd = 1 |
| 1199 |
while didadd: |
| 1200 |
didadd = 0 |
| 1201 |
for j in J: |
| 1202 |
for x in j.lrafter: |
| 1203 |
if x.lr0_added == _add_count: continue |
| 1204 |
|
| 1205 |
J.append(x.lr_next) |
| 1206 |
x.lr0_added = _add_count |
| 1207 |
didadd = 1 |
| 1208 |
|
| 1209 |
return J |
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
|
| 1215 |
|
| 1216 |
|
| 1217 |
|
| 1218 |
def lr0_goto(I,x): |
| 1219 |
|
| 1220 |
g = _lr_goto_cache.get((id(I),x),None) |
| 1221 |
if g: return g |
| 1222 |
|
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
s = _lr_goto_cache.get(x,None) |
| 1227 |
if not s: |
| 1228 |
s = { } |
| 1229 |
_lr_goto_cache[x] = s |
| 1230 |
|
| 1231 |
gs = [ ] |
| 1232 |
for p in I: |
| 1233 |
n = p.lr_next |
| 1234 |
if n and n.lrbefore == x: |
| 1235 |
s1 = s.get(id(n),None) |
| 1236 |
if not s1: |
| 1237 |
s1 = { } |
| 1238 |
s[id(n)] = s1 |
| 1239 |
gs.append(n) |
| 1240 |
s = s1 |
| 1241 |
g = s.get('$end',None) |
| 1242 |
if not g: |
| 1243 |
if gs: |
| 1244 |
g = lr0_closure(gs) |
| 1245 |
s['$end'] = g |
| 1246 |
else: |
| 1247 |
s['$end'] = gs |
| 1248 |
_lr_goto_cache[(id(I),x)] = g |
| 1249 |
return g |
| 1250 |
|
| 1251 |
_lr0_cidhash = { } |
| 1252 |
|
| 1253 |
|
| 1254 |
def lr0_items(): |
| 1255 |
|
| 1256 |
C = [ lr0_closure([Productions[0].lr_next]) ] |
| 1257 |
i = 0 |
| 1258 |
for I in C: |
| 1259 |
_lr0_cidhash[id(I)] = i |
| 1260 |
i += 1 |
| 1261 |
|
| 1262 |
|
| 1263 |
i = 0 |
| 1264 |
while i < len(C): |
| 1265 |
I = C[i] |
| 1266 |
i += 1 |
| 1267 |
|
| 1268 |
|
| 1269 |
asyms = { } |
| 1270 |
for ii in I: |
| 1271 |
for s in ii.usyms: |
| 1272 |
asyms[s] = None |
| 1273 |
|
| 1274 |
for x in asyms.keys(): |
| 1275 |
g = lr0_goto(I,x) |
| 1276 |
if not g: continue |
| 1277 |
if _lr0_cidhash.has_key(id(g)): continue |
| 1278 |
_lr0_cidhash[id(g)] = len(C) |
| 1279 |
C.append(g) |
| 1280 |
|
| 1281 |
return C |
| 1282 |
|
| 1283 |
|
| 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 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
def compute_nullable_nonterminals(): |
| 1315 |
nullable = {} |
| 1316 |
num_nullable = 0 |
| 1317 |
while 1: |
| 1318 |
for p in Productions[1:]: |
| 1319 |
if p.len == 0: |
| 1320 |
nullable[p.name] = 1 |
| 1321 |
continue |
| 1322 |
for t in p.prod: |
| 1323 |
if not nullable.has_key(t): break |
| 1324 |
else: |
| 1325 |
nullable[p.name] = 1 |
| 1326 |
if len(nullable) == num_nullable: break |
| 1327 |
num_nullable = len(nullable) |
| 1328 |
return nullable |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
|
| 1338 |
|
| 1339 |
|
| 1340 |
|
| 1341 |
def find_nonterminal_transitions(C): |
| 1342 |
trans = [] |
| 1343 |
for state in range(len(C)): |
| 1344 |
for p in C[state]: |
| 1345 |
if p.lr_index < p.len - 1: |
| 1346 |
t = (state,p.prod[p.lr_index+1]) |
| 1347 |
if Nonterminals.has_key(t[1]): |
| 1348 |
if t not in trans: trans.append(t) |
| 1349 |
state = state + 1 |
| 1350 |
return trans |
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
def dr_relation(C,trans,nullable): |
| 1362 |
dr_set = { } |
| 1363 |
state,N = trans |
| 1364 |
terms = [] |
| 1365 |
|
| 1366 |
g = lr0_goto(C[state],N) |
| 1367 |
for p in g: |
| 1368 |
if p.lr_index < p.len - 1: |
| 1369 |
a = p.prod[p.lr_index+1] |
| 1370 |
if Terminals.has_key(a): |
| 1371 |
if a not in terms: terms.append(a) |
| 1372 |
|
| 1373 |
|
| 1374 |
if state == 0 and N == Productions[0].prod[0]: |
| 1375 |
terms.append('$end') |
| 1376 |
|
| 1377 |
return terms |
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
def reads_relation(C, trans, empty): |
| 1386 |
|
| 1387 |
rel = [] |
| 1388 |
state, N = trans |
| 1389 |
|
| 1390 |
g = lr0_goto(C[state],N) |
| 1391 |
j = _lr0_cidhash.get(id(g),-1) |
| 1392 |
for p in g: |
| 1393 |
if p.lr_index < p.len - 1: |
| 1394 |
a = p.prod[p.lr_index + 1] |
| 1395 |
if empty.has_key(a): |
| 1396 |
rel.append((j,a)) |
| 1397 |
|
| 1398 |
return rel |
| 1399 |
|
| 1400 |
|
| 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 |
|
| 1428 |
def compute_lookback_includes(C,trans,nullable): |
| 1429 |
|
| 1430 |
lookdict = {} |
| 1431 |
includedict = {} |
| 1432 |
|
| 1433 |
|
| 1434 |
dtrans = {} |
| 1435 |
for t in trans: |
| 1436 |
dtrans[t] = 1 |
| 1437 |
|
| 1438 |
|
| 1439 |
for state,N in trans: |
| 1440 |
lookb = [] |
| 1441 |
includes = [] |
| 1442 |
for p in C[state]: |
| 1443 |
if p.name != N: continue |
| 1444 |
|
| 1445 |
|
| 1446 |
|
| 1447 |
|
| 1448 |
lr_index = p.lr_index |
| 1449 |
j = state |
| 1450 |
while lr_index < p.len - 1: |
| 1451 |
lr_index = lr_index + 1 |
| 1452 |
t = p.prod[lr_index] |
| 1453 |
|
| 1454 |
|
| 1455 |
if dtrans.has_key((j,t)): |
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
li = lr_index + 1 |
| 1461 |
while li < p.len: |
| 1462 |
if Terminals.has_key(p.prod[li]): break |
| 1463 |
if not nullable.has_key(p.prod[li]): break |
| 1464 |
li = li + 1 |
| 1465 |
else: |
| 1466 |
|
| 1467 |
includes.append((j,t)) |
| 1468 |
|
| 1469 |
g = lr0_goto(C[j],t) |
| 1470 |
j = _lr0_cidhash.get(id(g),-1) |
| 1471 |
|
| 1472 |
|
| 1473 |
for r in C[j]: |
| 1474 |
if r.name != p.name: continue |
| 1475 |
if r.len != p.len: continue |
| 1476 |
i = 0 |
| 1477 |
|
| 1478 |
while i < r.lr_index: |
| 1479 |
if r.prod[i] != p.prod[i+1]: break |
| 1480 |
i = i + 1 |
| 1481 |
else: |
| 1482 |
lookb.append((j,r)) |
| 1483 |
for i in includes: |
| 1484 |
if not includedict.has_key(i): includedict[i] = [] |
| 1485 |
includedict[i].append((state,N)) |
| 1486 |
lookdict[(state,N)] = lookb |
| 1487 |
|
| 1488 |
return lookdict,includedict |
| 1489 |
|
| 1490 |
|
| 1491 |
|
| 1492 |
|
| 1493 |
|
| 1494 |
|
| 1495 |
|
| 1496 |
|
| 1497 |
|
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
|
| 1505 |
|
| 1506 |
|
| 1507 |
def digraph(X,R,FP): |
| 1508 |
N = { } |
| 1509 |
for x in X: |
| 1510 |
N[x] = 0 |
| 1511 |
stack = [] |
| 1512 |
F = { } |
| 1513 |
for x in X: |
| 1514 |
if N[x] == 0: traverse(x,N,stack,F,X,R,FP) |
| 1515 |
return F |
| 1516 |
|
| 1517 |
def traverse(x,N,stack,F,X,R,FP): |
| 1518 |
stack.append(x) |
| 1519 |
d = len(stack) |
| 1520 |
N[x] = d |
| 1521 |
F[x] = FP(x) |
| 1522 |
|
| 1523 |
rel = R(x) |
| 1524 |
for y in rel: |
| 1525 |
if N[y] == 0: |
| 1526 |
traverse(y,N,stack,F,X,R,FP) |
| 1527 |
N[x] = min(N[x],N[y]) |
| 1528 |
for a in F.get(y,[]): |
| 1529 |
if a not in F[x]: F[x].append(a) |
| 1530 |
if N[x] == d: |
| 1531 |
N[stack[-1]] = sys.maxint |
| 1532 |
F[stack[-1]] = F[x] |
| 1533 |
element = stack.pop() |
| 1534 |
while element != x: |
| 1535 |
N[stack[-1]] = sys.maxint |
| 1536 |
F[stack[-1]] = F[x] |
| 1537 |
element = stack.pop() |
| 1538 |
|
| 1539 |
|
| 1540 |
|
| 1541 |
|
| 1542 |
|
| 1543 |
|
| 1544 |
|
| 1545 |
|
| 1546 |
|
| 1547 |
|
| 1548 |
|
| 1549 |
|
| 1550 |
|
| 1551 |
def compute_read_sets(C, ntrans, nullable): |
| 1552 |
FP = lambda x: dr_relation(C,x,nullable) |
| 1553 |
R = lambda x: reads_relation(C,x,nullable) |
| 1554 |
F = digraph(ntrans,R,FP) |
| 1555 |
return F |
| 1556 |
|
| 1557 |
|
| 1558 |
|
| 1559 |
|
| 1560 |
|
| 1561 |
|
| 1562 |
|
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
|
| 1567 |
|
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
|
| 1572 |
|
| 1573 |
def compute_follow_sets(ntrans,readsets,inclsets): |
| 1574 |
FP = lambda x: readsets[x] |
| 1575 |
R = lambda x: inclsets.get(x,[]) |
| 1576 |
F = digraph(ntrans,R,FP) |
| 1577 |
return F |
| 1578 |
|
| 1579 |
|
| 1580 |
|
| 1581 |
|
| 1582 |
|
| 1583 |
|
| 1584 |
|
| 1585 |
|
| 1586 |
|
| 1587 |
|
| 1588 |
|
| 1589 |
|
| 1590 |
|
| 1591 |
def add_lookaheads(lookbacks,followset): |
| 1592 |
for trans,lb in lookbacks.items(): |
| 1593 |
|
| 1594 |
for state,p in lb: |
| 1595 |
if not p.lookaheads.has_key(state): |
| 1596 |
p.lookaheads[state] = [] |
| 1597 |
f = followset.get(trans,[]) |
| 1598 |
for a in f: |
| 1599 |
if a not in p.lookaheads[state]: p.lookaheads[state].append(a) |
| 1600 |
|
| 1601 |
|
| 1602 |
|
| 1603 |
|
| 1604 |
|
| 1605 |
|
| 1606 |
|
| 1607 |
|
| 1608 |
def add_lalr_lookaheads(C): |
| 1609 |
|
| 1610 |
nullable = compute_nullable_nonterminals() |
| 1611 |
|
| 1612 |
|
| 1613 |
trans = find_nonterminal_transitions(C) |
| 1614 |
|
| 1615 |
|
| 1616 |
readsets = compute_read_sets(C,trans,nullable) |
| 1617 |
|
| 1618 |
|
| 1619 |
lookd, included = compute_lookback_includes(C,trans,nullable) |
| 1620 |
|
| 1621 |
|
| 1622 |
followsets = compute_follow_sets(trans,readsets,included) |
| 1623 |
|
| 1624 |
|
| 1625 |
add_lookaheads(lookd,followsets) |
| 1626 |
|
| 1627 |
|
| 1628 |
|
| 1629 |
|
| 1630 |
|
| 1631 |
|
| 1632 |
def lr_parse_table(method): |
| 1633 |
global _lr_method |
| 1634 |
goto = _lr_goto |
| 1635 |
action = _lr_action |
| 1636 |
actionp = { } |
| 1637 |
|
| 1638 |
_lr_method = method |
| 1639 |
|
| 1640 |
n_srconflict = 0 |
| 1641 |
n_rrconflict = 0 |
| 1642 |
|
| 1643 |
if yaccdebug: |
| 1644 |
sys.stderr.write("yacc: Generating %s parsing table...\n" % method) |
| 1645 |
_vf.write("\n\nParsing method: %s\n\n" % method) |
| 1646 |
|
| 1647 |
|
| 1648 |
|
| 1649 |
|
| 1650 |
C = lr0_items() |
| 1651 |
|
| 1652 |
if method == 'LALR': |
| 1653 |
add_lalr_lookaheads(C) |
| 1654 |
|
| 1655 |
|
| 1656 |
st = 0 |
| 1657 |
for I in C: |
| 1658 |
|
| 1659 |
actlist = [ ] |
| 1660 |
|
| 1661 |
if yaccdebug: |
| 1662 |
_vf.write("\nstate %d\n\n" % st) |
| 1663 |
for p in I: |
| 1664 |
_vf.write(" (%d) %s\n" % (p.number, str(p))) |
| 1665 |
_vf.write("\n") |
| 1666 |
|
| 1667 |
for p in I: |
| 1668 |
try: |
| 1669 |
if p.prod[-1] == ".": |
| 1670 |
if p.name == "S'": |
| 1671 |
|
| 1672 |
action[st,"$end"] = 0 |
| 1673 |
actionp[st,"$end"] = p |
| 1674 |
else: |
| 1675 |
|
| 1676 |
if method == 'LALR': |
| 1677 |
laheads = p.lookaheads[st] |
| 1678 |
else: |
| 1679 |
laheads = Follow[p.name] |
| 1680 |
for a in laheads: |
| 1681 |
actlist.append((a,p,"reduce using rule %d (%s)" % (p.number,p))) |
| 1682 |
r = action.get((st,a),None) |
| 1683 |
if r is not None: |
| 1684 |
|
| 1685 |
if r > 0: |
| 1686 |
|
| 1687 |
|
| 1688 |
|
| 1689 |
sprec,slevel = Productions[actionp[st,a].number].prec |
| 1690 |
rprec,rlevel = Precedence.get(a,('right',0)) |
| 1691 |
if (slevel < rlevel) or ((slevel == rlevel) and (rprec == 'left')): |
| 1692 |
|
| 1693 |
action[st,a] = -p.number |
| 1694 |
actionp[st,a] = p |
| 1695 |
if not slevel and not rlevel: |
| 1696 |
_vfc.write("shift/reduce conflict in state %d resolved as reduce.\n" % st) |
| 1697 |
_vf.write(" ! shift/reduce conflict for %s resolved as reduce.\n" % a) |
| 1698 |
n_srconflict += 1 |
| 1699 |
elif (slevel == rlevel) and (rprec == 'nonassoc'): |
| 1700 |
action[st,a] = None |
| 1701 |
else: |
| 1702 |
|
| 1703 |
if not rlevel: |
| 1704 |
_vfc.write("shift/reduce conflict in state %d resolved as shift.\n" % st) |
| 1705 |
_vf.write(" ! shift/reduce conflict for %s resolved as shift.\n" % a) |
| 1706 |
n_srconflict +=1 |
| 1707 |
elif r < 0: |
| 1708 |
|
| 1709 |
|
| 1710 |
oldp = Productions[-r] |
| 1711 |
pp = Productions[p.number] |
| 1712 |
if oldp.line > pp.line: |
| 1713 |
action[st,a] = -p.number |
| 1714 |
actionp[st,a] = p |
| 1715 |
|
| 1716 |
n_rrconflict += 1 |
| 1717 |
_vfc.write("reduce/reduce conflict in state %d resolved using rule %d (%s).\n" % (st, actionp[st,a].number, actionp[st,a])) |
| 1718 |
_vf.write(" ! reduce/reduce conflict for %s resolved using rule %d (%s).\n" % (a,actionp[st,a].number, actionp[st,a])) |
| 1719 |
else: |
| 1720 |
sys.stderr.write("Unknown conflict in state %d\n" % st) |
| 1721 |
else: |
| 1722 |
action[st,a] = -p.number |
| 1723 |
actionp[st,a] = p |
| 1724 |
else: |
| 1725 |
i = p.lr_index |
| 1726 |
a = p.prod[i+1] |
| 1727 |
if Terminals.has_key(a): |
| 1728 |
g = lr0_goto(I,a) |
| 1729 |
j = _lr0_cidhash.get(id(g),-1) |
| 1730 |
if j >= 0: |
| 1731 |
|
| 1732 |
actlist.append((a,p,"shift and go to state %d" % j)) |
| 1733 |
r = action.get((st,a),None) |
| 1734 |
if r is not None: |
| 1735 |
|
| 1736 |
if r > 0: |
| 1737 |
if r != j: |
| 1738 |
sys.stderr.write("Shift/shift conflict in state %d\n" % st) |
| 1739 |
elif r < 0: |
| 1740 |
|
| 1741 |
|
| 1742 |
|
| 1743 |
|
| 1744 |
rprec,rlevel = Productions[actionp[st,a].number].prec |
| 1745 |
sprec,slevel = Precedence.get(a,('right',0)) |
| 1746 |
if (slevel > rlevel) or ((slevel == rlevel) and (rprec != 'left')): |
| 1747 |
|
| 1748 |
action[st,a] = j |
| 1749 |
actionp[st,a] = p |
| 1750 |
if not rlevel: |
| 1751 |
n_srconflict += 1 |
| 1752 |
_vfc.write("shift/reduce conflict in state %d resolved as shift.\n" % st) |
| 1753 |
_vf.write(" ! shift/reduce conflict for %s resolved as shift.\n" % a) |
| 1754 |
elif (slevel == rlevel) and (rprec == 'nonassoc'): |
| 1755 |
action[st,a] = None |
| 1756 |
else: |
| 1757 |
|
| 1758 |
if not slevel and not rlevel: |
| 1759 |
n_srconflict +=1 |
| 1760 |
_vfc.write("shift/reduce conflict in state %d resolved as reduce.\n" % st) |
| 1761 |
_vf.write(" ! shift/reduce conflict for %s resolved as reduce.\n" % a) |
| 1762 |
|
| 1763 |
else: |
| 1764 |
sys.stderr.write("Unknown conflict in state %d\n" % st) |
| 1765 |
else: |
| 1766 |
action[st,a] = j |
| 1767 |
actionp[st,a] = p |
| 1768 |
|
| 1769 |
except Exception as e: |
| 1770 |
raise YaccError("Hosed in lr_parse_table", e) |
| 1771 |
|
| 1772 |
|
| 1773 |
if yaccdebug: |
| 1774 |
_actprint = { } |
| 1775 |
for a,p,m in actlist: |
| 1776 |
if action.has_key((st,a)): |
| 1777 |
if p is actionp[st,a]: |
| 1778 |
_vf.write(" %-15s %s\n" % (a,m)) |
| 1779 |
_actprint[(a,m)] = 1 |
| 1780 |
_vf.write("\n") |
| 1781 |
for a,p,m in actlist: |
| 1782 |
if action.has_key((st,a)): |
| 1783 |
if p is not actionp[st,a]: |
| 1784 |
if not _actprint.has_key((a,m)): |
| 1785 |
_vf.write(" ! %-15s [ %s ]\n" % (a,m)) |
| 1786 |
_actprint[(a,m)] = 1 |
| 1787 |
|
| 1788 |
|
| 1789 |
if yaccdebug: |
| 1790 |
_vf.write("\n") |
| 1791 |
nkeys = { } |
| 1792 |
for ii in I: |
| 1793 |
for s in ii.usyms: |
| 1794 |
if Nonterminals.has_key(s): |
| 1795 |
nkeys[s] = None |
| 1796 |
for n in nkeys.keys(): |
| 1797 |
g = lr0_goto(I,n) |
| 1798 |
j = _lr0_cidhash.get(id(g),-1) |
| 1799 |
if j >= 0: |
| 1800 |
goto[st,n] = j |
| 1801 |
if yaccdebug: |
| 1802 |
_vf.write(" %-30s shift and go to state %d\n" % (n,j)) |
| 1803 |
|
| 1804 |
st += 1 |
| 1805 |
|
| 1806 |
if yaccdebug: |
| 1807 |
if n_srconflict == 1: |
| 1808 |
sys.stderr.write("yacc: %d shift/reduce conflict\n" % n_srconflict) |
| 1809 |
if n_srconflict > 1: |
| 1810 |
sys.stderr.write("yacc: %d shift/reduce conflicts\n" % n_srconflict) |
| 1811 |
if n_rrconflict == 1: |
| 1812 |
sys.stderr.write("yacc: %d reduce/reduce conflict\n" % n_rrconflict) |
| 1813 |
if n_rrconflict > 1: |
| 1814 |
sys.stderr.write("yacc: %d reduce/reduce conflicts\n" % n_rrconflict) |
| 1815 |
|
| 1816 |
|
| 1817 |
|
| 1818 |
|
| 1819 |
|
| 1820 |
|
| 1821 |
|
| 1822 |
|
| 1823 |
|
| 1824 |
|
| 1825 |
|
| 1826 |
def lr_write_tables(modulename=tab_module,outputdir=''): |
| 1827 |
filename = os.path.join(outputdir,modulename) + ".py" |
| 1828 |
try: |
| 1829 |
f = open(filename,"w") |
| 1830 |
|
| 1831 |
f.write(""" |
| 1832 |
# %s |
| 1833 |
# This file is automatically generated. Do not edit. |
| 1834 |
|
| 1835 |
_lr_method = %s |
| 1836 |
|
| 1837 |
_lr_signature = %s |
| 1838 |
""" % (filename, repr(_lr_method), repr(Signature.digest()))) |
| 1839 |
|
| 1840 |
|
| 1841 |
smaller = 1 |
| 1842 |
|
| 1843 |
|
| 1844 |
if smaller: |
| 1845 |
items = { } |
| 1846 |
|
| 1847 |
for k,v in _lr_action.items(): |
| 1848 |
i = items.get(k[1]) |
| 1849 |
if not i: |
| 1850 |
i = ([],[]) |
| 1851 |
items[k[1]] = i |
| 1852 |
i[0].append(k[0]) |
| 1853 |
i[1].append(v) |
| 1854 |
|
| 1855 |
f.write("\n_lr_action_items = {") |
| 1856 |
for k,v in items.items(): |
| 1857 |
f.write("%r:([" % k) |
| 1858 |
for i in v[0]: |
| 1859 |
f.write("%r," % i) |
| 1860 |
f.write("],[") |
| 1861 |
for i in v[1]: |
| 1862 |
f.write("%r," % i) |
| 1863 |
|
| 1864 |
f.write("]),") |
| 1865 |
f.write("}\n") |
| 1866 |
|
| 1867 |
f.write(""" |
| 1868 |
_lr_action = { } |
| 1869 |
for _k, _v in _lr_action_items.items(): |
| 1870 |
for _x,_y in zip(_v[0],_v[1]): |
| 1871 |
_lr_action[(_x,_k)] = _y |
| 1872 |
del _lr_action_items |
| 1873 |
""") |
| 1874 |
|
| 1875 |
else: |
| 1876 |
f.write("\n_lr_action = { "); |
| 1877 |
for k,v in _lr_action.items(): |
| 1878 |
f.write("(%r,%r):%r," % (k[0],k[1],v)) |
| 1879 |
f.write("}\n"); |
| 1880 |
|
| 1881 |
if smaller: |
| 1882 |
|
| 1883 |
items = { } |
| 1884 |
|
| 1885 |
for k,v in _lr_goto.items(): |
| 1886 |
i = items.get(k[1]) |
| 1887 |
if not i: |
| 1888 |
i = ([],[]) |
| 1889 |
items[k[1]] = i |
| 1890 |
i[0].append(k[0]) |
| 1891 |
i[1].append(v) |
| 1892 |
|
| 1893 |
f.write("\n_lr_goto_items = {") |
| 1894 |
for k,v in items.items(): |
| 1895 |
f.write("%r:([" % k) |
| 1896 |
for i in v[0]: |
| 1897 |
f.write("%r," % i) |
| 1898 |
f.write("],[") |
| 1899 |
for i in v[1]: |
| 1900 |
f.write("%r," % i) |
| 1901 |
|
| 1902 |
f.write("]),") |
| 1903 |
f.write("}\n") |
| 1904 |
|
| 1905 |
f.write(""" |
| 1906 |
_lr_goto = { } |
| 1907 |
for _k, _v in _lr_goto_items.items(): |
| 1908 |
for _x,_y in zip(_v[0],_v[1]): |
| 1909 |
_lr_goto[(_x,_k)] = _y |
| 1910 |
del _lr_goto_items |
| 1911 |
""") |
| 1912 |
else: |
| 1913 |
f.write("\n_lr_goto = { "); |
| 1914 |
for k,v in _lr_goto.items(): |
| 1915 |
f.write("(%r,%r):%r," % (k[0],k[1],v)) |
| 1916 |
f.write("}\n"); |
| 1917 |
|
| 1918 |
|
| 1919 |
f.write("_lr_productions = [\n") |
| 1920 |
for p in Productions: |
| 1921 |
if p: |
| 1922 |
if (p.func): |
| 1923 |
f.write(" (%r,%d,%r,%r,%d),\n" % (p.name, p.len, p.func.__name__,p.file,p.line)) |
| 1924 |
else: |
| 1925 |
f.write(" (%r,%d,None,None,None),\n" % (p.name, p.len)) |
| 1926 |
else: |
| 1927 |
f.write(" None,\n") |
| 1928 |
f.write("]\n") |
| 1929 |
|
| 1930 |
f.close() |
| 1931 |
|
| 1932 |
except IOError as e: |
| 1933 |
print("Unable to create '%s'" % filename) |
| 1934 |
print(e) |
| 1935 |
return |
| 1936 |
|
| 1937 |
def lr_read_tables(module=tab_module,optimize=0): |
| 1938 |
global _lr_action, _lr_goto, _lr_productions, _lr_method |
| 1939 |
try: |
| 1940 |
exec("import %s as parsetab" % module) |
| 1941 |
global parsetab |
| 1942 |
|
| 1943 |
if (optimize) or (Signature.digest() == parsetab._lr_signature): |
| 1944 |
_lr_action = parsetab._lr_action |
| 1945 |
_lr_goto = parsetab._lr_goto |
| 1946 |
_lr_productions = parsetab._lr_productions |
| 1947 |
_lr_method = parsetab._lr_method |
| 1948 |
return 1 |
| 1949 |
else: |
| 1950 |
return 0 |
| 1951 |
|
| 1952 |
except (ImportError,AttributeError): |
| 1953 |
return 0 |
| 1954 |
|
| 1955 |
|
| 1956 |
|
| 1957 |
|
| 1958 |
|
| 1959 |
|
| 1960 |
try: |
| 1961 |
_INSTANCETYPE = (types.InstanceType, types.ObjectType) |
| 1962 |
except AttributeError: |
| 1963 |
_INSTANCETYPE = types.InstanceType |
| 1964 |
|
| 1965 |
|
| 1966 |
|
| 1967 |
|
| 1968 |
|
| 1969 |
|
| 1970 |
|
| 1971 |
|
| 1972 |
def yacc(method=default_lr, debug=yaccdebug, module=None, tabmodule=tab_module, start=None, check_recursion=1, optimize=0,write_tables=1,debugfile=debug_file,outputdir='', parserclass=Parser): |
| 1973 |
global yaccdebug |
| 1974 |
yaccdebug = debug |
| 1975 |
|
| 1976 |
initialize_vars() |
| 1977 |
files = { } |
| 1978 |
error = 0 |
| 1979 |
|
| 1980 |
|
| 1981 |
|
| 1982 |
Signature.update(method) |
| 1983 |
|
| 1984 |
|
| 1985 |
|
| 1986 |
|
| 1987 |
if module: |
| 1988 |
|
| 1989 |
if isinstance(module, types.ModuleType): |
| 1990 |
ldict = module.__dict__ |
| 1991 |
elif isinstance(module, _INSTANCETYPE): |
| 1992 |
_items = [(k,getattr(module,k)) for k in dir(module)] |
| 1993 |
ldict = { } |
| 1994 |
for i in _items: |
| 1995 |
ldict[i[0]] = i[1] |
| 1996 |
else: |
| 1997 |
raise ValueError("Expected a module") |
| 1998 |
|
| 1999 |
else: |
| 2000 |
|
| 2001 |
|
| 2002 |
|
| 2003 |
try: |
| 2004 |
raise RuntimeError |
| 2005 |
except RuntimeError: |
| 2006 |
e,b,t = sys.exc_info() |
| 2007 |
f = t.tb_frame |
| 2008 |
f = f.f_back |
| 2009 |
ldict = f.f_globals |
| 2010 |
|
| 2011 |
|
| 2012 |
if not start: |
| 2013 |
start = ldict.get("start",None) |
| 2014 |
if start: |
| 2015 |
Signature.update(start) |
| 2016 |
|
| 2017 |
|
| 2018 |
|
| 2019 |
if (optimize and lr_read_tables(tabmodule,1)): |
| 2020 |
|
| 2021 |
del Productions[:] |
| 2022 |
for p in _lr_productions: |
| 2023 |
if not p: |
| 2024 |
Productions.append(None) |
| 2025 |
else: |
| 2026 |
m = MiniProduction() |
| 2027 |
m.name = p[0] |
| 2028 |
m.len = p[1] |
| 2029 |
m.file = p[3] |
| 2030 |
m.line = p[4] |
| 2031 |
if p[2]: |
| 2032 |
m.func = ldict[p[2]] |
| 2033 |
Productions.append(m) |
| 2034 |
|
| 2035 |
else: |
| 2036 |
|
| 2037 |
if (module and isinstance(module,_INSTANCETYPE)): |
| 2038 |
tokens = getattr(module,"tokens",None) |
| 2039 |
else: |
| 2040 |
tokens = ldict.get("tokens",None) |
| 2041 |
|
| 2042 |
if not tokens: |
| 2043 |
raise YaccError("module does not define a list 'tokens'") |
| 2044 |
if not (isinstance(tokens,types.ListType) or isinstance(tokens,types.TupleType)): |
| 2045 |
raise YaccError("tokens must be a list or tuple.") |
| 2046 |
|
| 2047 |
|
| 2048 |
requires = ldict.get("require",None) |
| 2049 |
if requires: |
| 2050 |
if not (isinstance(requires,types.DictType)): |
| 2051 |
raise YaccError("require must be a dictionary.") |
| 2052 |
|
| 2053 |
for r,v in requires.items(): |
| 2054 |
try: |
| 2055 |
if not (isinstance(v,types.ListType)): |
| 2056 |
raise TypeError |
| 2057 |
v1 = [x.split(".") for x in v] |
| 2058 |
Requires[r] = v1 |
| 2059 |
except Exception: |
| 2060 |
print("Invalid specification for rule '%s' in require. Expected a list of strings" % r) |
| 2061 |
|
| 2062 |
|
| 2063 |
|
| 2064 |
|
| 2065 |
|
| 2066 |
|
| 2067 |
if 'error' in tokens: |
| 2068 |
print("yacc: Illegal token 'error'. Is a reserved word.") |
| 2069 |
raise YaccError("Illegal token name") |
| 2070 |
|
| 2071 |
for n in tokens: |
| 2072 |
if Terminals.has_key(n): |
| 2073 |
print("yacc: Warning. Token '%s' multiply defined." % n) |
| 2074 |
Terminals[n] = [ ] |
| 2075 |
|
| 2076 |
Terminals['error'] = [ ] |
| 2077 |
|
| 2078 |
|
| 2079 |
prec = ldict.get("precedence",None) |
| 2080 |
if prec: |
| 2081 |
if not (isinstance(prec,types.ListType) or isinstance(prec,types.TupleType)): |
| 2082 |
raise YaccError("precedence must be a list or tuple.") |
| 2083 |
add_precedence(prec) |
| 2084 |
Signature.update(repr(prec)) |
| 2085 |
|
| 2086 |
for n in tokens: |
| 2087 |
if not Precedence.has_key(n): |
| 2088 |
Precedence[n] = ('right',0) |
| 2089 |
|
| 2090 |
|
| 2091 |
ef = ldict.get('p_error',None) |
| 2092 |
if ef: |
| 2093 |
if isinstance(ef,types.FunctionType): |
| 2094 |
ismethod = 0 |
| 2095 |
elif isinstance(ef, types.MethodType): |
| 2096 |
ismethod = 1 |
| 2097 |
else: |
| 2098 |
raise YaccError("'p_error' defined, but is not a function or method.") |
| 2099 |
eline = ef.func_code.co_firstlineno |
| 2100 |
efile = ef.func_code.co_filename |
| 2101 |
files[efile] = None |
| 2102 |
|
| 2103 |
if (ef.func_code.co_argcount != 1+ismethod): |
| 2104 |
raise YaccError("%s:%d: p_error() requires 1 argument." % (efile,eline)) |
| 2105 |
global Errorfunc |
| 2106 |
Errorfunc = ef |
| 2107 |
else: |
| 2108 |
print("yacc: Warning. no p_error() function is defined.") |
| 2109 |
|
| 2110 |
|
| 2111 |
symbols = [ldict[f] for f in ldict.keys() |
| 2112 |
if (type(ldict[f]) in (types.FunctionType, types.MethodType) and ldict[f].__name__[:2] == 'p_' |
| 2113 |
and ldict[f].__name__ != 'p_error')] |
| 2114 |
|
| 2115 |
|
| 2116 |
if len(symbols) == 0: |
| 2117 |
raise YaccError("no rules of the form p_rulename are defined.") |
| 2118 |
|
| 2119 |
|
| 2120 |
symbols.sort(key=lambda func: func.__code__.co_firstlineno) |
| 2121 |
|
| 2122 |
|
| 2123 |
for f in symbols: |
| 2124 |
if (add_function(f)) < 0: |
| 2125 |
error += 1 |
| 2126 |
else: |
| 2127 |
files[f.func_code.co_filename] = None |
| 2128 |
|
| 2129 |
|
| 2130 |
for f in symbols: |
| 2131 |
if f.__doc__: |
| 2132 |
Signature.update(f.__doc__) |
| 2133 |
|
| 2134 |
lr_init_vars() |
| 2135 |
|
| 2136 |
if error: |
| 2137 |
raise YaccError("Unable to construct parser.") |
| 2138 |
|
| 2139 |
if not lr_read_tables(tabmodule): |
| 2140 |
|
| 2141 |
|
| 2142 |
for filename in files.keys(): |
| 2143 |
if not validate_file(filename): |
| 2144 |
error = 1 |
| 2145 |
|
| 2146 |
|
| 2147 |
validate_dict(ldict) |
| 2148 |
|
| 2149 |
if start and not Prodnames.has_key(start): |
| 2150 |
raise YaccError("Bad starting symbol '%s'" % start) |
| 2151 |
|
| 2152 |
augment_grammar(start) |
| 2153 |
error = verify_productions(cycle_check=check_recursion) |
| 2154 |
otherfunc = [ldict[f] for f in ldict.keys() |
| 2155 |
if (type(f) in (types.FunctionType,types.MethodType) and ldict[f].__name__[:2] != 'p_')] |
| 2156 |
|
| 2157 |
if error: |
| 2158 |
raise YaccError("Unable to construct parser.") |
| 2159 |
|
| 2160 |
build_lritems() |
| 2161 |
compute_first1() |
| 2162 |
compute_follow(start) |
| 2163 |
|
| 2164 |
if method in ['SLR','LALR']: |
| 2165 |
lr_parse_table(method) |
| 2166 |
else: |
| 2167 |
raise YaccError("Unknown parsing method '%s'" % method) |
| 2168 |
|
| 2169 |
if write_tables: |
| 2170 |
lr_write_tables(tabmodule,outputdir) |
| 2171 |
|
| 2172 |
if yaccdebug: |
| 2173 |
try: |
| 2174 |
f = open(os.path.join(outputdir,debugfile),"w") |
| 2175 |
f.write(_vfc.getvalue()) |
| 2176 |
f.write("\n\n") |
| 2177 |
f.write(_vf.getvalue()) |
| 2178 |
f.close() |
| 2179 |
except IOError as e: |
| 2180 |
print("yacc: can't create '%s'" % debugfile,e) |
| 2181 |
|
| 2182 |
|
| 2183 |
|
| 2184 |
|
| 2185 |
g = ParserPrototype("xyzzy") |
| 2186 |
g.productions = Productions |
| 2187 |
g.errorfunc = Errorfunc |
| 2188 |
g.action = _lr_action |
| 2189 |
g.goto = _lr_goto |
| 2190 |
g.method = _lr_method |
| 2191 |
g.require = Requires |
| 2192 |
|
| 2193 |
global parser |
| 2194 |
parser = g.init_parser() |
| 2195 |
|
| 2196 |
global parse |
| 2197 |
parse = parser.parse |
| 2198 |
|
| 2199 |
|
| 2200 |
if (not optimize): |
| 2201 |
yacc_cleanup() |
| 2202 |
return g |
| 2203 |
|
| 2204 |
|
| 2205 |
class ParserPrototype(object): |
| 2206 |
def __init__(self, magic=None): |
| 2207 |
if magic != "xyzzy": |
| 2208 |
raise YaccError('Use yacc()') |
| 2209 |
|
| 2210 |
def init_parser(self, parser=None): |
| 2211 |
if not parser: |
| 2212 |
parser = Parser() |
| 2213 |
parser.productions = self.productions |
| 2214 |
parser.errorfunc = self.errorfunc |
| 2215 |
parser.action = self.action |
| 2216 |
parser.goto = self.goto |
| 2217 |
parser.method = self.method |
| 2218 |
parser.require = self.require |
| 2219 |
return parser |
| 2220 |
|
| 2221 |
|
| 2222 |
|
| 2223 |
|
| 2224 |
def yacc_cleanup(): |
| 2225 |
global _lr_action, _lr_goto, _lr_method, _lr_goto_cache |
| 2226 |
del _lr_action, _lr_goto, _lr_method, _lr_goto_cache |
| 2227 |
|
| 2228 |
global Productions, Prodnames, Prodmap, Terminals |
| 2229 |
global Nonterminals, First, Follow, Precedence, LRitems |
| 2230 |
global Errorfunc, Signature, Requires |
| 2231 |
|
| 2232 |
del Productions, Prodnames, Prodmap, Terminals |
| 2233 |
del Nonterminals, First, Follow, Precedence, LRitems |
| 2234 |
del Errorfunc, Signature, Requires |
| 2235 |
|
| 2236 |
global _vf, _vfc |
| 2237 |
del _vf, _vfc |
| 2238 |
|
| 2239 |
|
| 2240 |
|
| 2241 |
def parse(*args,**kwargs): |
| 2242 |
raise YaccError("yacc: No parser built with yacc()") |
| 2243 |
|