diff --git a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java index a522cb02e..66aace43a 100644 --- a/tool/src/org/antlr/v4/codegen/model/RuleFunction.java +++ b/tool/src/org/antlr/v4/codegen/model/RuleFunction.java @@ -170,7 +170,19 @@ public class RuleFunction extends OutputModelObject { FrequencySet altFreq = getElementFrequenciesForAlt(ast); for (GrammarAST t : refs) { String refLabelName = t.getText(); - if ( altFreq.count(t.getText())>1 ) needsList.add(refLabelName); + if (needsList.contains(refLabelName)) { + continue; + } + + if ( altFreq.count(t.getText())>1 ) { + needsList.add(refLabelName); + } + else { + boolean inLoop = t.hasAncestor(CLOSURE) || t.hasAncestor(POSITIVE_CLOSURE); + if (inLoop) { + needsList.add(refLabelName); + } + } } } Set decls = new HashSet();