stanfordnlp/CoreNLP · error · SemgrexParseException

null while parsing semgrex expression: attr=

Error message

null while parsing semgrex expression: attr=

What it means

When parsing an attribute-contains expression like ':{key:value}' in semgrex, the parser verifies that the attribute name, key, and value tokens are all non-null. A null among them (e.g. a missing token before ':') triggers SemgrexParseException listing which of attr/key/value was null.

Solutions

  1. Provide all three parts in ':{attr:key:value}' form, e.g. ':{word:foo:"bar"}'
  2. Quote regex/identifier values correctly and ensure ':' or '!:' separates key and value
  3. Validate the pattern string before compiling, checking every '{' block is well-formed

Example fix

// before
SemgrexPattern p = SemgrexPattern.compile("{}:{:foo}");
// after
SemgrexPattern p = SemgrexPattern.compile("{}:{pos:foo}" );
Defensive patterns

Strategy: validation

Validate before calling

// each ':{...}' opening segment must look like 'attr:{key:value}' or 'attr:{key!:value}'
if (!Pattern.compile("\\w+:\\{\\w+(!?:)(\"[^\"]*\"|\\w+)").matcher(pattern).find() && pattern.contains(":{"))
  throw new IllegalArgumentException("malformed attribute expression: " + pattern);

Try / catch

try {
  p = SemgrexPattern.compile(pattern);
} catch (SemgrexParseException e) {
  if (e.getMessage().startsWith("null while parsing semgrex expression")) { /* inspect which of attr/key/value is null in the message and complete it */ }
  throw e;
}

Prevention

When it happens

Trigger: Writing a malformed attribute expression such as '{:foo:"bar"}' (missing attribute name), ':{key:}' (missing value), or ':{key!}' (missing type token) so one of the matched tokens stays null.

Common situations: Hand-editing semgrex attribute blocks; escaping issues that swallow a token; copy-paste dropping part of the ':{...}' expression.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/semgraph/semgrex/SemgrexParser.jj:321

	Token attr = null;
	Token key = null;
	Token value = null;
        Token attrType = null;
        boolean negated = false;
} {
        (attr = identifier()
           (( (attrType = ":" | attrType = "!:") (value = identifier() | value = <REGEX>) {
             if (attr != null && value != null) {
               negated = attrType.image.equals("!:");
               attributes.setAttribute(attr.image, value.image, negated);
             }
           }) 
           |
           ( ":{"
           ((key = identifier() | key = <REGEX>) (attrType = ":" | attrType = "!:") (value = identifier() | value = <REGEX>)
           {
             if (attr == null || key == null || value == null) {
               throw new SemgrexParseException("null while parsing semgrex expression: attr=" + attr +
                                               " key=" + key + " value=" + value);
             }
             negated = attrType.image.equals("!:");
             attributes.addContains(attr.image, key.image, value.image, negated);
           })
           ( ";" (key = identifier() | key = <REGEX>) (attrType = ":" | attrType = "!:") (value = identifier() | value = <REGEX>)
           {
             if (attr == null || key == null || value == null) {
               throw new SemgrexParseException("null while parsing semgrex expression: attr=" + attr +
                                               " key=" + key + " value=" + value);
             }
             negated = attrType.image.equals("!:");
             attributes.addContains(attr.image, key.image, value.image, negated);
           })*
           "}" ))
        )
	|
	( attr = <ROOT> { attributes.setRoot(true); } )

View on GitHub (pinned to 1b7edd19c4)