Top Fields Constructors Methods
org.antlr.v4.analysis

public Class LeftRecursionDetector

extends Object
Class Inheritance
Imports
org.antlr.v4.runtime.atn.ATN, .ATNState, .RuleStartState, .RuleStopState, .RuleTransition, .Transition, org.antlr.v4.runtime.misc.OrderedHashSet, org.antlr.v4.tool.Grammar, .Rule, java.util.ArrayList, .HashSet, .List, .Set

Field Summary

Modifier and TypeField and Description
public ATN
pack-priv Grammar
g

public List<Set<Rule>>
listOfRecursiveCycles

Holds a list of cycles (sets of rule names).

pack-priv Set<RuleStartState>
rulesVisitedPerRuleCheck

Which rule start states have we visited while looking for a single left-recursion check?

Constructor Summary

AccessConstructor and Description
public

Method Summary

Modifier and TypeMethod and Description
protected void
addRulesToCycle(Rule enclosingRule, Rule targetRule)

enclosingRule calls targetRule.

public void
public boolean
check(Rule enclosingRule, ATNState s, Set<ATNState> visitedStates)

From state s, look for any transition to a rule that is currently being traced.

Inherited from java.lang.Object:
cloneequalsfinalizegetClasshashCodenotifynotifyAlltoStringwaitwaitwait

Field Detail

atnback to summary
public ATN atn
gback to summary
pack-priv Grammar g
listOfRecursiveCyclesback to summary
public List<Set<Rule>> listOfRecursiveCycles

Holds a list of cycles (sets of rule names).

rulesVisitedPerRuleCheckback to summary
pack-priv Set<RuleStartState> rulesVisitedPerRuleCheck

Which rule start states have we visited while looking for a single left-recursion check?

Constructor Detail

LeftRecursionDetectorback to summary
public LeftRecursionDetector(Grammar g, ATN atn)

Method Detail

addRulesToCycleback to summary
protected void addRulesToCycle(Rule enclosingRule, Rule targetRule)

enclosingRule calls targetRule. Find the cycle containing the target and add the caller. Find the cycle containing the caller and add the target. If no cycles contain either, then create a new cycle.

checkback to summary
public void check()
checkback to summary
public boolean check(Rule enclosingRule, ATNState s, Set<ATNState> visitedStates)

From state s, look for any transition to a rule that is currently being traced. When tracing r, visitedPerRuleCheck has r initially. If you reach a rule stop state, return but notify the invoking rule that the called rule is nullable. This implies that invoking rule must look at follow transition for that invoking state. The visitedStates tracks visited states within a single rule so we can avoid epsilon-loop-induced infinite recursion here. Keep filling the cycles in listOfRecursiveCycles and also, as a side-effect, set leftRecursiveRules.