DFAState.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  1. #
  2. # Copyright (c) 2012-2017 The ANTLR Project. All rights reserved.
  3. # Use of this file is governed by the BSD 3-clause license that
  4. # can be found in the LICENSE.txt file in the project root.
  5. #/
  6. # Map a predicate to a predicted alternative.#/
  7. from io import StringIO
  8. from antlr4.atn.ATNConfigSet import ATNConfigSet
  9. from antlr4.atn.SemanticContext import SemanticContext
  10. class PredPrediction(object):
  11. __slots__ = ('alt', 'pred')
  12. def __init__(self, pred:SemanticContext, alt:int):
  13. self.alt = alt
  14. self.pred = pred
  15. def __str__(self):
  16. return "(" + str(self.pred) + ", " + str(self.alt) + ")"
  17. # A DFA state represents a set of possible ATN configurations.
  18. # As Aho, Sethi, Ullman p. 117 says "The DFA uses its state
  19. # to keep track of all possible states the ATN can be in after
  20. # reading each input symbol. That is to say, after reading
  21. # input a1a2..an, the DFA is in a state that represents the
  22. # subset T of the states of the ATN that are reachable from the
  23. # ATN's start state along some path labeled a1a2..an."
  24. # In conventional NFA→DFA conversion, therefore, the subset T
  25. # would be a bitset representing the set of states the
  26. # ATN could be in. We need to track the alt predicted by each
  27. # state as well, however. More importantly, we need to maintain
  28. # a stack of states, tracking the closure operations as they
  29. # jump from rule to rule, emulating rule invocations (method calls).
  30. # I have to add a stack to simulate the proper lookahead sequences for
  31. # the underlying LL grammar from which the ATN was derived.
  32. #
  33. # <p>I use a set of ATNConfig objects not simple states. An ATNConfig
  34. # is both a state (ala normal conversion) and a RuleContext describing
  35. # the chain of rules (if any) followed to arrive at that state.</p>
  36. #
  37. # <p>A DFA state may have multiple references to a particular state,
  38. # but with different ATN contexts (with same or different alts)
  39. # meaning that state was reached via a different set of rule invocations.</p>
  40. #/
  41. class DFAState(object):
  42. __slots__ = (
  43. 'stateNumber', 'configs', 'edges', 'isAcceptState', 'prediction',
  44. 'lexerActionExecutor', 'requiresFullContext', 'predicates'
  45. )
  46. def __init__(self, stateNumber:int=-1, configs:ATNConfigSet=ATNConfigSet()):
  47. self.stateNumber = stateNumber
  48. self.configs = configs
  49. # {@code edges[symbol]} points to target of symbol. Shift up by 1 so (-1)
  50. # {@link Token#EOF} maps to {@code edges[0]}.
  51. self.edges = None
  52. self.isAcceptState = False
  53. # if accept state, what ttype do we match or alt do we predict?
  54. # This is set to {@link ATN#INVALID_ALT_NUMBER} when {@link #predicates}{@code !=null} or
  55. # {@link #requiresFullContext}.
  56. self.prediction = 0
  57. self.lexerActionExecutor = None
  58. # Indicates that this state was created during SLL prediction that
  59. # discovered a conflict between the configurations in the state. Future
  60. # {@link ParserATNSimulator#execATN} invocations immediately jumped doing
  61. # full context prediction if this field is true.
  62. self.requiresFullContext = False
  63. # During SLL parsing, this is a list of predicates associated with the
  64. # ATN configurations of the DFA state. When we have predicates,
  65. # {@link #requiresFullContext} is {@code false} since full context prediction evaluates predicates
  66. # on-the-fly. If this is not null, then {@link #prediction} is
  67. # {@link ATN#INVALID_ALT_NUMBER}.
  68. #
  69. # <p>We only use these for non-{@link #requiresFullContext} but conflicting states. That
  70. # means we know from the context (it's $ or we don't dip into outer
  71. # context) that it's an ambiguity not a conflict.</p>
  72. #
  73. # <p>This list is computed by {@link ParserATNSimulator#predicateDFAState}.</p>
  74. self.predicates = None
  75. # Get the set of all alts mentioned by all ATN configurations in this
  76. # DFA state.
  77. def getAltSet(self):
  78. if self.configs is not None:
  79. return set(cfg.alt for cfg in self.configs) or None
  80. return None
  81. def __hash__(self):
  82. return hash(self.configs)
  83. # Two {@link DFAState} instances are equal if their ATN configuration sets
  84. # are the same. This method is used to see if a state already exists.
  85. #
  86. # <p>Because the number of alternatives and number of ATN configurations are
  87. # finite, there is a finite number of DFA states that can be processed.
  88. # This is necessary to show that the algorithm terminates.</p>
  89. #
  90. # <p>Cannot test the DFA state numbers here because in
  91. # {@link ParserATNSimulator#addDFAState} we need to know if any other state
  92. # exists that has this exact set of ATN configurations. The
  93. # {@link #stateNumber} is irrelevant.</p>
  94. def __eq__(self, other):
  95. # compare set of ATN configurations in this set with other
  96. if self is other:
  97. return True
  98. elif not isinstance(other, DFAState):
  99. return False
  100. else:
  101. return self.configs==other.configs
  102. def __str__(self):
  103. with StringIO() as buf:
  104. buf.write(str(self.stateNumber))
  105. buf.write(":")
  106. buf.write(str(self.configs))
  107. if self.isAcceptState:
  108. buf.write("=>")
  109. if self.predicates is not None:
  110. buf.write(str(self.predicates))
  111. else:
  112. buf.write(str(self.prediction))
  113. return buf.getvalue()