public class GrammarParserInterpreter extends ParserInterpreter
ParserInterpreter
that creates parse trees
that track alternative numbers for subtree roots.Modifier and Type | Class and Description |
---|---|
static class |
GrammarParserInterpreter.BailButConsumeErrorStrategy
We want to stop and track the first error but we cannot bail out like
BailErrorStrategy as consume() constructs trees. |
Parser.TraceListener, Parser.TrimToSizeListener
Modifier and Type | Field and Description |
---|---|
protected BitSet |
decisionStatesThatSetOuterAltNumInContext |
protected Grammar |
g
The grammar associated with this interpreter.
|
protected int[][] |
stateToAltsMap
|
_parentContextStack, atn, decisionToDFA, grammarFileName, overrideDecision, overrideDecisionAlt, overrideDecisionInputIndex, overrideDecisionReached, overrideDecisionRoot, rootContext, ruleNames, sharedContextCache, tokenNames
_buildParseTrees, _ctx, _errHandler, _input, _parseListeners, _precedenceStack, _syntaxErrors, matchedEOF
_interp, EOF
Constructor and Description |
---|
GrammarParserInterpreter(Grammar g,
ATN atn,
TokenStream input) |
GrammarParserInterpreter(Grammar g,
String grammarFileName,
Vocabulary vocabulary,
Collection<String> ruleNames,
ATN atn,
TokenStream input) |
Modifier and Type | Method and Description |
---|---|
protected InterpreterRuleContext |
createInterpreterRuleContext(ParserRuleContext parent,
int invokingStateNumber,
int ruleIndex) |
static ParserInterpreter |
deriveTempParserInterpreter(Grammar g,
Parser originalParser,
TokenStream tokens)
Derive a new parser from an old one that has knowledge of the grammar.
|
BitSet |
findOuterMostDecisionStates()
identify the ATN states where we need to set the outer alt number.
|
static List<ParserRuleContext> |
getAllPossibleParseTrees(Grammar g,
Parser originalParser,
TokenStream tokens,
int decision,
BitSet alts,
int startIndex,
int stopIndex,
int startRuleIndex)
Given an ambiguous parse information, return the list of ambiguous parse trees.
|
static List<ParserRuleContext> |
getLookaheadParseTrees(Grammar g,
ParserInterpreter originalParser,
TokenStream tokens,
int startRuleIndex,
int decision,
int startIndex,
int stopIndex)
Return a list of parse trees, one for each alternative in a decision
given the same input.
|
void |
reset() |
protected int |
visitDecisionState(DecisionState p)
Override this method so that we can record which alternative
was taken at each decision point.
|
addDecisionOverride, enterRecursionRule, getATN, getATNState, getGrammarFileName, getOverrideDecisionRoot, getRootContext, getRuleNames, getTokenNames, getVocabulary, parse, recover, recoverInline, visitRuleStopState, visitState
addContextToParseTree, addParseListener, compileParseTreePattern, compileParseTreePattern, consume, createErrorNode, createTerminalNode, dumpDFA, enterOuterAlt, enterRecursionRule, enterRule, exitRule, getATNWithBypassAlts, getBuildParseTree, getContext, getCurrentToken, getDFAStrings, getErrorHandler, getExpectedTokens, getExpectedTokensWithinCurrentRule, getInputStream, getInvokingContext, getNumberOfSyntaxErrors, getParseInfo, getParseListeners, getPrecedence, getRuleContext, getRuleIndex, getRuleInvocationStack, getRuleInvocationStack, getSourceName, getTokenFactory, getTokenStream, getTrimParseTree, inContext, isExpectedToken, isMatchedEOF, isTrace, match, matchWildcard, notifyErrorListeners, notifyErrorListeners, precpred, pushNewRecursionContext, removeParseListener, removeParseListeners, setBuildParseTree, setContext, setErrorHandler, setInputStream, setProfile, setTokenFactory, setTokenStream, setTrace, setTrimParseTree, triggerEnterRuleEvent, triggerExitRuleEvent, unrollRecursionContexts
action, addErrorListener, getErrorHeader, getErrorListenerDispatch, getErrorListeners, getInterpreter, getRuleIndexMap, getSerializedATN, getState, getTokenErrorDisplay, getTokenType, getTokenTypeMap, removeErrorListener, removeErrorListeners, sempred, setInterpreter, setState
protected final Grammar g
ParserInterpreter
from the standard distribution,
this can reference Grammar, which is in the tools area not
purely runtime.protected BitSet decisionStatesThatSetOuterAltNumInContext
protected int[][] stateToAltsMap
LeftRecursiveRule.getPrimaryAlts()
and
LeftRecursiveRule.getRecursiveOpAlts()
for states in
decisionStatesThatSetOuterAltNumInContext
. It only
caches decisions in left-recursive rules.public GrammarParserInterpreter(Grammar g, String grammarFileName, Vocabulary vocabulary, Collection<String> ruleNames, ATN atn, TokenStream input)
public GrammarParserInterpreter(Grammar g, ATN atn, TokenStream input)
protected InterpreterRuleContext createInterpreterRuleContext(ParserRuleContext parent, int invokingStateNumber, int ruleIndex)
createInterpreterRuleContext
in class ParserInterpreter
public void reset()
reset
in class ParserInterpreter
public BitSet findOuterMostDecisionStates()
protected int visitDecisionState(DecisionState p)
Left recursive rules are much more complicated to deal with: there is typically a decision for the primary alternatives and a decision to choose between the recursive operator alternatives. For example, the following left recursive rule has two primary and 2 recursive alternatives.
e : e '*' e | '-' INT | e '+' e | ID ;ANTLR rewrites that rule to be
e[int precedence] : ('-' INT | ID) ( {...}? '*' e[5] | {...}? '+' e[3] )* ;So, there are two decisions associated with picking the outermost alt. This complicates our tracking significantly. The outermost alternative number is a function of the decision (ATN state) within a left recursive rule and the predicted alternative coming back from adaptivePredict(). We use stateToAltsMap as a cache to avoid expensive calls to getRecursiveOpAlts().
visitDecisionState
in class ParserInterpreter
public static List<ParserRuleContext> getAllPossibleParseTrees(Grammar g, Parser originalParser, TokenStream tokens, int decision, BitSet alts, int startIndex, int stopIndex, int startRuleIndex) throws RecognitionException
g
- From which grammar should we drive alternative
numbers and alternative labels.originalParser
- The parser used to create ambiguityInfo; it
is not modified by this routine and can be either
a generated or interpreted parser. It's token
stream *is* reset/seek()'d.tokens
- A stream of tokens to use with the temporary parser.
This will often be just the token stream within the
original parser but here it is for flexibility.decision
- Which decision to try different alternatives for.alts
- The set of alternatives to try while re-parsing.startIndex
- The index of the first token of the ambiguous
input or other input of interest.stopIndex
- The index of the last token of the ambiguous input.
The start and stop indexes are used primarily to
identify how much of the resulting parse tree
to return.startRuleIndex
- The start rule for the entire grammar, not
the ambiguous decision. We re-parse the entire input
and so we need the original start rule.RecognitionException
- Throws upon syntax error while matching
ambig input.public static List<ParserRuleContext> getLookaheadParseTrees(Grammar g, ParserInterpreter originalParser, TokenStream tokens, int startRuleIndex, int decision, int startIndex, int stopIndex)
getAllPossibleParseTrees(org.antlr.v4.tool.Grammar, org.antlr.v4.runtime.Parser, org.antlr.v4.runtime.TokenStream, int, java.util.BitSet, int, int, int)
except
that it re-parses the input for every alternative in a decision,
not just the ambiguous ones (there is no alts parameter here).
This method also tries to reduce the size of the parse trees
by stripping away children of the tree that are completely out of range
of startIndex..stopIndex. Also, because errors are expected, we
use a specialized error handler that more or less bails out
but that also consumes the first erroneous token at least. This
ensures that an error node will be in the parse tree for display.
NOTES:
// we must parse the entire input now with decision overrides
// we cannot parse a subset because it could be that a decision
// above our decision of interest needs to read way past
// lookaheadInfo.stopIndex. It seems like there is no escaping
// the use of a full and complete token stream if we are
// resetting to token index 0 and re-parsing from the start symbol.
// It's not easy to restart parsing somewhere in the middle like a
// continuation because our call stack does not match the
// tree stack because of left recursive rule rewriting. grrrr!public static ParserInterpreter deriveTempParserInterpreter(Grammar g, Parser originalParser, TokenStream tokens)
ParserInterpreter
.Copyright © 1992–2020 ANTLR. All rights reserved.