Skip to main content
Glama
natl-set

ANTLR4 MCP Server

by natl-set

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:

  1. Identical Alternatives (ERROR)

    • Detects exact duplicate alternatives in rules

    • Example: expr: ID | NUMBER | ID → duplicate ID alternative

  2. Overlapping Prefixes (WARNING)

    • Finds alternatives that start with same tokens

    • Example: stmt: IF expr THEN stmt | IF expr THEN stmt ELSE stmt

    • Suggestion: Factor out common prefix

  3. Ambiguous Optionals (WARNING)

    • Detects A? A patterns (should be A+)

    • Detects A? A* patterns (A* is sufficient)

  4. Hidden Left Recursion (ERROR)

    • Detects indirect left recursion via other rules

    • Example: expr: term, term: expr PLUS → hidden recursion

  5. Lexer Conflicts (WARNING)

    • Identifies lexer rules that may overlap

    • Example: ID: [a-z]+ and KEYWORD: '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

TableJSON Schema
NameRequiredDescriptionDefault
from_fileNoOptional: path to a grammar file to read
grammar_contentNoThe ANTLR4 grammar file content
minPrefixLengthNoMinimum prefix length for overlap warnings (default: 2)
checkLeftRecursionNoCheck for hidden left recursion (default: true)
checkLexerConflictsNoCheck for lexer rule conflicts (default: true)
checkAmbiguousOptionalsNoCheck for ambiguous optional patterns like A? A (default: true)
checkOverlappingPrefixesNoCheck for alternatives with common prefixes (default: true)
checkIdenticalAlternativesNoCheck for duplicate alternatives (default: true)
Behavior4/5

Does the description disclose side effects, auth requirements, rate limits, or destructive behavior?

With no annotations, the description carries full burden, and it does well: it details detection capabilities by severity (ERROR vs WARNING), lists return contents (issues with severity, type, rule name, line number), and mentions suggestions and summary counts. It doesn't explicitly state that the tool is read-only or describe side effects, but its 'Analyze' and 'Detects' phrasing implies non-mutating behavior.

Agents need to know what a tool does to the world before calling it. Descriptions should go beyond structured annotations to explain consequences.

Conciseness5/5

Is the description appropriately sized, front-loaded, and free of redundancy?

The description is lengthy but well-structured with clear headers, numbered detection capabilities, options, returns, and an example. Every section adds necessary information and directly supports tool usage. It is front-loaded with purpose and when-to-use, ensuring the most critical details are immediately visible.

Shorter descriptions cost fewer tokens and are easier for agents to parse. Every sentence should earn its place.

Completeness5/5

Given the tool's complexity, does the description cover enough for an agent to succeed on first attempt?

Given the tool's complexity (8 params, no output schema, no annotations), the description is remarkably complete. It covers purpose, use cases, detection patterns, configurable options, return values, and an example invocation. It doesn't leave major gaps for the agent to infer, and the lack of an output schema is mitigated by a textual description of the return structure.

Complex tools with many parameters or behaviors need more documentation. Simple tools need less. This dimension scales expectations accordingly.

Parameters4/5

Does the description clarify parameter syntax, constraints, interactions, or defaults beyond what the schema provides?

Even though schema coverage is 100% with parameter descriptions, the description adds significant semantic value. For example, it explains what each check does (e.g., 'Identical Alternatives' detects duplicate alternatives) and clarifies 'minPrefixLength' as a threshold. The example usage reinforces parameter names and default behaviors, going beyond the schema's one-line descriptions.

Input schemas describe structure but not intent. Descriptions should explain non-obvious parameter relationships and valid value ranges.

Purpose5/5

Does the description clearly state what the tool does and how it differs from similar tools?

The description clearly states a specific action: 'Analyze grammar for common ambiguity patterns that may cause conflicts at runtime.' This sets it apart from sibling tools like analyze-grammar (general analysis) and validate-grammar (validation). The scope is precise, focusing on ambiguity detection.

Agents choose between tools based on descriptions. A clear purpose with a specific verb and resource helps agents select the right tool.

Usage Guidelines4/5

Does the description explain when to use this tool, when not to, or what alternatives exist?

The description provides an explicit 'When to use' section: 'Before compiling grammar, after making changes, or when diagnosing parser warnings.' This gives clear usage context. However, it does not explicitly mention when not to use it or direct users to alternative tools (e.g., detect-redos for regex-specific issues), so it falls short of a perfect 5.

Agents often have multiple tools that could apply. Explicit usage guidance like "use X instead of Y when Z" prevents misuse.

Install Server

Other Tools

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/natl-set/antlr4-mcp'

If you have feedback or need assistance with the MCP directory API, please join our Discord server