now builds frequency tables
[pspp] / src / language / stats / ctables.c
index c5ac106e066a845db92aaaa5d33a2fc5dbb7a7ad..563abdf7f1d99951db13b7522e1d65d9cb0f7b50 100644 (file)
 
 #include <config.h>
 
+#include "data/casereader.h"
+#include "data/dataset.h"
+#include "data/dictionary.h"
+#include "data/mrset.h"
 #include "language/command.h"
+#include "language/lexer/format-parser.h"
 #include "language/lexer/lexer.h"
+#include "language/lexer/variable-parser.h"
+#include "language/stats/freq.h"
+#include "libpspp/assertion.h"
 #include "libpspp/hmap.h"
 #include "libpspp/message.h"
+#include "libpspp/string-array.h"
 #include "output/pivot-table.h"
 
+#include "gl/minmax.h"
 #include "gl/xalloc.h"
 
 #include "gettext.h"
 #define _(msgid) gettext (msgid)
+#define N_(msgid) (msgid)
+
+enum ctables_vlabel
+  {
+    CTVL_DEFAULT = SETTINGS_VALUE_SHOW_DEFAULT,
+    CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
+    CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
+    CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
+    CTVL_NONE,
+  };
+static void UNUSED
+ctables_vlabel_unique (enum ctables_vlabel vlabel)
+{
+  /* This ensures that all of the values are unique. */
+  switch (vlabel)
+    {
+    case CTVL_DEFAULT:
+    case CTVL_NAME:
+    case CTVL_LABEL:
+    case CTVL_BOTH:
+    case CTVL_NONE:
+      abort ();
+    }
+}
+
+/* XXX:
+   - unweighted summaries (U*)
+   - lower confidence limits (*.LCL)
+   - upper confidence limits (*.UCL)
+   - standard error (*.SE)
+ */
+#define SUMMARIES                                                       \
+    /* All variables. */                                                \
+    S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL)            \
+    S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
+    S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
+    S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
+                                                                        \
+    /* Scale variables, totals, and subtotals. */                       \
+    S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE)  \
+    S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE)           \
+    S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE)     \
+    S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE)  \
+    S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE)  \
+    S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE)           \
+    S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE)   \
+    S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE)        \
+    S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
+    S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
+    S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE)              \
+    S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE)      \
+    S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
+    S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE)      \
+    S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
+    S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
+    S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
+    S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
+    S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
+    S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
+    S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
+    S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
+    S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
+                                                                        \
+    /* Multiple response sets. */                                       \
+  S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
+    S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
+    S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
+
+enum ctables_summary_function
+  {
+#define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
+    SUMMARIES
+#undef S
+  };
+
+enum {
+#define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
+  N_CTSF_FUNCTIONS = SUMMARIES
+#undef S
+};
 
 struct ctables
   {
@@ -39,27 +167,18 @@ struct ctables
        format.  Otherwise, this string is displayed. */
     char *missing;
 
-    /* Contains "struct ctables_vlabel" structs.  */
-    struct hmap vlabels;
+    /* Indexed by variable dictionary index. */
+    enum ctables_vlabel *vlabels;
 
     bool mrsets_count_duplicates; /* MRSETS. */
     bool smissing_listwise;       /* SMISSING. */
     struct variable *base_weight; /* WEIGHT. */
-    double hide_threshold;        /* HIDESMALLCOUNTS. */
+    int hide_threshold;           /* HIDESMALLCOUNTS. */
 
     struct ctables_table *tables;
     size_t n_tables;
   };
 
-struct ctables_vlabel
-  {
-    struct hmap_node hmap_node; /* In struct ctables's 'vlabels' hmap. */
-    const char *name;           /* Variable name. */
-
-    /* SETTINGS_VALUE_SHOW_DEFAULT is interpreted as "none". */
-    enum settings_value_show show;
-  };
-
 struct ctables_postcompute
   {
     struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
@@ -100,23 +219,177 @@ struct ctables_postcompute_expr
         /* CTPO_CAT_RANGE.
 
            XXX what about string ranges? */
-        struct
-          {
-            double low;         /* -DBL_MAX for LO. */
-            double high;        /* DBL_MAX for HIGH. */
-          }
-        range;
+        double range[2];
 
         /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
         struct ctables_postcompute_expr *subs[2];
       };
   };
 
+enum ctables_label_position
+  {
+    CTLP_NORMAL,
+    CTLP_OPPOSITE,
+    CTLP_LAYER,
+  };
+
 struct ctables_table
   {
     struct ctables_axis *axes[PIVOT_N_AXES];
 
+    enum pivot_axis_type slabels_position;
+    bool slabels_visible;
 
+    enum ctables_label_position row_labels;
+    enum ctables_label_position col_labels;
+
+    /* Indexed by variable dictionary index. */
+    struct ctables_categories **categories;
+    size_t n_categories;
+
+    double cilevel;
+
+    char *caption;
+    char *corner;
+    char *title;
+
+    struct ctables_chisq *chisq;
+    struct ctables_pairwise *pairwise;
+  };
+
+struct ctables_var
+  {
+    bool is_mrset;
+    union
+      {
+        struct variable *var;
+        const struct mrset *mrset;
+      };
+  };
+
+static const struct fmt_spec *
+ctables_var_get_print_format (const struct ctables_var *var)
+{
+  return (var->is_mrset
+          ? var_get_print_format (var->mrset->vars[0])
+          : var_get_print_format (var->var));
+}
+
+static const char *
+ctables_var_name (const struct ctables_var *var)
+{
+  return var->is_mrset ? var->mrset->name : var_get_name (var->var);
+}
+
+struct ctables_categories
+  {
+    size_t n_refs;
+
+    /* Explicit categories. */
+    struct ctables_cat_value *values;
+    size_t n_values;
+
+    /* Implicit categories. */
+    bool sort_ascending;
+    bool include_missing;
+    enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
+    enum ctables_summary_function sort_func;
+    struct variable *sort_func_var;
+    double percentile;
+
+    /* Totals. */
+    bool show_totals;
+    bool totals_before;
+    char *total_label;
+
+    /* Empty categories. */
+    bool show_empty;
+  };
+
+struct ctables_cat_value
+  {
+    enum ctables_cat_value_type
+      {
+        CCVT_NUMBER,
+        CCVT_STRING,
+        CCVT_RANGE,
+        CCVT_MISSING,
+        CCVT_OTHERNM,
+        CCVT_SUBTOTAL,
+        CCVT_HSUBTOTAL,
+      }
+    type;
+
+    union
+      {
+        double number;          /* CCVT_NUMBER. */
+        char *string;           /* CCVT_STRING. */
+        double range[2];        /* CCVT_RANGE. */
+        char *subtotal_label;   /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
+      };
+  };
+
+static void
+ctables_cat_value_uninit (struct ctables_cat_value *cv)
+{
+  if (!cv)
+    return;
+
+  switch (cv->type)
+    {
+    case CCVT_NUMBER:
+    case CCVT_RANGE:
+    case CCVT_MISSING:
+    case CCVT_OTHERNM:
+      break;
+
+    case CCVT_STRING:
+      free (cv->string);
+      break;
+
+    case CCVT_SUBTOTAL:
+    case CCVT_HSUBTOTAL:
+      free (cv->subtotal_label);
+    }
+}
+
+static void
+ctables_categories_unref (struct ctables_categories *c)
+{
+  if (!c)
+    return;
+
+  assert (c->n_refs > 0);
+  if (--c->n_refs)
+    return;
+
+  for (size_t i = 0; i < c->n_values; i++)
+    ctables_cat_value_uninit (&c->values[i]);
+  free (c->values);
+  free (c->total_label);
+  free (c);
+}
+
+/* Chi-square test (SIGTEST). */
+struct ctables_chisq
+  {
+    double alpha;
+    bool include_mrsets;
+    bool all_visible;
+  };
+
+/* Pairwise comparison test (COMPARETEST). */
+struct ctables_pairwise
+  {
+    enum { PROP, MEAN } type;
+    double alpha[2];
+    bool include_mrsets;
+    bool meansvariance_allcats;
+    bool all_visible;
+    enum { BONFERRONI = 1, BH } adjust;
+    bool merge;
+    bool apa_style;
+    bool show_sig;
   };
 
 struct ctables_axis
