stanfordnlp/CoreNLP · error · SemgrexParseException
Error parsing semgrex pattern
Error message
Error parsing semgrex pattern
What it means
SemgrexPattern.compile() wraps any ParseException or TokenMgrError produced by SemgrexParser.Root() into a SemgrexParseException carrying the full offending pattern string and the underlying cause. It is the top-level wrapper for every syntax/lexical error in semgrex, including the deprecated-syntax and uniq-validation errors thrown inside Root().
Solutions
- Read the cause (getCause()) — the inner ParseException/TokenMgrError pinpoints the offending token or rule (deprecated conjugation, bad uniq key, lexer error).
- Validate the pattern against current semgrex syntax; rewrite deprecated constructs like '[foo bar] < zzz' into 'zzz > foo > bar' or ':' branches.
- Catch SemgrexParseException at the boundary where user input is compiled and reject/report the pattern instead of letting it propagate.
- Print patternString (set on the returned pattern normally) alongside the error for debugging rule files.
Example fix
// before
SemgrexPattern p = SemgrexPattern.compile(userPattern); // may throw
// after
SemgrexPattern p;
try {
p = SemgrexPattern.compile(userPattern);
} catch (SemgrexParseException e) {
throw new IllegalArgumentException("Bad semgrex: " + userPattern, e);
} Defensive patterns
Strategy: try-catch
Validate before calling
// validate before compiling
if (semgrex == null || semgrex.isBlank()) throw new IllegalArgumentException("Empty semgrex"); Try / catch
try {
return SemgrexPattern.compile(semgrex, env);
} catch (SemgrexParseException e) {
throw new IllegalArgumentException("Bad semgrex pattern: " + semgrex, e);
} Prevention
- Always catch SemgrexParseException (it wraps both ParseException and TokenMgrError) at the compile boundary
- Log getCause() — it identifies the exact token/rule that failed
- Validate user-supplied patterns at load time, not at match time
- Add a unit test compiling every shipped rule file
When it happens
Trigger: Calling SemgrexPattern.compile(semgrex[, env]) with any syntactically invalid pattern: unbalanced brackets/parens, illegal tokens, deprecated node conjugation ('< [foo bar]'), uniq of an unknown node, or characters the tokenizer rejects (TokenMgrError).
Common situations: User-supplied semgrex from config files or rule lists; patterns written for other regex dialects (expecting standard regex syntax); patterns migrated from old CoreNLP versions using now-illegal syntax; typo'd relation or attribute syntax.
Related errors
- Semgrex pattern asked for uniq of node which does not…
- : Parser grammar does not exist
- : No 1best segmentation available
- : Does not support parse operation.
- Unknown sequence pattern variable
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/1548abf3b1c7003f.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/semgraph/semgrex/SemgrexPattern.java:408
// compile method
// -------------------------------------------------------------
/**
* Creates a pattern from the given string.
*
* @param semgrex The pattern string
* @return A SemgrexPattern for the string.
*/
public static SemgrexPattern compile(String semgrex, Env env) {
try {
SemgrexParser parser = new SemgrexParser(new StringReader(semgrex + '\n'));
SemgrexPattern newPattern = parser.Root();
newPattern.setEnv(env);
newPattern.patternString = semgrex;
return newPattern;
} catch (ParseException | TokenMgrError ex) {
throw new SemgrexParseException("Error parsing semgrex pattern " + semgrex, ex);
}
}
public static SemgrexPattern compile(String semgrex) {
return compile(semgrex, new Env());
}
public String pattern() {
return patternString;
}
/**
* Recursively sets the env variable to this pattern in this and in all its children
*
* @param env An Env
*/
public void setEnv(Env env) {
this.env = env;View on GitHub (pinned to 1b7edd19c4)