stanfordnlp/CoreNLP · error · Error

Field already defined:

Error message

Field already defined: 

What it means

A plain java.lang.Error thrown inside the grammar's node-attribute production when the same field name appears twice in a node attribute map (e.g. [word:"a" word:"b"]). The parser builds a Map of attributes and rejects duplicates so match semantics stay unambiguous. Because it is a raw Error (not a ParseException), it will not be caught by catch(ParseException) blocks.

Solutions

  1. Remove or rename the duplicate attribute in the node expression so each field appears once.
  2. Combine duplicate fields into a single value where semantics allow (e.g. use a regex alternation for word).
  3. If generating node strings in code, deduplicate attribute names before assembling the string.
  4. Catch Error (or better, pre-validate) since this is thrown as java.lang.Error, not ParseException.

Example fix

// before
parser.parseNode(env, "[word: foo tag: NN word: bar]"); // Error: Field already defined: word
// after
parser.parseNode(env, "[word: /foo|bar/ tag: NN]");
Defensive patterns

Strategy: validation

Validate before calling

// Pre-check: no repeated attribute names inside a node expression
static void checkNoDupFields(String node) {
  java.util.regex.Matcher m = java.util.regex.Pattern.compile("(\\w+)\\s*:").matcher(node);
  java.util.Set<String> seen = new java.util.HashSet<>();
  while (m.find()) { if (!seen.add(m.group(1))) throw new IllegalArgumentException("Duplicate field in node: " + m.group(1)); }
}

Try / catch

try {
  parser.parseNode(env, nodeExpr);
} catch (Error e) {
  if (e.getMessage() != null && e.getMessage().startsWith("Field already defined")) {
    throw new IllegalArgumentException("Duplicate attribute in node: " + nodeExpr, e);
  }
  throw e;
}

Prevention

When it happens

Trigger: Parsing a TokensRegex node expression that lists the same attribute name twice, e.g. [word:"foo" tag:"NN" word:"bar"], via parseNode or any grammar path that constructs node attributes.

Common situations: Hand-written rules where a copy-paste duplicated an attribute; programmatically generated node strings that concatenate attribute fragments without deduplication; merging two rule templates that share a field.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/ling/tokensregex/parser/TokenSequenceParser.jj:303

			( ("," | ";")  FieldValue(env, attributes)
		    )*
		"}"
    )
  	{
  	  return new Expressions.CompositeValue(/*"COMPOSITE", */ attributes, false);
	}
}

Map<String,Expression> FieldValue(Env env, Map<String,Expression> attributes) : {
	String fieldname = null;
	Expression expr = null;
}   {
	    fieldname = RelaxedString()
	    (    ":" expr = Expression(env) )
	    {
	      if (fieldname != null && expr != null)  {
	        if (attributes.containsKey(fieldname)) {
                throw new Error("Field already defined: " + fieldname);
	        }
	        attributes.put(fieldname, expr);
	      }
     	  return attributes;
	    }
	}

Value BasicValue(Env env) : {
	Token tok = null;
	Token head = null;
	Token tail = null;
	SequencePattern.PatternExpr seqRegex = null;
}   {
        tok = <REGEX> { return new Expressions.RegexValue(/*"REGEX",*/ tok.image.substring(1,tok.image.length()-1)); }
        |
        tok = <STR> { return new Expressions.PrimitiveValue<String>("STRING", parseQuotedString(tok.image) ); }
        |
        tok = IntegerToken() { return new Expressions.PrimitiveValue<Number>("INTEGER", parseInteger(tok.image)); }

View on GitHub (pinned to 1b7edd19c4)