ParserATNSimulator.py 78 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649
  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. #
  7. # The embodiment of the adaptive LL(*), ALL(*), parsing strategy.
  8. #
  9. # <p>
  10. # The basic complexity of the adaptive strategy makes it harder to understand.
  11. # We begin with ATN simulation to build paths in a DFA. Subsequent prediction
  12. # requests go through the DFA first. If they reach a state without an edge for
  13. # the current symbol, the algorithm fails over to the ATN simulation to
  14. # complete the DFA path for the current input (until it finds a conflict state
  15. # or uniquely predicting state).</p>
  16. #
  17. # <p>
  18. # All of that is done without using the outer context because we want to create
  19. # a DFA that is not dependent upon the rule invocation stack when we do a
  20. # prediction. One DFA works in all contexts. We avoid using context not
  21. # necessarily because it's slower, although it can be, but because of the DFA
  22. # caching problem. The closure routine only considers the rule invocation stack
  23. # created during prediction beginning in the decision rule. For example, if
  24. # prediction occurs without invoking another rule's ATN, there are no context
  25. # stacks in the configurations. When lack of context leads to a conflict, we
  26. # don't know if it's an ambiguity or a weakness in the strong LL(*) parsing
  27. # strategy (versus full LL(*)).</p>
  28. #
  29. # <p>
  30. # When SLL yields a configuration set with conflict, we rewind the input and
  31. # retry the ATN simulation, this time using full outer context without adding
  32. # to the DFA. Configuration context stacks will be the full invocation stacks
  33. # from the start rule. If we get a conflict using full context, then we can
  34. # definitively say we have a true ambiguity for that input sequence. If we
  35. # don't get a conflict, it implies that the decision is sensitive to the outer
  36. # context. (It is not context-sensitive in the sense of context-sensitive
  37. # grammars.)</p>
  38. #
  39. # <p>
  40. # The next time we reach this DFA state with an SLL conflict, through DFA
  41. # simulation, we will again retry the ATN simulation using full context mode.
  42. # This is slow because we can't save the results and have to "interpret" the
  43. # ATN each time we get that input.</p>
  44. #
  45. # <p>
  46. # <strong>CACHING FULL CONTEXT PREDICTIONS</strong></p>
  47. #
  48. # <p>
  49. # We could cache results from full context to predicted alternative easily and
  50. # that saves a lot of time but doesn't work in presence of predicates. The set
  51. # of visible predicates from the ATN start state changes depending on the
  52. # context, because closure can fall off the end of a rule. I tried to cache
  53. # tuples (stack context, semantic context, predicted alt) but it was slower
  54. # than interpreting and much more complicated. Also required a huge amount of
  55. # memory. The goal is not to create the world's fastest parser anyway. I'd like
  56. # to keep this algorithm simple. By launching multiple threads, we can improve
  57. # the speed of parsing across a large number of files.</p>
  58. #
  59. # <p>
  60. # There is no strict ordering between the amount of input used by SLL vs LL,
  61. # which makes it really hard to build a cache for full context. Let's say that
  62. # we have input A B C that leads to an SLL conflict with full context X. That
  63. # implies that using X we might only use A B but we could also use A B C D to
  64. # resolve conflict. Input A B C D could predict alternative 1 in one position
  65. # in the input and A B C E could predict alternative 2 in another position in
  66. # input. The conflicting SLL configurations could still be non-unique in the
  67. # full context prediction, which would lead us to requiring more input than the
  68. # original A B C. To make a prediction cache work, we have to track the exact
  69. # input used during the previous prediction. That amounts to a cache that maps
  70. # X to a specific DFA for that context.</p>
  71. #
  72. # <p>
  73. # Something should be done for left-recursive expression predictions. They are
  74. # likely LL(1) + pred eval. Easier to do the whole SLL unless error and retry
  75. # with full LL thing Sam does.</p>
  76. #
  77. # <p>
  78. # <strong>AVOIDING FULL CONTEXT PREDICTION</strong></p>
  79. #
  80. # <p>
  81. # We avoid doing full context retry when the outer context is empty, we did not
  82. # dip into the outer context by falling off the end of the decision state rule,
  83. # or when we force SLL mode.</p>
  84. #
  85. # <p>
  86. # As an example of the not dip into outer context case, consider as super
  87. # constructor calls versus function calls. One grammar might look like
  88. # this:</p>
  89. #
  90. # <pre>
  91. # ctorBody
  92. # : '{' superCall? stat* '}'
  93. # ;
  94. # </pre>
  95. #
  96. # <p>
  97. # Or, you might see something like</p>
  98. #
  99. # <pre>
  100. # stat
  101. # : superCall ';'
  102. # | expression ';'
  103. # | ...
  104. # ;
  105. # </pre>
  106. #
  107. # <p>
  108. # In both cases I believe that no closure operations will dip into the outer
  109. # context. In the first case ctorBody in the worst case will stop at the '}'.
  110. # In the 2nd case it should stop at the ';'. Both cases should stay within the
  111. # entry rule and not dip into the outer context.</p>
  112. #
  113. # <p>
  114. # <strong>PREDICATES</strong></p>
  115. #
  116. # <p>
  117. # Predicates are always evaluated if present in either SLL or LL both. SLL and
  118. # LL simulation deals with predicates differently. SLL collects predicates as
  119. # it performs closure operations like ANTLR v3 did. It delays predicate
  120. # evaluation until it reaches and accept state. This allows us to cache the SLL
  121. # ATN simulation whereas, if we had evaluated predicates on-the-fly during
  122. # closure, the DFA state configuration sets would be different and we couldn't
  123. # build up a suitable DFA.</p>
  124. #
  125. # <p>
  126. # When building a DFA accept state during ATN simulation, we evaluate any
  127. # predicates and return the sole semantically valid alternative. If there is
  128. # more than 1 alternative, we report an ambiguity. If there are 0 alternatives,
  129. # we throw an exception. Alternatives without predicates act like they have
  130. # true predicates. The simple way to think about it is to strip away all
  131. # alternatives with false predicates and choose the minimum alternative that
  132. # remains.</p>
  133. #
  134. # <p>
  135. # When we start in the DFA and reach an accept state that's predicated, we test
  136. # those and return the minimum semantically viable alternative. If no
  137. # alternatives are viable, we throw an exception.</p>
  138. #
  139. # <p>
  140. # During full LL ATN simulation, closure always evaluates predicates and
  141. # on-the-fly. This is crucial to reducing the configuration set size during
  142. # closure. It hits a landmine when parsing with the Java grammar, for example,
  143. # without this on-the-fly evaluation.</p>
  144. #
  145. # <p>
  146. # <strong>SHARING DFA</strong></p>
  147. #
  148. # <p>
  149. # All instances of the same parser share the same decision DFAs through a
  150. # static field. Each instance gets its own ATN simulator but they share the
  151. # same {@link #decisionToDFA} field. They also share a
  152. # {@link PredictionContextCache} object that makes sure that all
  153. # {@link PredictionContext} objects are shared among the DFA states. This makes
  154. # a big size difference.</p>
  155. #
  156. # <p>
  157. # <strong>THREAD SAFETY</strong></p>
  158. #
  159. # <p>
  160. # The {@link ParserATNSimulator} locks on the {@link #decisionToDFA} field when
  161. # it adds a new DFA object to that array. {@link #addDFAEdge}
  162. # locks on the DFA for the current decision when setting the
  163. # {@link DFAState#edges} field. {@link #addDFAState} locks on
  164. # the DFA for the current decision when looking up a DFA state to see if it
  165. # already exists. We must make sure that all requests to add DFA states that
  166. # are equivalent result in the same shared DFA object. This is because lots of
  167. # threads will be trying to update the DFA at once. The
  168. # {@link #addDFAState} method also locks inside the DFA lock
  169. # but this time on the shared context cache when it rebuilds the
  170. # configurations' {@link PredictionContext} objects using cached
  171. # subgraphs/nodes. No other locking occurs, even during DFA simulation. This is
  172. # safe as long as we can guarantee that all threads referencing
  173. # {@code s.edge[t]} get the same physical target {@link DFAState}, or
  174. # {@code null}. Once into the DFA, the DFA simulation does not reference the
  175. # {@link DFA#states} map. It follows the {@link DFAState#edges} field to new
  176. # targets. The DFA simulator will either find {@link DFAState#edges} to be
  177. # {@code null}, to be non-{@code null} and {@code dfa.edges[t]} null, or
  178. # {@code dfa.edges[t]} to be non-null. The
  179. # {@link #addDFAEdge} method could be racing to set the field
  180. # but in either case the DFA simulator works; if {@code null}, and requests ATN
  181. # simulation. It could also race trying to get {@code dfa.edges[t]}, but either
  182. # way it will work because it's not doing a test and set operation.</p>
  183. #
  184. # <p>
  185. # <strong>Starting with SLL then failing to combined SLL/LL (Two-Stage
  186. # Parsing)</strong></p>
  187. #
  188. # <p>
  189. # Sam pointed out that if SLL does not give a syntax error, then there is no
  190. # point in doing full LL, which is slower. We only have to try LL if we get a
  191. # syntax error. For maximum speed, Sam starts the parser set to pure SLL
  192. # mode with the {@link BailErrorStrategy}:</p>
  193. #
  194. # <pre>
  195. # parser.{@link Parser#getInterpreter() getInterpreter()}.{@link #setPredictionMode setPredictionMode}{@code (}{@link PredictionMode#SLL}{@code )};
  196. # parser.{@link Parser#setErrorHandler setErrorHandler}(new {@link BailErrorStrategy}());
  197. # </pre>
  198. #
  199. # <p>
  200. # If it does not get a syntax error, then we're done. If it does get a syntax
  201. # error, we need to retry with the combined SLL/LL strategy.</p>
  202. #
  203. # <p>
  204. # The reason this works is as follows. If there are no SLL conflicts, then the
  205. # grammar is SLL (at least for that input set). If there is an SLL conflict,
  206. # the full LL analysis must yield a set of viable alternatives which is a
  207. # subset of the alternatives reported by SLL. If the LL set is a singleton,
  208. # then the grammar is LL but not SLL. If the LL set is the same size as the SLL
  209. # set, the decision is SLL. If the LL set has size &gt; 1, then that decision
  210. # is truly ambiguous on the current input. If the LL set is smaller, then the
  211. # SLL conflict resolution might choose an alternative that the full LL would
  212. # rule out as a possibility based upon better context information. If that's
  213. # the case, then the SLL parse will definitely get an error because the full LL
  214. # analysis says it's not viable. If SLL conflict resolution chooses an
  215. # alternative within the LL set, them both SLL and LL would choose the same
  216. # alternative because they both choose the minimum of multiple conflicting
  217. # alternatives.</p>
  218. #
  219. # <p>
  220. # Let's say we have a set of SLL conflicting alternatives {@code {1, 2, 3}} and
  221. # a smaller LL set called <em>s</em>. If <em>s</em> is {@code {2, 3}}, then SLL
  222. # parsing will get an error because SLL will pursue alternative 1. If
  223. # <em>s</em> is {@code {1, 2}} or {@code {1, 3}} then both SLL and LL will
  224. # choose the same alternative because alternative one is the minimum of either
  225. # set. If <em>s</em> is {@code {2}} or {@code {3}} then SLL will get a syntax
  226. # error. If <em>s</em> is {@code {1}} then SLL will succeed.</p>
  227. #
  228. # <p>
  229. # Of course, if the input is invalid, then we will get an error for sure in
  230. # both SLL and LL parsing. Erroneous input will therefore require 2 passes over
  231. # the input.</p>
  232. #
  233. import sys
  234. from antlr4 import DFA
  235. from antlr4.PredictionContext import PredictionContextCache, PredictionContext, SingletonPredictionContext, \
  236. PredictionContextFromRuleContext
  237. from antlr4.BufferedTokenStream import TokenStream
  238. from antlr4.Parser import Parser
  239. from antlr4.ParserRuleContext import ParserRuleContext
  240. from antlr4.RuleContext import RuleContext
  241. from antlr4.Token import Token
  242. from antlr4.Utils import str_list
  243. from antlr4.atn.ATN import ATN
  244. from antlr4.atn.ATNConfig import ATNConfig
  245. from antlr4.atn.ATNConfigSet import ATNConfigSet
  246. from antlr4.atn.ATNSimulator import ATNSimulator
  247. from antlr4.atn.ATNState import StarLoopEntryState, DecisionState, RuleStopState, ATNState
  248. from antlr4.atn.PredictionMode import PredictionMode
  249. from antlr4.atn.SemanticContext import SemanticContext, AND, andContext, orContext
  250. from antlr4.atn.Transition import Transition, RuleTransition, ActionTransition, PrecedencePredicateTransition, \
  251. PredicateTransition, AtomTransition, SetTransition, NotSetTransition
  252. from antlr4.dfa.DFAState import DFAState, PredPrediction
  253. from antlr4.error.Errors import NoViableAltException
  254. class ParserATNSimulator(ATNSimulator):
  255. __slots__ = (
  256. 'parser', 'decisionToDFA', 'predictionMode', '_input', '_startIndex',
  257. '_outerContext', '_dfa', 'mergeCache'
  258. )
  259. debug = False
  260. debug_list_atn_decisions = False
  261. dfa_debug = False
  262. retry_debug = False
  263. def __init__(self, parser:Parser, atn:ATN, decisionToDFA:list, sharedContextCache:PredictionContextCache):
  264. super().__init__(atn, sharedContextCache)
  265. self.parser = parser
  266. self.decisionToDFA = decisionToDFA
  267. # SLL, LL, or LL + exact ambig detection?#
  268. self.predictionMode = PredictionMode.LL
  269. # LAME globals to avoid parameters!!!!! I need these down deep in predTransition
  270. self._input = None
  271. self._startIndex = 0
  272. self._outerContext = None
  273. self._dfa = None
  274. # Each prediction operation uses a cache for merge of prediction contexts.
  275. # Don't keep around as it wastes huge amounts of memory. DoubleKeyMap
  276. # isn't synchronized but we're ok since two threads shouldn't reuse same
  277. # parser/atnsim object because it can only handle one input at a time.
  278. # This maps graphs a and b to merged result c. (a,b)&rarr;c. We can avoid
  279. # the merge if we ever see a and b again. Note that (b,a)&rarr;c should
  280. # also be examined during cache lookup.
  281. #
  282. self.mergeCache = None
  283. def reset(self):
  284. pass
  285. def adaptivePredict(self, input:TokenStream, decision:int, outerContext:ParserRuleContext):
  286. if ParserATNSimulator.debug or ParserATNSimulator.debug_list_atn_decisions:
  287. print("adaptivePredict decision " + str(decision) +
  288. " exec LA(1)==" + self.getLookaheadName(input) +
  289. " line " + str(input.LT(1).line) + ":" +
  290. str(input.LT(1).column))
  291. self._input = input
  292. self._startIndex = input.index
  293. self._outerContext = outerContext
  294. dfa = self.decisionToDFA[decision]
  295. self._dfa = dfa
  296. m = input.mark()
  297. index = input.index
  298. # Now we are certain to have a specific decision's DFA
  299. # But, do we still need an initial state?
  300. try:
  301. if dfa.precedenceDfa:
  302. # the start state for a precedence DFA depends on the current
  303. # parser precedence, and is provided by a DFA method.
  304. s0 = dfa.getPrecedenceStartState(self.parser.getPrecedence())
  305. else:
  306. # the start state for a "regular" DFA is just s0
  307. s0 = dfa.s0
  308. if s0 is None:
  309. if outerContext is None:
  310. outerContext = ParserRuleContext.EMPTY
  311. if ParserATNSimulator.debug or ParserATNSimulator.debug_list_atn_decisions:
  312. print("predictATN decision " + str(dfa.decision) +
  313. " exec LA(1)==" + self.getLookaheadName(input) +
  314. ", outerContext=" + outerContext.toString(self.parser.literalNames, None))
  315. fullCtx = False
  316. s0_closure = self.computeStartState(dfa.atnStartState, ParserRuleContext.EMPTY, fullCtx)
  317. if dfa.precedenceDfa:
  318. # If this is a precedence DFA, we use applyPrecedenceFilter
  319. # to convert the computed start state to a precedence start
  320. # state. We then use DFA.setPrecedenceStartState to set the
  321. # appropriate start state for the precedence level rather
  322. # than simply setting DFA.s0.
  323. #
  324. dfa.s0.configs = s0_closure # not used for prediction but useful to know start configs anyway
  325. s0_closure = self.applyPrecedenceFilter(s0_closure)
  326. s0 = self.addDFAState(dfa, DFAState(configs=s0_closure))
  327. dfa.setPrecedenceStartState(self.parser.getPrecedence(), s0)
  328. else:
  329. s0 = self.addDFAState(dfa, DFAState(configs=s0_closure))
  330. dfa.s0 = s0
  331. alt = self.execATN(dfa, s0, input, index, outerContext)
  332. if ParserATNSimulator.debug:
  333. print("DFA after predictATN: " + dfa.toString(self.parser.literalNames))
  334. return alt
  335. finally:
  336. self._dfa = None
  337. self.mergeCache = None # wack cache after each prediction
  338. input.seek(index)
  339. input.release(m)
  340. # Performs ATN simulation to compute a predicted alternative based
  341. # upon the remaining input, but also updates the DFA cache to avoid
  342. # having to traverse the ATN again for the same input sequence.
  343. # There are some key conditions we're looking for after computing a new
  344. # set of ATN configs (proposed DFA state):
  345. # if the set is empty, there is no viable alternative for current symbol
  346. # does the state uniquely predict an alternative?
  347. # does the state have a conflict that would prevent us from
  348. # putting it on the work list?
  349. # We also have some key operations to do:
  350. # add an edge from previous DFA state to potentially new DFA state, D,
  351. # upon current symbol but only if adding to work list, which means in all
  352. # cases except no viable alternative (and possibly non-greedy decisions?)
  353. # collecting predicates and adding semantic context to DFA accept states
  354. # adding rule context to context-sensitive DFA accept states
  355. # consuming an input symbol
  356. # reporting a conflict
  357. # reporting an ambiguity
  358. # reporting a context sensitivity
  359. # reporting insufficient predicates
  360. # cover these cases:
  361. # dead end
  362. # single alt
  363. # single alt + preds
  364. # conflict
  365. # conflict + preds
  366. #
  367. def execATN(self, dfa:DFA, s0:DFAState, input:TokenStream, startIndex:int, outerContext:ParserRuleContext ):
  368. if ParserATNSimulator.debug or ParserATNSimulator.debug_list_atn_decisions:
  369. print("execATN decision " + str(dfa.decision) +
  370. " exec LA(1)==" + self.getLookaheadName(input) +
  371. " line " + str(input.LT(1).line) + ":" + str(input.LT(1).column))
  372. previousD = s0
  373. if ParserATNSimulator.debug:
  374. print("s0 = " + str(s0))
  375. t = input.LA(1)
  376. while True: # while more work
  377. D = self.getExistingTargetState(previousD, t)
  378. if D is None:
  379. D = self.computeTargetState(dfa, previousD, t)
  380. if D is self.ERROR:
  381. # if any configs in previous dipped into outer context, that
  382. # means that input up to t actually finished entry rule
  383. # at least for SLL decision. Full LL doesn't dip into outer
  384. # so don't need special case.
  385. # We will get an error no matter what so delay until after
  386. # decision; better error message. Also, no reachable target
  387. # ATN states in SLL implies LL will also get nowhere.
  388. # If conflict in states that dip out, choose min since we
  389. # will get error no matter what.
  390. e = self.noViableAlt(input, outerContext, previousD.configs, startIndex)
  391. input.seek(startIndex)
  392. alt = self.getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previousD.configs, outerContext)
  393. if alt!=ATN.INVALID_ALT_NUMBER:
  394. return alt
  395. raise e
  396. if D.requiresFullContext and self.predictionMode != PredictionMode.SLL:
  397. # IF PREDS, MIGHT RESOLVE TO SINGLE ALT => SLL (or syntax error)
  398. conflictingAlts = D.configs.conflictingAlts
  399. if D.predicates is not None:
  400. if ParserATNSimulator.debug:
  401. print("DFA state has preds in DFA sim LL failover")
  402. conflictIndex = input.index
  403. if conflictIndex != startIndex:
  404. input.seek(startIndex)
  405. conflictingAlts = self.evalSemanticContext(D.predicates, outerContext, True)
  406. if len(conflictingAlts)==1:
  407. if ParserATNSimulator.debug:
  408. print("Full LL avoided")
  409. return min(conflictingAlts)
  410. if conflictIndex != startIndex:
  411. # restore the index so reporting the fallback to full
  412. # context occurs with the index at the correct spot
  413. input.seek(conflictIndex)
  414. if ParserATNSimulator.dfa_debug:
  415. print("ctx sensitive state " + str(outerContext) +" in " + str(D))
  416. fullCtx = True
  417. s0_closure = self.computeStartState(dfa.atnStartState, outerContext, fullCtx)
  418. self.reportAttemptingFullContext(dfa, conflictingAlts, D.configs, startIndex, input.index)
  419. alt = self.execATNWithFullContext(dfa, D, s0_closure, input, startIndex, outerContext)
  420. return alt
  421. if D.isAcceptState:
  422. if D.predicates is None:
  423. return D.prediction
  424. stopIndex = input.index
  425. input.seek(startIndex)
  426. alts = self.evalSemanticContext(D.predicates, outerContext, True)
  427. if len(alts)==0:
  428. raise self.noViableAlt(input, outerContext, D.configs, startIndex)
  429. elif len(alts)==1:
  430. return min(alts)
  431. else:
  432. # report ambiguity after predicate evaluation to make sure the correct
  433. # set of ambig alts is reported.
  434. self.reportAmbiguity(dfa, D, startIndex, stopIndex, False, alts, D.configs)
  435. return min(alts)
  436. previousD = D
  437. if t != Token.EOF:
  438. input.consume()
  439. t = input.LA(1)
  440. #
  441. # Get an existing target state for an edge in the DFA. If the target state
  442. # for the edge has not yet been computed or is otherwise not available,
  443. # this method returns {@code null}.
  444. #
  445. # @param previousD The current DFA state
  446. # @param t The next input symbol
  447. # @return The existing target DFA state for the given input symbol
  448. # {@code t}, or {@code null} if the target state for this edge is not
  449. # already cached
  450. #
  451. def getExistingTargetState(self, previousD:DFAState, t:int):
  452. edges = previousD.edges
  453. if edges is None or t + 1 < 0 or t + 1 >= len(edges):
  454. return None
  455. else:
  456. return edges[t + 1]
  457. #
  458. # Compute a target state for an edge in the DFA, and attempt to add the
  459. # computed state and corresponding edge to the DFA.
  460. #
  461. # @param dfa The DFA
  462. # @param previousD The current DFA state
  463. # @param t The next input symbol
  464. #
  465. # @return The computed target DFA state for the given input symbol
  466. # {@code t}. If {@code t} does not lead to a valid DFA state, this method
  467. # returns {@link #ERROR}.
  468. #
  469. def computeTargetState(self, dfa:DFA, previousD:DFAState, t:int):
  470. reach = self.computeReachSet(previousD.configs, t, False)
  471. if reach is None:
  472. self.addDFAEdge(dfa, previousD, t, self.ERROR)
  473. return self.ERROR
  474. # create new target state; we'll add to DFA after it's complete
  475. D = DFAState(configs=reach)
  476. predictedAlt = self.getUniqueAlt(reach)
  477. if ParserATNSimulator.debug:
  478. altSubSets = PredictionMode.getConflictingAltSubsets(reach)
  479. print("SLL altSubSets=" + str(altSubSets) + ", configs=" + str(reach) +
  480. ", predict=" + str(predictedAlt) + ", allSubsetsConflict=" +
  481. str(PredictionMode.allSubsetsConflict(altSubSets)) + ", conflictingAlts=" +
  482. str(self.getConflictingAlts(reach)))
  483. if predictedAlt!=ATN.INVALID_ALT_NUMBER:
  484. # NO CONFLICT, UNIQUELY PREDICTED ALT
  485. D.isAcceptState = True
  486. D.configs.uniqueAlt = predictedAlt
  487. D.prediction = predictedAlt
  488. elif PredictionMode.hasSLLConflictTerminatingPrediction(self.predictionMode, reach):
  489. # MORE THAN ONE VIABLE ALTERNATIVE
  490. D.configs.conflictingAlts = self.getConflictingAlts(reach)
  491. D.requiresFullContext = True
  492. # in SLL-only mode, we will stop at this state and return the minimum alt
  493. D.isAcceptState = True
  494. D.prediction = min(D.configs.conflictingAlts)
  495. if D.isAcceptState and D.configs.hasSemanticContext:
  496. self.predicateDFAState(D, self.atn.getDecisionState(dfa.decision))
  497. if D.predicates is not None:
  498. D.prediction = ATN.INVALID_ALT_NUMBER
  499. # all adds to dfa are done after we've created full D state
  500. D = self.addDFAEdge(dfa, previousD, t, D)
  501. return D
  502. def predicateDFAState(self, dfaState:DFAState, decisionState:DecisionState):
  503. # We need to test all predicates, even in DFA states that
  504. # uniquely predict alternative.
  505. nalts = len(decisionState.transitions)
  506. # Update DFA so reach becomes accept state with (predicate,alt)
  507. # pairs if preds found for conflicting alts
  508. altsToCollectPredsFrom = self.getConflictingAltsOrUniqueAlt(dfaState.configs)
  509. altToPred = self.getPredsForAmbigAlts(altsToCollectPredsFrom, dfaState.configs, nalts)
  510. if altToPred is not None:
  511. dfaState.predicates = self.getPredicatePredictions(altsToCollectPredsFrom, altToPred)
  512. dfaState.prediction = ATN.INVALID_ALT_NUMBER # make sure we use preds
  513. else:
  514. # There are preds in configs but they might go away
  515. # when OR'd together like {p}? || NONE == NONE. If neither
  516. # alt has preds, resolve to min alt
  517. dfaState.prediction = min(altsToCollectPredsFrom)
  518. # comes back with reach.uniqueAlt set to a valid alt
  519. def execATNWithFullContext(self, dfa:DFA, D:DFAState, # how far we got before failing over
  520. s0:ATNConfigSet,
  521. input:TokenStream,
  522. startIndex:int,
  523. outerContext:ParserRuleContext):
  524. if ParserATNSimulator.debug or ParserATNSimulator.debug_list_atn_decisions:
  525. print("execATNWithFullContext", str(s0))
  526. fullCtx = True
  527. foundExactAmbig = False
  528. reach = None
  529. previous = s0
  530. input.seek(startIndex)
  531. t = input.LA(1)
  532. predictedAlt = -1
  533. while (True): # while more work
  534. reach = self.computeReachSet(previous, t, fullCtx)
  535. if reach is None:
  536. # if any configs in previous dipped into outer context, that
  537. # means that input up to t actually finished entry rule
  538. # at least for LL decision. Full LL doesn't dip into outer
  539. # so don't need special case.
  540. # We will get an error no matter what so delay until after
  541. # decision; better error message. Also, no reachable target
  542. # ATN states in SLL implies LL will also get nowhere.
  543. # If conflict in states that dip out, choose min since we
  544. # will get error no matter what.
  545. e = self.noViableAlt(input, outerContext, previous, startIndex)
  546. input.seek(startIndex)
  547. alt = self.getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(previous, outerContext)
  548. if alt!=ATN.INVALID_ALT_NUMBER:
  549. return alt
  550. else:
  551. raise e
  552. altSubSets = PredictionMode.getConflictingAltSubsets(reach)
  553. if ParserATNSimulator.debug:
  554. print("LL altSubSets=" + str(altSubSets) + ", predict=" +
  555. str(PredictionMode.getUniqueAlt(altSubSets)) + ", resolvesToJustOneViableAlt=" +
  556. str(PredictionMode.resolvesToJustOneViableAlt(altSubSets)))
  557. reach.uniqueAlt = self.getUniqueAlt(reach)
  558. # unique prediction?
  559. if reach.uniqueAlt!=ATN.INVALID_ALT_NUMBER:
  560. predictedAlt = reach.uniqueAlt
  561. break
  562. elif self.predictionMode is not PredictionMode.LL_EXACT_AMBIG_DETECTION:
  563. predictedAlt = PredictionMode.resolvesToJustOneViableAlt(altSubSets)
  564. if predictedAlt != ATN.INVALID_ALT_NUMBER:
  565. break
  566. else:
  567. # In exact ambiguity mode, we never try to terminate early.
  568. # Just keeps scarfing until we know what the conflict is
  569. if PredictionMode.allSubsetsConflict(altSubSets) and PredictionMode.allSubsetsEqual(altSubSets):
  570. foundExactAmbig = True
  571. predictedAlt = PredictionMode.getSingleViableAlt(altSubSets)
  572. break
  573. # else there are multiple non-conflicting subsets or
  574. # we're not sure what the ambiguity is yet.
  575. # So, keep going.
  576. previous = reach
  577. if t != Token.EOF:
  578. input.consume()
  579. t = input.LA(1)
  580. # If the configuration set uniquely predicts an alternative,
  581. # without conflict, then we know that it's a full LL decision
  582. # not SLL.
  583. if reach.uniqueAlt != ATN.INVALID_ALT_NUMBER :
  584. self.reportContextSensitivity(dfa, predictedAlt, reach, startIndex, input.index)
  585. return predictedAlt
  586. # We do not check predicates here because we have checked them
  587. # on-the-fly when doing full context prediction.
  588. #
  589. # In non-exact ambiguity detection mode, we might actually be able to
  590. # detect an exact ambiguity, but I'm not going to spend the cycles
  591. # needed to check. We only emit ambiguity warnings in exact ambiguity
  592. # mode.
  593. #
  594. # For example, we might know that we have conflicting configurations.
  595. # But, that does not mean that there is no way forward without a
  596. # conflict. It's possible to have nonconflicting alt subsets as in:
  597. # altSubSets=[{1, 2}, {1, 2}, {1}, {1, 2}]
  598. # from
  599. #
  600. # [(17,1,[5 $]), (13,1,[5 10 $]), (21,1,[5 10 $]), (11,1,[$]),
  601. # (13,2,[5 10 $]), (21,2,[5 10 $]), (11,2,[$])]
  602. #
  603. # In this case, (17,1,[5 $]) indicates there is some next sequence that
  604. # would resolve this without conflict to alternative 1. Any other viable
  605. # next sequence, however, is associated with a conflict. We stop
  606. # looking for input because no amount of further lookahead will alter
  607. # the fact that we should predict alternative 1. We just can't say for
  608. # sure that there is an ambiguity without looking further.
  609. self.reportAmbiguity(dfa, D, startIndex, input.index, foundExactAmbig, None, reach)
  610. return predictedAlt
  611. def computeReachSet(self, closure:ATNConfigSet, t:int, fullCtx:bool):
  612. if ParserATNSimulator.debug:
  613. print("in computeReachSet, starting closure: " + str(closure))
  614. if self.mergeCache is None:
  615. self.mergeCache = dict()
  616. intermediate = ATNConfigSet(fullCtx)
  617. # Configurations already in a rule stop state indicate reaching the end
  618. # of the decision rule (local context) or end of the start rule (full
  619. # context). Once reached, these configurations are never updated by a
  620. # closure operation, so they are handled separately for the performance
  621. # advantage of having a smaller intermediate set when calling closure.
  622. #
  623. # For full-context reach operations, separate handling is required to
  624. # ensure that the alternative matching the longest overall sequence is
  625. # chosen when multiple such configurations can match the input.
  626. skippedStopStates = None
  627. # First figure out where we can reach on input t
  628. for c in closure:
  629. if ParserATNSimulator.debug:
  630. print("testing " + self.getTokenName(t) + " at " + str(c))
  631. if isinstance(c.state, RuleStopState):
  632. if fullCtx or t == Token.EOF:
  633. if skippedStopStates is None:
  634. skippedStopStates = list()
  635. skippedStopStates.append(c)
  636. continue
  637. for trans in c.state.transitions:
  638. target = self.getReachableTarget(trans, t)
  639. if target is not None:
  640. intermediate.add(ATNConfig(state=target, config=c), self.mergeCache)
  641. # Now figure out where the reach operation can take us...
  642. reach = None
  643. # This block optimizes the reach operation for intermediate sets which
  644. # trivially indicate a termination state for the overall
  645. # adaptivePredict operation.
  646. #
  647. # The conditions assume that intermediate
  648. # contains all configurations relevant to the reach set, but this
  649. # condition is not true when one or more configurations have been
  650. # withheld in skippedStopStates, or when the current symbol is EOF.
  651. #
  652. if skippedStopStates is None and t!=Token.EOF:
  653. if len(intermediate)==1:
  654. # Don't pursue the closure if there is just one state.
  655. # It can only have one alternative; just add to result
  656. # Also don't pursue the closure if there is unique alternative
  657. # among the configurations.
  658. reach = intermediate
  659. elif self.getUniqueAlt(intermediate)!=ATN.INVALID_ALT_NUMBER:
  660. # Also don't pursue the closure if there is unique alternative
  661. # among the configurations.
  662. reach = intermediate
  663. # If the reach set could not be trivially determined, perform a closure
  664. # operation on the intermediate set to compute its initial value.
  665. #
  666. if reach is None:
  667. reach = ATNConfigSet(fullCtx)
  668. closureBusy = set()
  669. treatEofAsEpsilon = t == Token.EOF
  670. for c in intermediate:
  671. self.closure(c, reach, closureBusy, False, fullCtx, treatEofAsEpsilon)
  672. if t == Token.EOF:
  673. # After consuming EOF no additional input is possible, so we are
  674. # only interested in configurations which reached the end of the
  675. # decision rule (local context) or end of the start rule (full
  676. # context). Update reach to contain only these configurations. This
  677. # handles both explicit EOF transitions in the grammar and implicit
  678. # EOF transitions following the end of the decision or start rule.
  679. #
  680. # When reach==intermediate, no closure operation was performed. In
  681. # this case, removeAllConfigsNotInRuleStopState needs to check for
  682. # reachable rule stop states as well as configurations already in
  683. # a rule stop state.
  684. #
  685. # This is handled before the configurations in skippedStopStates,
  686. # because any configurations potentially added from that list are
  687. # already guaranteed to meet this condition whether or not it's
  688. # required.
  689. #
  690. reach = self.removeAllConfigsNotInRuleStopState(reach, reach is intermediate)
  691. # If skippedStopStates is not null, then it contains at least one
  692. # configuration. For full-context reach operations, these
  693. # configurations reached the end of the start rule, in which case we
  694. # only add them back to reach if no configuration during the current
  695. # closure operation reached such a state. This ensures adaptivePredict
  696. # chooses an alternative matching the longest overall sequence when
  697. # multiple alternatives are viable.
  698. #
  699. if skippedStopStates is not None and ( (not fullCtx) or (not PredictionMode.hasConfigInRuleStopState(reach))):
  700. for c in skippedStopStates:
  701. reach.add(c, self.mergeCache)
  702. if len(reach)==0:
  703. return None
  704. else:
  705. return reach
  706. #
  707. # Return a configuration set containing only the configurations from
  708. # {@code configs} which are in a {@link RuleStopState}. If all
  709. # configurations in {@code configs} are already in a rule stop state, this
  710. # method simply returns {@code configs}.
  711. #
  712. # <p>When {@code lookToEndOfRule} is true, this method uses
  713. # {@link ATN#nextTokens} for each configuration in {@code configs} which is
  714. # not already in a rule stop state to see if a rule stop state is reachable
  715. # from the configuration via epsilon-only transitions.</p>
  716. #
  717. # @param configs the configuration set to update
  718. # @param lookToEndOfRule when true, this method checks for rule stop states
  719. # reachable by epsilon-only transitions from each configuration in
  720. # {@code configs}.
  721. #
  722. # @return {@code configs} if all configurations in {@code configs} are in a
  723. # rule stop state, otherwise return a new configuration set containing only
  724. # the configurations from {@code configs} which are in a rule stop state
  725. #
  726. def removeAllConfigsNotInRuleStopState(self, configs:ATNConfigSet, lookToEndOfRule:bool):
  727. if PredictionMode.allConfigsInRuleStopStates(configs):
  728. return configs
  729. result = ATNConfigSet(configs.fullCtx)
  730. for config in configs:
  731. if isinstance(config.state, RuleStopState):
  732. result.add(config, self.mergeCache)
  733. continue
  734. if lookToEndOfRule and config.state.epsilonOnlyTransitions:
  735. nextTokens = self.atn.nextTokens(config.state)
  736. if Token.EPSILON in nextTokens:
  737. endOfRuleState = self.atn.ruleToStopState[config.state.ruleIndex]
  738. result.add(ATNConfig(state=endOfRuleState, config=config), self.mergeCache)
  739. return result
  740. def computeStartState(self, p:ATNState, ctx:RuleContext, fullCtx:bool):
  741. # always at least the implicit call to start rule
  742. initialContext = PredictionContextFromRuleContext(self.atn, ctx)
  743. configs = ATNConfigSet(fullCtx)
  744. for i in range(0, len(p.transitions)):
  745. target = p.transitions[i].target
  746. c = ATNConfig(target, i+1, initialContext)
  747. closureBusy = set()
  748. self.closure(c, configs, closureBusy, True, fullCtx, False)
  749. return configs
  750. #
  751. # This method transforms the start state computed by
  752. # {@link #computeStartState} to the special start state used by a
  753. # precedence DFA for a particular precedence value. The transformation
  754. # process applies the following changes to the start state's configuration
  755. # set.
  756. #
  757. # <ol>
  758. # <li>Evaluate the precedence predicates for each configuration using
  759. # {@link SemanticContext#evalPrecedence}.</li>
  760. # <li>Remove all configurations which predict an alternative greater than
  761. # 1, for which another configuration that predicts alternative 1 is in the
  762. # same ATN state with the same prediction context. This transformation is
  763. # valid for the following reasons:
  764. # <ul>
  765. # <li>The closure block cannot contain any epsilon transitions which bypass
  766. # the body of the closure, so all states reachable via alternative 1 are
  767. # part of the precedence alternatives of the transformed left-recursive
  768. # rule.</li>
  769. # <li>The "primary" portion of a left recursive rule cannot contain an
  770. # epsilon transition, so the only way an alternative other than 1 can exist
  771. # in a state that is also reachable via alternative 1 is by nesting calls
  772. # to the left-recursive rule, with the outer calls not being at the
  773. # preferred precedence level.</li>
  774. # </ul>
  775. # </li>
  776. # </ol>
  777. #
  778. # <p>
  779. # The prediction context must be considered by this filter to address
  780. # situations like the following.
  781. # </p>
  782. # <code>
  783. # <pre>
  784. # grammar TA;
  785. # prog: statement* EOF;
  786. # statement: letterA | statement letterA 'b' ;
  787. # letterA: 'a';
  788. # </pre>
  789. # </code>
  790. # <p>
  791. # If the above grammar, the ATN state immediately before the token
  792. # reference {@code 'a'} in {@code letterA} is reachable from the left edge
  793. # of both the primary and closure blocks of the left-recursive rule
  794. # {@code statement}. The prediction context associated with each of these
  795. # configurations distinguishes between them, and prevents the alternative
  796. # which stepped out to {@code prog} (and then back in to {@code statement}
  797. # from being eliminated by the filter.
  798. # </p>
  799. #
  800. # @param configs The configuration set computed by
  801. # {@link #computeStartState} as the start state for the DFA.
  802. # @return The transformed configuration set representing the start state
  803. # for a precedence DFA at a particular precedence level (determined by
  804. # calling {@link Parser#getPrecedence}).
  805. #
  806. def applyPrecedenceFilter(self, configs:ATNConfigSet):
  807. statesFromAlt1 = dict()
  808. configSet = ATNConfigSet(configs.fullCtx)
  809. for config in configs:
  810. # handle alt 1 first
  811. if config.alt != 1:
  812. continue
  813. updatedContext = config.semanticContext.evalPrecedence(self.parser, self._outerContext)
  814. if updatedContext is None:
  815. # the configuration was eliminated
  816. continue
  817. statesFromAlt1[config.state.stateNumber] = config.context
  818. if updatedContext is not config.semanticContext:
  819. configSet.add(ATNConfig(config=config, semantic=updatedContext), self.mergeCache)
  820. else:
  821. configSet.add(config, self.mergeCache)
  822. for config in configs:
  823. if config.alt == 1:
  824. # already handled
  825. continue
  826. # In the future, this elimination step could be updated to also
  827. # filter the prediction context for alternatives predicting alt>1
  828. # (basically a graph subtraction algorithm).
  829. #
  830. if not config.precedenceFilterSuppressed:
  831. context = statesFromAlt1.get(config.state.stateNumber, None)
  832. if context==config.context:
  833. # eliminated
  834. continue
  835. configSet.add(config, self.mergeCache)
  836. return configSet
  837. def getReachableTarget(self, trans:Transition, ttype:int):
  838. if trans.matches(ttype, 0, self.atn.maxTokenType):
  839. return trans.target
  840. else:
  841. return None
  842. def getPredsForAmbigAlts(self, ambigAlts:set, configs:ATNConfigSet, nalts:int):
  843. # REACH=[1|1|[]|0:0, 1|2|[]|0:1]
  844. # altToPred starts as an array of all null contexts. The entry at index i
  845. # corresponds to alternative i. altToPred[i] may have one of three values:
  846. # 1. null: no ATNConfig c is found such that c.alt==i
  847. # 2. SemanticContext.NONE: At least one ATNConfig c exists such that
  848. # c.alt==i and c.semanticContext==SemanticContext.NONE. In other words,
  849. # alt i has at least one unpredicated config.
  850. # 3. Non-NONE Semantic Context: There exists at least one, and for all
  851. # ATNConfig c such that c.alt==i, c.semanticContext!=SemanticContext.NONE.
  852. #
  853. # From this, it is clear that NONE||anything==NONE.
  854. #
  855. altToPred = [None] * (nalts + 1)
  856. for c in configs:
  857. if c.alt in ambigAlts:
  858. altToPred[c.alt] = orContext(altToPred[c.alt], c.semanticContext)
  859. nPredAlts = 0
  860. for i in range(1, nalts+1):
  861. if altToPred[i] is None:
  862. altToPred[i] = SemanticContext.NONE
  863. elif altToPred[i] is not SemanticContext.NONE:
  864. nPredAlts += 1
  865. # nonambig alts are null in altToPred
  866. if nPredAlts==0:
  867. altToPred = None
  868. if ParserATNSimulator.debug:
  869. print("getPredsForAmbigAlts result " + str_list(altToPred))
  870. return altToPred
  871. def getPredicatePredictions(self, ambigAlts:set, altToPred:list):
  872. pairs = []
  873. containsPredicate = False
  874. for i in range(1, len(altToPred)):
  875. pred = altToPred[i]
  876. # unpredicated is indicated by SemanticContext.NONE
  877. if ambigAlts is not None and i in ambigAlts:
  878. pairs.append(PredPrediction(pred, i))
  879. if pred is not SemanticContext.NONE:
  880. containsPredicate = True
  881. if not containsPredicate:
  882. return None
  883. return pairs
  884. #
  885. # This method is used to improve the localization of error messages by
  886. # choosing an alternative rather than throwing a
  887. # {@link NoViableAltException} in particular prediction scenarios where the
  888. # {@link #ERROR} state was reached during ATN simulation.
  889. #
  890. # <p>
  891. # The default implementation of this method uses the following
  892. # algorithm to identify an ATN configuration which successfully parsed the
  893. # decision entry rule. Choosing such an alternative ensures that the
  894. # {@link ParserRuleContext} returned by the calling rule will be complete
  895. # and valid, and the syntax error will be reported later at a more
  896. # localized location.</p>
  897. #
  898. # <ul>
  899. # <li>If a syntactically valid path or paths reach the end of the decision rule and
  900. # they are semantically valid if predicated, return the min associated alt.</li>
  901. # <li>Else, if a semantically invalid but syntactically valid path exist
  902. # or paths exist, return the minimum associated alt.
  903. # </li>
  904. # <li>Otherwise, return {@link ATN#INVALID_ALT_NUMBER}.</li>
  905. # </ul>
  906. #
  907. # <p>
  908. # In some scenarios, the algorithm described above could predict an
  909. # alternative which will result in a {@link FailedPredicateException} in
  910. # the parser. Specifically, this could occur if the <em>only</em> configuration
  911. # capable of successfully parsing to the end of the decision rule is
  912. # blocked by a semantic predicate. By choosing this alternative within
  913. # {@link #adaptivePredict} instead of throwing a
  914. # {@link NoViableAltException}, the resulting
  915. # {@link FailedPredicateException} in the parser will identify the specific
  916. # predicate which is preventing the parser from successfully parsing the
  917. # decision rule, which helps developers identify and correct logic errors
  918. # in semantic predicates.
  919. # </p>
  920. #
  921. # @param configs The ATN configurations which were valid immediately before
  922. # the {@link #ERROR} state was reached
  923. # @param outerContext The is the \gamma_0 initial parser context from the paper
  924. # or the parser stack at the instant before prediction commences.
  925. #
  926. # @return The value to return from {@link #adaptivePredict}, or
  927. # {@link ATN#INVALID_ALT_NUMBER} if a suitable alternative was not
  928. # identified and {@link #adaptivePredict} should report an error instead.
  929. #
  930. def getSynValidOrSemInvalidAltThatFinishedDecisionEntryRule(self, configs:ATNConfigSet, outerContext:ParserRuleContext):
  931. semValidConfigs, semInvalidConfigs = self.splitAccordingToSemanticValidity(configs, outerContext)
  932. alt = self.getAltThatFinishedDecisionEntryRule(semValidConfigs)
  933. if alt!=ATN.INVALID_ALT_NUMBER: # semantically/syntactically viable path exists
  934. return alt
  935. # Is there a syntactically valid path with a failed pred?
  936. if len(semInvalidConfigs)>0:
  937. alt = self.getAltThatFinishedDecisionEntryRule(semInvalidConfigs)
  938. if alt!=ATN.INVALID_ALT_NUMBER: # syntactically viable path exists
  939. return alt
  940. return ATN.INVALID_ALT_NUMBER
  941. def getAltThatFinishedDecisionEntryRule(self, configs:ATNConfigSet):
  942. alts = set()
  943. for c in configs:
  944. if c.reachesIntoOuterContext>0 or (isinstance(c.state, RuleStopState) and c.context.hasEmptyPath() ):
  945. alts.add(c.alt)
  946. if len(alts)==0:
  947. return ATN.INVALID_ALT_NUMBER
  948. else:
  949. return min(alts)
  950. # Walk the list of configurations and split them according to
  951. # those that have preds evaluating to true/false. If no pred, assume
  952. # true pred and include in succeeded set. Returns Pair of sets.
  953. #
  954. # Create a new set so as not to alter the incoming parameter.
  955. #
  956. # Assumption: the input stream has been restored to the starting point
  957. # prediction, which is where predicates need to evaluate.
  958. #
  959. def splitAccordingToSemanticValidity(self, configs:ATNConfigSet, outerContext:ParserRuleContext):
  960. succeeded = ATNConfigSet(configs.fullCtx)
  961. failed = ATNConfigSet(configs.fullCtx)
  962. for c in configs:
  963. if c.semanticContext is not SemanticContext.NONE:
  964. predicateEvaluationResult = c.semanticContext.eval(self.parser, outerContext)
  965. if predicateEvaluationResult:
  966. succeeded.add(c)
  967. else:
  968. failed.add(c)
  969. else:
  970. succeeded.add(c)
  971. return (succeeded,failed)
  972. # Look through a list of predicate/alt pairs, returning alts for the
  973. # pairs that win. A {@code NONE} predicate indicates an alt containing an
  974. # unpredicated config which behaves as "always true." If !complete
  975. # then we stop at the first predicate that evaluates to true. This
  976. # includes pairs with null predicates.
  977. #
  978. def evalSemanticContext(self, predPredictions:list, outerContext:ParserRuleContext, complete:bool):
  979. predictions = set()
  980. for pair in predPredictions:
  981. if pair.pred is SemanticContext.NONE:
  982. predictions.add(pair.alt)
  983. if not complete:
  984. break
  985. continue
  986. predicateEvaluationResult = pair.pred.eval(self.parser, outerContext)
  987. if ParserATNSimulator.debug or ParserATNSimulator.dfa_debug:
  988. print("eval pred " + str(pair) + "=" + str(predicateEvaluationResult))
  989. if predicateEvaluationResult:
  990. if ParserATNSimulator.debug or ParserATNSimulator.dfa_debug:
  991. print("PREDICT " + str(pair.alt))
  992. predictions.add(pair.alt)
  993. if not complete:
  994. break
  995. return predictions
  996. # TODO: If we are doing predicates, there is no point in pursuing
  997. # closure operations if we reach a DFA state that uniquely predicts
  998. # alternative. We will not be caching that DFA state and it is a
  999. # waste to pursue the closure. Might have to advance when we do
  1000. # ambig detection thought :(
  1001. #
  1002. def closure(self, config:ATNConfig, configs:ATNConfigSet, closureBusy:set, collectPredicates:bool, fullCtx:bool, treatEofAsEpsilon:bool):
  1003. initialDepth = 0
  1004. self.closureCheckingStopState(config, configs, closureBusy, collectPredicates,
  1005. fullCtx, initialDepth, treatEofAsEpsilon)
  1006. def closureCheckingStopState(self, config:ATNConfig, configs:ATNConfigSet, closureBusy:set, collectPredicates:bool, fullCtx:bool, depth:int, treatEofAsEpsilon:bool):
  1007. if ParserATNSimulator.debug:
  1008. print("closure(" + str(config) + ")")
  1009. if isinstance(config.state, RuleStopState):
  1010. # We hit rule end. If we have context info, use it
  1011. # run thru all possible stack tops in ctx
  1012. if not config.context.isEmpty():
  1013. for i in range(0, len(config.context)):
  1014. state = config.context.getReturnState(i)
  1015. if state is PredictionContext.EMPTY_RETURN_STATE:
  1016. if fullCtx:
  1017. configs.add(ATNConfig(state=config.state, context=PredictionContext.EMPTY, config=config), self.mergeCache)
  1018. continue
  1019. else:
  1020. # we have no context info, just chase follow links (if greedy)
  1021. if ParserATNSimulator.debug:
  1022. print("FALLING off rule " + self.getRuleName(config.state.ruleIndex))
  1023. self.closure_(config, configs, closureBusy, collectPredicates,
  1024. fullCtx, depth, treatEofAsEpsilon)
  1025. continue
  1026. returnState = self.atn.states[state]
  1027. newContext = config.context.getParent(i) # "pop" return state
  1028. c = ATNConfig(state=returnState, alt=config.alt, context=newContext, semantic=config.semanticContext)
  1029. # While we have context to pop back from, we may have
  1030. # gotten that context AFTER having falling off a rule.
  1031. # Make sure we track that we are now out of context.
  1032. c.reachesIntoOuterContext = config.reachesIntoOuterContext
  1033. self.closureCheckingStopState(c, configs, closureBusy, collectPredicates, fullCtx, depth - 1, treatEofAsEpsilon)
  1034. return
  1035. elif fullCtx:
  1036. # reached end of start rule
  1037. configs.add(config, self.mergeCache)
  1038. return
  1039. else:
  1040. # else if we have no context info, just chase follow links (if greedy)
  1041. if ParserATNSimulator.debug:
  1042. print("FALLING off rule " + self.getRuleName(config.state.ruleIndex))
  1043. self.closure_(config, configs, closureBusy, collectPredicates, fullCtx, depth, treatEofAsEpsilon)
  1044. # Do the actual work of walking epsilon edges#
  1045. def closure_(self, config:ATNConfig, configs:ATNConfigSet, closureBusy:set, collectPredicates:bool, fullCtx:bool, depth:int, treatEofAsEpsilon:bool):
  1046. p = config.state
  1047. # optimization
  1048. if not p.epsilonOnlyTransitions:
  1049. configs.add(config, self.mergeCache)
  1050. # make sure to not return here, because EOF transitions can act as
  1051. # both epsilon transitions and non-epsilon transitions.
  1052. first = True
  1053. for t in p.transitions:
  1054. if first:
  1055. first = False
  1056. if self.canDropLoopEntryEdgeInLeftRecursiveRule(config):
  1057. continue
  1058. continueCollecting = collectPredicates and not isinstance(t, ActionTransition)
  1059. c = self.getEpsilonTarget(config, t, continueCollecting, depth == 0, fullCtx, treatEofAsEpsilon)
  1060. if c is not None:
  1061. newDepth = depth
  1062. if isinstance( config.state, RuleStopState):
  1063. # target fell off end of rule; mark resulting c as having dipped into outer context
  1064. # We can't get here if incoming config was rule stop and we had context
  1065. # track how far we dip into outer context. Might
  1066. # come in handy and we avoid evaluating context dependent
  1067. # preds if this is > 0.
  1068. if self._dfa is not None and self._dfa.precedenceDfa:
  1069. if t.outermostPrecedenceReturn == self._dfa.atnStartState.ruleIndex:
  1070. c.precedenceFilterSuppressed = True
  1071. c.reachesIntoOuterContext += 1
  1072. if c in closureBusy:
  1073. # avoid infinite recursion for right-recursive rules
  1074. continue
  1075. closureBusy.add(c)
  1076. configs.dipsIntoOuterContext = True # TODO: can remove? only care when we add to set per middle of this method
  1077. newDepth -= 1
  1078. if ParserATNSimulator.debug:
  1079. print("dips into outer ctx: " + str(c))
  1080. else:
  1081. if not t.isEpsilon:
  1082. if c in closureBusy:
  1083. # avoid infinite recursion for EOF* and EOF+
  1084. continue
  1085. closureBusy.add(c)
  1086. if isinstance(t, RuleTransition):
  1087. # latch when newDepth goes negative - once we step out of the entry context we can't return
  1088. if newDepth >= 0:
  1089. newDepth += 1
  1090. self.closureCheckingStopState(c, configs, closureBusy, continueCollecting, fullCtx, newDepth, treatEofAsEpsilon)
  1091. # Implements first-edge (loop entry) elimination as an optimization
  1092. # during closure operations. See antlr/antlr4#1398.
  1093. #
  1094. # The optimization is to avoid adding the loop entry config when
  1095. # the exit path can only lead back to the same
  1096. # StarLoopEntryState after popping context at the rule end state
  1097. # (traversing only epsilon edges, so we're still in closure, in
  1098. # this same rule).
  1099. #
  1100. # We need to detect any state that can reach loop entry on
  1101. # epsilon w/o exiting rule. We don't have to look at FOLLOW
  1102. # links, just ensure that all stack tops for config refer to key
  1103. # states in LR rule.
  1104. #
  1105. # To verify we are in the right situation we must first check
  1106. # closure is at a StarLoopEntryState generated during LR removal.
  1107. # Then we check that each stack top of context is a return state
  1108. # from one of these cases:
  1109. #
  1110. # 1. 'not' expr, '(' type ')' expr. The return state points at loop entry state
  1111. # 2. expr op expr. The return state is the block end of internal block of (...)*
  1112. # 3. 'between' expr 'and' expr. The return state of 2nd expr reference.
  1113. # That state points at block end of internal block of (...)*.
  1114. # 4. expr '?' expr ':' expr. The return state points at block end,
  1115. # which points at loop entry state.
  1116. #
  1117. # If any is true for each stack top, then closure does not add a
  1118. # config to the current config set for edge[0], the loop entry branch.
  1119. #
  1120. # Conditions fail if any context for the current config is:
  1121. #
  1122. # a. empty (we'd fall out of expr to do a global FOLLOW which could
  1123. # even be to some weird spot in expr) or,
  1124. # b. lies outside of expr or,
  1125. # c. lies within expr but at a state not the BlockEndState
  1126. # generated during LR removal
  1127. #
  1128. # Do we need to evaluate predicates ever in closure for this case?
  1129. #
  1130. # No. Predicates, including precedence predicates, are only
  1131. # evaluated when computing a DFA start state. I.e., only before
  1132. # the lookahead (but not parser) consumes a token.
  1133. #
  1134. # There are no epsilon edges allowed in LR rule alt blocks or in
  1135. # the "primary" part (ID here). If closure is in
  1136. # StarLoopEntryState any lookahead operation will have consumed a
  1137. # token as there are no epsilon-paths that lead to
  1138. # StarLoopEntryState. We do not have to evaluate predicates
  1139. # therefore if we are in the generated StarLoopEntryState of a LR
  1140. # rule. Note that when making a prediction starting at that
  1141. # decision point, decision d=2, compute-start-state performs
  1142. # closure starting at edges[0], edges[1] emanating from
  1143. # StarLoopEntryState. That means it is not performing closure on
  1144. # StarLoopEntryState during compute-start-state.
  1145. #
  1146. # How do we know this always gives same prediction answer?
  1147. #
  1148. # Without predicates, loop entry and exit paths are ambiguous
  1149. # upon remaining input +b (in, say, a+b). Either paths lead to
  1150. # valid parses. Closure can lead to consuming + immediately or by
  1151. # falling out of this call to expr back into expr and loop back
  1152. # again to StarLoopEntryState to match +b. In this special case,
  1153. # we choose the more efficient path, which is to take the bypass
  1154. # path.
  1155. #
  1156. # The lookahead language has not changed because closure chooses
  1157. # one path over the other. Both paths lead to consuming the same
  1158. # remaining input during a lookahead operation. If the next token
  1159. # is an operator, lookahead will enter the choice block with
  1160. # operators. If it is not, lookahead will exit expr. Same as if
  1161. # closure had chosen to enter the choice block immediately.
  1162. #
  1163. # Closure is examining one config (some loopentrystate, some alt,
  1164. # context) which means it is considering exactly one alt. Closure
  1165. # always copies the same alt to any derived configs.
  1166. #
  1167. # How do we know this optimization doesn't mess up precedence in
  1168. # our parse trees?
  1169. #
  1170. # Looking through expr from left edge of stat only has to confirm
  1171. # that an input, say, a+b+c; begins with any valid interpretation
  1172. # of an expression. The precedence actually doesn't matter when
  1173. # making a decision in stat seeing through expr. It is only when
  1174. # parsing rule expr that we must use the precedence to get the
  1175. # right interpretation and, hence, parse tree.
  1176. #
  1177. # @since 4.6
  1178. #
  1179. def canDropLoopEntryEdgeInLeftRecursiveRule(self, config):
  1180. # return False
  1181. p = config.state
  1182. # First check to see if we are in StarLoopEntryState generated during
  1183. # left-recursion elimination. For efficiency, also check if
  1184. # the context has an empty stack case. If so, it would mean
  1185. # global FOLLOW so we can't perform optimization
  1186. # Are we the special loop entry/exit state? or SLL wildcard
  1187. if p.stateType != ATNState.STAR_LOOP_ENTRY \
  1188. or not p.isPrecedenceDecision \
  1189. or config.context.isEmpty() \
  1190. or config.context.hasEmptyPath():
  1191. return False
  1192. # Require all return states to return back to the same rule
  1193. # that p is in.
  1194. numCtxs = len(config.context)
  1195. for i in range(0, numCtxs): # for each stack context
  1196. returnState = self.atn.states[config.context.getReturnState(i)]
  1197. if returnState.ruleIndex != p.ruleIndex:
  1198. return False
  1199. decisionStartState = p.transitions[0].target
  1200. blockEndStateNum = decisionStartState.endState.stateNumber
  1201. blockEndState = self.atn.states[blockEndStateNum]
  1202. # Verify that the top of each stack context leads to loop entry/exit
  1203. # state through epsilon edges and w/o leaving rule.
  1204. for i in range(0, numCtxs): # for each stack context
  1205. returnStateNumber = config.context.getReturnState(i)
  1206. returnState = self.atn.states[returnStateNumber]
  1207. # all states must have single outgoing epsilon edge
  1208. if len(returnState.transitions) != 1 or not returnState.transitions[0].isEpsilon:
  1209. return False
  1210. # Look for prefix op case like 'not expr', (' type ')' expr
  1211. returnStateTarget = returnState.transitions[0].target
  1212. if returnState.stateType == ATNState.BLOCK_END and returnStateTarget is p:
  1213. continue
  1214. # Look for 'expr op expr' or case where expr's return state is block end
  1215. # of (...)* internal block; the block end points to loop back
  1216. # which points to p but we don't need to check that
  1217. if returnState is blockEndState:
  1218. continue
  1219. # Look for ternary expr ? expr : expr. The return state points at block end,
  1220. # which points at loop entry state
  1221. if returnStateTarget is blockEndState:
  1222. continue
  1223. # Look for complex prefix 'between expr and expr' case where 2nd expr's
  1224. # return state points at block end state of (...)* internal block
  1225. if returnStateTarget.stateType == ATNState.BLOCK_END \
  1226. and len(returnStateTarget.transitions) == 1 \
  1227. and returnStateTarget.transitions[0].isEpsilon \
  1228. and returnStateTarget.transitions[0].target is p:
  1229. continue
  1230. # anything else ain't conforming
  1231. return False
  1232. return True
  1233. def getRuleName(self, index:int):
  1234. if self.parser is not None and index>=0:
  1235. return self.parser.ruleNames[index]
  1236. else:
  1237. return "<rule " + str(index) + ">"
  1238. epsilonTargetMethods = dict()
  1239. epsilonTargetMethods[Transition.RULE] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
  1240. sim.ruleTransition(config, t)
  1241. epsilonTargetMethods[Transition.PRECEDENCE] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
  1242. sim.precedenceTransition(config, t, collectPredicates, inContext, fullCtx)
  1243. epsilonTargetMethods[Transition.PREDICATE] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
  1244. sim.predTransition(config, t, collectPredicates, inContext, fullCtx)
  1245. epsilonTargetMethods[Transition.ACTION] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
  1246. sim.actionTransition(config, t)
  1247. epsilonTargetMethods[Transition.EPSILON] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
  1248. ATNConfig(state=t.target, config=config)
  1249. epsilonTargetMethods[Transition.ATOM] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
  1250. ATNConfig(state=t.target, config=config) if treatEofAsEpsilon and t.matches(Token.EOF, 0, 1) else None
  1251. epsilonTargetMethods[Transition.RANGE] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
  1252. ATNConfig(state=t.target, config=config) if treatEofAsEpsilon and t.matches(Token.EOF, 0, 1) else None
  1253. epsilonTargetMethods[Transition.SET] = lambda sim, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon: \
  1254. ATNConfig(state=t.target, config=config) if treatEofAsEpsilon and t.matches(Token.EOF, 0, 1) else None
  1255. def getEpsilonTarget(self, config:ATNConfig, t:Transition, collectPredicates:bool, inContext:bool, fullCtx:bool, treatEofAsEpsilon:bool):
  1256. m = self.epsilonTargetMethods.get(t.serializationType, None)
  1257. if m is None:
  1258. return None
  1259. else:
  1260. return m(self, config, t, collectPredicates, inContext, fullCtx, treatEofAsEpsilon)
  1261. def actionTransition(self, config:ATNConfig, t:ActionTransition):
  1262. if ParserATNSimulator.debug:
  1263. print("ACTION edge " + str(t.ruleIndex) + ":" + str(t.actionIndex))
  1264. return ATNConfig(state=t.target, config=config)
  1265. def precedenceTransition(self, config:ATNConfig, pt:PrecedencePredicateTransition, collectPredicates:bool, inContext:bool, fullCtx:bool):
  1266. if ParserATNSimulator.debug:
  1267. print("PRED (collectPredicates=" + str(collectPredicates) + ") " +
  1268. str(pt.precedence) + ">=_p, ctx dependent=true")
  1269. if self.parser is not None:
  1270. print("context surrounding pred is " + str(self.parser.getRuleInvocationStack()))
  1271. c = None
  1272. if collectPredicates and inContext:
  1273. if fullCtx:
  1274. # In full context mode, we can evaluate predicates on-the-fly
  1275. # during closure, which dramatically reduces the size of
  1276. # the config sets. It also obviates the need to test predicates
  1277. # later during conflict resolution.
  1278. currentPosition = self._input.index
  1279. self._input.seek(self._startIndex)
  1280. predSucceeds = pt.getPredicate().eval(self.parser, self._outerContext)
  1281. self._input.seek(currentPosition)
  1282. if predSucceeds:
  1283. c = ATNConfig(state=pt.target, config=config) # no pred context
  1284. else:
  1285. newSemCtx = andContext(config.semanticContext, pt.getPredicate())
  1286. c = ATNConfig(state=pt.target, semantic=newSemCtx, config=config)
  1287. else:
  1288. c = ATNConfig(state=pt.target, config=config)
  1289. if ParserATNSimulator.debug:
  1290. print("config from pred transition=" + str(c))
  1291. return c
  1292. def predTransition(self, config:ATNConfig, pt:PredicateTransition, collectPredicates:bool, inContext:bool, fullCtx:bool):
  1293. if ParserATNSimulator.debug:
  1294. print("PRED (collectPredicates=" + str(collectPredicates) + ") " + str(pt.ruleIndex) +
  1295. ":" + str(pt.predIndex) + ", ctx dependent=" + str(pt.isCtxDependent))
  1296. if self.parser is not None:
  1297. print("context surrounding pred is " + str(self.parser.getRuleInvocationStack()))
  1298. c = None
  1299. if collectPredicates and (not pt.isCtxDependent or (pt.isCtxDependent and inContext)):
  1300. if fullCtx:
  1301. # In full context mode, we can evaluate predicates on-the-fly
  1302. # during closure, which dramatically reduces the size of
  1303. # the config sets. It also obviates the need to test predicates
  1304. # later during conflict resolution.
  1305. currentPosition = self._input.index
  1306. self._input.seek(self._startIndex)
  1307. predSucceeds = pt.getPredicate().eval(self.parser, self._outerContext)
  1308. self._input.seek(currentPosition)
  1309. if predSucceeds:
  1310. c = ATNConfig(state=pt.target, config=config) # no pred context
  1311. else:
  1312. newSemCtx = andContext(config.semanticContext, pt.getPredicate())
  1313. c = ATNConfig(state=pt.target, semantic=newSemCtx, config=config)
  1314. else:
  1315. c = ATNConfig(state=pt.target, config=config)
  1316. if ParserATNSimulator.debug:
  1317. print("config from pred transition=" + str(c))
  1318. return c
  1319. def ruleTransition(self, config:ATNConfig, t:RuleTransition):
  1320. if ParserATNSimulator.debug:
  1321. print("CALL rule " + self.getRuleName(t.target.ruleIndex) + ", ctx=" + str(config.context))
  1322. returnState = t.followState
  1323. newContext = SingletonPredictionContext.create(config.context, returnState.stateNumber)
  1324. return ATNConfig(state=t.target, context=newContext, config=config )
  1325. def getConflictingAlts(self, configs:ATNConfigSet):
  1326. altsets = PredictionMode.getConflictingAltSubsets(configs)
  1327. return PredictionMode.getAlts(altsets)
  1328. # Sam pointed out a problem with the previous definition, v3, of
  1329. # ambiguous states. If we have another state associated with conflicting
  1330. # alternatives, we should keep going. For example, the following grammar
  1331. #
  1332. # s : (ID | ID ID?) ';' ;
  1333. #
  1334. # When the ATN simulation reaches the state before ';', it has a DFA
  1335. # state that looks like: [12|1|[], 6|2|[], 12|2|[]]. Naturally
  1336. # 12|1|[] and 12|2|[] conflict, but we cannot stop processing this node
  1337. # because alternative to has another way to continue, via [6|2|[]].
  1338. # The key is that we have a single state that has config's only associated
  1339. # with a single alternative, 2, and crucially the state transitions
  1340. # among the configurations are all non-epsilon transitions. That means
  1341. # we don't consider any conflicts that include alternative 2. So, we
  1342. # ignore the conflict between alts 1 and 2. We ignore a set of
  1343. # conflicting alts when there is an intersection with an alternative
  1344. # associated with a single alt state in the state&rarr;config-list map.
  1345. #
  1346. # It's also the case that we might have two conflicting configurations but
  1347. # also a 3rd nonconflicting configuration for a different alternative:
  1348. # [1|1|[], 1|2|[], 8|3|[]]. This can come about from grammar:
  1349. #
  1350. # a : A | A | A B ;
  1351. #
  1352. # After matching input A, we reach the stop state for rule A, state 1.
  1353. # State 8 is the state right before B. Clearly alternatives 1 and 2
  1354. # conflict and no amount of further lookahead will separate the two.
  1355. # However, alternative 3 will be able to continue and so we do not
  1356. # stop working on this state. In the previous example, we're concerned
  1357. # with states associated with the conflicting alternatives. Here alt
  1358. # 3 is not associated with the conflicting configs, but since we can continue
  1359. # looking for input reasonably, I don't declare the state done. We
  1360. # ignore a set of conflicting alts when we have an alternative
  1361. # that we still need to pursue.
  1362. #
  1363. def getConflictingAltsOrUniqueAlt(self, configs:ATNConfigSet):
  1364. conflictingAlts = None
  1365. if configs.uniqueAlt!= ATN.INVALID_ALT_NUMBER:
  1366. conflictingAlts = set()
  1367. conflictingAlts.add(configs.uniqueAlt)
  1368. else:
  1369. conflictingAlts = configs.conflictingAlts
  1370. return conflictingAlts
  1371. def getTokenName(self, t:int):
  1372. if t==Token.EOF:
  1373. return "EOF"
  1374. if self.parser is not None and \
  1375. self.parser.literalNames is not None and \
  1376. t < len(self.parser.literalNames):
  1377. return self.parser.literalNames[t] + "<" + str(t) + ">"
  1378. else:
  1379. return str(t)
  1380. def getLookaheadName(self, input:TokenStream):
  1381. return self.getTokenName(input.LA(1))
  1382. # Used for debugging in adaptivePredict around execATN but I cut
  1383. # it out for clarity now that alg. works well. We can leave this
  1384. # "dead" code for a bit.
  1385. #
  1386. def dumpDeadEndConfigs(self, nvae:NoViableAltException):
  1387. print("dead end configs: ")
  1388. for c in nvae.getDeadEndConfigs():
  1389. trans = "no edges"
  1390. if len(c.state.transitions)>0:
  1391. t = c.state.transitions[0]
  1392. if isinstance(t, AtomTransition):
  1393. trans = "Atom "+ self.getTokenName(t.label)
  1394. elif isinstance(t, SetTransition):
  1395. neg = isinstance(t, NotSetTransition)
  1396. trans = ("~" if neg else "")+"Set "+ str(t.set)
  1397. print(c.toString(self.parser, True) + ":" + trans, file=sys.stderr)
  1398. def noViableAlt(self, input:TokenStream, outerContext:ParserRuleContext, configs:ATNConfigSet, startIndex:int):
  1399. return NoViableAltException(self.parser, input, input.get(startIndex), input.LT(1), configs, outerContext)
  1400. def getUniqueAlt(self, configs:ATNConfigSet):
  1401. alt = ATN.INVALID_ALT_NUMBER
  1402. for c in configs:
  1403. if alt == ATN.INVALID_ALT_NUMBER:
  1404. alt = c.alt # found first alt
  1405. elif c.alt!=alt:
  1406. return ATN.INVALID_ALT_NUMBER
  1407. return alt
  1408. #
  1409. # Add an edge to the DFA, if possible. This method calls
  1410. # {@link #addDFAState} to ensure the {@code to} state is present in the
  1411. # DFA. If {@code from} is {@code null}, or if {@code t} is outside the
  1412. # range of edges that can be represented in the DFA tables, this method
  1413. # returns without adding the edge to the DFA.
  1414. #
  1415. # <p>If {@code to} is {@code null}, this method returns {@code null}.
  1416. # Otherwise, this method returns the {@link DFAState} returned by calling
  1417. # {@link #addDFAState} for the {@code to} state.</p>
  1418. #
  1419. # @param dfa The DFA
  1420. # @param from The source state for the edge
  1421. # @param t The input symbol
  1422. # @param to The target state for the edge
  1423. #
  1424. # @return If {@code to} is {@code null}, this method returns {@code null};
  1425. # otherwise this method returns the result of calling {@link #addDFAState}
  1426. # on {@code to}
  1427. #
  1428. def addDFAEdge(self, dfa:DFA, from_:DFAState, t:int, to:DFAState):
  1429. if ParserATNSimulator.debug:
  1430. print("EDGE " + str(from_) + " -> " + str(to) + " upon " + self.getTokenName(t))
  1431. if to is None:
  1432. return None
  1433. to = self.addDFAState(dfa, to) # used existing if possible not incoming
  1434. if from_ is None or t < -1 or t > self.atn.maxTokenType:
  1435. return to
  1436. if from_.edges is None:
  1437. from_.edges = [None] * (self.atn.maxTokenType + 2)
  1438. from_.edges[t+1] = to # connect
  1439. if ParserATNSimulator.debug:
  1440. names = None if self.parser is None else self.parser.literalNames
  1441. print("DFA=\n" + dfa.toString(names))
  1442. return to
  1443. #
  1444. # Add state {@code D} to the DFA if it is not already present, and return
  1445. # the actual instance stored in the DFA. If a state equivalent to {@code D}
  1446. # is already in the DFA, the existing state is returned. Otherwise this
  1447. # method returns {@code D} after adding it to the DFA.
  1448. #
  1449. # <p>If {@code D} is {@link #ERROR}, this method returns {@link #ERROR} and
  1450. # does not change the DFA.</p>
  1451. #
  1452. # @param dfa The dfa
  1453. # @param D The DFA state to add
  1454. # @return The state stored in the DFA. This will be either the existing
  1455. # state if {@code D} is already in the DFA, or {@code D} itself if the
  1456. # state was not already present.
  1457. #
  1458. def addDFAState(self, dfa:DFA, D:DFAState):
  1459. if D is self.ERROR:
  1460. return D
  1461. existing = dfa.states.get(D, None)
  1462. if existing is not None:
  1463. return existing
  1464. D.stateNumber = len(dfa.states)
  1465. if not D.configs.readonly:
  1466. D.configs.optimizeConfigs(self)
  1467. D.configs.setReadonly(True)
  1468. dfa.states[D] = D
  1469. if ParserATNSimulator.debug:
  1470. print("adding new DFA state: " + str(D))
  1471. return D
  1472. def reportAttemptingFullContext(self, dfa:DFA, conflictingAlts:set, configs:ATNConfigSet, startIndex:int, stopIndex:int):
  1473. if ParserATNSimulator.debug or ParserATNSimulator.retry_debug:
  1474. print("reportAttemptingFullContext decision=" + str(dfa.decision) + ":" + str(configs) +
  1475. ", input=" + self.parser.getTokenStream().getText(startIndex, stopIndex))
  1476. if self.parser is not None:
  1477. self.parser.getErrorListenerDispatch().reportAttemptingFullContext(self.parser, dfa, startIndex, stopIndex, conflictingAlts, configs)
  1478. def reportContextSensitivity(self, dfa:DFA, prediction:int, configs:ATNConfigSet, startIndex:int, stopIndex:int):
  1479. if ParserATNSimulator.debug or ParserATNSimulator.retry_debug:
  1480. print("reportContextSensitivity decision=" + str(dfa.decision) + ":" + str(configs) +
  1481. ", input=" + self.parser.getTokenStream().getText(startIndex, stopIndex))
  1482. if self.parser is not None:
  1483. self.parser.getErrorListenerDispatch().reportContextSensitivity(self.parser, dfa, startIndex, stopIndex, prediction, configs)
  1484. # If context sensitive parsing, we know it's ambiguity not conflict#
  1485. def reportAmbiguity(self, dfa:DFA, D:DFAState, startIndex:int, stopIndex:int,
  1486. exact:bool, ambigAlts:set, configs:ATNConfigSet ):
  1487. if ParserATNSimulator.debug or ParserATNSimulator.retry_debug:
  1488. # ParserATNPathFinder finder = new ParserATNPathFinder(parser, atn);
  1489. # int i = 1;
  1490. # for (Transition t : dfa.atnStartState.transitions) {
  1491. # print("ALT "+i+"=");
  1492. # print(startIndex+".."+stopIndex+", len(input)="+parser.getInputStream().size());
  1493. # TraceTree path = finder.trace(t.target, parser.getContext(), (TokenStream)parser.getInputStream(),
  1494. # startIndex, stopIndex);
  1495. # if ( path!=null ) {
  1496. # print("path = "+path.toStringTree());
  1497. # for (TraceTree leaf : path.leaves) {
  1498. # List<ATNState> states = path.getPathToNode(leaf);
  1499. # print("states="+states);
  1500. # }
  1501. # }
  1502. # i++;
  1503. # }
  1504. print("reportAmbiguity " + str(ambigAlts) + ":" + str(configs) +
  1505. ", input=" + self.parser.getTokenStream().getText(startIndex, stopIndex))
  1506. if self.parser is not None:
  1507. self.parser.getErrorListenerDispatch().reportAmbiguity(self.parser, dfa, startIndex, stopIndex, exact, ambigAlts, configs)