stanfordnlp/CoreNLP · error · RuntimeException
Error when parsing
Error message
Error when parsing
What it means
TokenSequencePattern.compile wraps any exception from the token-sequence parser in RuntimeException('Error when parsing ' + string). It means the pattern string is not syntactically valid TokensRegex (or the environment/parser failed while compiling it); the original cause is attached as the exception cause.
Solutions
- Read the getCause() of the RuntimeException — it contains the actual parser error and position.
- Validate brackets, quotes, and case/quantifier syntax in the pattern string.
- Ensure all variables/labels used by the pattern are registered in the Env before compile.
- Test the pattern in isolation with a minimal compile call to isolate the failing fragment.
- For TokenMgrError-style lexer failures, check for illegal characters in the string.
Example fix
// before
TokenSequencePattern p = TokenSequencePattern.compile("[ { word:/NN/ } "); // unbalanced
// after
TokenSequencePattern p = TokenSequencePattern.compile("[ { word:/NN/ } ]"); Defensive patterns
Strategy: try-catch
Validate before calling
if (pattern == null || pattern.trim().isEmpty()) throw new IllegalArgumentException("empty TokensRegex pattern");
int depth = 0;
for (char c : pattern.toCharArray()) {
if (c == '[') depth++;
if (c == ']') depth--;
if (depth < 0) throw new IllegalArgumentException("unbalanced brackets in: " + pattern);
}
if (depth != 0) throw new IllegalArgumentException("unbalanced brackets in: " + pattern); Try / catch
try {
TokenSequencePattern p = TokenSequencePattern.compile(env, pattern);
} catch (RuntimeException e) {
// 'Error when parsing <string>' — inspect e.getCause() for the real parser error
throw new InvalidPatternException("bad pattern: " + pattern, e.getCause());
} Prevention
- Always log e.getCause(), not just the wrapper message.
- Validate pattern strings (balanced brackets/quotes) before compile.
- Keep patterns in reviewed resource files rather than inline string concatenation.
- Pin CoreNLP versions and re-test all rule patterns on upgrades.
When it happens
Trigger: TokenSequencePattern.compile(env, string) or compile(string) with a malformed pattern such as unbalanced brackets, illegal tokens, bad quantifier syntax, or references to undefined rules/variables in the Env.
Common situations: Hand-written TokensRegex rules in config files with typos; patterns copied from regex that use unsupported syntax; missing environment bindings after upgrading CoreNLP models/pipelines.
Related errors
- slurpFile IO problem
- slurpReader IO problem
- Invalid sequence pattern variable class:
- Invalid annotation key
- Unknown rule type:
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/5a37cd9c816148bf.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ling/tokensregex/TokenSequencePattern.java:191
}
/**
* Compiles a regular expression over tokens into a TokenSequencePattern
* using the specified environment.
*
* @param env Environment to use
* @param string Regular expression to be compiled
* @return Compiled TokenSequencePattern
*/
public static TokenSequencePattern compile(Env env, String string) {
try {
// SequencePattern.PatternExpr nodeSequencePattern = TokenSequenceParser.parseSequence(env, string);
// return new TokenSequencePattern(string, nodeSequencePattern);
// TODO: Check token sequence parser?
Pair<PatternExpr, SequenceMatchAction<CoreMap>> p = env.parser.parseSequenceWithAction(env, string);
return new TokenSequencePattern(string, p.first(), p.second());
} catch (Exception ex) {
throw new RuntimeException("Error when parsing " + string, ex);
}
}
/**
* Compiles a sequence of regular expressions into a TokenSequencePattern
* using the default environment.
*
* @param strings List of regular expression to be compiled
* @return Compiled TokenSequencePattern
*/
public static TokenSequencePattern compile(String... strings)
{
return compile(DEFAULT_ENV, strings);
}
/**
* Compiles a sequence of regular expressions into a TokenSequencePattern
* using the specified environment.View on GitHub (pinned to 1b7edd19c4)