stanfordnlp/CoreNLP · error · TregexParseException
Could not parse " + tregex
Error message
Could not parse " + tregex
What it means
TregexPatternCompiler.compile parses a tregex expression string into a TregexPattern. When the JavaCC parser throws TokenMgrError (lexical failure) the string is rethrown as a TregexParseException with the pattern text attached.
Solutions
- Look at the tregex string in the message and find the illegal character or bad escape at the reported token position.
- Escape special characters (e.g. use \\. for literal dots, quote node names) so the tokenizer accepts them.
- Test the pattern incrementally in the Tregex GUI or with a minimal compile() call to isolate the offending token.
- If the pattern comes from a config file, check encoding issues (invisible/non-ASCII characters).
Example fix
// before
String tregex = "NP > (S . VP)"; // '(' after '>' is a lexical/grammar error
// after
String tregex = "NP > (S . VP)".replace("(", "\\("); // or correct the grammar: NP > (S $+ VP) Defensive patterns
Strategy: try-catch
Validate before calling
static boolean isValidTregex(String tregex, Function<String, String> basicCat, HeadFinder hf) {
try { TregexPatternCompiler.defaultCompiler.compile(tregex, basicCat, hf); return true; }
catch (Exception | TokenMgrError e) { return false; }
} Try / catch
try {
TregexPattern p = TregexPatternCompiler.defaultCompiler.compile(tregex);
} catch (TregexParseException e) {
log.warn("Invalid tregex pattern: " + tregex, e);
} Prevention
- Escape regex metacharacters and special characters in hand-written node names.
- Lint patterns with a small compile() dry-run before committing them to config.
- Watch out for invisible/non-ASCII characters when patterns come from files or user input.
When it happens
Trigger: Calling compile(tregex) with a pattern containing characters that are not valid tregex tokens — e.g. stray unescaped operators like '?', invalid escapes, or illegal characters the tokenizer cannot match before any grammar rule fires.
Common situations: Hand-written patterns with unescaped regex metacharacters; patterns built by string concatenation that accidentally include whitespace or control characters; patterns copied from other grammar syntaxes (SQL wildcards, glob).
Related errors
- Use of node conjugation (expressions such as '< [foo bar]'…
- Arc input is in unexpected format:
- Attempt to create ChineseSimWordAvgDepGrammar before…
- Attribute already defined:
- Attribute already defined
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/51356fb08ac3c472.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/trees/tregex/TregexPatternCompiler.java:143
* requiring clients to catch it, we wrap it in a ParseException.
* (The original Error's are thrown in TregexParserTokenManager.)
*
* @param tregex The pattern to parse
* @return A new TregexPattern object based on this string
* @throws TregexParseException If the expression is syntactically invalid
*/
public TregexPattern compile(String tregex) {
for (Pair<String, String> macro : macros) {
tregex = tregex.replaceAll(macro.first(), macro.second());
}
TregexPattern pattern;
try {
TregexParser parser = new TregexParser(new StringReader(tregex + '\n'),
basicCatFunction, headFinder);
pattern = parser.Root();
pattern.setKnownVariables(parser.knownVariables);
} catch (TokenMgrError tme) {
throw new TregexParseException("Could not parse " + tregex, tme);
} catch (ParseException e) {
throw new TregexParseException("Could not parse " + tregex, e);
} catch (IllegalStateException e) {
throw new TregexParseException("Could not parse " + tregex, e);
}
pattern.setPatternString(tregex);
return pattern;
}
}
View on GitHub (pinned to 1b7edd19c4)