d71754d01e88c1974dbb62abb9766a4a184422b1
[pspp] / src / language / stats / ctables.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2021 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #include <config.h>
18
19 #include "data/dataset.h"
20 #include "data/dictionary.h"
21 #include "data/mrset.h"
22 #include "language/command.h"
23 #include "language/lexer/format-parser.h"
24 #include "language/lexer/lexer.h"
25 #include "language/lexer/variable-parser.h"
26 #include "libpspp/assertion.h"
27 #include "libpspp/hmap.h"
28 #include "libpspp/message.h"
29 #include "libpspp/string-array.h"
30 #include "output/pivot-table.h"
31
32 #include "gl/minmax.h"
33 #include "gl/xalloc.h"
34
35 #include "gettext.h"
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) (msgid)
38
39 enum ctables_vlabel
40   {
41     CTVL_DEFAULT = SETTINGS_VALUE_SHOW_DEFAULT,
42     CTVL_NAME = SETTINGS_VALUE_SHOW_VALUE,
43     CTVL_LABEL = SETTINGS_VALUE_SHOW_LABEL,
44     CTVL_BOTH = SETTINGS_VALUE_SHOW_BOTH,
45     CTVL_NONE,
46   };
47 static void UNUSED
48 ctables_vlabel_unique (enum ctables_vlabel vlabel)
49 {
50   /* This ensures that all of the values are unique. */
51   switch (vlabel)
52     {
53     case CTVL_DEFAULT:
54     case CTVL_NAME:
55     case CTVL_LABEL:
56     case CTVL_BOTH:
57     case CTVL_NONE:
58       abort ();
59     }
60 }
61
62 /* XXX:
63    - unweighted summaries (U*)
64    - lower confidence limits (*.LCL)
65    - upper confidence limits (*.UCL)
66    - standard error (*.SE)
67  */
68 #define SUMMARIES                                                       \
69     /* All variables. */                                                \
70     S(CTSF_COUNT, "COUNT", N_("Count"), CTF_COUNT, CTFA_ALL)            \
71     S(CTSF_ECOUNT, "ECOUNT", N_("Adjusted Count"), CTF_COUNT, CTFA_ALL) \
72     S(CTSF_ROWPCT_COUNT, "ROWPCT.COUNT", N_("Row %"), CTF_PERCENT, CTFA_ALL) \
73     S(CTSF_COLPCT_COUNT, "COLPCT.COUNT", N_("Column %"), CTF_PERCENT, CTFA_ALL) \
74     S(CTSF_TABLEPCT_COUNT, "TABLEPCT.COUNT", N_("Table %"), CTF_PERCENT, CTFA_ALL) \
75     S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT.COUNT", N_("Subtable %"), CTF_PERCENT, CTFA_ALL) \
76     S(CTSF_LAYERPCT_COUNT, "LAYERPCT.COUNT", N_("Layer %"), CTF_PERCENT, CTFA_ALL) \
77     S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT.COUNT", N_("Layer Row %"), CTF_PERCENT, CTFA_ALL) \
78     S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT.COUNT", N_("Layer Column %"), CTF_PERCENT, CTFA_ALL) \
79     S(CTSF_ROWPCT_VALIDN, "ROWPCT.VALIDN", N_("Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
80     S(CTSF_COLPCT_VALIDN, "COLPCT.VALIDN", N_("Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
81     S(CTSF_TABLEPCT_VALIDN, "TABLEPCT.VALIDN", N_("Table Valid N %"), CTF_PERCENT, CTFA_ALL) \
82     S(CTSF_SUBTABLEPCT_VALIDN, "SUBTABLEPCT.VALIDN", N_("Subtable Valid N %"), CTF_PERCENT, CTFA_ALL) \
83     S(CTSF_LAYERPCT_VALIDN, "LAYERPCT.VALIDN", N_("Layer Valid N %"), CTF_PERCENT, CTFA_ALL) \
84     S(CTSF_LAYERROWPCT_VALIDN, "LAYERROWPCT.VALIDN", N_("Layer Row Valid N %"), CTF_PERCENT, CTFA_ALL) \
85     S(CTSF_LAYERCOLPCT_VALIDN, "LAYERCOLPCT.VALIDN", N_("Layer Column Valid N %"), CTF_PERCENT, CTFA_ALL) \
86     S(CTSF_ROWPCT_TOTALN, "ROWPCT.TOTALN", N_("Row Total N %"), CTF_PERCENT, CTFA_ALL) \
87     S(CTSF_COLPCT_TOTALN, "COLPCT.TOTALN", N_("Column Total N %"), CTF_PERCENT, CTFA_ALL) \
88     S(CTSF_TABLEPCT_TOTALN, "TABLEPCT.TOTALN", N_("Table Total N %"), CTF_PERCENT, CTFA_ALL) \
89     S(CTSF_SUBTABLEPCT_TOTALN, "SUBTABLEPCT.TOTALN", N_("Subtable Total N %"), CTF_PERCENT, CTFA_ALL) \
90     S(CTSF_LAYERPCT_TOTALN, "LAYERPCT.TOTALN", N_("Layer Total N %"), CTF_PERCENT, CTFA_ALL) \
91     S(CTSF_LAYERROWPCT_TOTALN, "LAYERROWPCT.TOTALN", N_("Layer Row Total N %"), CTF_PERCENT, CTFA_ALL) \
92     S(CTSF_LAYERCOLPCT_TOTALN, "LAYERCOLPCT.TOTALN", N_("Layer Column Total N %"), CTF_PERCENT, CTFA_ALL) \
93                                                                         \
94     /* Scale variables, totals, and subtotals. */                       \
95     S(CTSF_MAXIMUM, "MAXIMUM", N_("Maximum"), CTF_GENERAL, CTFA_SCALE)  \
96     S(CTSF_MEAN, "MEAN", N_("Mean"), CTF_GENERAL, CTFA_SCALE)           \
97     S(CTSF_MEDIAN, "MEDIAN", N_("Median"), CTF_GENERAL, CTFA_SCALE)     \
98     S(CTSF_MINIMUM, "MINIMUM", N_("Minimum"), CTF_GENERAL, CTFA_SCALE)  \
99     S(CTSF_MISSING, "MISSING", N_("Missing"), CTF_GENERAL, CTFA_SCALE)  \
100     S(CTSF_MODE, "MODE", N_("Mode"), CTF_GENERAL, CTFA_SCALE)           \
101     S(CTSF_PTILE, "PTILE", N_("Percentile"), CTF_GENERAL, CTFA_SCALE)   \
102     S(CTSF_RANGE, "RANGE", N_("Range"), CTF_GENERAL, CTFA_SCALE)        \
103     S(CTSF_SEMEAN, "SEMEAN", N_("Std Error of Mean"), CTF_GENERAL, CTFA_SCALE) \
104     S(CTSF_STDDEV, "STDDEV", N_("Std Deviation"), CTF_GENERAL, CTFA_SCALE) \
105     S(CTSF_SUM, "SUM", N_("Sum"), CTF_GENERAL, CTFA_SCALE)              \
106     S(CSTF_TOTALN, "TOTALN", N_("Total N"), CTF_COUNT, CTFA_SCALE)      \
107     S(CTSF_ETOTALN, "ETOTALN", N_("Adjusted Total N"), CTF_COUNT, CTFA_SCALE) \
108     S(CTSF_VALIDN, "VALIDN", N_("Valid N"), CTF_COUNT, CTFA_SCALE)      \
109     S(CTSF_EVALIDN, "EVALIDN", N_("Adjusted Valid N"), CTF_COUNT, CTFA_SCALE) \
110     S(CTSF_VARIANCE, "VARIANCE", N_("Variance"), CTF_GENERAL, CTFA_SCALE) \
111     S(CTSF_ROWPCT_SUM, "ROWPCT.SUM", N_("Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
112     S(CTSF_COLPCT_SUM, "COLPCT.SUM", N_("Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
113     S(CTSF_TABLEPCT_SUM, "TABLEPCT.SUM", N_("Table Sum %"), CTF_PERCENT, CTFA_SCALE) \
114     S(CTSF_SUBTABLEPCT_SUM, "SUBTABLEPCT.SUM", N_("Subtable Sum %"), CTF_PERCENT, CTFA_SCALE) \
115     S(CTSF_LAYERPCT_SUM, "LAYERPCT.SUM", N_("Layer Sum %"), CTF_PERCENT, CTFA_SCALE) \
116     S(CTSF_LAYERROWPCT_SUM, "LAYERROWPCT.SUM", N_("Layer Row Sum %"), CTF_PERCENT, CTFA_SCALE) \
117     S(CTSF_LAYERCOLPCT_SUM, "LAYERCOLPCT.SUM", N_("Layer Column Sum %"), CTF_PERCENT, CTFA_SCALE) \
118                                                                         \
119     /* Multiple response sets. */                                       \
120   S(CTSF_RESPONSES, "RESPONSES", N_("Responses"), CTF_COUNT, CTFA_MRSETS) \
121     S(CTSF_ROWPCT_RESPONSES, "ROWPCT.RESPONSES", N_("Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
122     S(CTSF_COLPCT_RESPONSES, "COLPCT.RESPONSES", N_("Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
123     S(CTSF_TABLEPCT_RESPONSES, "TABLEPCT.RESPONSES", N_("Table Responses %"), CTF_PERCENT, CTFA_MRSETS) \
124     S(CTSF_SUBTABLEPCT_RESPONSES, "SUBTABLEPCT.RESPONSES", N_("Subtable Responses %"), CTF_PERCENT, CTFA_MRSETS) \
125     S(CTSF_LAYERPCT_RESPONSES, "LAYERPCT.RESPONSES", N_("Layer Responses %"), CTF_PERCENT, CTFA_MRSETS) \
126     S(CTSF_LAYERROWPCT_RESPONSES, "LAYERROWPCT.RESPONSES", N_("Layer Row Responses %"), CTF_PERCENT, CTFA_MRSETS) \
127     S(CTSF_LAYERCOLPCT_RESPONSES, "LAYERCOLPCT.RESPONSES", N_("Layer Column Responses %"), CTF_PERCENT, CTFA_MRSETS) \
128     S(CTSF_ROWPCT_RESPONSES_COUNT, "ROWPCT.RESPONSES.COUNT", N_("Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
129     S(CTSF_COLPCT_RESPONSES_COUNT, "COLPCT.RESPONSES.COUNT", N_("Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
130     S(CTSF_TABLEPCT_RESPONSES_COUNT, "TABLEPCT.RESPONSES.COUNT", N_("Table Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
131     S(CTSF_SUBTABLEPCT_RESPONSES_COUNT, "SUBTABLEPCT.RESPONSES.COUNT", N_("Subtable Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
132     S(CTSF_LAYERPCT_RESPONSES_COUNT, "LAYERPCT.RESPONSES.COUNT", N_("Layer Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
133     S(CTSF_LAYERROWPCT_RESPONSES_COUNT, "LAYERROWPCT.RESPONSES.COUNT", N_("Layer Row Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
134     S(CTSF_LAYERCOLPCT_RESPONSES_COUNT, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Responses % (Base: Count)"), CTF_PERCENT, CTFA_MRSETS) \
135     S(CTSF_ROWPCT_COUNT_RESPONSES, "ROWPCT.COUNT.RESPONSES", N_("Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
136     S(CTSF_COLPCT_COUNT_RESPONSES, "COLPCT.COUNT.RESPONSES", N_("Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
137     S(CTSF_TABLEPCT_COUNT_RESPONSES, "TABLEPCT.COUNT.RESPONSES", N_("Table Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
138     S(CTSF_SUBTABLEPCT_COUNT_RESPONSES, "SUBTABLEPCT.COUNT.RESPONSES", N_("Subtable Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
139     S(CTSF_LAYERPCT_COUNT_RESPONSES, "LAYERPCT.COUNT.RESPONSES", N_("Layer Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
140     S(CTSF_LAYERROWPCT_COUNT_RESPONSES, "LAYERROWPCT.COUNT.RESPONSES", N_("Layer Row Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS) \
141     S(CTSF_LAYERCOLPCT_COUNT_RESPONSES, "LAYERCOLPCT.RESPONSES.COUNT", N_("Layer Column Count % (Base: Responses)"), CTF_PERCENT, CTFA_MRSETS)
142
143 enum ctables_summary_function
144   {
145 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
146     SUMMARIES
147 #undef S
148   };
149
150 enum {
151 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
152   N_CTSF_FUNCTIONS = SUMMARIES
153 #undef S
154 };
155
156 struct ctables
157   {
158     struct pivot_table_look *look;
159
160     /* If this is NULL, zeros are displayed using the normal print format.
161        Otherwise, this string is displayed. */
162     char *zero;
163
164     /* If this is NULL, missing values are displayed using the normal print
165        format.  Otherwise, this string is displayed. */
166     char *missing;
167
168     /* Indexed by variable dictionary index. */
169     enum ctables_vlabel *vlabels;
170
171     bool mrsets_count_duplicates; /* MRSETS. */
172     bool smissing_listwise;       /* SMISSING. */
173     struct variable *base_weight; /* WEIGHT. */
174     int hide_threshold;           /* HIDESMALLCOUNTS. */
175
176     struct ctables_table *tables;
177     size_t n_tables;
178   };
179
180 struct ctables_postcompute
181   {
182     struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
183     const char *name;           /* Name, without leading &. */
184
185     struct ctables_postcompute_expr *expr;
186     char *label;
187     /* XXX FORMAT */
188     bool hide_source_cats;
189   };
190
191 struct ctables_postcompute_expr
192   {
193     enum ctables_postcompute_op
194       {
195         /* Terminals. */
196         CTPO_CAT_NUMBER,
197         CTPO_CAT_STRING,
198         CTPO_CAT_RANGE,
199         CTPO_CAT_MISSING,
200         /* XXX OTHERNM */
201         /* XXX SUBTOTAL and HSUBTOTAL */
202
203         /* Nonterminals. */
204         CTPO_ADD,
205         CTPO_SUB,
206         CTPO_MUL,
207         CTPO_DIV,
208         CTPO_POW,
209       }
210     op;
211
212     union
213       {
214         /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
215         double number;
216
217         /* CTPO_CAT_RANGE.
218
219            XXX what about string ranges? */
220         double range[2];
221
222         /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
223         struct ctables_postcompute_expr *subs[2];
224       };
225   };
226
227 enum ctables_label_position
228   {
229     CTLP_NORMAL,
230     CTLP_OPPOSITE,
231     CTLP_LAYER,
232   };
233
234 struct ctables_table
235   {
236     struct ctables_axis *axes[PIVOT_N_AXES];
237
238     enum pivot_axis_type slabels_position;
239     bool slabels_visible;
240
241     enum ctables_label_position row_labels;
242     enum ctables_label_position col_labels;
243
244     /* Indexed by variable dictionary index. */
245     struct ctables_categories **categories;
246     size_t n_categories;
247
248     double cilevel;
249
250     char *caption;
251     char *corner;
252     char *title;
253
254     struct ctables_chisq *chisq;
255     struct ctables_pairwise *pairwise;
256   };
257
258 struct ctables_var
259   {
260     bool is_mrset;
261     union
262       {
263         struct variable *var;
264         const struct mrset *mrset;
265       };
266   };
267
268 static const struct fmt_spec *
269 ctables_var_get_print_format (const struct ctables_var *var)
270 {
271   return (var->is_mrset
272           ? var_get_print_format (var->mrset->vars[0])
273           : var_get_print_format (var->var));
274 }
275
276 static const char *
277 ctables_var_name (const struct ctables_var *var)
278 {
279   return var->is_mrset ? var->mrset->name : var_get_name (var->var);
280 }
281
282 struct ctables_categories
283   {
284     size_t n_refs;
285
286     /* Explicit categories. */
287     struct ctables_cat_value *values;
288     size_t n_values;
289
290     /* Implicit categories. */
291     bool sort_ascending;
292     bool include_missing;
293     enum { CTCS_VALUE, CTCS_LABEL, CTCS_FUNCTION } key;
294     enum ctables_summary_function sort_func;
295     struct variable *sort_func_var;
296     double percentile;
297
298     /* Totals. */
299     bool show_totals;
300     bool totals_before;
301     char *total_label;
302
303     /* Empty categories. */
304     bool show_empty;
305   };
306
307 struct ctables_cat_value
308   {
309     enum ctables_cat_value_type
310       {
311         CCVT_NUMBER,
312         CCVT_STRING,
313         CCVT_RANGE,
314         CCVT_MISSING,
315         CCVT_OTHERNM,
316         CCVT_SUBTOTAL,
317         CCVT_HSUBTOTAL,
318       }
319     type;
320
321     union
322       {
323         double number;          /* CCVT_NUMBER. */
324         char *string;           /* CCVT_STRING. */
325         double range[2];        /* CCVT_RANGE. */
326         char *subtotal_label;   /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
327       };
328   };
329
330 static void
331 ctables_cat_value_uninit (struct ctables_cat_value *cv)
332 {
333   if (!cv)
334     return;
335
336   switch (cv->type)
337     {
338     case CCVT_NUMBER:
339     case CCVT_RANGE:
340     case CCVT_MISSING:
341     case CCVT_OTHERNM:
342       break;
343
344     case CCVT_STRING:
345       free (cv->string);
346       break;
347
348     case CCVT_SUBTOTAL:
349     case CCVT_HSUBTOTAL:
350       free (cv->subtotal_label);
351     }
352 }
353
354 static void
355 ctables_categories_unref (struct ctables_categories *c)
356 {
357   if (!c)
358     return;
359
360   assert (c->n_refs > 0);
361   if (--c->n_refs)
362     return;
363
364   for (size_t i = 0; i < c->n_values; i++)
365     ctables_cat_value_uninit (&c->values[i]);
366   free (c->values);
367   free (c->total_label);
368   free (c);
369 }
370
371 /* Chi-square test (SIGTEST). */
372 struct ctables_chisq
373   {
374     double alpha;
375     bool include_mrsets;
376     bool all_visible;
377   };
378
379 /* Pairwise comparison test (COMPARETEST). */
380 struct ctables_pairwise
381   {
382     enum { PROP, MEAN } type;
383     double alpha[2];
384     bool include_mrsets;
385     bool meansvariance_allcats;
386     bool all_visible;
387     enum { BONFERRONI = 1, BH } adjust;
388     bool merge;
389     bool apa_style;
390     bool show_sig;
391   };
392
393 struct ctables_axis
394   {
395     enum ctables_axis_op
396       {
397         /* Terminals. */
398         CTAO_VAR,
399
400         /* Nonterminals. */
401         CTAO_STACK,             /* + */
402         CTAO_NEST,              /* > */
403       }
404     op;
405
406     union
407       {
408         /* Terminals. */
409         struct
410           {
411             struct ctables_var var;
412             bool scale;
413             struct ctables_summary *summaries;
414             size_t n_summaries;
415             size_t allocated_summaries;
416           };
417
418         /* Nonterminals. */
419         struct ctables_axis *subs[2];
420       };
421
422     struct msg_location *loc;
423   };
424
425 static void ctables_axis_destroy (struct ctables_axis *);
426
427 enum ctables_format
428   {
429     CTF_COUNT,
430     CTF_PERCENT,
431     CTF_GENERAL
432   };
433
434 enum ctables_function_availability
435   {
436     CTFA_ALL,                /* Any variables. */
437     CTFA_SCALE,              /* Only scale variables, totals, and subtotals. */
438     CTFA_MRSETS,             /* Only multiple-response sets */
439   };
440
441 struct ctables_summary
442   {
443     enum ctables_summary_function function;
444     double percentile;          /* CTSF_PTILE only. */
445     char *label;
446     struct fmt_spec format;     /* XXX extra CTABLES formats */
447   };
448
449 static void
450 ctables_summary_uninit (struct ctables_summary *s)
451 {
452   if (s)
453     free (s->label);
454 }
455
456 static bool
457 parse_col_width (struct lexer *lexer, const char *name, double *width)
458 {
459   lex_match (lexer, T_EQUALS);
460   if (lex_match_id (lexer, "DEFAULT"))
461     *width = SYSMIS;
462   else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
463     {
464       *width = lex_number (lexer);
465       lex_get (lexer);
466     }
467   else
468     return false;
469
470   return true;
471 }
472
473 static bool
474 parse_bool (struct lexer *lexer, bool *b)
475 {
476   if (lex_match_id (lexer, "NO"))
477     *b = false;
478   else if (lex_match_id (lexer, "YES"))
479     *b = true;
480   else
481     {
482       lex_error_expecting (lexer, "YES", "NO");
483       return false;
484     }
485   return true;
486 }
487
488 static enum ctables_function_availability
489 ctables_function_availability (enum ctables_summary_function f)
490 {
491   static enum ctables_function_availability availability[] = {
492 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
493     SUMMARIES
494 #undef S
495   };
496
497   return availability[f];
498 }
499
500 static bool
501 parse_ctables_summary_function (struct lexer *lexer,
502                                 enum ctables_summary_function *f)
503 {
504   struct pair
505     {
506       enum ctables_summary_function function;
507       struct substring name;
508     };
509   static struct pair names[] = {
510 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
511     { ENUM, SS_LITERAL_INITIALIZER (NAME) },
512     SUMMARIES
513
514     /* The .COUNT suffix may be omitted. */
515     S(CTSF_ROWPCT_COUNT, "ROWPCT", _, _, _)
516     S(CTSF_COLPCT_COUNT, "COLPCT", _, _, _)
517     S(CTSF_TABLEPCT_COUNT, "TABLEPCT", _, _, _)
518     S(CTSF_SUBTABLEPCT_COUNT, "SUBTABLEPCT", _, _, _)
519     S(CTSF_LAYERPCT_COUNT, "LAYERPCT", _, _, _)
520     S(CTSF_LAYERROWPCT_COUNT, "LAYERROWPCT", _, _, _)
521     S(CTSF_LAYERCOLPCT_COUNT, "LAYERCOLPCT", _, _, _)
522 #undef S
523   };
524
525   if (!lex_force_id (lexer))
526     return false;
527
528   for (size_t i = 0; i < sizeof names / sizeof *names; i++)
529     if (ss_equals_case (names[i].name, lex_tokss (lexer)))
530       {
531         *f = names[i].function;
532         lex_get (lexer);
533         return true;
534       }
535
536   lex_error (lexer, _("Expecting summary function name."));
537   return false;
538 }
539
540 static void
541 ctables_axis_destroy (struct ctables_axis *axis)
542 {
543   if (!axis)
544     return;
545
546   switch (axis->op)
547     {
548     case CTAO_VAR:
549       for (size_t i = 0; i < axis->n_summaries; i++)
550         ctables_summary_uninit (&axis->summaries[i]);
551       free (axis->summaries);
552       break;
553
554     case CTAO_STACK:
555     case CTAO_NEST:
556       ctables_axis_destroy (axis->subs[0]);
557       ctables_axis_destroy (axis->subs[1]);
558       break;
559     }
560   msg_location_destroy (axis->loc);
561   free (axis);
562 }
563
564 static struct ctables_axis *
565 ctables_axis_new_nonterminal (enum ctables_axis_op op,
566                               struct ctables_axis *sub0,
567                               struct ctables_axis *sub1,
568                               struct lexer *lexer, int start_ofs)
569 {
570   struct ctables_axis *axis = xmalloc (sizeof *axis);
571   *axis = (struct ctables_axis) {
572     .op = op,
573     .subs = { sub0, sub1 },
574     .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
575   };
576   return axis;
577 }
578
579 struct ctables_axis_parse_ctx
580   {
581     struct lexer *lexer;
582     struct dictionary *dict;
583     struct ctables *ct;
584     struct ctables_table *t;
585   };
586
587 static struct fmt_spec
588 ctables_summary_default_format (enum ctables_summary_function function,
589                                 const struct ctables_var *var)
590 {
591   static const enum ctables_format default_formats[] = {
592 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
593     SUMMARIES
594 #undef S
595   };
596   switch (default_formats[function])
597     {
598     case CTF_COUNT:
599       return (struct fmt_spec) { .type = FMT_F, .w = 40 };
600
601     case CTF_PERCENT:
602       return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
603
604     case CTF_GENERAL:
605       return *ctables_var_get_print_format (var);
606
607     default:
608       NOT_REACHED ();
609     }
610 }
611
612 static const char *
613 ctables_summary_function_name (enum ctables_summary_function function)
614 {
615   static const char *names[] = {
616 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
617     SUMMARIES
618 #undef S
619   };
620   return names[function];
621 }
622
623 static bool
624 add_summary (struct ctables_axis *axis,
625              enum ctables_summary_function function, double percentile,
626              const char *label, const struct fmt_spec *format,
627              const struct msg_location *loc)
628 {
629   if (axis->op == CTAO_VAR)
630     {
631       if (axis->n_summaries >= axis->allocated_summaries)
632         axis->summaries = x2nrealloc (axis->summaries,
633                                       &axis->allocated_summaries,
634                                       sizeof *axis->summaries);
635
636       const char *function_name = ctables_summary_function_name (function);
637       const char *var_name = ctables_var_name (&axis->var);
638       switch (ctables_function_availability (function))
639         {
640         case CTFA_MRSETS:
641           if (!axis->var.is_mrset)
642             {
643               msg_at (SE, loc, _("Summary function %s applies only to multiple "
644                                  "response sets."), function_name);
645               msg_at (SN, axis->loc, _("'%s' is not a multiple response set."),
646                       var_name);
647               return false;
648             }
649           break;
650
651         case CTFA_SCALE:
652           if (!axis->scale)
653             {
654               msg_at (SE, loc,
655                       _("Summary function %s applies only to scale variables."),
656                       function_name);
657               msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
658                       var_name);
659               return false;
660             }
661           break;
662
663         case CTFA_ALL:
664           break;
665         }
666
667       struct ctables_summary *dst = &axis->summaries[axis->n_summaries++];
668       *dst = (struct ctables_summary) {
669         .function = function,
670         .percentile = percentile,
671         .label = xstrdup (label),
672         .format = (format ? *format
673                    : ctables_summary_default_format (function, &axis->var)),
674       };
675       return true;
676     }
677   else
678     {
679       for (size_t i = 0; i < 2; i++)
680         if (!add_summary (axis->subs[i], function, percentile, label, format,
681                           loc))
682           return false;
683       return true;
684     }
685 }
686
687 static struct ctables_axis *ctables_axis_parse_stack (
688   struct ctables_axis_parse_ctx *);
689
690 static bool
691 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
692                    struct ctables_var *var)
693 {
694   if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
695     {
696       *var = (struct ctables_var) {
697         .is_mrset = true,
698         .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
699       };
700       if (!var->mrset)
701         {
702           lex_error (lexer, _("'%s' does not name a multiple-response set "
703                               "in the active file dictionary."),
704                      lex_tokcstr (lexer));
705           return false;
706         }
707       lex_get (lexer);
708       return true;
709     }
710   else
711     {
712       *var = (struct ctables_var) {
713         .is_mrset = false,
714         .var = parse_variable (lexer, dict),
715       };
716       return var->var != NULL;
717     }
718 }
719
720 static struct ctables_axis *
721 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
722 {
723   if (lex_match (ctx->lexer, T_LPAREN))
724     {
725       struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
726       if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
727         {
728           ctables_axis_destroy (sub);
729           return NULL;
730         }
731       return sub;
732     }
733
734   if (!lex_force_id (ctx->lexer))
735     return NULL;
736
737   int start_ofs = lex_ofs (ctx->lexer);
738   struct ctables_var var;
739   if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
740     return NULL;
741
742   struct ctables_axis *axis = xmalloc (sizeof *axis);
743   *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
744
745   /* XXX should figure out default measures by reading data */
746   axis->scale = (var.is_mrset ? false
747                  : lex_match_phrase (ctx->lexer, "[S]") ? true
748                  : lex_match_phrase (ctx->lexer, "[C]") ? false
749                  : var_get_measure (var.var) == MEASURE_SCALE);
750   axis->loc = lex_ofs_location (ctx->lexer, start_ofs,
751                                 lex_ofs (ctx->lexer) - 1);
752   return axis;
753 }
754
755 static struct ctables_axis *
756 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
757 {
758   struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
759   if (!sub || !lex_match (ctx->lexer, T_LBRACK))
760     return sub;
761
762   do
763     {
764       int start_ofs = lex_ofs (ctx->lexer);
765
766       /* Parse function. */
767       enum ctables_summary_function function;
768       if (!parse_ctables_summary_function (ctx->lexer, &function))
769         goto error;
770
771       /* Parse percentile. */
772       double percentile = 0;
773       if (function == CTSF_PTILE)
774         {
775           if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
776             goto error;
777           percentile = lex_number (ctx->lexer);
778           lex_get (ctx->lexer);
779         }
780
781       /* Parse label. */
782       char *label;
783       if (lex_is_string (ctx->lexer))
784         {
785           label = ss_xstrdup (lex_tokss (ctx->lexer));
786           lex_get (ctx->lexer);
787         }
788       else if (function == CTSF_PTILE)
789         label = xasprintf (_("Percentile %.2f"), percentile);
790       else
791         {
792           static const char *default_labels[] = {
793 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
794             SUMMARIES
795 #undef S
796           };
797           label = xstrdup (gettext (default_labels[function]));
798         }
799
800       /* Parse format. */
801       struct fmt_spec format;
802       const struct fmt_spec *formatp;
803       if (lex_token (ctx->lexer) == T_ID)
804         {
805           if (!parse_format_specifier (ctx->lexer, &format)
806               || !fmt_check_output (&format)
807               || !fmt_check_type_compat (&format, VAL_NUMERIC))
808             {
809               free (label);
810               goto error;
811             }
812           formatp = &format;
813         }
814       else
815         formatp = NULL;
816
817       struct msg_location *loc = lex_ofs_location (ctx->lexer, start_ofs,
818                                                    lex_ofs (ctx->lexer) - 1);
819       add_summary (sub, function, percentile, label, formatp, loc);
820       free (label);
821       msg_location_destroy (loc);
822
823       lex_match (ctx->lexer, T_COMMA);
824     }
825   while (!lex_match (ctx->lexer, T_RBRACK));
826
827   return sub;
828
829 error:
830   ctables_axis_destroy (sub);
831   return NULL;
832 }
833
834 static const struct ctables_axis *
835 find_scale (const struct ctables_axis *axis)
836 {
837   if (!axis)
838     return NULL;
839   else if (axis->op == CTAO_VAR)
840     {
841       if (axis->scale)
842         {
843           assert (!axis->var.is_mrset);
844           return axis;
845         }
846       else
847         return NULL;
848     }
849   else
850     {
851       for (size_t i = 0; i < 2; i++)
852         {
853           const struct ctables_axis *scale = find_scale (axis->subs[i]);
854           if (scale)
855             return scale;
856         }
857       return NULL;
858     }
859 }
860
861 static const struct ctables_axis *
862 find_categorical_summary (const struct ctables_axis *axis)
863 {
864   if (!axis)
865     return NULL;
866   else if (axis->op == CTAO_VAR)
867     return !axis->scale && axis->n_summaries ? axis : NULL;
868   else
869     {
870       for (size_t i = 0; i < 2; i++)
871         {
872           const struct ctables_axis *sum
873             = find_categorical_summary (axis->subs[i]);
874           if (sum)
875             return sum;
876         }
877       return NULL;
878     }
879 }
880
881 static struct ctables_axis *
882 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
883 {
884   int start_ofs = lex_ofs (ctx->lexer);
885   struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
886   if (!lhs)
887     return NULL;
888
889   while (lex_match (ctx->lexer, T_GT))
890     {
891       struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
892       if (!rhs)
893         return NULL;
894
895       struct ctables_axis *nest = ctables_axis_new_nonterminal (
896         CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
897
898       const struct ctables_axis *outer_scale = find_scale (lhs);
899       const struct ctables_axis *inner_scale = find_scale (rhs);
900       if (outer_scale && inner_scale)
901         {
902           msg_at (SE, nest->loc, _("Cannot nest scale variables."));
903           msg_at (SN, outer_scale->loc, _("This is an outer scale variable."));
904           msg_at (SN, inner_scale->loc, _("This is an inner scale variable."));
905           ctables_axis_destroy (nest);
906           return NULL;
907         }
908
909       const struct ctables_axis *outer_sum = find_categorical_summary (lhs);
910       if (outer_sum)
911         {
912           msg_at (SE, nest->loc,
913                   _("Summaries may only be requested for categorical variables "
914                     "at the innermost nesting level."));
915           msg_at (SN, outer_sum->loc,
916                   _("This outer categorical variable has a summary."));
917           ctables_axis_destroy (nest);
918           return NULL;
919         }
920
921       lhs = nest;
922     }
923
924   return lhs;
925 }
926
927 static struct ctables_axis *
928 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
929 {
930   int start_ofs = lex_ofs (ctx->lexer);
931   struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
932   if (!lhs)
933     return NULL;
934
935   while (lex_match (ctx->lexer, T_PLUS))
936     {
937       struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
938       if (!rhs)
939         return NULL;
940
941       lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
942                                           ctx->lexer, start_ofs);
943     }
944
945   return lhs;
946 }
947
948 static bool
949 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
950                     struct ctables *ct, struct ctables_table *t,
951                     enum pivot_axis_type a)
952 {
953   if (lex_token (lexer) == T_BY
954       || lex_token (lexer) == T_SLASH
955       || lex_token (lexer) == T_ENDCMD)
956     return true;
957
958   struct ctables_axis_parse_ctx ctx = {
959     .lexer = lexer,
960     .dict = dict,
961     .ct = ct,
962     .t = t
963   };
964   t->axes[a] = ctables_axis_parse_stack (&ctx);
965   return t->axes[a] != NULL;
966 }
967
968 static void
969 ctables_chisq_destroy (struct ctables_chisq *chisq)
970 {
971   free (chisq);
972 }
973
974 static void
975 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
976 {
977   free (pairwise);
978 }
979
980 static void
981 ctables_table_uninit (struct ctables_table *t)
982 {
983   if (!t)
984     return;
985
986   for (size_t i = 0; i < t->n_categories; i++)
987     ctables_categories_unref (t->categories[i]);
988   free (t->categories);
989
990   ctables_axis_destroy (t->axes[PIVOT_AXIS_COLUMN]);
991   ctables_axis_destroy (t->axes[PIVOT_AXIS_ROW]);
992   ctables_axis_destroy (t->axes[PIVOT_AXIS_LAYER]);
993   free (t->caption);
994   free (t->corner);
995   free (t->title);
996   ctables_chisq_destroy (t->chisq);
997   ctables_pairwise_destroy (t->pairwise);
998 }
999
1000 static void
1001 ctables_destroy (struct ctables *ct)
1002 {
1003   if (!ct)
1004     return;
1005
1006   pivot_table_look_unref (ct->look);
1007   free (ct->zero);
1008   free (ct->missing);
1009   free (ct->vlabels);
1010   for (size_t i = 0; i < ct->n_tables; i++)
1011     ctables_table_uninit (&ct->tables[i]);
1012   free (ct->tables);
1013   free (ct);
1014 }
1015
1016 static struct ctables_cat_value
1017 ccvt_range (double low, double high)
1018 {
1019   return (struct ctables_cat_value) {
1020     .type = CCVT_RANGE,
1021     .range = { low, high }
1022   };
1023 }
1024
1025 static bool
1026 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1027                                 struct ctables_table *t)
1028 {
1029   if (!lex_match_id (lexer, "VARIABLES"))
1030     return false;
1031   lex_match (lexer, T_EQUALS);
1032
1033   struct variable **vars;
1034   size_t n_vars;
1035   if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
1036     return false;
1037
1038   struct ctables_categories *c = xmalloc (sizeof *c);
1039   *c = (struct ctables_categories) { .n_refs = n_vars };
1040   for (size_t i = 0; i < n_vars; i++)
1041     {
1042       struct ctables_categories **cp
1043         = &t->categories[var_get_dict_index (vars[i])];
1044       ctables_categories_unref (*cp);
1045       *cp = c;
1046     }
1047   free (vars);
1048
1049   if (lex_match (lexer, T_LBRACK))
1050     {
1051       size_t allocated_values = 0;
1052       do
1053         {
1054           if (c->n_values >= allocated_values)
1055             c->values = x2nrealloc (c->values, &allocated_values,
1056                                     sizeof *c->values);
1057
1058           struct ctables_cat_value *v = &c->values[c->n_values];
1059           if (lex_match_id (lexer, "OTHERNM"))
1060             v->type = CCVT_OTHERNM;
1061           else if (lex_match_id (lexer, "MISSING"))
1062             v->type = CCVT_MISSING;
1063           else if (lex_match_id (lexer, "SUBTOTAL"))
1064             *v = (struct ctables_cat_value)
1065               { .type = CCVT_SUBTOTAL, .subtotal_label = NULL };
1066           else if (lex_match_id (lexer, "HSUBTOTAL"))
1067             *v = (struct ctables_cat_value)
1068               { .type = CCVT_HSUBTOTAL, .subtotal_label = NULL };
1069           else if (lex_match_id (lexer, "LO"))
1070             {
1071               if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1072                 return false;
1073               *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1074               lex_get (lexer);
1075             }
1076           else if (lex_is_number (lexer))
1077             {
1078               double number = lex_number (lexer);
1079               lex_get (lexer);
1080               if (lex_match_id (lexer, "THRU"))
1081                 {
1082                   v->type = CCVT_RANGE;
1083                   v->range[0] = number;
1084                   if (lex_match_id (lexer, "HI"))
1085                     *v = ccvt_range (number, DBL_MAX);
1086                   else
1087                     {
1088                       if (!lex_force_num (lexer))
1089                         return false;
1090                       *v = ccvt_range (number, lex_number (lexer));
1091                       lex_get (lexer);
1092                     }
1093                 }
1094               else
1095                 *v = (struct ctables_cat_value) {
1096                   .type = CCVT_NUMBER,
1097                   .number = number
1098                 };
1099             }
1100           else if (lex_is_string (lexer))
1101             {
1102               *v = (struct ctables_cat_value) {
1103                 .type = CCVT_STRING,
1104                 .string = ss_xstrdup (lex_tokss (lexer)),
1105               };
1106               lex_get (lexer);
1107             }
1108           else
1109             {
1110               lex_error (lexer, NULL);
1111               return false;
1112             }
1113
1114           if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1115               && lex_match (lexer, T_EQUALS))
1116             {
1117               if (!lex_force_string (lexer))
1118                 return false;
1119
1120               v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1121               lex_get (lexer);
1122             }
1123
1124           c->n_values++;
1125           lex_match (lexer, T_COMMA);
1126         }
1127       while (!lex_match (lexer, T_RBRACK));
1128     }
1129
1130   while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1131     {
1132       if (!c->n_values && lex_match_id (lexer, "ORDER"))
1133         {
1134           lex_match (lexer, T_EQUALS);
1135           if (lex_match_id (lexer, "A"))
1136             c->sort_ascending = true;
1137           else if (lex_match_id (lexer, "D"))
1138             c->sort_ascending = false;
1139           else
1140             {
1141               lex_error_expecting (lexer, "A", "D");
1142               return false;
1143             }
1144         }
1145       else if (!c->n_values && lex_match_id (lexer, "KEY"))
1146         {
1147           lex_match (lexer, T_EQUALS);
1148           if (lex_match_id (lexer, "VALUE"))
1149             c->key = CTCS_VALUE;
1150           else if (lex_match_id (lexer, "LABEL"))
1151             c->key = CTCS_LABEL;
1152           else
1153             {
1154               c->key = CTCS_FUNCTION;
1155               if (!parse_ctables_summary_function (lexer, &c->sort_func))
1156                 return false;
1157
1158               if (lex_match (lexer, T_LPAREN))
1159                 {
1160                   c->sort_func_var = parse_variable (lexer, dict);
1161                   if (!c->sort_func_var)
1162                     return false;
1163
1164                   if (c->sort_func == CTSF_PTILE)
1165                     {
1166                       lex_match (lexer, T_COMMA);
1167                       if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1168                         return false;
1169                       c->percentile = lex_number (lexer);
1170                       lex_get (lexer);
1171                     }
1172
1173                   if (!lex_force_match (lexer, T_RPAREN))
1174                     return false;
1175                 }
1176               else if (ctables_function_availability (c->sort_func)
1177                        == CTFA_SCALE)
1178                 {
1179                   bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1180                   return false;
1181                 }
1182             }
1183         }
1184       else if (!c->n_values && lex_match_id (lexer, "MISSING"))
1185         {
1186           lex_match (lexer, T_EQUALS);
1187           if (lex_match_id (lexer, "INCLUDE"))
1188             c->include_missing = true;
1189           else if (lex_match_id (lexer, "EXCLUDE"))
1190             c->include_missing = false;
1191           else
1192             {
1193               lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1194               return false;
1195             }
1196         }
1197       else if (lex_match_id (lexer, "TOTAL"))
1198         {
1199           lex_match (lexer, T_EQUALS);
1200           if (!parse_bool (lexer, &c->show_totals))
1201             return false;
1202         }
1203       else if (lex_match_id (lexer, "LABEL"))
1204         {
1205           lex_match (lexer, T_EQUALS);
1206           if (!lex_force_string (lexer))
1207             return false;
1208           free (c->total_label);
1209           c->total_label = ss_xstrdup (lex_tokss (lexer));
1210           lex_get (lexer);
1211         }
1212       else if (lex_match_id (lexer, "POSITION"))
1213         {
1214           lex_match (lexer, T_EQUALS);
1215           if (lex_match_id (lexer, "BEFORE"))
1216             c->totals_before = true;
1217           else if (lex_match_id (lexer, "AFTER"))
1218             c->totals_before = false;
1219           else
1220             {
1221               lex_error_expecting (lexer, "BEFORE", "AFTER");
1222               return false;
1223             }
1224         }
1225       else if (lex_match_id (lexer, "EMPTY"))
1226         {
1227           lex_match (lexer, T_EQUALS);
1228           if (lex_match_id (lexer, "INCLUDE"))
1229             c->show_empty = true;
1230           else if (lex_match_id (lexer, "EXCLUDE"))
1231             c->show_empty = false;
1232           else
1233             {
1234               lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1235               return false;
1236             }
1237         }
1238       else
1239         {
1240           if (!c->n_values)
1241             lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1242                                  "TOTAL", "LABEL", "POSITION", "EMPTY");
1243           else
1244             lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1245           return false;
1246         }
1247     }
1248   return true;
1249 }
1250
1251 struct ctables_freqtab
1252   {
1253     struct variable **vars;
1254     size_t n_vars;
1255
1256     struct hmap data;           /* Contains "struct freq"s. */
1257   };
1258
1259 static struct string_array
1260 nest_fts (struct string_array sa0, struct string_array sa1)
1261 {
1262   if (!sa0.n)
1263     return sa1;
1264   else if (!sa1.n)
1265     return sa0;
1266
1267   struct string_array sa = STRING_ARRAY_INITIALIZER;
1268   for (size_t i = 0; i < sa0.n; i++)
1269     for (size_t j = 0; j < sa1.n; j++)
1270       string_array_append_nocopy (&sa, xasprintf ("%s, %s",
1271                                                   sa0.strings[i],
1272                                                   sa1.strings[j]));
1273   string_array_destroy (&sa0);
1274   string_array_destroy (&sa1);
1275   return sa;
1276 }
1277
1278 static struct string_array
1279 enumerate_fts (const struct ctables_axis *a)
1280 {
1281   struct string_array sa = STRING_ARRAY_INITIALIZER;
1282   if (!a)
1283     return sa;
1284
1285   switch (a->op)
1286     {
1287     case CTAO_VAR:
1288       string_array_append (&sa, ctables_var_name (&a->var));
1289       break;
1290
1291     case CTAO_STACK:
1292       sa = enumerate_fts (a->subs[0]);
1293       struct string_array sa2 = enumerate_fts (a->subs[1]);
1294       for (size_t i = 0; i < sa2.n; i++)
1295         string_array_append_nocopy (&sa, sa2.strings[i]);
1296       free (sa2.strings);
1297       break;
1298
1299     case CTAO_NEST:
1300       return nest_fts (enumerate_fts (a->subs[0]),
1301                        enumerate_fts (a->subs[1]));
1302     }
1303   return sa;
1304 }
1305
1306 static void
1307 ctables_execute (struct ctables *ct)
1308 {
1309   for (size_t i = 0; i < ct->n_tables; i++)
1310     {
1311       struct ctables_table *t = &ct->tables[i];
1312       struct string_array sa = enumerate_fts (t->axes[PIVOT_AXIS_ROW]);
1313       sa = nest_fts (sa, enumerate_fts (t->axes[PIVOT_AXIS_COLUMN]));
1314       sa = nest_fts (sa, enumerate_fts (t->axes[PIVOT_AXIS_LAYER]));
1315       for (size_t i = 0; i < sa.n; i++)
1316         puts (sa.strings[i]);
1317       putc ('\n', stdout);
1318       string_array_destroy (&sa);
1319     }
1320 }
1321
1322 int
1323 cmd_ctables (struct lexer *lexer, struct dataset *ds)
1324 {
1325   size_t n_vars = dict_get_n_vars (dataset_dict (ds));
1326   enum ctables_vlabel *vlabels = xnmalloc (n_vars, sizeof *vlabels);
1327   for (size_t i = 0; i < n_vars; i++)
1328     vlabels[i] = CTVL_DEFAULT;
1329
1330   struct ctables *ct = xmalloc (sizeof *ct);
1331   *ct = (struct ctables) {
1332     .look = pivot_table_look_unshare (pivot_table_look_ref (
1333                                         pivot_table_look_get_default ())),
1334     .vlabels = vlabels,
1335     .hide_threshold = 5,
1336   };
1337
1338   if (!lex_force_match (lexer, T_SLASH))
1339     goto error;
1340
1341   while (!lex_match_id (lexer, "TABLE"))
1342     {
1343       if (lex_match_id (lexer, "FORMAT"))
1344         {
1345           double widths[2] = { SYSMIS, SYSMIS };
1346           double units_per_inch = 72.0;
1347
1348           while (lex_token (lexer) != T_SLASH)
1349             {
1350               if (lex_match_id (lexer, "MINCOLWIDTH"))
1351                 {
1352                   if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
1353                     goto error;
1354                 }
1355               else if (lex_match_id (lexer, "MAXCOLWIDTH"))
1356                 {
1357                   if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
1358                     goto error;
1359                 }
1360               else if (lex_match_id (lexer, "UNITS"))
1361                 {
1362                   lex_match (lexer, T_EQUALS);
1363                   if (lex_match_id (lexer, "POINTS"))
1364                     units_per_inch = 72.0;
1365                   else if (lex_match_id (lexer, "INCHES"))
1366                     units_per_inch = 1.0;
1367                   else if (lex_match_id (lexer, "CM"))
1368                     units_per_inch = 2.54;
1369                   else
1370                     {
1371                       lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
1372                       goto error;
1373                     }
1374                 }
1375               else if (lex_match_id (lexer, "EMPTY"))
1376                 {
1377                   free (ct->zero);
1378                   ct->zero = NULL;
1379
1380                   lex_match (lexer, T_EQUALS);
1381                   if (lex_match_id (lexer, "ZERO"))
1382                     {
1383                       /* Nothing to do. */
1384                     }
1385                   else if (lex_match_id (lexer, "BLANK"))
1386                     ct->zero = xstrdup ("");
1387                   else if (lex_force_string (lexer))
1388                     {
1389                       ct->zero = ss_xstrdup (lex_tokss (lexer));
1390                       lex_get (lexer);
1391                     }
1392                   else
1393                     goto error;
1394                 }
1395               else if (lex_match_id (lexer, "MISSING"))
1396                 {
1397                   lex_match (lexer, T_EQUALS);
1398                   if (!lex_force_string (lexer))
1399                     goto error;
1400
1401                   free (ct->missing);
1402                   ct->missing = (strcmp (lex_tokcstr (lexer), ".")
1403                                  ? ss_xstrdup (lex_tokss (lexer))
1404                                  : NULL);
1405                   lex_get (lexer);
1406                 }
1407               else
1408                 {
1409                   lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
1410                                        "UNITS", "EMPTY", "MISSING");
1411                   goto error;
1412                 }
1413             }
1414
1415           if (widths[0] != SYSMIS && widths[1] != SYSMIS
1416               && widths[0] > widths[1])
1417             {
1418               msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
1419               goto error;
1420             }
1421
1422           for (size_t i = 0; i < 2; i++)
1423             if (widths[i] != SYSMIS)
1424               {
1425                 int *wr = ct->look->width_ranges[TABLE_HORZ];
1426                 wr[i] = widths[i] / units_per_inch * 96.0;
1427                 if (wr[0] > wr[1])
1428                   wr[!i] = wr[i];
1429               }
1430         }
1431       else if (lex_match_id (lexer, "VLABELS"))
1432         {
1433           if (!lex_force_match_id (lexer, "VARIABLES"))
1434             goto error;
1435           lex_match (lexer, T_EQUALS);
1436
1437           struct variable **vars;
1438           size_t n_vars;
1439           if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
1440                                 PV_NO_SCRATCH))
1441             goto error;
1442
1443           if (!lex_force_match_id (lexer, "DISPLAY"))
1444             {
1445               free (vars);
1446               goto error;
1447             }
1448           lex_match (lexer, T_EQUALS);
1449
1450           enum ctables_vlabel vlabel;
1451           if (lex_match_id (lexer, "DEFAULT"))
1452             vlabel = CTVL_DEFAULT;
1453           else if (lex_match_id (lexer, "NAME"))
1454             vlabel = CTVL_NAME;
1455           else if (lex_match_id (lexer, "LABEL"))
1456             vlabel = CTVL_LABEL;
1457           else if (lex_match_id (lexer, "BOTH"))
1458             vlabel = CTVL_BOTH;
1459           else if (lex_match_id (lexer, "NONE"))
1460             vlabel = CTVL_NONE;
1461           else
1462             {
1463               lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
1464                                    "BOTH", "NONE");
1465               free (vars);
1466               goto error;
1467             }
1468
1469           for (size_t i = 0; i < n_vars; i++)
1470             ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
1471           free (vars);
1472         }
1473       else if (lex_match_id (lexer, "MRSETS"))
1474         {
1475           if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
1476             goto error;
1477           lex_match (lexer, T_EQUALS);
1478           if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
1479             goto error;
1480         }
1481       else if (lex_match_id (lexer, "SMISSING"))
1482         {
1483           if (lex_match_id (lexer, "VARIABLE"))
1484             ct->smissing_listwise = false;
1485           else if (lex_match_id (lexer, "LISTWISE"))
1486             ct->smissing_listwise = true;
1487           else
1488             {
1489               lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
1490               goto error;
1491             }
1492         }
1493       /* XXX PCOMPUTE */
1494       else if (lex_match_id (lexer, "WEIGHT"))
1495         {
1496           if (!lex_force_match_id (lexer, "VARIABLE"))
1497             goto error;
1498           lex_match (lexer, T_EQUALS);
1499           ct->base_weight = parse_variable (lexer, dataset_dict (ds));
1500           if (!ct->base_weight)
1501             goto error;
1502         }
1503       else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
1504         {
1505           if (!lex_force_match_id (lexer, "COUNT"))
1506             goto error;
1507           lex_match (lexer, T_EQUALS);
1508           if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
1509             goto error;
1510           ct->hide_threshold = lex_integer (lexer);
1511           lex_get (lexer);
1512         }
1513       else
1514         {
1515           lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
1516                                "SMISSING", "PCOMPUTE", "PPROPERTIES",
1517                                "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
1518           goto error;
1519         }
1520
1521       if (!lex_force_match (lexer, T_SLASH))
1522         goto error;
1523     }
1524
1525   size_t allocated_tables = 0;
1526   do
1527     {
1528       if (ct->n_tables >= allocated_tables)
1529         ct->tables = x2nrealloc (ct->tables, &allocated_tables,
1530                                  sizeof *ct->tables);
1531
1532       struct ctables_table *t = &ct->tables[ct->n_tables++];
1533       *t = (struct ctables_table) {
1534         .slabels_position = PIVOT_AXIS_COLUMN,
1535         .slabels_visible = true,
1536         .row_labels = CTLP_NORMAL,
1537         .col_labels = CTLP_NORMAL,
1538         .categories = xcalloc (dict_get_n_vars (dataset_dict (ds)),
1539                                sizeof *t->categories),
1540         .n_categories = dict_get_n_vars (dataset_dict (ds)),
1541         .cilevel = 95,
1542       };
1543
1544       lex_match (lexer, T_EQUALS);
1545       if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
1546         goto error;
1547       if (lex_match (lexer, T_BY))
1548         {
1549           if (!ctables_axis_parse (lexer, dataset_dict (ds),
1550                                    ct, t, PIVOT_AXIS_COLUMN))
1551             goto error;
1552
1553           if (lex_match (lexer, T_BY))
1554             {
1555               if (!ctables_axis_parse (lexer, dataset_dict (ds),
1556                                        ct, t, PIVOT_AXIS_LAYER))
1557                 goto error;
1558             }
1559         }
1560
1561       if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
1562           && !t->axes[PIVOT_AXIS_LAYER])
1563         {
1564           lex_error (lexer, _("At least one variable must be specified."));
1565           goto error;
1566         }
1567
1568       const struct ctables_axis *scales[PIVOT_N_AXES];
1569       size_t n_scales = 0;
1570       for (size_t i = 0; i < 3; i++)
1571         {
1572           scales[i] = find_scale (t->axes[i]);
1573           if (scales[i])
1574             n_scales++;
1575         }
1576       if (n_scales > 1)
1577         {
1578           msg (SE, _("Scale variables may appear only on one dimension."));
1579           if (scales[PIVOT_AXIS_ROW])
1580             msg_at (SN, scales[PIVOT_AXIS_ROW]->loc,
1581                     _("This scale variable appears in the rows dimension."));
1582           if (scales[PIVOT_AXIS_COLUMN])
1583             msg_at (SN, scales[PIVOT_AXIS_COLUMN]->loc,
1584                     _("This scale variable appears in the columns dimension."));
1585           if (scales[PIVOT_AXIS_LAYER])
1586             msg_at (SN, scales[PIVOT_AXIS_LAYER]->loc,
1587                     _("This scale variable appears in the layer dimension."));
1588           goto error;
1589         }
1590
1591       if (lex_token (lexer) == T_ENDCMD)
1592         break;
1593       if (!lex_force_match (lexer, T_SLASH))
1594         break;
1595
1596       while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
1597         {
1598           if (lex_match_id (lexer, "SLABELS"))
1599             {
1600               while (lex_token (lexer) != T_SLASH)
1601                 {
1602                   if (lex_match_id (lexer, "POSITION"))
1603                     {
1604                       lex_match (lexer, T_EQUALS);
1605                       if (lex_match_id (lexer, "COLUMN"))
1606                         t->slabels_position = PIVOT_AXIS_COLUMN;
1607                       else if (lex_match_id (lexer, "ROW"))
1608                         t->slabels_position = PIVOT_AXIS_ROW;
1609                       else if (lex_match_id (lexer, "LAYER"))
1610                         t->slabels_position = PIVOT_AXIS_LAYER;
1611                       else
1612                         {
1613                           lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
1614                           goto error;
1615                         }
1616                     }
1617                   else if (lex_match_id (lexer, "VISIBLE"))
1618                     {
1619                       lex_match (lexer, T_EQUALS);
1620                       if (!parse_bool (lexer, &t->slabels_visible))
1621                         goto error;
1622                     }
1623                   else
1624                     {
1625                       lex_error_expecting (lexer, "POSITION", "VISIBLE");
1626                       goto error;
1627                     }
1628                 }
1629             }
1630           else if (lex_match_id (lexer, "CLABELS"))
1631             {
1632               while (lex_token (lexer) != T_SLASH)
1633                 {
1634                   if (lex_match_id (lexer, "AUTO"))
1635                     t->row_labels = t->col_labels = CTLP_NORMAL;
1636                   else if (lex_match_id (lexer, "ROWLABELS"))
1637                     {
1638                       lex_match (lexer, T_EQUALS);
1639                       if (lex_match_id (lexer, "OPPOSITE"))
1640                         t->row_labels = CTLP_OPPOSITE;
1641                       else if (lex_match_id (lexer, "LAYER"))
1642                         t->row_labels = CTLP_LAYER;
1643                       else
1644                         {
1645                           lex_error_expecting (lexer, "OPPOSITE", "LAYER");
1646                           goto error;
1647                         }
1648                     }
1649                   else if (lex_match_id (lexer, "COLLABELS"))
1650                     {
1651                       lex_match (lexer, T_EQUALS);
1652                       if (lex_match_id (lexer, "OPPOSITE"))
1653                         t->col_labels = CTLP_OPPOSITE;
1654                       else if (lex_match_id (lexer, "LAYER"))
1655                         t->col_labels = CTLP_LAYER;
1656                       else
1657                         {
1658                           lex_error_expecting (lexer, "OPPOSITE", "LAYER");
1659                           goto error;
1660                         }
1661                     }
1662                   else
1663                     {
1664                       lex_error_expecting (lexer, "AUTO", "ROWLABELS",
1665                                            "COLLABELS");
1666                       goto error;
1667                     }
1668                 }
1669             }
1670           else if (lex_match_id (lexer, "CRITERIA"))
1671             {
1672               if (!lex_force_match_id (lexer, "CILEVEL"))
1673                 goto error;
1674               lex_match (lexer, T_EQUALS);
1675
1676               if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
1677                 goto error;
1678               t->cilevel = lex_number (lexer);
1679               lex_get (lexer);
1680             }
1681           else if (lex_match_id (lexer, "CATEGORIES"))
1682             {
1683               if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
1684                 goto error;
1685             }
1686           else if (lex_match_id (lexer, "TITLES"))
1687             {
1688               do
1689                 {
1690                   char **textp;
1691                   if (lex_match_id (lexer, "CAPTION"))
1692                     textp = &t->caption;
1693                   else if (lex_match_id (lexer, "CORNER"))
1694                     textp = &t->corner;
1695                   else if (lex_match_id (lexer, "TITLE"))
1696                     textp = &t->title;
1697                   else
1698                     {
1699                       lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
1700                       goto error;
1701                     }
1702                   lex_match (lexer, T_EQUALS);
1703
1704                   struct string s = DS_EMPTY_INITIALIZER;
1705                   while (lex_is_string (lexer))
1706                     {
1707                       if (!ds_is_empty (&s))
1708                         ds_put_byte (&s, ' ');
1709                       ds_put_substring (&s, lex_tokss (lexer));
1710                       lex_get (lexer);
1711                     }
1712                   free (*textp);
1713                   *textp = ds_steal_cstr (&s);
1714                 }
1715               while (lex_token (lexer) != T_SLASH
1716                      && lex_token (lexer) != T_ENDCMD);
1717             }
1718           else if (lex_match_id (lexer, "SIGTEST"))
1719             {
1720               if (!t->chisq)
1721                 {
1722                   t->chisq = xmalloc (sizeof *t->chisq);
1723                   *t->chisq = (struct ctables_chisq) {
1724                     .alpha = .05,
1725                     .include_mrsets = true,
1726                     .all_visible = true,
1727                   };
1728                 }
1729
1730               do
1731                 {
1732                   if (lex_match_id (lexer, "TYPE"))
1733                     {
1734                       lex_match (lexer, T_EQUALS);
1735                       if (!lex_force_match_id (lexer, "CHISQUARE"))
1736                         goto error;
1737                     }
1738                   else if (lex_match_id (lexer, "ALPHA"))
1739                     {
1740                       lex_match (lexer, T_EQUALS);
1741                       if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
1742                         goto error;
1743                       t->chisq->alpha = lex_number (lexer);
1744                       lex_get (lexer);
1745                     }
1746                   else if (lex_match_id (lexer, "INCLUDEMRSETS"))
1747                     {
1748                       lex_match (lexer, T_EQUALS);
1749                       if (parse_bool (lexer, &t->chisq->include_mrsets))
1750                         goto error;
1751                     }
1752                   else if (lex_match_id (lexer, "CATEGORIES"))
1753                     {
1754                       lex_match (lexer, T_EQUALS);
1755                       if (lex_match_id (lexer, "ALLVISIBLE"))
1756                         t->chisq->all_visible = true;
1757                       else if (lex_match_id (lexer, "SUBTOTALS"))
1758                         t->chisq->all_visible = false;
1759                       else
1760                         {
1761                           lex_error_expecting (lexer,
1762                                                "ALLVISIBLE", "SUBTOTALS");
1763                           goto error;
1764                         }
1765                     }
1766                   else
1767                     {
1768                       lex_error_expecting (lexer, "TYPE", "ALPHA",
1769                                            "INCLUDEMRSETS", "CATEGORIES");
1770                       goto error;
1771                     }
1772                 }
1773               while (lex_token (lexer) != T_SLASH
1774                      && lex_token (lexer) != T_ENDCMD);
1775             }
1776           else if (lex_match_id (lexer, "COMPARETEST"))
1777             {
1778               if (!t->pairwise)
1779                 {
1780                   t->pairwise = xmalloc (sizeof *t->pairwise);
1781                   *t->pairwise = (struct ctables_pairwise) {
1782                     .type = PROP,
1783                     .alpha = { .05, .05 },
1784                     .adjust = BONFERRONI,
1785                     .include_mrsets = true,
1786                     .meansvariance_allcats = true,
1787                     .all_visible = true,
1788                     .merge = false,
1789                     .apa_style = true,
1790                     .show_sig = false,
1791                   };
1792                 }
1793
1794               do
1795                 {
1796                   if (lex_match_id (lexer, "TYPE"))
1797                     {
1798                       lex_match (lexer, T_EQUALS);
1799                       if (lex_match_id (lexer, "PROP"))
1800                         t->pairwise->type = PROP;
1801                       else if (lex_match_id (lexer, "MEAN"))
1802                         t->pairwise->type = MEAN;
1803                       else
1804                         {
1805                           lex_error_expecting (lexer, "PROP", "MEAN");
1806                           goto error;
1807                         }
1808                     }
1809                   else if (lex_match_id (lexer, "ALPHA"))
1810                     {
1811                       lex_match (lexer, T_EQUALS);
1812
1813                       if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
1814                         goto error;
1815                       double a0 = lex_number (lexer);
1816                       lex_get (lexer);
1817
1818                       lex_match (lexer, T_COMMA);
1819                       if (lex_is_number (lexer))
1820                         {
1821                           if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
1822                             goto error;
1823                           double a1 = lex_number (lexer);
1824                           lex_get (lexer);
1825
1826                           t->pairwise->alpha[0] = MIN (a0, a1);
1827                           t->pairwise->alpha[1] = MAX (a0, a1);
1828                         }
1829                       else
1830                         t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
1831                     }
1832                   else if (lex_match_id (lexer, "ADJUST"))
1833                     {
1834                       lex_match (lexer, T_EQUALS);
1835                       if (lex_match_id (lexer, "BONFERRONI"))
1836                         t->pairwise->adjust = BONFERRONI;
1837                       else if (lex_match_id (lexer, "BH"))
1838                         t->pairwise->adjust = BH;
1839                       else if (lex_match_id (lexer, "NONE"))
1840                         t->pairwise->adjust = 0;
1841                       else
1842                         {
1843                           lex_error_expecting (lexer, "BONFERRONI", "BH",
1844                                                "NONE");
1845                           goto error;
1846                         }
1847                     }
1848                   else if (lex_match_id (lexer, "INCLUDEMRSETS"))
1849                     {
1850                       lex_match (lexer, T_EQUALS);
1851                       if (!parse_bool (lexer, &t->pairwise->include_mrsets))
1852                         goto error;
1853                     }
1854                   else if (lex_match_id (lexer, "MEANSVARIANCE"))
1855                     {
1856                       lex_match (lexer, T_EQUALS);
1857                       if (lex_match_id (lexer, "ALLCATS"))
1858                         t->pairwise->meansvariance_allcats = true;
1859                       else if (lex_match_id (lexer, "TESTEDCATS"))
1860                         t->pairwise->meansvariance_allcats = false;
1861                       else
1862                         {
1863                           lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
1864                           goto error;
1865                         }
1866                     }
1867                   else if (lex_match_id (lexer, "CATEGORIES"))
1868                     {
1869                       lex_match (lexer, T_EQUALS);
1870                       if (lex_match_id (lexer, "ALLVISIBLE"))
1871                         t->pairwise->all_visible = true;
1872                       else if (lex_match_id (lexer, "SUBTOTALS"))
1873                         t->pairwise->all_visible = false;
1874                       else
1875                         {
1876                           lex_error_expecting (lexer, "ALLVISIBLE",
1877                                                "SUBTOTALS");
1878                           goto error;
1879                         }
1880                     }
1881                   else if (lex_match_id (lexer, "MERGE"))
1882                     {
1883                       lex_match (lexer, T_EQUALS);
1884                       if (!parse_bool (lexer, &t->pairwise->merge))
1885                         goto error;
1886                     }
1887                   else if (lex_match_id (lexer, "STYLE"))
1888                     {
1889                       lex_match (lexer, T_EQUALS);
1890                       if (lex_match_id (lexer, "APA"))
1891                         t->pairwise->apa_style = true;
1892                       else if (lex_match_id (lexer, "SIMPLE"))
1893                         t->pairwise->apa_style = false;
1894                       else
1895                         {
1896                           lex_error_expecting (lexer, "APA", "SIMPLE");
1897                           goto error;
1898                         }
1899                     }
1900                   else if (lex_match_id (lexer, "SHOWSIG"))
1901                     {
1902                       lex_match (lexer, T_EQUALS);
1903                       if (!parse_bool (lexer, &t->pairwise->show_sig))
1904                         goto error;
1905                     }
1906                   else
1907                     {
1908                       lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
1909                                            "INCLUDEMRSETS", "MEANSVARIANCE",
1910                                            "CATEGORIES", "MERGE", "STYLE",
1911                                            "SHOWSIG");
1912                       goto error;
1913                     }
1914                 }
1915               while (lex_token (lexer) != T_SLASH
1916                      && lex_token (lexer) != T_ENDCMD);
1917             }
1918           else
1919             {
1920               lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
1921                                    "CRITERIA", "CATEGORIES", "TITLES",
1922                                    "SIGTEST", "COMPARETEST");
1923               goto error;
1924             }
1925         }
1926
1927       if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
1928         {
1929           msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
1930           goto error;
1931         }
1932
1933     }
1934   while (lex_token (lexer) != T_ENDCMD);
1935
1936   ctables_execute (ct);
1937   ctables_destroy (ct);
1938   return CMD_SUCCESS;
1939
1940 error:
1941   ctables_destroy (ct);
1942   return CMD_FAILURE;
1943 }
1944