121121
122122
123123"""
124- __all__ = ["SpecError" , "UnexpectedToken" , "Nonterm" ,
125- "Precedence" , "Spec" , "Token" , "Lr" , "Glr" ,
126- "ModuleSpecSource" ]
127-
128- from parsing .errors import (ParsingError , SpecError , # noqa
129- UnexpectedToken , AnyException ) # noqa
130- from parsing .grammar import (Precedence , Production , SymbolSpec , # noqa
131- NontermSpec , TokenSpec , EndOfInput , # noqa
132- Epsilon , epsilon , NontermStart , # noqa
133- ShiftAction , ReduceAction )
134- from parsing .ast import Symbol , Nonterm , Token # noqa
124+ __all__ = [
125+ "SpecError" ,
126+ "UnexpectedToken" ,
127+ "Nonterm" ,
128+ "Precedence" ,
129+ "Spec" ,
130+ "Token" ,
131+ "Lr" ,
132+ "Glr" ,
133+ "ModuleSpecSource" ,
134+ ]
135+
136+ from parsing .errors import ( # noqa: F401
137+ ParsingError ,
138+ SpecError ,
139+ UnexpectedToken ,
140+ AnyException ,
141+ )
142+ from parsing .grammar import ( # noqa: F401
143+ Precedence ,
144+ Production ,
145+ SymbolSpec ,
146+ NontermSpec ,
147+ TokenSpec ,
148+ EndOfInput ,
149+ Epsilon ,
150+ epsilon ,
151+ NontermStart ,
152+ ShiftAction ,
153+ ReduceAction ,
154+ )
155+ from parsing .ast import Symbol , Nonterm , Token # noqa: F401
135156from parsing .automaton import Spec
136157from parsing .module_spec import ModuleSpecSource
137158
@@ -185,7 +206,8 @@ def __setStart(self, start):
185206A list of parsing results. For LR parsing, there is only ever one
186207result, but for compatibility with the Glr interface, start is a
187208list.
188- """ )
209+ """ ,
210+ )
189211
190212 def __getVerbose (self ):
191213 return self ._verbose
@@ -246,15 +268,20 @@ def _act(self, sym, symSpec):
246268 self ._printStack ()
247269
248270 def _printStack (self ):
249- print ("STACK:" , end = ' ' )
271+ print ("STACK:" , end = " " )
250272 for node in self ._stack :
251- print ("%r" % node [0 ], end = ' ' )
273+ print ("%r" % node [0 ], end = " " )
252274 print ()
253- print (" " , end = ' ' )
275+ print (" " , end = " " )
254276 for node in self ._stack :
255- print ("%r%s" % (
256- node [1 ], (" " * (len ("%r" % node [0 ]) - len ("%r" % node [1 ])))),
257- end = ' ' )
277+ print (
278+ "%r%s"
279+ % (
280+ node [1 ],
281+ (" " * (len ("%r" % node [0 ]) - len ("%r" % node [1 ]))),
282+ ),
283+ end = " " ,
284+ )
258285 print ()
259286
260287 def _reduce (self , production ):
@@ -284,6 +311,7 @@ def _production(self, production, rhs):
284311
285312 return r
286313
314+
287315# ===========================================================================
288316# Begin graph-structured stack (GSS) classes.
289317#
@@ -310,8 +338,7 @@ def __repr__(self):
310338 return "{%r}" % self .value
311339
312340 def __eq__ (self , other ):
313- if self .node != other .node \
314- or self .value != other .value :
341+ if self .node != other .node or self .value != other .value :
315342 return False
316343 return True
317344
@@ -378,17 +405,17 @@ def _pathsRecurse(self, pathLen, path):
378405 path .pop (0 )
379406 path .pop (0 )
380407
408+
381409#
382410# End graph-structured stack (GSS) classes.
383411# ========================================================================
384412
385413
386414class Glr (Lr ):
387415 """
388- GLR parser. The Glr class uses a Spec instance in order to parse input
389- that is fed to it via the token() method, and terminated via the eoi()
390- method.
391- """
416+ GLR parser. The Glr class uses a Spec instance in order to parse input
417+ that is fed to it via the token() method, and terminated via the eoi()
418+ method."""
392419
393420 def __init__ (self , spec ):
394421 Lr .__init__ (self , spec )
@@ -405,8 +432,7 @@ def reset(self):
405432
406433 def token (self , token ):
407434 """
408- Feed a token to the parser.
409- """
435+ Feed a token to the parser."""
410436 if self ._verbose :
411437 print ("%s" % ("-" * 80 ))
412438 print ("INPUT: %r" % token )
@@ -417,8 +443,7 @@ def token(self, token):
417443
418444 def eoi (self ):
419445 """
420- Signal end-of-input to the parser.
421- """
446+ Signal end-of-input to the parser."""
422447 token = EndOfInput (self )
423448 self .token (token )
424449
@@ -468,33 +493,41 @@ def _reductions(self, sym, symSpec):
468493 if type (action ) == ReduceAction :
469494 if len (action .production .rhs ) == 0 :
470495 if action .production not in epsilons :
471- assert len (
472- [path for path in top .paths (0 )]) == 1
496+ assert (
497+ len ([path for path in top .paths (0 )]) == 1
498+ )
473499 path = [p for p in top .paths (0 )][0 ]
474500 epsilons [action .production ] = [top ]
475501 workQ .append ((path , action .production ))
476502 if self ._verbose :
477- print (" --> enqueue(a) %r" %
478- action .production )
503+ print (
504+ " --> enqueue(a) %r"
505+ % action .production
506+ )
479507 print (" %r" % path )
480508 elif top not in epsilons [action .production ]:
481- assert len (
482- [path for path in top .paths (0 )]) == 1
509+ assert (
510+ len ([path for path in top .paths (0 )]) == 1
511+ )
483512 path = [p for p in top .paths (0 )][0 ]
484513 epsilons [action .production ].append (top )
485514 workQ .append ((path , action .production ))
486515 if self ._verbose :
487- print (" --> enqueue(b) %r" %
488- action .production )
516+ print (
517+ " --> enqueue(b) %r"
518+ % action .production
519+ )
489520 print (" %r" % path )
490521 else :
491522 # Iterate over all reduction paths through stack
492523 # and enqueue them.
493524 for path in top .paths (len (action .production .rhs )):
494525 workQ .append ((path , action .production ))
495526 if self ._verbose :
496- print (" --> enqueue(c) %r" %
497- action .production )
527+ print (
528+ " --> enqueue(c) %r"
529+ % action .production
530+ )
498531 print (" %r" % path )
499532 i += 1
500533
@@ -526,8 +559,10 @@ def _reduce(self, workQ, epsilons, path, production, symSpec):
526559 below = path [0 ]
527560 done = False
528561 for top in self ._gss :
529- if top .nextState == \
530- self ._spec ._goto [below .nextState ][production .lhs ]:
562+ if (
563+ top .nextState
564+ == self ._spec ._goto [below .nextState ][production .lhs ]
565+ ):
531566 # top is compatible with the reduction result we want to add to
532567 # the set of stack tops.
533568 for edge in top .edges ():
@@ -539,12 +574,18 @@ def _reduce(self, workQ, epsilons, path, production, symSpec):
539574 value = production .lhs .nontermType .merge (edge .value , r )
540575 if self ._verbose :
541576 if value == edge .value :
542- print (" %s" %
543- ("-" * len ("%r" % edge .value )))
577+ print (
578+ " %s"
579+ % ("-" * len ("%r" % edge .value ))
580+ )
544581 else :
545- print (" %s %s" %
546- ((" " * len ("%r" % edge .value )),
547- "-" * len ("%r" % r )))
582+ print (
583+ " %s %s"
584+ % (
585+ (" " * len ("%r" % edge .value )),
586+ "-" * len ("%r" % r ),
587+ )
588+ )
548589 edge .value = value
549590 done = True
550591 break
@@ -557,17 +598,21 @@ def _reduce(self, workQ, epsilons, path, production, symSpec):
557598 # Enqueue reduction paths that were created as a result of
558599 # the new link.
559600 self ._enqueueLimitedReductions (
560- workQ , epsilons , edge , symSpec )
601+ workQ , epsilons , edge , symSpec
602+ )
561603 done = True
562604 break
563605 if not done :
564606 # There is no compatible stack top, so create a new one.
565607 top = Gssn (
566- below , r , self ._spec ._goto [below .nextState ][production .lhs ])
608+ below , r , self ._spec ._goto [below .nextState ][production .lhs ]
609+ )
567610 self ._gss .append (top )
568611 if self ._verbose :
569- print (" --> shift(c) %r" %
570- self ._spec ._goto [below .nextState ][production .lhs ])
612+ print (
613+ " --> shift(c) %r"
614+ % self ._spec ._goto [below .nextState ][production .lhs ]
615+ )
571616 self ._enqueueLimitedReductions (workQ , epsilons , top .edge , symSpec )
572617
573618 # Enqueue paths that incorporate edge.
@@ -579,8 +624,10 @@ def _enqueueLimitedReductions(self, workQ, epsilons, edge, symSpec):
579624 for action in self ._spec ._action [top .nextState ][symSpec ]:
580625 if type (action ) == ReduceAction :
581626 if len (action .production .rhs ) == 0 :
582- if (gotos [top .nextState ][action .production .lhs ] ==
583- top .nextState ):
627+ if (
628+ gotos [top .nextState ][action .production .lhs ]
629+ == top .nextState
630+ ):
584631 # Do nothing, since enqueueing a reduction
585632 # would result in performing the same reduction
586633 # twice.
@@ -590,16 +637,20 @@ def _enqueueLimitedReductions(self, workQ, epsilons, edge, symSpec):
590637 epsilons [action .production ] = [top ]
591638 workQ .append ((path , action .production ))
592639 if self ._verbose :
593- print (" --> enqueue(d) %r" %
594- action .production )
640+ print (
641+ " --> enqueue(d) %r"
642+ % action .production
643+ )
595644 print (" %r" % path )
596645 elif top not in epsilons [action .production ]:
597646 path = [top ]
598647 epsilons [action .production ].append (top )
599648 workQ .append ((path , action .production ))
600649 if self ._verbose :
601- print (" --> enqueue(e) %r" %
602- action .production )
650+ print (
651+ " --> enqueue(e) %r"
652+ % action .production
653+ )
603654 print (" %r" % path )
604655 else :
605656 # Iterate over all reduction paths through stack
@@ -608,8 +659,10 @@ def _enqueueLimitedReductions(self, workQ, epsilons, edge, symSpec):
608659 if edge in path [1 ::2 ]:
609660 workQ .append ((path , action .production ))
610661 if self ._verbose :
611- print (" --> enqueue(f) %r" %
612- action .production )
662+ print (
663+ " --> enqueue(f) %r"
664+ % action .production
665+ )
613666 print (" %r" % path )
614667
615668 def _shifts (self , sym , symSpec ):
@@ -644,10 +697,10 @@ def _printStack(self):
644697 for top in self ._gss :
645698 for path in top .paths ():
646699 if i == 0 :
647- print ("STK 0:" , end = ' ' )
700+ print ("STK 0:" , end = " " )
648701 else :
649- print (" %d:" % i , end = ' ' )
702+ print (" %d:" % i , end = " " )
650703 for elm in path :
651- print ("%r" % elm , end = ' ' )
704+ print ("%r" % elm , end = " " )
652705 print ()
653706 i += 1
0 commit comments