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

  1. Read the cause (getCause()) — the inner ParseException/TokenMgrError pinpoints the offending token or rule (deprecated conjugation, bad uniq key, lexer error).
  2. Validate the pattern against current semgrex syntax; rewrite deprecated constructs like '[foo bar] < zzz' into 'zzz > foo > bar' or ':' branches.
  3. Catch SemgrexParseException at the boundary where user input is compiled and reject/report the pattern instead of letting it propagate.
  4. 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

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


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)