stanfordnlp/CoreNLP · error · TsurgeonParseException

Error parsing Tsurgeon expression: " + operationString

Error message

Error parsing Tsurgeon expression: " + operationString

What it means

Tsurgeon.parseOperation feeds the operation string to the generated TsurgeonParser; a ParseException or TokenMgrError from the parser is rethrown as TsurgeonParseException("Error parsing Tsurgeon expression: ..."). It means the tsurgeon expression is syntactically invalid.

Solutions

  1. Fix the syntax of the operation string (check operator spelling, parentheses, node names)
  2. Test the operation with a minimal parseOperation call to isolate the offending line in a multi-line script
  3. Strip non-ASCII/whitespace artifacts from the script file (check encoding)
  4. Wrap parsing in try-catch for TsurgeonParseException and report the failing line to the user

Example fix

// before
Tsurgeon.parseOperation("excise named-node named-node1"); // missing required arguments -> parse error
// after
Tsurgeon.parseOperation("excise named-node named-node1 relabel-node");
Defensive patterns

Strategy: try-catch

Validate before calling

null

Try / catch

try {
  TsurgeonPattern p = Tsurgeon.parseOperation(opString);
} catch (TsurgeonParseException e) {
  throw new IllegalArgumentException("Invalid tsurgeon operation: " + opString, e);
}

Prevention

When it happens

Trigger: Calling Tsurgeon.parseOperation (directly or via getTsurgeonOperationsFromReader) with a syntactically bad operation string: misspelled operators, unbalanced parentheses, illegal tokens, or stray characters.

Common situations: Hand-edited tsurgeon scripts with typos; copy-paste artifacts (smart quotes, invisible characters); operations written for a different library/version that uses different syntax; a line from a paired tregex/tsurgeon script accidentally fed as an operation.

Understand the failure class

Background: "Invalid ... format", "must be in format X", "does not look like a ..." — invalid argument format errors across CLI tools and libraries — this error's family across 17 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/trees/tregex/tsurgeon/Tsurgeon.java:653

  /**
   * Parses an operation string into a {@link TsurgeonPattern}.  Throws an {@link TsurgeonParseException} if
   * the operation string is ill-formed.
   * <p>
   * Example of use:
   * <p>
   * <code>
   * TsurgeonPattern p = Tsurgeon.parseOperation("prune ed");
   * </code>
   * @param operationString The operation to perform, as a text string
   * @return the operation pattern.
   */
  public static TsurgeonPattern parseOperation(String operationString) {
    try {
      TsurgeonParser parser =
        new TsurgeonParser(new StringReader(operationString + '\n'));
      return parser.Root();
    } catch (ParseException | TokenMgrError e) {
      throw new TsurgeonParseException("Error parsing Tsurgeon expression: " +
                                       operationString, e);
    }
  }

  /**
   * Collects a list of operation patterns into a sequence of operations to be applied.  Required to keep track of global properties
   * across a sequence of operations.  For example, if you want to insert a named node and then coindex it with another node,
   * you will need to collect the insertion and coindexation operations into a single TsurgeonPattern so that tsurgeon is aware
   * of the name of the new node and coindexation becomes possible.
   *
   * @param patterns a list of {@link TsurgeonPattern} operations that you want to collect together into a single compound operation
   * @return a new {@link TsurgeonPattern} that performs all the operations in the sequence of the {@code patterns} argument
   */
  public static TsurgeonPattern collectOperations(List<TsurgeonPattern> patterns) {
    return new TsurgeonPatternRoot(patterns.toArray(new TsurgeonPattern[patterns.size()]));
  }

}

View on GitHub (pinned to 1b7edd19c4)