stanfordnlp/CoreNLP · error · RuntimeException

Error compiling

Error message

Error compiling 

What it means

SequencePattern.compile(env, string) parses a TokensRegex pattern string with the environment's parser and wraps any exception thrown during parsing or construction in a RuntimeException 'Error compiling <string> using environment <env>'. The original cause is chained, so it indicates a malformed pattern or a bad environment (missing types/options).

Solutions

  1. Read the chained cause (ex.getCause()) to find the actual syntax problem
  2. Validate the pattern string syntax against TokensRegex grammar (balanced brackets, valid operators)
  3. Register all referenced variables/NodePattern types in the Env before compiling
  4. Compile rules with the same CoreNLP version they were written for
  5. Catch RuntimeException around compile() to surface which rule string failed

Example fix

// before
SequencePattern<CoreMap> p = SequencePattern.compile(env, "([word: /foo/] )"); // unbalanced paren
// after
SequencePattern<CoreMap> p = SequencePattern.compile(env, "[word: /foo/]");
Defensive patterns

Strategy: try-catch

Validate before calling

// sanity checks before compile
if (string == null || string.trim().isEmpty()) throw new IllegalArgumentException("Empty pattern");
// optionally check balanced brackets
int depth = 0;
for (char c : string.toCharArray()) {
  if (c == '(' || c == '[') depth++;
  if (c == ')' || c == ']') depth--;
  if (depth < 0) throw new IllegalArgumentException("Unbalanced brackets in: " + string);
}
if (depth != 0) throw new IllegalArgumentException("Unbalanced brackets in: " + string);

Try / catch

try {
  SequencePattern<CoreMap> p = SequencePattern.compile(env, ruleString);
} catch (RuntimeException e) {
  log.error("Error compiling rule: " + ruleString, e.getCause());
  throw new IllegalArgumentException("Bad TokensRegex rule: " + ruleString, e);
}

Prevention

When it happens

Trigger: Calling SequencePattern.compile(env, patternString) where the string has invalid TokensRegex syntax, references unknown variables/types in the Env, or the parser throws for any reason (e.g. bad nested regex or action syntax).

Common situations: Typos in TokensRegex rule files, referencing variables not registered in the Env, loading rules across CoreNLP versions with changed syntax, or malformed expressions like unbalanced brackets.

Related errors


AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10). Data as JSON: /api/errors/76b6852c85023026. Report an issue: GitHub.

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/SequencePattern.java:191

  public SequenceMatchAction<T> getAction() {
    return action;
  }

  public void setAction(SequenceMatchAction<T> action) {
    this.action = action;
  }

  public int getTotalGroups() {
    return totalGroups;
  }

  // Compiles string (regex) to NFA for doing pattern simulation
  public static <T> SequencePattern<T> compile(Env env, String string) {
    try {
      Pair<PatternExpr, SequenceMatchAction<T>> p = env.parser.parseSequenceWithAction(env, string);
      return new SequencePattern<>(string, p.first(), p.second());
    } catch (Exception ex) {
      throw new RuntimeException("Error compiling " + string + " using environment " + env);
    }
    //throw new UnsupportedOperationException("Compile from string not implemented");
  }

  protected static <T> SequencePattern<T> compile(SequencePattern.PatternExpr nodeSequencePattern) {
    return new SequencePattern<>(nodeSequencePattern);
  }

  public SequenceMatcher<T> getMatcher(List<? extends T> tokens) {
    return new SequenceMatcher<>(this, tokens);
  }

  public <OUT> OUT findNodePattern(Function<NodePattern<T>, OUT> filter) {
    Queue<State> todo = new LinkedList<>();
    Set<State> seen = new HashSet<>();
    todo.add(root);
    seen.add(root);
    while (!todo.isEmpty()) {

View on GitHub (pinned to 1b7edd19c4)