@@ -125,10 +398,9 @@ struct ctables_axis
       {
         /* Terminals. */
         CTAO_VAR,
-        CTAO_MRSET,
 
         /* Nonterminals. */
-        CTAO_CONCAT,            /* + */
+        CTAO_STACK,             /* + */
         CTAO_NEST,              /* > */
       }
     op;
@@ -138,109 +410,1687 @@ struct ctables_axis
         /* Terminals. */
         struct
           {
-            union
-              {
-                struct variable *var;
-                struct mrset *mrset;
-              };
-
+            struct ctables_var var;
             bool scale;
             struct ctables_summary *summaries;
             size_t n_summaries;
+            size_t allocated_summaries;
           };
 
         /* Nonterminals. */
         struct ctables_axis *subs[2];
       };
+
+    struct msg_location *loc;
+  };
+
+static void ctables_axis_destroy (struct ctables_axis *);
+
+enum ctables_format
+  {
+    CTF_COUNT,
+    CTF_PERCENT,
+    CTF_GENERAL
+  };
+
+enum ctables_function_availability
+  {
+    CTFA_ALL,                /* Any variables. */
+    CTFA_SCALE,              /* Only scale variables, totals, and subtotals. */
+    CTFA_MRSETS,             /* Only multiple-response sets */
   };
 
 struct ctables_summary
   {
-    enum ctables_summary_function
+    enum ctables_summary_function function;
+    double percentile;          /* CTSF_PTILE only. */
+    char *label;
+    struct fmt_spec format;     /* XXX extra CTABLES formats */
+  };
+
+static void
+ctables_summary_uninit (struct ctables_summary *s)
+{
+  if (s)
+    free (s->label);
+}
+
+static bool
+parse_col_width (struct lexer *lexer, const char *name, double *width)
+{
+  lex_match (lexer, T_EQUALS);
+  if (lex_match_id (lexer, "DEFAULT"))
+    *width = SYSMIS;
+  else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
+    {
+      *width = lex_number (lexer);
+      lex_get (lexer);
+    }
+  else
+    return false;
+
+  return true;
+}
+
+static bool
+parse_bool (struct lexer *lexer, bool *b)
+{
+  if (lex_match_id (lexer, "NO"))
+    *b = false;
+  else if (lex_match_id (lexer, "YES"))
+    *b = true;
+  else
+    {
+      lex_error_expecting (lexer, "YES", "NO");
+      return false;
+    }
+  return true;
+}
+
+static enum ctables_function_availability
+ctables_function_availability (enum ctables_summary_function f)
+{
+  static enum ctables_function_availability availability[] = {
+#define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
+    SUMMARIES
+#undef S
+  };
+
+  return availability[f];
+}
+
+static bool
+parse_ctables_summary_function (struct lexer *lexer,
+                                enum ctables_summary_function *f)
+{
+  struct pair
+    {
+      enum ctables_summary_function function;
+      struct substring name;
+    };
+  static struct pair names[] = {
+#define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
+    { ENUM, SS_LITERAL_INITIALIZER (NAME) },
+    SUMMARIES
+
+    /* The .COUNT suffix may be omitted. */
+    S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
+    S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
+    S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
+    S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
+    S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
+    S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
+    S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
+#undef S
+  };
+
+  if (!lex_force_id (lexer))
+    return false;
+
+  for (size_t i = 0; i < sizeof names / sizeof *names; i++)
+    if (ss_equals_case (names[i].name, lex_tokss (lexer)))
       {
-        /* All variables. */
-        CTSF_COUNT,
-        CTSF_ECOUNT,
-        CTSF_ROWPCT_COUNT,
-        CTSF_COLPCT_COUNT,
-        CTSF_TABLEPCT_COUNT,
-        CTSF_SUBTABLEPCT_COUNT,
-        CTSF_LAYERPCT_COUNT,
-        CTSF_LAYERROWPCT_COUNT,
-        CTSF_LAYERCOLPCT_COUNT,
-        CTSF_ROWPCT_VALIDN,
-        CTSF_COLPCT_VALIDN,
-        CTSF_TABLEPCT_VALIDN,
-        CTSF_SUBTABLEPCT_VALIDN,
-        CTSF_LAYERPCT_VALIDN,
-        CTSF_LAYERROWPCT_VALIDN,
-        CTSF_LAYERCOLPCT_VALIDN,
-        CTSF_ROWPCT_TOTALN,
-        CTSF_COLPCT_TOTALN,
-        CTSF_TABLEPCT_TOTALN,
-        CTSF_SUBTABLEPCT_TOTALN,
-        CTSF_LAYERPCT_TOTALN,
-        CTSF_LAYERROWPCT_TOTALN,
-        CTSF_LAYERCOLPCT_TOTALN,
-
-        /* Scale variables, totals, and subtotals. */
-        CTSF_MAXIMUM,
-        CTSF_MEAN,
-        CTSF_MEDIAN,
-        CTSF_MINIMUM,
-        CTSF_MISSING,
-        CTSF_MODE,
-        CTSF_PTILE,
-        CTSF_RANGE,
-        CTSF_SEMAN,
-        CTSF_STDDEV,
-        CTSF_SUM,
-        CSTF_TOTALN,
-        CTSF_ETOTALN,
-        CTSF_VALIDN,
-        CTSF_EVALIDN,
-        CTSF_VARIANCE,
-        CTSF_ROWPCT_SUM,
-        CTSF_COLPCT_SUM,
-        CTSF_TABLEPCT_SUM,
-        CTSF_SUBTABLEPCT_SUM,
-        CTSF_LAYERPCT_SUM,
-        CTSF_LAYERROWPCT_SUM,
-        CTSF_LAYERCOLPCT_SUM,
-
-        /* Multiple response sets. */
-        CTSF_ROWPCT_RESPONSES,
-        CTSF_COLPCT_RESPONSES,
-        CTSF_TABLEPCT_RESPONSES,
-        CTSF_SUBTABLEPCT_RESPONSES,
-        CTSF_LAYERPCT_RESPONSES,
-        CTSF_LAYERROWPCT_RESPONSES,
-        CTSF_LAYERCOLPCT_RESPONSES,
-        CTSF_ROWPCT_RESPONSES_COUNT,
-        CTSF_COLPCT_RESPONSES_COUNT,
-        CTSF_TABLEPCT_RESPONSES_COUNT,
-        CTSF_SUBTABLEPCT_RESPONSES_COUNT,
-        CTSF_LAYERPCT_RESPONSES_COUNT,
-        CTSF_LAYERROWPCT_RESPONSES_COUNT,
-        CTSF_LAYERCOLPCT_RESPONSES_COUNT,
-        CTSF_ROWPCT_COUNT_RESPONSES,
-        CTSF_COLPCT_COUNT_RESPONSES,
-        CTSF_TABLEPCT_COUNT_RESPONSES,
-        CTSF_SUBTABLEPCT_COUNT_RESPONSES,
-        CTSF_LAYERPCT_COUNT_RESPONSES,
-        CTSF_LAYERROWPCT_COUNT_RESPONSES,
-        CTSF_LAYERCOLPCT_COUNT_RESPONSES,
+        *f = names[i].function;
+        lex_get (lexer);
+        return true;
       }
-    function;
 
-    char *label;
-    struct fmt_spec format;     /* XXX extra CTABLES formats */
+  lex_error (lexer, _("Expecting summary function name."));
+  return false;
+}
+
+static void
+ctables_axis_destroy (struct ctables_axis *axis)
+{
+  if (!axis)
+    return;
+
+  switch (axis->op)
+    {
+    case CTAO_VAR:
+      for (size_t i = 0; i < axis->n_summaries; i++)
+        ctables_summary_uninit (&axis->summaries[i]);
+      free (axis->summaries);
+      break;
+
+    case CTAO_STACK:
+    case CTAO_NEST:
+      ctables_axis_destroy (axis->subs[0]);
+      ctables_axis_destroy (axis->subs[1]);
+      break;
+    }
+  msg_location_destroy (axis->loc);
+  free (axis);
+}
+
+static struct ctables_axis *
+ctables_axis_new_nonterminal (enum ctables_axis_op op,
+                              struct ctables_axis *sub0,
+                              struct ctables_axis *sub1,
+                              struct lexer *lexer, int start_ofs)
+{
+  struct ctables_axis *axis = xmalloc (sizeof *axis);
+  *axis = (struct ctables_axis) {
+    .op = op,
+    .subs = { sub0, sub1 },
+    .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
+  };
+  return axis;
+}
+
+struct ctables_axis_parse_ctx
+  {
+    struct lexer *lexer;
+    struct dictionary *dict;
+    struct ctables *ct;
+    struct ctables_table *t;
+  };
+
+static struct fmt_spec
+ctables_summary_default_format (enum ctables_summary_function function,
+                                const struct ctables_var *var)
+{
+  static const enum ctables_format default_formats[] = {
+#define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
+    SUMMARIES
+#undef S
+  };
+  switch (default_formats[function])
+    {
+    case CTF_COUNT:
+      return (struct fmt_spec) { .type = FMT_F, .w = 40 };
+
+    case CTF_PERCENT:
+      return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
+
+    case CTF_GENERAL:
+      return *ctables_var_get_print_format (var);
+
+    default:
+      NOT_REACHED ();
+    }
+}
+
+static const char *
+ctables_summary_function_name (enum ctables_summary_function function)
+{
+  static const char *names[] = {
+#define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
+    SUMMARIES
+#undef S
+  };
+  return names[function];
+}
+
+static bool
+add_summary (struct ctables_axis *axis,
+             enum ctables_summary_function function, double percentile,
+             const char *label, const struct fmt_spec *format,
+             const struct msg_location *loc)
+{
+  if (axis->op == CTAO_VAR)
+    {
+      if (axis->n_summaries >= axis->allocated_summaries)
+        axis->summaries = x2nrealloc (axis->summaries,
+                                      &axis->allocated_summaries,
+                                      sizeof *axis->summaries);
+
+      const char *function_name = ctables_summary_function_name (function);
+      const char *var_name = ctables_var_name (&axis->var);
+      switch (ctables_function_availability (function))
+        {
+        case CTFA_MRSETS:
+          if (!axis->var.is_mrset)
+            {
+              msg_at (SE, loc, _("Summary function %s applies only to multiple "
+                                 "response sets."), function_name);
+              msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
+                      var_name);
+              return false;
+            }
+          break;
+
+        case CTFA_SCALE:
+          if (!axis->scale)
+            {
+              msg_at (SE, loc,
+                      _("Summary function %s applies only to scale variables."),
+                      function_name);
+              msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
+                      var_name);
+              return false;
+            }
+          break;
+
+        case CTFA_ALL:
+          break;
+        }
+
+      struct ctables_summary *dst = &axis->summaries[axis->n_summaries++];
+      *dst = (struct ctables_summary) {
+        .function = function,
+        .percentile = percentile,
+        .label = xstrdup (label),
+        .format = (format ? *format
+                   : ctables_summary_default_format (function, &axis->var)),
+      };
+      return true;
+    }
+  else
+    {
+      for (size_t i = 0; i < 2; i++)
+        if (!add_summary (axis->subs[i], function, percentile, label, format,
+                          loc))
+          return false;
+      return true;
+    }
+}
+
+static struct ctables_axis *ctables_axis_parse_stack (
+  struct ctables_axis_parse_ctx *);
+
+static bool
+ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
+                   struct ctables_var *var)
+{
+  if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
+    {
+      *var = (struct ctables_var) {
+        .is_mrset = true,
+        .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
+      };
+      if (!var->mrset)
+        {
+          lex_error (lexer, _("'%s' does not name a multiple-response set "
+                              "in the active file dictionary."),
+                     lex_tokcstr (lexer));
+          return false;
+        }
+      lex_get (lexer);
+      return true;
+    }
+  else
+    {
+      *var = (struct ctables_var) {
+        .is_mrset = false,
+        .var = parse_variable (lexer, dict),
+      };
+      return var->var != NULL;
+    }
+}
+
+static struct ctables_axis *
+ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
+{
+  if (lex_match (ctx->lexer, T_LPAREN))
+    {
+      struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
+      if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
+        {
+          ctables_axis_destroy (sub);
+          return NULL;
+        }
+      return sub;
+    }
+
+  if (!lex_force_id (ctx->lexer))
+    return NULL;
+
+  int start_ofs = lex_ofs (ctx->lexer);
+  struct ctables_var var;
+  if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
+    return NULL;
+
+  struct ctables_axis *axis = xmalloc (sizeof *axis);
+  *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
+
+  /* XXX should figure out default measures by reading data */
+  axis->scale = (var.is_mrset ? false
+                 : lex_match_phrase (ctx->lexer, "[S]") ? true
+                 : lex_match_phrase (ctx->lexer, "[C]") ? false
+                 : var_get_measure (var.var) == MEASURE_SCALE);
+  axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
+                                lex_ofs (ctx->lexer) - 1);
+  return axis;
+}
+
+static struct ctables_axis *
+ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
+{
+  struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
+  if (!sub || !lex_match (ctx->lexer, T_LBRACK))
+    return sub;
+
+  do
+    {
+      int start_ofs = lex_ofs (ctx->lexer);
+
+      /* Parse function. */
+      enum ctables_summary_function function;
+      if (!parse_ctables_summary_function (ctx->lexer, &function))
+        goto error;
+
+      /* Parse percentile. */
+      double percentile = 0;
+      if (function == CTSF_PTILE)
+        {
+          if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
+            goto error;
+          percentile = lex_number (ctx->lexer);
+          lex_get (ctx->lexer);
+        }
+
+      /* Parse label. */
+      char *label;
+      if (lex_is_string (ctx->lexer))
+        {
+          label = ss_xstrdup (lex_tokss (ctx->lexer));
+          lex_get (ctx->lexer);
+        }
+      else if (function == CTSF_PTILE)
+        label = xasprintf (_("Percentile %.2f"), percentile);
+      else
+        {
+          static const char *default_labels[] = {
+#define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
+            SUMMARIES
+#undef S
+          };
+          label = xstrdup (gettext (default_labels[function]));
+        }
+
+      /* Parse format. */
+      struct fmt_spec format;
+      const struct fmt_spec *formatp;
+      if (lex_token (ctx->lexer) == T_ID)
+        {
+          if (!parse_format_specifier (ctx->lexer, &format)
+              || !fmt_check_output (&format)
+              || !fmt_check_type_compat (&format, VAL_NUMERIC))
+            {
+              free (label);
+              goto error;
+            }
+          formatp = &format;
+        }
+      else
+        formatp = NULL;
+
+      struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
+                                                   lex_ofs (ctx->lexer) - 1);
+      add_summary (sub, function, percentile, label, formatp, loc);
+      free (label);
+      msg_location_destroy (loc);
+
+      lex_match (ctx->lexer, T_COMMA);
+    }
+  while (!lex_match (ctx->lexer, T_RBRACK));
+
+  return sub;
+
+error:
+  ctables_axis_destroy (sub);
+  return NULL;
+}
+
+static const struct ctables_axis *
+find_scale (const struct ctables_axis *axis)
+{
+  if (!axis)
+    return NULL;
+  else if (axis->op == CTAO_VAR)
+    {
+      if (axis->scale)
+        {
+          assert (!axis->var.is_mrset);
+          return axis;
+        }
+      else
+        return NULL;
+    }
+  else
+    {
+      for (size_t i = 0; i < 2; i++)
+        {
+          const struct ctables_axis *scale = find_scale (axis->subs[i]);
+          if (scale)
+            return scale;
+        }
+      return NULL;
+    }
+}
+
+static const struct ctables_axis *
+find_categorical_summary (const struct ctables_axis *axis)
+{
+  if (!axis)
+    return NULL;
+  else if (axis->op == CTAO_VAR)
+    return !axis->scale && axis->n_summaries ? axis : NULL;
+  else
+    {
+      for (size_t i = 0; i < 2; i++)
+        {
+          const struct ctables_axis *sum
+            = find_categorical_summary (axis->subs[i]);
+          if (sum)
+            return sum;
+        }
+      return NULL;
+    }
+}
+
+static struct ctables_axis *
+ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
+{
+  int start_ofs = lex_ofs (ctx->lexer);
+  struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
+  if (!lhs)
+    return NULL;
+
+  while (lex_match (ctx->lexer, T_GT))
+    {
+      struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
+      if (!rhs)
+        return NULL;
+
+      struct ctables_axis *nest = ctables_axis_new_nonterminal (
+        CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
+
+      const struct ctables_axis *outer_scale = find_scale (lhs);
+      const struct ctables_axis *inner_scale = find_scale (rhs);
+      if (outer_scale && inner_scale)
+        {
+          msg_at (SE, nest->loc, _("Cannot nest scale variables."));
+          msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
+          msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
+          ctables_axis_destroy (nest);
+          return NULL;
+        }
+
+      const struct ctables_axis *outer_sum = find_categorical_summary (lhs);
+      if (outer_sum)
+        {
+          msg_at (SE, nest->loc,
+                  _("Summaries may only be requested for categorical variables "
+                    "at the innermost nesting level."));
+          msg_at (SN, outer_sum->loc,
+                  _("This outer categorical variable has a summary."));
+          ctables_axis_destroy (nest);
+          return NULL;
+        }
+
+      lhs = nest;
+    }
+
+  return lhs;
+}
+
+static struct ctables_axis *
+ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
+{
+  int start_ofs = lex_ofs (ctx->lexer);
+  struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
+  if (!lhs)
+    return NULL;
+
+  while (lex_match (ctx->lexer, T_PLUS))
+    {
+      struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
+      if (!rhs)
+        return NULL;
+
+      lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
+                                          ctx->lexer, start_ofs);
+    }
+
+  return lhs;
+}
+
+static bool
+ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
+                    struct ctables *ct, struct ctables_table *t,
+                    enum pivot_axis_type a)
+{
+  if (lex_token (lexer) == T_BY
+      || lex_token (lexer) == T_SLASH
+      || lex_token (lexer) == T_ENDCMD)
+    return true;
+
+  struct ctables_axis_parse_ctx ctx = {
+    .lexer = lexer,
+    .dict = dict,
+    .ct = ct,
+    .t = t
+  };
+  t->axes[a] = ctables_axis_parse_stack (&ctx);
+  return t->axes[a] != NULL;
+}
+
+static void
+ctables_chisq_destroy (struct ctables_chisq *chisq)
+{
+  free (chisq);
+}
+
+static void
+ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
+{
+  free (pairwise);
+}
+
+static void
+ctables_table_uninit (struct ctables_table *t)
+{
+  if (!t)
+    return;
+
+  for (size_t i = 0; i < t->n_categories; i++)
+    ctables_categories_unref (t->categories[i]);
+  free (t->categories);
+
+  ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
+  ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
+  ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
+  free (t->caption);
+  free (t->corner);
+  free (t->title);
+  ctables_chisq_destroy (t->chisq);
+  ctables_pairwise_destroy (t->pairwise);
+}
+
+static void
+ctables_destroy (struct ctables *ct)
+{
+  if (!ct)
+    return;
+
+  pivot_table_look_unref (ct->look);
+  free (ct->zero);
+  free (ct->missing);
+  free (ct->vlabels);
+  for (size_t i = 0; i < ct->n_tables; i++)
+    ctables_table_uninit (&ct->tables[i]);
+  free (ct->tables);
+  free (ct);
+}
+
+static struct ctables_cat_value
+ccvt_range (double low, double high)
+{
+  return (struct ctables_cat_value) {
+    .type = CCVT_RANGE,
+    .range = { low, high }
+  };
+}
+
+static bool
+ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
+                                struct ctables_table *t)
+{
+  if (!lex_match_id (lexer, "VARIABLES"))
+    return false;
+  lex_match (lexer, T_EQUALS);
+
+  struct variable **vars;
+  size_t n_vars;
+  if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
+    return false;
+
+  struct ctables_categories *c = xmalloc (sizeof *c);
+  *c = (struct ctables_categories) { .n_refs = n_vars };
+  for (size_t i = 0; i < n_vars; i++)
+    {
+      struct ctables_categories **cp
+        = &t->categories[var_get_dict_index (vars[i])];
+      ctables_categories_unref (*cp);
+      *cp = c;
+    }
+  free (vars);
+
+  if (lex_match (lexer, T_LBRACK))
+    {
+      size_t allocated_values = 0;
+      do
+        {
+          if (c->n_values >= allocated_values)
+            c->values = x2nrealloc (c->values, &allocated_values,
+                                    sizeof *c->values);
+
+          struct ctables_cat_value *v = &c->values[c->n_values];
+          if (lex_match_id (lexer, "OTHERNM"))
+            v->type = CCVT_OTHERNM;
+          else if (lex_match_id (lexer, "MISSING"))
+            v->type = CCVT_MISSING;
+          else if (lex_match_id (lexer, "SUBTOTAL"))
+            *v = (struct ctables_cat_value)
+              { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
+          else if (lex_match_id (lexer, "HSUBTOTAL"))
+            *v = (struct ctables_cat_value)
+              { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
+          else if (lex_match_id (lexer, "LO"))
+            {
+              if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
+                return false;
+              *v = ccvt_range (-DBL_MAX, lex_number (lexer));
+              lex_get (lexer);
+            }
+          else if (lex_is_number (lexer))
+            {
+              double number = lex_number (lexer);
+              lex_get (lexer);
+              if (lex_match_id (lexer, "THRU"))
+                {
+                  v->type = CCVT_RANGE;
+                  v->range[0] = number;
+                  if (lex_match_id (lexer, "HI"))
+                    *v = ccvt_range (number, DBL_MAX);
+                  else
+                    {
+                      if (!lex_force_num (lexer))
+                        return false;
+                      *v = ccvt_range (number, lex_number (lexer));
+                      lex_get (lexer);
+                    }
+                }
+              else
+                *v = (struct ctables_cat_value) {
+                  .type = CCVT_NUMBER,
+                  .number = number
+                };
+            }
+          else if (lex_is_string (lexer))
+            {
+              *v = (struct ctables_cat_value) {
+                .type = CCVT_STRING,
+                .string = ss_xstrdup (lex_tokss (lexer)),
+              };
+              lex_get (lexer);
+            }
+          else
+            {
+              lex_error (lexer, NULL);
+              return false;
+            }
+
+          if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
+              && lex_match (lexer, T_EQUALS))
+            {
+              if (!lex_force_string (lexer))
+                return false;
+
+              v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
+              lex_get (lexer);
+            }
+
+          c->n_values++;
+          lex_match (lexer, T_COMMA);
+        }
+      while (!lex_match (lexer, T_RBRACK));
+    }
+
+  while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
+    {
+      if (!c->n_values && lex_match_id (lexer, "ORDER"))
+        {
+          lex_match (lexer, T_EQUALS);
+          if (lex_match_id (lexer, "A"))
+            c->sort_ascending = true;
+          else if (lex_match_id (lexer, "D"))
+            c->sort_ascending = false;
+          else
+            {
+              lex_error_expecting (lexer, "A", "D");
+              return false;
+            }
+        }
+      else if (!c->n_values && lex_match_id (lexer, "KEY"))
+        {
+          lex_match (lexer, T_EQUALS);
+          if (lex_match_id (lexer, "VALUE"))
+            c->key = CTCS_VALUE;
+          else if (lex_match_id (lexer, "LABEL"))
+            c->key = CTCS_LABEL;
+          else
+            {
+              c->key = CTCS_FUNCTION;
+              if (!parse_ctables_summary_function (lexer, &c->sort_func))
+                return false;
+
+              if (lex_match (lexer, T_LPAREN))
+                {
+                  c->sort_func_var = parse_variable (lexer, dict);
+                  if (!c->sort_func_var)
+                    return false;
+
+                  if (c->sort_func == CTSF_PTILE)
+                    {
+                      lex_match (lexer, T_COMMA);
+                      if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
+                        return false;
+                      c->percentile = lex_number (lexer);
+                      lex_get (lexer);
+                    }
+
+                  if (!lex_force_match (lexer, T_RPAREN))
+                    return false;
+                }
+              else if (ctables_function_availability (c->sort_func)
+                       == CTFA_SCALE)
+                {
+                  bool UNUSED b = lex_force_match (lexer, T_LPAREN);
+                  return false;
+                }
+            }
+        }
+      else if (!c->n_values && lex_match_id (lexer, "MISSING"))
+        {
+          lex_match (lexer, T_EQUALS);
+          if (lex_match_id (lexer, "INCLUDE"))
+            c->include_missing = true;
+          else if (lex_match_id (lexer, "EXCLUDE"))
+            c->include_missing = false;
+          else
+            {
+              lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
+              return false;
+            }
+        }
+      else if (lex_match_id (lexer, "TOTAL"))
+        {
+          lex_match (lexer, T_EQUALS);
+          if (!parse_bool (lexer, &c->show_totals))
+            return false;
+        }
+      else if (lex_match_id (lexer, "LABEL"))
+        {
+          lex_match (lexer, T_EQUALS);
+          if (!lex_force_string (lexer))
+            return false;
+          free (c->total_label);
+          c->total_label = ss_xstrdup (lex_tokss (lexer));
+          lex_get (lexer);
+        }
+      else if (lex_match_id (lexer, "POSITION"))
+        {
+          lex_match (lexer, T_EQUALS);
+          if (lex_match_id (lexer, "BEFORE"))
+            c->totals_before = true;
+          else if (lex_match_id (lexer, "AFTER"))
+            c->totals_before = false;
+          else
+            {
+              lex_error_expecting (lexer, "BEFORE", "AFTER");
+              return false;
+            }
+        }
+      else if (lex_match_id (lexer, "EMPTY"))
+        {
+          lex_match (lexer, T_EQUALS);
+          if (lex_match_id (lexer, "INCLUDE"))
+            c->show_empty = true;
+          else if (lex_match_id (lexer, "EXCLUDE"))
+            c->show_empty = false;
+          else
+            {
+              lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
+              return false;
+            }
+        }
+      else
+        {
+          if (!c->n_values)
+            lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
+                                 "TOTAL", "LABEL", "POSITION", "EMPTY");
+          else
+            lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
+          return false;
+        }
+    }
+  return true;
+}
+
+struct var_array
+  {
+    struct variable **vars;
+    size_t n;
+  };
+
+static void
+var_array_uninit (struct var_array *va)
+{
+  if (va)
+    free (va->vars);
+}
+
+struct var_array2
+  {
+    struct var_array *vas;
+    size_t n;
   };
 
