antlr/antlr4 · error · InvalidOperationException

Rule dependency version mismatch: {0} has maximum dependency

Error message

Rule dependency version mismatch: {0} has maximum dependency version {1} (expected {2}) in {3}

What it means

Error "Rule dependency version mismatch: {0} has maximum dependency version {1} (expected {2}) in {3}" thrown in antlr/antlr4.

Source

Thrown at runtime/CSharp/src/Misc/RuleDependencyChecker.cs:172

                        {
                            continue;
                        }
                        @checked.Set(descendant);
                        int required = CheckDependencyVersion(errors, dependency, ruleNames, ruleVersions, descendant, "descendant");
                        highestRequiredDependency = Math.Max(highestRequiredDependency, required);
                    }
                }
                int declaredVersion = dependency.Item1.Version;
                if (declaredVersion > highestRequiredDependency)
                {
                    string message = string.Format("Rule dependency version mismatch: {0} has maximum dependency version {1} (expected {2}) in {3}", ruleNames[dependency.Item1.Rule], highestRequiredDependency, declaredVersion, dependency.Item1.Recognizer.ToString());
                    errors.AppendLine(dependency.Item2.ToString());
                    errors.AppendLine(message);
                }
            }
            if (errors.Length > 0)
            {
                throw new InvalidOperationException(errors.ToString());
            }
        }

        private static readonly Dependents ImplementedDependents = Dependents.Self | Dependents.Parents | Dependents.Children | Dependents.Ancestors | Dependents.Descendants;

        private static void ReportUnimplementedDependents(StringBuilder errors, Tuple<RuleDependencyAttribute, ICustomAttributeProvider> dependency, Dependents dependents)
        {
            Dependents unimplemented = dependents;
            unimplemented &= ~ImplementedDependents;
            if (unimplemented != Dependents.None)
            {
                string message = string.Format("Cannot validate the following dependents of rule {0}: {1}", dependency.Item1.Rule, unimplemented);
                errors.AppendLine(message);
            }
        }

        private static int CheckDependencyVersion(StringBuilder errors, Tuple<RuleDependencyAttribute, ICustomAttributeProvider> dependency, string[] ruleNames, int[] ruleVersions, int relatedRule, string relation)
        {

View on GitHub (pinned to 7d5770395b)

Solutions

  1. Regenerate the ANTLR code with a runtime version compatible with the grammar's dependency version, or update the runtime to the expected version.
  2. Align the RuleDependency attribute/annotation version in the grammar or recognizer with the expected serialized version.
  3. Keep all generated recognizers and the runtime from the same ANTLR release.

Example fix

// regenerate code with the same ANTLR version as the runtime, e.g.
// dotnet antlr4 -Dlanguage=CSharp MyGrammar.g4  (matching Antlr4.Runtime.Standard version)

When it happens

Trigger: RuleDependencyChecker encounters a @RuleDependency annotation whose version exceeds the recognized maximum for that rule.

Common situations: Align the dependency version in @RuleVersion/@RuleDependency annotations with the grammar's declared rule versions.


AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14). Data as JSON: /api/errors/ae039caf7a764561. Report an issue: GitHub.