stanfordnlp/CoreNLP · error · RuntimeException

Unknown state <state>

Error message

Unknown state <state>

What it means

GenerateTrees builds synthetic parse trees recursively from a grammar, and produceTree(state) maps each grammar state to a tree node via its children. If the requested state string is not a known nonterminal in the loaded grammar, no branch matches and the method throws this RuntimeException instead of returning a Tree. It signals that the caller (or the input grammar file) referenced a state the generator does not know about.

Solutions

  1. Check the state string against the nonterminals actually present in the grammar input file and fix the spelling
  2. Regenerate or supply the complete grammar file so every state reachable from the root exists
  3. If calling produceTree programmatically, validate the state against the grammar's known states before invoking it
  4. Run with the correct CLI arguments (input grammar, output, numtrees) via help() guidance

Example fix

// before
Tree t = GenerateTrees.produceTree("NP-OBJ"); // typo: grammar defines NP-OBJ only
// after
Tree t = GenerateTrees.produceTree("NP-OBJ");
Defensive patterns

Strategy: validation

Validate before calling

Set<String> knownStates = readNonterminalsFromGrammar(grammarFile);
if (!knownStates.contains(state)) throw new IllegalArgumentException("State not in grammar: " + state);

Try / catch

try {
    Tree t = GenerateTrees.produceTree(state);
} catch (RuntimeException e) {
    if (e.getMessage().startsWith("Unknown state")) {
        // fall back to default state or report bad grammar input
    } else throw e;
}

Prevention

When it happens

Trigger: Calling produceTree("S") (directly or via tree()/the CLI GenerateTrees <input> <output> <numtrees>) with a state name that is absent from the grammar data read from the input file; a grammar file whose nonterminal labels do not include the state being expanded.

Common situations: Hand-editing or truncating the grammar input file so some LHS nonterminals are missing; passing a main-class argument that points at the wrong file; typos in state names when scripting tree generation.

Understand the failure class

Background: Invalid enum value errors: "Unknown type", "Invalid scope", "must be one of" — when a string is not on the library's allowed list — this error's family across 23 libraries.

Related errors


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

Appendix: source

Thrown at src/edu/stanford/nlp/trees/GenerateTrees.java:212

      Tree root = tf.newTreeNode(state, children);
      return root;
    }
    
    Counter<List<String>> nonTerminal = nonTerminals.get(state);
    if (nonTerminal != null) {
      // found a nonterminal production.  produce a list of
      // recursive expansions, then attach them all to a node with
      // the expected state
      List<String> labels = Counters.sample(nonTerminal, random);
      List<Tree> children = new ArrayList<>();
      for (String childLabel : labels) {
        children.add(produceTree(childLabel));
      }
      Tree root = tf.newTreeNode(state, children);
      return root;
    }
    
    throw new RuntimeException("Unknown state " + state);
  }

  public static void help() {
    System.out.println("Command line should be ");
    System.out.println("  edu.stanford.nlp.trees.GenerateTrees <input> <output> <numtrees>");
  }
  
  public static void main(String[] args) {
    if (args.length == 0 || args[0].equals("-h")) {
      help();
      System.exit(0);
    }
    GenerateTrees grammar = new GenerateTrees();
    grammar.readGrammar(args[0]);
    int numTrees = Integer.valueOf(args[2]);
    grammar.produceTrees(args[1], numTrees);
  }
}

View on GitHub (pinned to 1b7edd19c4)