stanfordnlp/CoreNLP · error · java.lang.RuntimeException

Error parsing to node pattern

Error message

Error parsing  to node pattern

What it means

When a variable bound in the environment is a String, getNodePattern parses it as a TokensRegex node expression. If TokenSequenceParser.parseNode fails, the exception is wrapped in a RuntimeException 'Error parsing <obj> to node pattern'.

Solutions

  1. Fix the string so it is valid node-pattern TokensRegex syntax; inspect the wrapped cause (pex) for position info
  2. Bind a NodePattern object directly (e.g. new Env(...) with CoreMapNodePattern) instead of a string to skip parsing
  3. Test the expression standalone in a rule before binding

Example fix

// before
env.bind("$num", "[0-9]+");
// after
env.bind("$num", "/[0-9]+/");
Defensive patterns

Strategy: try-catch

Validate before calling

// pre-validate by attempting a parse before binding
try { new TokenSequenceParser().parseNode(env, exprString); } catch (Exception e) { throw new IllegalArgumentException("Not valid node syntax: " + exprString, e); }
env.bind(name, exprString);

Try / catch

try { NodePattern np = env.getNodePattern(name); } catch (RuntimeException e) { log.error("Bad node-pattern string for {}: {}", name, e.getMessage()); throw new IllegalArgumentException("Fix bound string: " + name, e.getCause()); }

Prevention

When it happens

Trigger: env.bind(name, "some string") where the string is not valid node-pattern syntax, then referencing $name in node position.

Common situations: Hand-written rule strings with syntax errors, quoting mistakes (unbalanced / or "), or passing plain words containing characters the node grammar rejects.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/Env.java:347

    if (obj != null) {
      if (obj instanceof SequencePattern) {
        SequencePattern seqPattern = (SequencePattern) obj;
        if (seqPattern.getPatternExpr() instanceof SequencePattern.NodePatternExpr) {
          return ((SequencePattern.NodePatternExpr) seqPattern.getPatternExpr()).nodePattern;
        } else {
          throw new Error("Invalid node pattern class: " + seqPattern.getPatternExpr().getClass() + " for variable " + name);
        }
      } else if (obj instanceof SequencePattern.NodePatternExpr) {
        SequencePattern.NodePatternExpr pe = (SequencePattern.NodePatternExpr) obj;
        return pe.nodePattern;
      } else if (obj instanceof NodePattern) {
        return (NodePattern) obj;
      } else if (obj instanceof String) {
        try {
          SequencePattern.NodePatternExpr pe = (SequencePattern.NodePatternExpr) parser.parseNode(this, (String) obj);
          return pe.nodePattern;
        } catch (Exception pex) {
          throw new RuntimeException("Error parsing " + obj + " to node pattern", pex);
        }
      } else {
        throw new Error("Invalid node pattern variable class: " + obj.getClass() + " for variable " + name);
      }
    }
    return null;
  }

  public SequencePattern.PatternExpr getSequencePatternExpr(String name, boolean copy) {
    Object obj = variables.get(name);
    if (obj != null) {
      if (obj instanceof SequencePattern) {
        SequencePattern seqPattern = (SequencePattern) obj;
        return seqPattern.getPatternExpr();
      } else if (obj instanceof SequencePattern.PatternExpr) {
        SequencePattern.PatternExpr pe = (SequencePattern.PatternExpr) obj;
        return (copy)? pe.copy():pe;
      } else if (obj instanceof NodePattern) {

View on GitHub (pinned to 1b7edd19c4)