+static void
+var_array2_uninit (struct var_array2 *vaa)
+{
+  if (vaa)
+    {
+      for (size_t i = 0; i < vaa->n; i++)
+        var_array_uninit (&vaa->vas[i]);
+      free (vaa->vas);
+    }
+}
+
+static struct var_array2
+nest_fts (struct var_array2 va0, struct var_array2 va1)
+{
+  if (!va0.n)
+    return va1;
+  else if (!va1.n)
+    return va0;
+
+  struct var_array2 vaa = { .vas = xnmalloc (va0.n, va1.n * sizeof *vaa.vas) };
+  for (size_t i = 0; i < va0.n; i++)
+    for (size_t j = 0; j < va1.n; j++)
+      {
+        size_t allocate = va0.vas[i].n + va1.vas[j].n;
+        struct variable **vars = xnmalloc (allocate, sizeof *vars);
+        size_t n = 0;
+        for (size_t k = 0; k < va0.vas[i].n; k++)
+          vars[n++] = va0.vas[i].vars[k];
+        for (size_t k = 0; k < va1.vas[j].n; k++)
+          vars[n++] = va1.vas[j].vars[k];
+        assert (n == allocate);
+
+        vaa.vas[vaa.n++] = (struct var_array) { .vars = vars, n = n };
+      }
+  var_array2_uninit (&va0);
+  var_array2_uninit (&va1);
+  return vaa;
+}
+
+static struct var_array2
+stack_fts (struct var_array2 va0, struct var_array2 va1)
+{
+  struct var_array2 vaa = { .vas = xnmalloc (va0.n + va1.n, sizeof *vaa.vas) };
+  for (size_t i = 0; i < va0.n; i++)
+    vaa.vas[vaa.n++] = va0.vas[i];
+  for (size_t i = 0; i < va1.n; i++)
+    vaa.vas[vaa.n++] = va1.vas[i];
+  assert (vaa.n == va0.n + va1.n);
+  free (va0.vas);
+  free (va1.vas);
+  return vaa;
+}
+
+static struct var_array2
+enumerate_fts (const struct ctables_axis *a)
+{
+  if (!a)
+    return (struct var_array2) { .n = 0 };
+
+  switch (a->op)
+    {
+    case CTAO_VAR:
+      assert (!a->var.is_mrset);
+      struct variable **v = xmalloc (sizeof *v);
+      *v = a->var.var;
+      struct var_array *va = xmalloc (sizeof *va);
+      *va = (struct var_array) { .vars = v, .n = 1 };
+      return (struct var_array2) { .vas = va, .n = 1 };
+
+    case CTAO_STACK:
+      return stack_fts (enumerate_fts (a->subs[0]),
+                        enumerate_fts (a->subs[1]));
+
+    case CTAO_NEST:
+      return nest_fts (enumerate_fts (a->subs[0]),
+                       enumerate_fts (a->subs[1]));
+    }
+
+  NOT_REACHED ();
+}
+
+struct ctables_freqtab
+  {
+    struct var_array vars;
+    struct hmap data;           /* Contains "struct freq"s. */
+  };
+
+static bool
+ctables_execute (struct dataset *ds, struct ctables *ct)
+{
+  struct ctables_freqtab **fts = NULL;
+  size_t n_fts = 0;
+  size_t allocated_fts = 0;
+  for (size_t i = 0; i < ct->n_tables; i++)
+    {
+      struct ctables_table *t = &ct->tables[i];
+      struct var_array2 vaa = enumerate_fts (t->axes[PIVOT_AXIS_ROW]);
+      vaa = nest_fts (vaa, enumerate_fts (t->axes[PIVOT_AXIS_COLUMN]));
+      vaa = nest_fts (vaa, enumerate_fts (t->axes[PIVOT_AXIS_LAYER]));
+      for (size_t i = 0; i < vaa.n; i++)
+        {
+          for (size_t j = 0; j < vaa.vas[i].n; j++)
+            {
+              if (j)
+                fputs (", ", stdout);
+              fputs (var_get_name (vaa.vas[i].vars[j]), stdout);
+            }
+          putchar ('\n');
+        }
+
+      for (size_t j = 0; j < vaa.n; j++)
+        {
+          if (n_fts >= allocated_fts)
+            fts = x2nrealloc (fts, &allocated_fts, sizeof *fts);
+
+          struct ctables_freqtab *ft = xmalloc (sizeof *ft);
+          *ft = (struct ctables_freqtab) {
+            .vars = vaa.vas[j],
+            .data = HMAP_INITIALIZER (ft->data),
+          };
+          fts[n_fts++] = ft;
+        }
+
+      free (vaa.vas);
+    }
+
+  struct casereader *input = casereader_create_filter_weight (proc_open (ds),
+                                                              dataset_dict (ds),
+                                                              NULL, NULL);
+  bool warn_on_invalid = true;
+  for (struct ccase *c = casereader_read (input); c;
+       case_unref (c), c = casereader_read (input))
+    {
+      double weight = dict_get_case_weight (dataset_dict (ds), c,
+                                            &warn_on_invalid);
+      for (size_t i = 0; i < n_fts; i++)
+        {
+          struct ctables_freqtab *ft = fts[i];
+
+          size_t hash = 0;
+
+          for (size_t j = 0; j < ft->vars.n; j++)
+            {
+              const struct variable *var = ft->vars.vars[j];
+              hash = value_hash (case_data (c, var), var_get_width (var), hash);
+            }
+
+          struct freq *f;
+          HMAP_FOR_EACH_WITH_HASH (f, struct freq, node, hash, &ft->data)
+            {
+              for (size_t j = 0; j < ft->vars.n; j++)
+                {
+                  const struct variable *var = ft->vars.vars[j];
+                  if (!value_equal (case_data (c, var), &f->values[j],
+                                    var_get_width (var)))
+                    goto next_hash_node;
+                }
+
+              f->count += weight;
+              goto next_ft;
+
+            next_hash_node: ;
+            }
+
+        f = xmalloc (table_entry_size (ft->vars.n));
+        f->count = weight;
+        for (size_t j = 0; j < ft->vars.n; j++)
+          {
+            const struct variable *var = ft->vars.vars[j];
+            value_clone (&f->values[j], case_data (c, var),
+                         var_get_width (var));
+          }
+        hmap_insert (&ft->data, &f->node, hash);
+
+        next_ft: ;
+        }
+    }
+  casereader_destroy (input);
+
+  for (size_t i = 0; i < n_fts; i++)
+    {
+      struct ctables_freqtab *ft = fts[i];
+      struct freq *f, *next;
+      HMAP_FOR_EACH_SAFE (f, next, struct freq, node, &ft->data)
+        {
+          hmap_delete (&ft->data, &f->node);
+          for (size_t j = 0; j < ft->vars.n; j++)
+            {
+              const struct variable *var = ft->vars.vars[j];
+              value_destroy (&f->values[j], var_get_width (var));
+            }
+          free (f);
+        }
+      hmap_destroy (&ft->data);
+      var_array_uninit (&ft->vars);
+      free (ft);
+    }
+  free (fts);
+
+  return proc_commit (ds);
+}
+
 int
 cmd_ctables (struct lexer *lexer, struct dataset *ds)
 {
-  
+  size_t n_vars = dict_get_n_vars (dataset_dict (ds));
+  enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
+  for (size_t i = 0; i < n_vars; i++)
+    vlabels[i] = CTVL_DEFAULT;
+
+  struct ctables *ct = xmalloc (sizeof *ct);
+  *ct = (struct ctables) {
+    .look = pivot_table_look_unshare (pivot_table_look_ref (
+                                        pivot_table_look_get_default ())),
+    .vlabels = vlabels,
+    .hide_threshold = 5,
+  };
+
+  if (!lex_force_match (lexer, T_SLASH))
+    goto error;
+
+  while (!lex_match_id (lexer, "TABLE"))
+    {
+      if (lex_match_id (lexer, "FORMAT"))
+        {
+          double widths[2] = { SYSMIS, SYSMIS };
+          double units_per_inch = 72.0;
+
+          while (lex_token (lexer) != T_SLASH)
+            {
+              if (lex_match_id (lexer, "MINCOLWIDTH"))
+                {
+                  if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
+                    goto error;
+                }
+              else if (lex_match_id (lexer, "MAXCOLWIDTH"))
+                {
+                  if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
+                    goto error;
+                }
+              else if (lex_match_id (lexer, "UNITS"))
+                {
+                  lex_match (lexer, T_EQUALS);
+                  if (lex_match_id (lexer, "POINTS"))
+                    units_per_inch = 72.0;
+                  else if (lex_match_id (lexer, "INCHES"))
+                    units_per_inch = 1.0;
+                  else if (lex_match_id (lexer, "CM"))
+                    units_per_inch = 2.54;
+                  else
+                    {
+                      lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
+                      goto error;
+                    }
+                }
+              else if (lex_match_id (lexer, "EMPTY"))
+                {
+                  free (ct->zero);
+                  ct->zero = NULL;
+
+                  lex_match (lexer, T_EQUALS);
+                  if (lex_match_id (lexer, "ZERO"))
+                    {
+                      /* Nothing to do. */
+                    }
+                  else if (lex_match_id (lexer, "BLANK"))
+                    ct->zero = xstrdup ("");
+                  else if (lex_force_string (lexer))
+                    {
+                      ct->zero = ss_xstrdup (lex_tokss (lexer));
+                      lex_get (lexer);
+                    }
+                  else
+                    goto error;
+                }
+              else if (lex_match_id (lexer, "MISSING"))
+                {
+                  lex_match (lexer, T_EQUALS);
+                  if (!lex_force_string (lexer))
+                    goto error;
+
+                  free (ct->missing);
+                  ct->missing = (strcmp (lex_tokcstr (lexer), ".")
+                                 ? ss_xstrdup (lex_tokss (lexer))
+                                 : NULL);
+                  lex_get (lexer);
+                }
+              else
+                {
+                  lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
+                                       "UNITS", "EMPTY", "MISSING");
+                  goto error;
+                }
+            }
+
+          if (widths[0] != SYSMIS && widths[1] != SYSMIS
+              && widths[0] > widths[1])
+            {
+              msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
+              goto error;
+            }
+
+          for (size_t i = 0; i < 2; i++)
+            if (widths[i] != SYSMIS)
+              {
+                int *wr = ct->look->width_ranges[TABLE_HORZ];
+                wr[i] = widths[i] / units_per_inch * 96.0;
+                if (wr[0] > wr[1])
+                  wr[!i] = wr[i];
+              }
+        }
+      else if (lex_match_id (lexer, "VLABELS"))
+        {
+          if (!lex_force_match_id (lexer, "VARIABLES"))
+            goto error;
+          lex_match (lexer, T_EQUALS);
+
+          struct variable **vars;
+          size_t n_vars;
+          if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
+                                PV_NO_SCRATCH))
+            goto error;
+
+          if (!lex_force_match_id (lexer, "DISPLAY"))
+            {
+              free (vars);
+              goto error;
+            }
+          lex_match (lexer, T_EQUALS);
+
+          enum ctables_vlabel vlabel;
+          if (lex_match_id (lexer, "DEFAULT"))
+            vlabel = CTVL_DEFAULT;
+          else if (lex_match_id (lexer, "NAME"))
+            vlabel = CTVL_NAME;
+          else if (lex_match_id (lexer, "LABEL"))
+            vlabel = CTVL_LABEL;
+          else if (lex_match_id (lexer, "BOTH"))
+            vlabel = CTVL_BOTH;
+          else if (lex_match_id (lexer, "NONE"))
+            vlabel = CTVL_NONE;
+          else
+            {
+              lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
+                                   "BOTH", "NONE");
+              free (vars);
+              goto error;
+            }
+
+          for (size_t i = 0; i < n_vars; i++)
+            ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
+          free (vars);
+        }
+      else if (lex_match_id (lexer, "MRSETS"))
+        {
+          if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
+            goto error;
+          lex_match (lexer, T_EQUALS);
+          if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
+            goto error;
+        }
+      else if (lex_match_id (lexer, "SMISSING"))
+        {
+          if (lex_match_id (lexer, "VARIABLE"))
+            ct->smissing_listwise = false;
+          else if (lex_match_id (lexer, "LISTWISE"))
+            ct->smissing_listwise = true;
+          else
+            {
+              lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
+              goto error;
+            }
+        }
+      /* XXX PCOMPUTE */
+      else if (lex_match_id (lexer, "WEIGHT"))
+        {
+          if (!lex_force_match_id (lexer, "VARIABLE"))
+            goto error;
+          lex_match (lexer, T_EQUALS);
+          ct->base_weight = parse_variable (lexer, dataset_dict (ds));
+          if (!ct->base_weight)
+            goto error;
+        }
+      else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
+        {
+          if (!lex_force_match_id (lexer, "COUNT"))
+            goto error;
+          lex_match (lexer, T_EQUALS);
+          if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
+            goto error;
+          ct->hide_threshold = lex_integer (lexer);
+          lex_get (lexer);
+        }
+      else
+        {
+          lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
+                               "SMISSING", "PCOMPUTE", "PPROPERTIES",
+                               "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
+          goto error;
+        }
+
+      if (!lex_force_match (lexer, T_SLASH))
+        goto error;
+    }
+
+  size_t allocated_tables = 0;
+  do
+    {
+      if (ct->n_tables >= allocated_tables)
+        ct->tables = x2nrealloc (ct->tables, &allocated_tables,
+                                 sizeof *ct->tables);
+
+      struct ctables_table *t = &ct->tables[ct->n_tables++];
+      *t = (struct ctables_table) {
+        .slabels_position = PIVOT_AXIS_COLUMN,
+        .slabels_visible = true,
+        .row_labels = CTLP_NORMAL,
+        .col_labels = CTLP_NORMAL,
+        .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
+                               sizeof *t->categories),
+        .n_categories = dict_get_n_vars (dataset_dict (ds)),
+        .cilevel = 95,
+      };
+
+      lex_match (lexer, T_EQUALS);
+      if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
+        goto error;
+      if (lex_match (lexer, T_BY))
+        {
+          if (!ctables_axis_parse (lexer, dataset_dict (ds),
+                                   ct, t, PIVOT_AXIS_COLUMN))
+            goto error;
+
+          if (lex_match (lexer, T_BY))
+            {
+              if (!ctables_axis_parse (lexer, dataset_dict (ds),
+                                       ct, t, PIVOT_AXIS_LAYER))
+                goto error;
+            }
+        }
+
+      if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
+          && !t->axes[PIVOT_AXIS_LAYER])
+        {
+          lex_error (lexer, _("At least one variable must be specified."));
+          goto error;
+        }
+
+      const struct ctables_axis *scales[PIVOT_N_AXES];
+      size_t n_scales = 0;
+      for (size_t i = 0; i < 3; i++)
+        {
+          scales[i] = find_scale (t->axes[i]);
+          if (scales[i])
+            n_scales++;
+        }
+      if (n_scales > 1)
+        {
+          msg (SE, _("Scale variables may appear only on one dimension."));
+          if (scales[PIVOT_AXIS_ROW])
+            msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
+                    _("This scale variable appears in the rows dimension."));
+          if (scales[PIVOT_AXIS_COLUMN])
+            msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
+                    _("This scale variable appears in the columns dimension."));
+          if (scales[PIVOT_AXIS_LAYER])
+            msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
+                    _("This scale variable appears in the layer dimension."));
+          goto error;
+        }
+
+      if (lex_token (lexer) == T_ENDCMD)
+        break;
+      if (!lex_force_match (lexer, T_SLASH))
+        break;
+
+      while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
+        {
+          if (lex_match_id (lexer, "SLABELS"))
+            {
+              while (lex_token (lexer) != T_SLASH)
+                {
+                  if (lex_match_id (lexer, "POSITION"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (lex_match_id (lexer, "COLUMN"))
+                        t->slabels_position = PIVOT_AXIS_COLUMN;
+                      else if (lex_match_id (lexer, "ROW"))
+                        t->slabels_position = PIVOT_AXIS_ROW;
+                      else if (lex_match_id (lexer, "LAYER"))
+                        t->slabels_position = PIVOT_AXIS_LAYER;
+                      else
+                        {
+                          lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
+                          goto error;
+                        }
+                    }
+                  else if (lex_match_id (lexer, "VISIBLE"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (!parse_bool (lexer, &t->slabels_visible))
+                        goto error;
+                    }
+                  else
+                    {
+                      lex_error_expecting (lexer, "POSITION", "VISIBLE");
+                      goto error;
+                    }
+                }
+            }
+          else if (lex_match_id (lexer, "CLABELS"))
+            {
+              while (lex_token (lexer) != T_SLASH)
+                {
+                  if (lex_match_id (lexer, "AUTO"))
+                    t->row_labels = t->col_labels = CTLP_NORMAL;
+                  else if (lex_match_id (lexer, "ROWLABELS"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (lex_match_id (lexer, "OPPOSITE"))
+                        t->row_labels = CTLP_OPPOSITE;
+                      else if (lex_match_id (lexer, "LAYER"))
+                        t->row_labels = CTLP_LAYER;
+                      else
+                        {
+                          lex_error_expecting (lexer, "OPPOSITE", "LAYER");
+                          goto error;
+                        }
+                    }
+                  else if (lex_match_id (lexer, "COLLABELS"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (lex_match_id (lexer, "OPPOSITE"))
+                        t->col_labels = CTLP_OPPOSITE;
+                      else if (lex_match_id (lexer, "LAYER"))
+                        t->col_labels = CTLP_LAYER;
+                      else
+                        {
+                          lex_error_expecting (lexer, "OPPOSITE", "LAYER");
+                          goto error;
+                        }
+                    }
+                  else
+                    {
+                      lex_error_expecting (lexer, "AUTO", "ROWLABELS",
+                                           "COLLABELS");
+                      goto error;
+                    }
+                }
+            }
+          else if (lex_match_id (lexer, "CRITERIA"))
+            {
+              if (!lex_force_match_id (lexer, "CILEVEL"))
+                goto error;
+              lex_match (lexer, T_EQUALS);
+
+              if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
+                goto error;
+              t->cilevel = lex_number (lexer);
+              lex_get (lexer);
+            }
+          else if (lex_match_id (lexer, "CATEGORIES"))
+            {
+              if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
+                goto error;
+            }
+          else if (lex_match_id (lexer, "TITLES"))
+            {
+              do
+                {
+                  char **textp;
+                  if (lex_match_id (lexer, "CAPTION"))
+                    textp = &t->caption;
+                  else if (lex_match_id (lexer, "CORNER"))
+                    textp = &t->corner;
+                  else if (lex_match_id (lexer, "TITLE"))
+                    textp = &t->title;
+                  else
+                    {
+                      lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
+                      goto error;
+                    }
+                  lex_match (lexer, T_EQUALS);
+
+                  struct string s = DS_EMPTY_INITIALIZER;
+                  while (lex_is_string (lexer))
+                    {
+                      if (!ds_is_empty (&s))
+                        ds_put_byte (&s, ' ');
+                      ds_put_substring (&s, lex_tokss (lexer));
+                      lex_get (lexer);
+                    }
+                  free (*textp);
+                  *textp = ds_steal_cstr (&s);
+                }
+              while (lex_token (lexer) != T_SLASH
+                     && lex_token (lexer) != T_ENDCMD);
+            }
+          else if (lex_match_id (lexer, "SIGTEST"))
+            {
+              if (!t->chisq)
+                {
+                  t->chisq = xmalloc (sizeof *t->chisq);
+                  *t->chisq = (struct ctables_chisq) {
+                    .alpha = .05,
+                    .include_mrsets = true,
+                    .all_visible = true,
+                  };
+                }
+
+              do
+                {
+                  if (lex_match_id (lexer, "TYPE"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (!lex_force_match_id (lexer, "CHISQUARE"))
+                        goto error;
+                    }
+                  else if (lex_match_id (lexer, "ALPHA"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
+                        goto error;
+                      t->chisq->alpha = lex_number (lexer);
+                      lex_get (lexer);
+                    }
+                  else if (lex_match_id (lexer, "INCLUDEMRSETS"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (parse_bool (lexer, &t->chisq->include_mrsets))
+                        goto error;
+                    }
+                  else if (lex_match_id (lexer, "CATEGORIES"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (lex_match_id (lexer, "ALLVISIBLE"))
+                        t->chisq->all_visible = true;
+                      else if (lex_match_id (lexer, "SUBTOTALS"))
+                        t->chisq->all_visible = false;
+                      else
+                        {
+                          lex_error_expecting (lexer,
+                                               "ALLVISIBLE", "SUBTOTALS");
+                          goto error;
+                        }
+                    }
+                  else
+                    {
+                      lex_error_expecting (lexer, "TYPE", "ALPHA",
+                                           "INCLUDEMRSETS", "CATEGORIES");
+                      goto error;
+                    }
+                }
+              while (lex_token (lexer) != T_SLASH
+                     && lex_token (lexer) != T_ENDCMD);
+            }
+          else if (lex_match_id (lexer, "COMPARETEST"))
+            {
+              if (!t->pairwise)
+                {
+                  t->pairwise = xmalloc (sizeof *t->pairwise);
+                  *t->pairwise = (struct ctables_pairwise) {
+                    .type = PROP,
+                    .alpha = { .05, .05 },
+                    .adjust = BONFERRONI,
+                    .include_mrsets = true,
+                    .meansvariance_allcats = true,
+                    .all_visible = true,
+                    .merge = false,
+                    .apa_style = true,
+                    .show_sig = false,
+                  };
+                }
+
+              do
+                {
+                  if (lex_match_id (lexer, "TYPE"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (lex_match_id (lexer, "PROP"))
+                        t->pairwise->type = PROP;
+                      else if (lex_match_id (lexer, "MEAN"))
+                        t->pairwise->type = MEAN;
+                      else
+                        {
+                          lex_error_expecting (lexer, "PROP", "MEAN");
+                          goto error;
+                        }
+                    }
+                  else if (lex_match_id (lexer, "ALPHA"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+
+                      if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
+                        goto error;
+                      double a0 = lex_number (lexer);
+                      lex_get (lexer);
+
+                      lex_match (lexer, T_COMMA);
+                      if (lex_is_number (lexer))
+                        {
+                          if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
+                            goto error;
+                          double a1 = lex_number (lexer);
+                          lex_get (lexer);
+
+                          t->pairwise->alpha[0] = MIN (a0, a1);
+                          t->pairwise->alpha[1] = MAX (a0, a1);
+                        }
+                      else
+                        t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
+                    }
+                  else if (lex_match_id (lexer, "ADJUST"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (lex_match_id (lexer, "BONFERRONI"))
+                        t->pairwise->adjust = BONFERRONI;
+                      else if (lex_match_id (lexer, "BH"))
+                        t->pairwise->adjust = BH;
+                      else if (lex_match_id (lexer, "NONE"))
+                        t->pairwise->adjust = 0;
+                      else
+                        {
+                          lex_error_expecting (lexer, "BONFERRONI", "BH",
+                                               "NONE");
+                          goto error;
+                        }
+                    }
+                  else if (lex_match_id (lexer, "INCLUDEMRSETS"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (!parse_bool (lexer, &t->pairwise->include_mrsets))
+                        goto error;
+                    }
+                  else if (lex_match_id (lexer, "MEANSVARIANCE"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (lex_match_id (lexer, "ALLCATS"))
+                        t->pairwise->meansvariance_allcats = true;
+                      else if (lex_match_id (lexer, "TESTEDCATS"))
+                        t->pairwise->meansvariance_allcats = false;
+                      else
+                        {
+                          lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
+                          goto error;
+                        }
+                    }
+                  else if (lex_match_id (lexer, "CATEGORIES"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (lex_match_id (lexer, "ALLVISIBLE"))
+                        t->pairwise->all_visible = true;
+                      else if (lex_match_id (lexer, "SUBTOTALS"))
+                        t->pairwise->all_visible = false;
+                      else
+                        {
+                          lex_error_expecting (lexer, "ALLVISIBLE",
+                                               "SUBTOTALS");
+                          goto error;
+                        }
+                    }
+                  else if (lex_match_id (lexer, "MERGE"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (!parse_bool (lexer, &t->pairwise->merge))
+                        goto error;
+                    }
+                  else if (lex_match_id (lexer, "STYLE"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (lex_match_id (lexer, "APA"))
+                        t->pairwise->apa_style = true;
+                      else if (lex_match_id (lexer, "SIMPLE"))
+                        t->pairwise->apa_style = false;
+                      else
+                        {
+                          lex_error_expecting (lexer, "APA", "SIMPLE");
+                          goto error;
+                        }
+                    }
+                  else if (lex_match_id (lexer, "SHOWSIG"))
+                    {
+                      lex_match (lexer, T_EQUALS);
+                      if (!parse_bool (lexer, &t->pairwise->show_sig))
+                        goto error;
+                    }
+                  else
+                    {
+                      lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
+                                           "INCLUDEMRSETS", "MEANSVARIANCE",
+                                           "CATEGORIES", "MERGE", "STYLE",
+                                           "SHOWSIG");
+                      goto error;
+                    }
+                }
+              while (lex_token (lexer) != T_SLASH
+                     && lex_token (lexer) != T_ENDCMD);
+            }
+          else
+            {
+              lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
+                                   "CRITERIA", "CATEGORIES", "TITLES",
+                                   "SIGTEST", "COMPARETEST");
+              goto error;
+            }
+        }
+
+      if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
+        {
+          msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
+          goto error;
+        }
+
+    }
+  while (lex_token (lexer) != T_ENDCMD);
+
+  bool ok = ctables_execute (ds, ct);
+  ctables_destroy (ct);
+  return ok ? CMD_SUCCESS : CMD_FAILURE;
 
+error:
+  ctables_destroy (ct);
+  return CMD_FAILURE;
 }