antlr/antlr4 · error · IllegalArgumentException
Missing path element at end of path
Error message
Missing path element at end of path
What it means
XPath.getXPathElement expects a word token (wildcard, token ref, rule ref, or string literal) but received EOF, meaning the path ended right where an element was required. Typical trigger: a trailing '/' or '//' with nothing after it.
Source
Thrown at runtime/Java/src/org/antlr/v4/runtime/tree/xpath/XPath.java:159
case Token.EOF :
break loop;
default :
throw new IllegalArgumentException("Unknowth path element "+el);
}
}
return elements.toArray(new XPathElement[0]);
}
/**
* Convert word like {@code *} or {@code ID} or {@code expr} to a path
* element. {@code anywhere} is {@code true} if {@code //} precedes the
* word.
*/
protected XPathElement getXPathElement(Token wordToken, boolean anywhere) {
if ( wordToken.getType()==Token.EOF ) {
throw new IllegalArgumentException("Missing path element at end of path");
}
String word = wordToken.getText();
int ttype = parser.getTokenType(word);
int ruleIndex = parser.getRuleIndex(word);
switch ( wordToken.getType() ) {
case XPathLexer.WILDCARD :
return anywhere ?
new XPathWildcardAnywhereElement() :
new XPathWildcardElement();
case XPathLexer.TOKEN_REF :
case XPathLexer.STRING :
if ( ttype==Token.INVALID_TYPE ) {
throw new IllegalArgumentException(word+
" at index "+
wordToken.getStartIndex()+
" isn't a valid token name");
}
return anywhere ?View on GitHub (pinned to 7d5770395b)
Solutions
- Remove the trailing '/' or '//' or complete it with a rule name, token name, '*', or 'name:"literal"'
- When concatenating path segments, join with '/' and trim trailing separators: path.replaceAll("/+$", "")
Example fix
// before
String xpath = base + "/"; // ends with separator
XPath.findAll(tree, xpath, parser);
// after
String xpath = base.replaceAll("/+$", "");
XPath.findAll(tree, xpath, parser); Defensive patterns
Strategy: validation
Validate before calling
String cleaned = path.replaceAll("/+$", "");
boolean endsWithSeparator = path.endsWith("/") || path.endsWith("//"); Try / catch
try { XPath.findAll(tree, path, parser); } catch (IllegalArgumentException e) { if (e.getMessage().contains("Missing path element")) { /* fix trailing separator */ } } Prevention
- Trim trailing separators when concatenating path fragments
- Keep XPath queries as named constants reviewed against the grammar
When it happens
Trigger: new XPath(parser, "/expr//") or XPath.findAll(tree, "/statement/", parser) — any path whose last step is empty.
Common situations: Concatenating path fragments dynamically so a separator is appended without the following element name; trailing-slip typos in hand-written queries.
Related errors
- Invalid tokens or characters at index "+pos+" in path '"+pat
- Could not read path: "+path
- Unknowth path element "+el
- {word} at index {startIndex} isn't a valid token name
- {word} at index {startIndex} isn't a valid rule name
AI-assisted analysis of antlr/antlr4@7d5770395b (2026-08-14).
Data as JSON: /api/errors/6e1d6c828c4984b5.
Report an issue: GitHub.