121
121
122
122
123
123
"""
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
135
156
from parsing .automaton import Spec
136
157
from parsing .module_spec import ModuleSpecSource
137
158
@@ -185,7 +206,8 @@ def __setStart(self, start):
185
206
A list of parsing results. For LR parsing, there is only ever one
186
207
result, but for compatibility with the Glr interface, start is a
187
208
list.
188
- """ )
209
+ """ ,
210
+ )
189
211
190
212
def __getVerbose (self ):
191
213
return self ._verbose
@@ -246,15 +268,20 @@ def _act(self, sym, symSpec):
246
268
self ._printStack ()
247
269
248
270
def _printStack (self ):
249
- print ("STACK:" , end = ' ' )
271
+ print ("STACK:" , end = " " )
250
272
for node in self ._stack :
251
- print ("%r" % node [0 ], end = ' ' )
273
+ print ("%r" % node [0 ], end = " " )
252
274
print ()
253
- print (" " , end = ' ' )
275
+ print (" " , end = " " )
254
276
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
+ )
258
285
print ()
259
286
260
287
def _reduce (self , production ):
@@ -284,6 +311,7 @@ def _production(self, production, rhs):
284
311
285
312
return r
286
313
314
+
287
315
# ===========================================================================
288
316
# Begin graph-structured stack (GSS) classes.
289
317
#
@@ -310,8 +338,7 @@ def __repr__(self):
310
338
return "{%r}" % self .value
311
339
312
340
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 :
315
342
return False
316
343
return True
317
344
@@ -378,17 +405,17 @@ def _pathsRecurse(self, pathLen, path):
378
405
path .pop (0 )
379
406
path .pop (0 )
380
407
408
+
381
409
#
382
410
# End graph-structured stack (GSS) classes.
383
411
# ========================================================================
384
412
385
413
386
414
class Glr (Lr ):
387
415
"""
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."""
392
419
393
420
def __init__ (self , spec ):
394
421
Lr .__init__ (self , spec )
@@ -405,8 +432,7 @@ def reset(self):
405
432
406
433
def token (self , token ):
407
434
"""
408
- Feed a token to the parser.
409
- """
435
+ Feed a token to the parser."""
410
436
if self ._verbose :
411
437
print ("%s" % ("-" * 80 ))
412
438
print ("INPUT: %r" % token )
@@ -417,8 +443,7 @@ def token(self, token):
417
443
418
444
def eoi (self ):
419
445
"""
420
- Signal end-of-input to the parser.
421
- """
446
+ Signal end-of-input to the parser."""
422
447
token = EndOfInput (self )
423
448
self .token (token )
424
449
@@ -468,33 +493,41 @@ def _reductions(self, sym, symSpec):
468
493
if type (action ) == ReduceAction :
469
494
if len (action .production .rhs ) == 0 :
470
495
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
+ )
473
499
path = [p for p in top .paths (0 )][0 ]
474
500
epsilons [action .production ] = [top ]
475
501
workQ .append ((path , action .production ))
476
502
if self ._verbose :
477
- print (" --> enqueue(a) %r" %
478
- action .production )
503
+ print (
504
+ " --> enqueue(a) %r"
505
+ % action .production
506
+ )
479
507
print (" %r" % path )
480
508
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
+ )
483
512
path = [p for p in top .paths (0 )][0 ]
484
513
epsilons [action .production ].append (top )
485
514
workQ .append ((path , action .production ))
486
515
if self ._verbose :
487
- print (" --> enqueue(b) %r" %
488
- action .production )
516
+ print (
517
+ " --> enqueue(b) %r"
518
+ % action .production
519
+ )
489
520
print (" %r" % path )
490
521
else :
491
522
# Iterate over all reduction paths through stack
492
523
# and enqueue them.
493
524
for path in top .paths (len (action .production .rhs )):
494
525
workQ .append ((path , action .production ))
495
526
if self ._verbose :
496
- print (" --> enqueue(c) %r" %
497
- action .production )
527
+ print (
528
+ " --> enqueue(c) %r"
529
+ % action .production
530
+ )
498
531
print (" %r" % path )
499
532
i += 1
500
533
@@ -526,8 +559,10 @@ def _reduce(self, workQ, epsilons, path, production, symSpec):
526
559
below = path [0 ]
527
560
done = False
528
561
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
+ ):
531
566
# top is compatible with the reduction result we want to add to
532
567
# the set of stack tops.
533
568
for edge in top .edges ():
@@ -539,12 +574,18 @@ def _reduce(self, workQ, epsilons, path, production, symSpec):
539
574
value = production .lhs .nontermType .merge (edge .value , r )
540
575
if self ._verbose :
541
576
if value == edge .value :
542
- print (" %s" %
543
- ("-" * len ("%r" % edge .value )))
577
+ print (
578
+ " %s"
579
+ % ("-" * len ("%r" % edge .value ))
580
+ )
544
581
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
+ )
548
589
edge .value = value
549
590
done = True
550
591
break
@@ -557,17 +598,21 @@ def _reduce(self, workQ, epsilons, path, production, symSpec):
557
598
# Enqueue reduction paths that were created as a result of
558
599
# the new link.
559
600
self ._enqueueLimitedReductions (
560
- workQ , epsilons , edge , symSpec )
601
+ workQ , epsilons , edge , symSpec
602
+ )
561
603
done = True
562
604
break
563
605
if not done :
564
606
# There is no compatible stack top, so create a new one.
565
607
top = Gssn (
566
- below , r , self ._spec ._goto [below .nextState ][production .lhs ])
608
+ below , r , self ._spec ._goto [below .nextState ][production .lhs ]
609
+ )
567
610
self ._gss .append (top )
568
611
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
+ )
571
616
self ._enqueueLimitedReductions (workQ , epsilons , top .edge , symSpec )
572
617
573
618
# Enqueue paths that incorporate edge.
@@ -579,8 +624,10 @@ def _enqueueLimitedReductions(self, workQ, epsilons, edge, symSpec):
579
624
for action in self ._spec ._action [top .nextState ][symSpec ]:
580
625
if type (action ) == ReduceAction :
581
626
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
+ ):
584
631
# Do nothing, since enqueueing a reduction
585
632
# would result in performing the same reduction
586
633
# twice.
@@ -590,16 +637,20 @@ def _enqueueLimitedReductions(self, workQ, epsilons, edge, symSpec):
590
637
epsilons [action .production ] = [top ]
591
638
workQ .append ((path , action .production ))
592
639
if self ._verbose :
593
- print (" --> enqueue(d) %r" %
594
- action .production )
640
+ print (
641
+ " --> enqueue(d) %r"
642
+ % action .production
643
+ )
595
644
print (" %r" % path )
596
645
elif top not in epsilons [action .production ]:
597
646
path = [top ]
598
647
epsilons [action .production ].append (top )
599
648
workQ .append ((path , action .production ))
600
649
if self ._verbose :
601
- print (" --> enqueue(e) %r" %
602
- action .production )
650
+ print (
651
+ " --> enqueue(e) %r"
652
+ % action .production
653
+ )
603
654
print (" %r" % path )
604
655
else :
605
656
# Iterate over all reduction paths through stack
@@ -608,8 +659,10 @@ def _enqueueLimitedReductions(self, workQ, epsilons, edge, symSpec):
608
659
if edge in path [1 ::2 ]:
609
660
workQ .append ((path , action .production ))
610
661
if self ._verbose :
611
- print (" --> enqueue(f) %r" %
612
- action .production )
662
+ print (
663
+ " --> enqueue(f) %r"
664
+ % action .production
665
+ )
613
666
print (" %r" % path )
614
667
615
668
def _shifts (self , sym , symSpec ):
@@ -644,10 +697,10 @@ def _printStack(self):
644
697
for top in self ._gss :
645
698
for path in top .paths ():
646
699
if i == 0 :
647
- print ("STK 0:" , end = ' ' )
700
+ print ("STK 0:" , end = " " )
648
701
else :
649
- print (" %d:" % i , end = ' ' )
702
+ print (" %d:" % i , end = " " )
650
703
for elm in path :
651
- print ("%r" % elm , end = ' ' )
704
+ print ("%r" % elm , end = " " )
652
705
print ()
653
706
i += 1
0 commit comments