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
- Fix the string so it is valid node-pattern TokensRegex syntax; inspect the wrapped cause (pex) for position info
- Bind a NodePattern object directly (e.g. new Env(...) with CoreMapNodePattern) instead of a string to skip parsing
- 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
- Prefer binding NodePattern objects over strings when possible
- Test each string expression standalone before binding
- Keep regex quoting consistent (/.../ for patterns)
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
- Invalid value for key:
- Unknown annotation key:
- Error parsing file:
- Invalid node pattern class: for variable
- Invalid node pattern variable class: for variable
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)