analyze-ambiguities
Detects ambiguity patterns in ANTLR4 grammars—duplicate alternatives, overlapping prefixes, hidden left recursion, and lexer conflicts—providing fixes to prevent runtime parsing errors.
Instructions
Analyze grammar for common ambiguity patterns that may cause conflicts at runtime.
When to use: Before compiling grammar, after making changes, or when diagnosing parser warnings.
Detection Capabilities:
Identical Alternatives (ERROR)
Detects exact duplicate alternatives in rules
Example:
expr: ID | NUMBER | ID→ duplicate ID alternative
Overlapping Prefixes (WARNING)
Finds alternatives that start with same tokens
Example:
stmt: IF expr THEN stmt | IF expr THEN stmt ELSE stmtSuggestion: Factor out common prefix
Ambiguous Optionals (WARNING)
Detects
A? Apatterns (should beA+)Detects
A? A*patterns (A* is sufficient)
Hidden Left Recursion (ERROR)
Detects indirect left recursion via other rules
Example:
expr: term,term: expr PLUS→ hidden recursion
Lexer Conflicts (WARNING)
Identifies lexer rules that may overlap
Example:
ID: [a-z]+andKEYWORD: 'if'→ keyword is also valid ID
Options:
Selective checks: Enable/disable specific ambiguity patterns
Minimum prefix length: Set threshold for prefix overlap warnings
Severity levels: ERROR (must fix), WARNING (should review), INFO (optional)
Returns:
List of issues with severity, type, rule name, line number
Detailed descriptions and actionable suggestions
Summary counts (errors, warnings, infos)
Example usage: from_file: "MyGrammar.g4" checkIdenticalAlternatives: true checkOverlappingPrefixes: true minPrefixLength: 2
Input Schema
| Name | Required | Description | Default |
|---|---|---|---|
| from_file | No | Optional: path to a grammar file to read | |
| grammar_content | No | The ANTLR4 grammar file content | |
| minPrefixLength | No | Minimum prefix length for overlap warnings (default: 2) | |
| checkLeftRecursion | No | Check for hidden left recursion (default: true) | |
| checkLexerConflicts | No | Check for lexer rule conflicts (default: true) | |
| checkAmbiguousOptionals | No | Check for ambiguous optional patterns like A? A (default: true) | |
| checkOverlappingPrefixes | No | Check for alternatives with common prefixes (default: true) | |
| checkIdenticalAlternatives | No | Check for duplicate alternatives (default: true) |