static void saveParserDataToText(ParserData pd, String filename) {
    try {
      System.err.print("Writing parser in text grammar format to file " + filename);
      OutputStream os;
      if (filename.endsWith(".gz")) {
        // it's faster to do the buffering _outside_ the gzipping as here
        os = new BufferedOutputStream(new GZIPOutputStream(new FileOutputStream(filename)));
      } else {
        os = new BufferedOutputStream(new FileOutputStream(filename));
      }
      PrintWriter out = new PrintWriter(os);
      String prefix = "BEGIN ";
      out.println(prefix + "OPTIONS");
      if (pd.pt != null) {
        pd.pt.writeData(out);
      }
      out.println();
      System.err.print(".");
      out.println(prefix + "LEXICON");
      if (pd.lex != null) {
        pd.lex.writeData(out);
      }
      out.println();
      System.err.print(".");
      out.println(prefix + "UNARY_GRAMMAR");
      if (pd.ug != null) {
        pd.ug.writeData(out);
      }
      out.println();
      System.err.print(".");
      out.println(prefix + "BINARY_GRAMMAR");
      if (pd.bg != null) {
        pd.bg.writeData(out);
      }
      out.println();
      System.err.print(".");
      out.println(prefix + "DEPENDENCY_GRAMMAR");
      if (pd.dg != null) {
        pd.dg.writeData(out);
      }
      out.println();
      System.err.print(".");
      out.flush();
      out.close();
      System.err.println("done.");
    } catch (IOException e) {
      System.err.println("Trouble saving parser data to ASCII format.");
      throw new RuntimeIOException(e);
    }
  }