stanfordnlp/CoreNLP · error · Error
Unknown sequence pattern variable
Error message
Unknown sequence pattern variable
What it means
A plain java.lang.Error thrown by the grammar's sequence-variable production when a REGEXVAR token references a name not registered in the parsing Env (env.getSequencePatternExpr(name, true) returns null). Variables must be defined in the environment (via Env setResult/ bind) before they can be referenced in a pattern. It is thrown as an Error, not a ParseException.
Solutions
- Define the variable before use, either with env.bind(name, value) or by loading the rule file that defines it first.
- Check the variable name spelling and case against the definition in the rules file.
- Load all dependent rule files into the same Env in dependency order.
- Pre-validate by calling env.getSequencePatternExpr(name, false) yourself and reporting missing names with a friendly message.
Example fix
// before
SequencePattern<CoreMap> p = SequencePattern.compile(env, "$DATE"); // Error: Unknown sequence pattern variable $DATE
// after
env.bind("$DATE", SequencePattern.compile(env, "[ { ner:DATE } ]+"));
SequencePattern<CoreMap> p = SequencePattern.compile(env, "$DATE"); Defensive patterns
Strategy: validation
Validate before calling
// Pre-check: every $VAR referenced must be defined in the Env
static void checkVarsDefined(Env env, String pattern) {
java.util.regex.Matcher m = java.util.regex.Pattern.compile("\\$[A-Za-z_][A-Za-z0-9_]*").matcher(pattern);
while (m.find()) {
if (env.getSequencePatternExpr(m.group(), false) == null)
throw new IllegalArgumentException("Undefined variable in pattern: " + m.group());
}
} Try / catch
try {
SequencePattern<CoreMap> p = SequencePattern.compile(env, pattern);
} catch (Error e) {
if (e.getMessage() != null && e.getMessage().startsWith("Unknown sequence pattern variable")) {
throw new IllegalArgumentException("Define the variable with env.bind() before compiling: " + pattern, e);
}
throw e;
} Prevention
- Bind all variables (env.bind) before compiling patterns that reference them
- Load defining rule files before dependent ones
- Check $VAR name spelling and case
- Enumerate available variables with env for diagnostics
When it happens
Trigger: Parsing a sequence pattern containing $VAR (REGEXVAR) where $VAR was never bound in the Env, e.g. SequencePattern.compile(env, "$MYVAR") without env.bind or a prior rule defining MYVAR.
Common situations: Rules files referencing variables defined in other files that were not loaded; typos in variable names ($Date vs $DATE); loading a rule file out of order so the variable definition rule hasn't run yet.
Related errors
- Unknown sequence pattern variable
- Attribute match already defined
- Attribute already defined
- Field already defined:
- Attribute match already defined:
AI-assisted analysis of stanfordnlp/CoreNLP@1b7edd19c4 (2026-09-10).
Data as JSON: /api/errors/96f2b992eda891f7.
Report an issue: GitHub.
Appendix: source
Thrown at src/edu/stanford/nlp/ling/tokensregex/parser/TokenSequenceParser.jj:797
|
"["
( node = NodeDisjConj(env) )
"]"
)
{ return node; }
}
SequencePattern.PatternExpr SeqVar(Env env) : {
SequencePattern.PatternExpr expr;
Token name;
} {
(
name = <REGEXVAR>
{
expr = env.getSequencePatternExpr(name.image, true);
if (expr == null) {
throw new Error("Unknown sequence pattern variable " + name.image);
}
}
)
{ return expr; }
}
SequencePattern.PatternExpr SeqBackRef(Env env) : {
Token name;
} {
name = <BACKREF>
{
int v = Integer.parseInt(name.image.substring(1));
return new SequencePattern.BackRefPatternExpr(CoreMapNodePattern.TEXT_ATTR_EQUAL_CHECKER, v);
}
}
View on GitHub (pinned to 1b7edd19c4)