1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2021 Free Software Foundation, Inc.
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.
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.
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/>. */
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"
32 #include "gl/minmax.h"
33 #include "gl/xalloc.h"
36 #define _(msgid) gettext (msgid)
37 #define N_(msgid) (msgid)
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,
48 ctables_vlabel_unique (enum ctables_vlabel vlabel)
50 /* This ensures that all of the values are unique. */
63 - unweighted summaries (U*)
64 - lower confidence limits (*.LCL)
65 - upper confidence limits (*.UCL)
66 - standard error (*.SE)
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) \
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) \
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)
143 enum ctables_summary_function
145 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) ENUM,
151 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) +1
152 N_CTSF_FUNCTIONS = SUMMARIES
158 struct pivot_table_look *look;
160 /* If this is NULL, zeros are displayed using the normal print format.
161 Otherwise, this string is displayed. */
164 /* If this is NULL, missing values are displayed using the normal print
165 format. Otherwise, this string is displayed. */
168 /* Indexed by variable dictionary index. */
169 enum ctables_vlabel *vlabels;
171 bool mrsets_count_duplicates; /* MRSETS. */
172 bool smissing_listwise; /* SMISSING. */
173 struct variable *base_weight; /* WEIGHT. */
174 int hide_threshold; /* HIDESMALLCOUNTS. */
176 struct ctables_table *tables;
180 struct ctables_postcompute
182 struct hmap_node hmap_node; /* In struct ctables's 'pcompute' hmap. */
183 const char *name; /* Name, without leading &. */
185 struct ctables_postcompute_expr *expr;
188 bool hide_source_cats;
191 struct ctables_postcompute_expr
193 enum ctables_postcompute_op
201 /* XXX SUBTOTAL and HSUBTOTAL */
214 /* CTPO_CAT_NUMBER, CTPO_NUMBER. */
219 XXX what about string ranges? */
222 /* CTPO_ADD, CTPO_SUB, CTPO_MUL, CTPO_DIV, CTPO_POW. */
223 struct ctables_postcompute_expr *subs[2];
227 enum ctables_label_position
236 struct ctables_axis *axes[PIVOT_N_AXES];
238 enum pivot_axis_type slabels_position;
239 bool slabels_visible;
241 enum ctables_label_position row_labels;
242 enum ctables_label_position col_labels;
244 /* Indexed by variable dictionary index. */
245 struct ctables_categories **categories;
254 struct ctables_chisq *chisq;
255 struct ctables_pairwise *pairwise;
263 struct variable *var;
264 const struct mrset *mrset;
268 static const struct fmt_spec *
269 ctables_var_get_print_format (const struct ctables_var *var)
271 return (var->is_mrset
272 ? var_get_print_format (var->mrset->vars[0])
273 : var_get_print_format (var->var));
277 ctables_var_name (const struct ctables_var *var)
279 return var->is_mrset ? var->mrset->name : var_get_name (var->var);
282 struct ctables_categories
286 /* Explicit categories. */
287 struct ctables_cat_value *values;
290 /* Implicit categories. */
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;
303 /* Empty categories. */
307 struct ctables_cat_value
309 enum ctables_cat_value_type
323 double number; /* CCVT_NUMBER. */
324 char *string; /* CCVT_STRING. */
325 double range[2]; /* CCVT_RANGE. */
326 char *subtotal_label; /* CCVT_SUBTOTAL, CCVT_HSUBTOTAL. */
331 ctables_cat_value_uninit (struct ctables_cat_value *cv)
350 free (cv->subtotal_label);
355 ctables_categories_unref (struct ctables_categories *c)
360 assert (c->n_refs > 0);
364 for (size_t i = 0; i < c->n_values; i++)
365 ctables_cat_value_uninit (&c->values[i]);
367 free (c->total_label);
371 /* Chi-square test (SIGTEST). */
379 /* Pairwise comparison test (COMPARETEST). */
380 struct ctables_pairwise
382 enum { PROP, MEAN } type;
385 bool meansvariance_allcats;
387 enum { BONFERRONI = 1, BH } adjust;
411 struct ctables_var var;
413 struct ctables_summary *summaries;
415 size_t allocated_summaries;
419 struct ctables_axis *subs[2];
422 struct msg_location *loc;
425 static void ctables_axis_destroy (struct ctables_axis *);
434 enum ctables_function_availability
436 CTFA_ALL, /* Any variables. */
437 CTFA_SCALE, /* Only scale variables, totals, and subtotals. */
438 CTFA_MRSETS, /* Only multiple-response sets */
441 struct ctables_summary
443 enum ctables_summary_function function;
444 double percentile; /* CTSF_PTILE only. */
446 struct fmt_spec format; /* XXX extra CTABLES formats */
450 ctables_summary_uninit (struct ctables_summary *s)
457 parse_col_width (struct lexer *lexer, const char *name, double *width)
459 lex_match (lexer, T_EQUALS);
460 if (lex_match_id (lexer, "DEFAULT"))
462 else if (lex_force_num_range_closed (lexer, name, 0, DBL_MAX))
464 *width = lex_number (lexer);
474 parse_bool (struct lexer *lexer, bool *b)
476 if (lex_match_id (lexer, "NO"))
478 else if (lex_match_id (lexer, "YES"))
482 lex_error_expecting (lexer, "YES", "NO");
488 static enum ctables_function_availability
489 ctables_function_availability (enum ctables_summary_function f)
491 static enum ctables_function_availability availability[] = {
492 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = AVAILABILITY,
497 return availability[f];
501 parse_ctables_summary_function (struct lexer *lexer,
502 enum ctables_summary_function *f)
506 enum ctables_summary_function function;
507 struct substring name;
509 static struct pair names[] = {
510 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) \
511 { ENUM, SS_LITERAL_INITIALIZER (NAME) },
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", _, _, _)
525 if (!lex_force_id (lexer))
528 for (size_t i = 0; i < sizeof names / sizeof *names; i++)
529 if (ss_equals_case (names[i].name, lex_tokss (lexer)))
531 *f = names[i].function;
536 lex_error (lexer, _("Expecting summary function name."));
541 ctables_axis_destroy (struct ctables_axis *axis)
549 for (size_t i = 0; i < axis->n_summaries; i++)
550 ctables_summary_uninit (&axis->summaries[i]);
551 free (axis->summaries);
556 ctables_axis_destroy (axis->subs[0]);
557 ctables_axis_destroy (axis->subs[1]);
560 msg_location_destroy (axis->loc);
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)
570 struct ctables_axis *axis = xmalloc (sizeof *axis);
571 *axis = (struct ctables_axis) {
573 .subs = { sub0, sub1 },
574 .loc = lex_ofs_location (lexer, start_ofs, lex_ofs (lexer) - 1),
579 struct ctables_axis_parse_ctx
582 struct dictionary *dict;
584 struct ctables_table *t;
587 static struct fmt_spec
588 ctables_summary_default_format (enum ctables_summary_function function,
589 const struct ctables_var *var)
591 static const enum ctables_format default_formats[] = {
592 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = FORMAT,
596 switch (default_formats[function])
599 return (struct fmt_spec) { .type = FMT_F, .w = 40 };
602 return (struct fmt_spec) { .type = FMT_PCT, .w = 40, .d = 1 };
605 return *ctables_var_get_print_format (var);
613 ctables_summary_function_name (enum ctables_summary_function function)
615 static const char *names[] = {
616 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = NAME,
620 return names[function];
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)
629 if (axis->op == CTAO_VAR)
631 if (axis->n_summaries >= axis->allocated_summaries)
632 axis->summaries = x2nrealloc (axis->summaries,
633 &axis->allocated_summaries,
634 sizeof *axis->summaries);
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))
641 if (!axis->var.is_mrset)
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."),
655 _("Summary function %s applies only to scale variables."),
657 msg_at (SN, axis->loc, _("'%s' is not a scale variable."),
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)),
679 for (size_t i = 0; i < 2; i++)
680 if (!add_summary (axis->subs[i], function, percentile, label, format,
687 static struct ctables_axis *ctables_axis_parse_stack (
688 struct ctables_axis_parse_ctx *);
691 ctables_var_parse (struct lexer *lexer, struct dictionary *dict,
692 struct ctables_var *var)
694 if (ss_starts_with (lex_tokss (lexer), ss_cstr ("$")))
696 *var = (struct ctables_var) {
698 .mrset = dict_lookup_mrset (dict, lex_tokcstr (lexer))
702 lex_error (lexer, _("'%s' does not name a multiple-response set "
703 "in the active file dictionary."),
704 lex_tokcstr (lexer));
712 *var = (struct ctables_var) {
714 .var = parse_variable (lexer, dict),
716 return var->var != NULL;
720 static struct ctables_axis *
721 ctables_axis_parse_primary (struct ctables_axis_parse_ctx *ctx)
723 if (lex_match (ctx->lexer, T_LPAREN))
725 struct ctables_axis *sub = ctables_axis_parse_stack (ctx);
726 if (!sub || !lex_force_match (ctx->lexer, T_RPAREN))
728 ctables_axis_destroy (sub);
734 if (!lex_force_id (ctx->lexer))
737 int start_ofs = lex_ofs (ctx->lexer);
738 struct ctables_var var;
739 if (!ctables_var_parse (ctx->lexer, ctx->dict, &var))
742 struct ctables_axis *axis = xmalloc (sizeof *axis);
743 *axis = (struct ctables_axis) { .op = CTAO_VAR, .var = var };
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);
755 static struct ctables_axis *
756 ctables_axis_parse_postfix (struct ctables_axis_parse_ctx *ctx)
758 struct ctables_axis *sub = ctables_axis_parse_primary (ctx);
759 if (!sub || !lex_match (ctx->lexer, T_LBRACK))
764 int start_ofs = lex_ofs (ctx->lexer);
766 /* Parse function. */
767 enum ctables_summary_function function;
768 if (!parse_ctables_summary_function (ctx->lexer, &function))
771 /* Parse percentile. */
772 double percentile = 0;
773 if (function == CTSF_PTILE)
775 if (!lex_force_num_range_closed (ctx->lexer, "PTILE", 0, 100))
777 percentile = lex_number (ctx->lexer);
778 lex_get (ctx->lexer);
783 if (lex_is_string (ctx->lexer))
785 label = ss_xstrdup (lex_tokss (ctx->lexer));
786 lex_get (ctx->lexer);
788 else if (function == CTSF_PTILE)
789 label = xasprintf (_("Percentile %.2f"), percentile);
792 static const char *default_labels[] = {
793 #define S(ENUM, NAME, LABEL, FORMAT, AVAILABILITY) [ENUM] = LABEL,
797 label = xstrdup (gettext (default_labels[function]));
801 struct fmt_spec format;
802 const struct fmt_spec *formatp;
803 if (lex_token (ctx->lexer) == T_ID)
805 if (!parse_format_specifier (ctx->lexer, &format)
806 || !fmt_check_output (&format)
807 || !fmt_check_type_compat (&format, VAL_NUMERIC))
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);
821 msg_location_destroy (loc);
823 lex_match (ctx->lexer, T_COMMA);
825 while (!lex_match (ctx->lexer, T_RBRACK));
830 ctables_axis_destroy (sub);
834 static const struct ctables_axis *
835 find_scale (const struct ctables_axis *axis)
839 else if (axis->op == CTAO_VAR)
843 assert (!axis->var.is_mrset);
851 for (size_t i = 0; i < 2; i++)
853 const struct ctables_axis *scale = find_scale (axis->subs[i]);
861 static const struct ctables_axis *
862 find_categorical_summary (const struct ctables_axis *axis)
866 else if (axis->op == CTAO_VAR)
867 return !axis->scale && axis->n_summaries ? axis : NULL;
870 for (size_t i = 0; i < 2; i++)
872 const struct ctables_axis *sum
873 = find_categorical_summary (axis->subs[i]);
881 static struct ctables_axis *
882 ctables_axis_parse_nest (struct ctables_axis_parse_ctx *ctx)
884 int start_ofs = lex_ofs (ctx->lexer);
885 struct ctables_axis *lhs = ctables_axis_parse_postfix (ctx);
889 while (lex_match (ctx->lexer, T_GT))
891 struct ctables_axis *rhs = ctables_axis_parse_postfix (ctx);
895 struct ctables_axis *nest = ctables_axis_new_nonterminal (
896 CTAO_NEST, lhs, rhs, ctx->lexer, start_ofs);
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)
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);
909 const struct ctables_axis *outer_sum = find_categorical_summary (lhs);
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);
927 static struct ctables_axis *
928 ctables_axis_parse_stack (struct ctables_axis_parse_ctx *ctx)
930 int start_ofs = lex_ofs (ctx->lexer);
931 struct ctables_axis *lhs = ctables_axis_parse_nest (ctx);
935 while (lex_match (ctx->lexer, T_PLUS))
937 struct ctables_axis *rhs = ctables_axis_parse_nest (ctx);
941 lhs = ctables_axis_new_nonterminal (CTAO_STACK, lhs, rhs,
942 ctx->lexer, start_ofs);
949 ctables_axis_parse (struct lexer *lexer, struct dictionary *dict,
950 struct ctables *ct, struct ctables_table *t,
951 enum pivot_axis_type a)
953 if (lex_token (lexer) == T_BY
954 || lex_token (lexer) == T_SLASH
955 || lex_token (lexer) == T_ENDCMD)
958 struct ctables_axis_parse_ctx ctx = {
964 t->axes[a] = ctables_axis_parse_stack (&ctx);
965 return t->axes[a] != NULL;
969 ctables_chisq_destroy (struct ctables_chisq *chisq)
975 ctables_pairwise_destroy (struct ctables_pairwise *pairwise)
981 ctables_table_uninit (struct ctables_table *t)
986 for (size_t i = 0; i < t->n_categories; i++)
987 ctables_categories_unref (t->categories[i]);
988 free (t->categories);
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]);
996 ctables_chisq_destroy (t->chisq);
997 ctables_pairwise_destroy (t->pairwise);
1001 ctables_destroy (struct ctables *ct)
1006 pivot_table_look_unref (ct->look);
1010 for (size_t i = 0; i < ct->n_tables; i++)
1011 ctables_table_uninit (&ct->tables[i]);
1016 static struct ctables_cat_value
1017 ccvt_range (double low, double high)
1019 return (struct ctables_cat_value) {
1021 .range = { low, high }
1026 ctables_table_parse_categories (struct lexer *lexer, struct dictionary *dict,
1027 struct ctables_table *t)
1029 if (!lex_match_id (lexer, "VARIABLES"))
1031 lex_match (lexer, T_EQUALS);
1033 struct variable **vars;
1035 if (!parse_variables (lexer, dict, &vars, &n_vars, PV_NO_SCRATCH))
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++)
1042 struct ctables_categories **cp
1043 = &t->categories[var_get_dict_index (vars[i])];
1044 ctables_categories_unref (*cp);
1049 if (lex_match (lexer, T_LBRACK))
1051 size_t allocated_values = 0;
1054 if (c->n_values >= allocated_values)
1055 c->values = x2nrealloc (c->values, &allocated_values,
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"))
1071 if (!lex_force_match_id (lexer, "THRU") || lex_force_num (lexer))
1073 *v = ccvt_range (-DBL_MAX, lex_number (lexer));
1076 else if (lex_is_number (lexer))
1078 double number = lex_number (lexer);
1080 if (lex_match_id (lexer, "THRU"))
1082 v->type = CCVT_RANGE;
1083 v->range[0] = number;
1084 if (lex_match_id (lexer, "HI"))
1085 *v = ccvt_range (number, DBL_MAX);
1088 if (!lex_force_num (lexer))
1090 *v = ccvt_range (number, lex_number (lexer));
1095 *v = (struct ctables_cat_value) {
1096 .type = CCVT_NUMBER,
1100 else if (lex_is_string (lexer))
1102 *v = (struct ctables_cat_value) {
1103 .type = CCVT_STRING,
1104 .string = ss_xstrdup (lex_tokss (lexer)),
1110 lex_error (lexer, NULL);
1114 if ((v->type == CCVT_SUBTOTAL || v->type == CCVT_HSUBTOTAL)
1115 && lex_match (lexer, T_EQUALS))
1117 if (!lex_force_string (lexer))
1120 v->subtotal_label = ss_xstrdup (lex_tokss (lexer));
1125 lex_match (lexer, T_COMMA);
1127 while (!lex_match (lexer, T_RBRACK));
1130 while (lex_token (lexer) != T_SLASH && lex_token (lexer) != T_ENDCMD)
1132 if (!c->n_values && lex_match_id (lexer, "ORDER"))
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;
1141 lex_error_expecting (lexer, "A", "D");
1145 else if (!c->n_values && lex_match_id (lexer, "KEY"))
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;
1154 c->key = CTCS_FUNCTION;
1155 if (!parse_ctables_summary_function (lexer, &c->sort_func))
1158 if (lex_match (lexer, T_LPAREN))
1160 c->sort_func_var = parse_variable (lexer, dict);
1161 if (!c->sort_func_var)
1164 if (c->sort_func == CTSF_PTILE)
1166 lex_match (lexer, T_COMMA);
1167 if (!lex_force_num_range_closed (lexer, "PTILE", 0, 100))
1169 c->percentile = lex_number (lexer);
1173 if (!lex_force_match (lexer, T_RPAREN))
1176 else if (ctables_function_availability (c->sort_func)
1179 bool UNUSED b = lex_force_match (lexer, T_LPAREN);
1184 else if (!c->n_values && lex_match_id (lexer, "MISSING"))
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;
1193 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1197 else if (lex_match_id (lexer, "TOTAL"))
1199 lex_match (lexer, T_EQUALS);
1200 if (!parse_bool (lexer, &c->show_totals))
1203 else if (lex_match_id (lexer, "LABEL"))
1205 lex_match (lexer, T_EQUALS);
1206 if (!lex_force_string (lexer))
1208 free (c->total_label);
1209 c->total_label = ss_xstrdup (lex_tokss (lexer));
1212 else if (lex_match_id (lexer, "POSITION"))
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;
1221 lex_error_expecting (lexer, "BEFORE", "AFTER");
1225 else if (lex_match_id (lexer, "EMPTY"))
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;
1234 lex_error_expecting (lexer, "INCLUDE", "EXCLUDE");
1241 lex_error_expecting (lexer, "ORDER", "KEY", "MISSING",
1242 "TOTAL", "LABEL", "POSITION", "EMPTY");
1244 lex_error_expecting (lexer, "TOTAL", "LABEL", "POSITION", "EMPTY");
1251 struct ctables_freqtab
1253 struct variable **vars;
1256 struct hmap data; /* Contains "struct freq"s. */
1259 static struct string_array
1260 nest_fts (struct string_array sa0, struct string_array sa1)
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",
1273 string_array_destroy (&sa0);
1274 string_array_destroy (&sa1);
1278 static struct string_array
1279 enumerate_fts (const struct ctables_axis *a)
1281 struct string_array sa = STRING_ARRAY_INITIALIZER;
1288 string_array_append (&sa, ctables_var_name (&a->var));
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]);
1300 return nest_fts (enumerate_fts (a->subs[0]),
1301 enumerate_fts (a->subs[1]));
1307 ctables_execute (struct ctables *ct)
1309 for (size_t i = 0; i < ct->n_tables; i++)
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);
1323 cmd_ctables (struct lexer *lexer, struct dataset *ds)
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;
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 ())),
1335 .hide_threshold = 5,
1338 if (!lex_force_match (lexer, T_SLASH))
1341 while (!lex_match_id (lexer, "TABLE"))
1343 if (lex_match_id (lexer, "FORMAT"))
1345 double widths[2] = { SYSMIS, SYSMIS };
1346 double units_per_inch = 72.0;
1348 while (lex_token (lexer) != T_SLASH)
1350 if (lex_match_id (lexer, "MINCOLWIDTH"))
1352 if (!parse_col_width (lexer, "MINCOLWIDTH", &widths[0]))
1355 else if (lex_match_id (lexer, "MAXCOLWIDTH"))
1357 if (!parse_col_width (lexer, "MAXCOLWIDTH", &widths[1]))
1360 else if (lex_match_id (lexer, "UNITS"))
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;
1371 lex_error_expecting (lexer, "POINTS", "INCHES", "CM");
1375 else if (lex_match_id (lexer, "EMPTY"))
1380 lex_match (lexer, T_EQUALS);
1381 if (lex_match_id (lexer, "ZERO"))
1383 /* Nothing to do. */
1385 else if (lex_match_id (lexer, "BLANK"))
1386 ct->zero = xstrdup ("");
1387 else if (lex_force_string (lexer))
1389 ct->zero = ss_xstrdup (lex_tokss (lexer));
1395 else if (lex_match_id (lexer, "MISSING"))
1397 lex_match (lexer, T_EQUALS);
1398 if (!lex_force_string (lexer))
1402 ct->missing = (strcmp (lex_tokcstr (lexer), ".")
1403 ? ss_xstrdup (lex_tokss (lexer))
1409 lex_error_expecting (lexer, "MINCOLWIDTH", "MAXCOLWIDTH",
1410 "UNITS", "EMPTY", "MISSING");
1415 if (widths[0] != SYSMIS && widths[1] != SYSMIS
1416 && widths[0] > widths[1])
1418 msg (SE, _("MINCOLWIDTH must not be greater than MAXCOLWIDTH."));
1422 for (size_t i = 0; i < 2; i++)
1423 if (widths[i] != SYSMIS)
1425 int *wr = ct->look->width_ranges[TABLE_HORZ];
1426 wr[i] = widths[i] / units_per_inch * 96.0;
1431 else if (lex_match_id (lexer, "VLABELS"))
1433 if (!lex_force_match_id (lexer, "VARIABLES"))
1435 lex_match (lexer, T_EQUALS);
1437 struct variable **vars;
1439 if (!parse_variables (lexer, dataset_dict (ds), &vars, &n_vars,
1443 if (!lex_force_match_id (lexer, "DISPLAY"))
1448 lex_match (lexer, T_EQUALS);
1450 enum ctables_vlabel vlabel;
1451 if (lex_match_id (lexer, "DEFAULT"))
1452 vlabel = CTVL_DEFAULT;
1453 else if (lex_match_id (lexer, "NAME"))
1455 else if (lex_match_id (lexer, "LABEL"))
1456 vlabel = CTVL_LABEL;
1457 else if (lex_match_id (lexer, "BOTH"))
1459 else if (lex_match_id (lexer, "NONE"))
1463 lex_error_expecting (lexer, "DEFAULT", "NAME", "LABEL",
1469 for (size_t i = 0; i < n_vars; i++)
1470 ct->vlabels[var_get_dict_index (vars[i])] = vlabel;
1473 else if (lex_match_id (lexer, "MRSETS"))
1475 if (!lex_force_match_id (lexer, "COUNTDUPLICATES"))
1477 lex_match (lexer, T_EQUALS);
1478 if (!parse_bool (lexer, &ct->mrsets_count_duplicates))
1481 else if (lex_match_id (lexer, "SMISSING"))
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;
1489 lex_error_expecting (lexer, "VARIABLE", "LISTWISE");
1494 else if (lex_match_id (lexer, "WEIGHT"))
1496 if (!lex_force_match_id (lexer, "VARIABLE"))
1498 lex_match (lexer, T_EQUALS);
1499 ct->base_weight = parse_variable (lexer, dataset_dict (ds));
1500 if (!ct->base_weight)
1503 else if (lex_match_id (lexer, "HIDESMALLCOUNTS"))
1505 if (!lex_force_match_id (lexer, "COUNT"))
1507 lex_match (lexer, T_EQUALS);
1508 if (!lex_force_int_range (lexer, "HIDESMALLCOUNTS COUNT", 2, INT_MAX))
1510 ct->hide_threshold = lex_integer (lexer);
1515 lex_error_expecting (lexer, "FORMAT", "VLABELS", "MRSETS",
1516 "SMISSING", "PCOMPUTE", "PPROPERTIES",
1517 "WEIGHT", "HIDESMALLCOUNTS", "TABLE");
1521 if (!lex_force_match (lexer, T_SLASH))
1525 size_t allocated_tables = 0;
1528 if (ct->n_tables >= allocated_tables)
1529 ct->tables = x2nrealloc (ct->tables, &allocated_tables,
1530 sizeof *ct->tables);
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)),
1544 lex_match (lexer, T_EQUALS);
1545 if (!ctables_axis_parse (lexer, dataset_dict (ds), ct, t, PIVOT_AXIS_ROW))
1547 if (lex_match (lexer, T_BY))
1549 if (!ctables_axis_parse (lexer, dataset_dict (ds),
1550 ct, t, PIVOT_AXIS_COLUMN))
1553 if (lex_match (lexer, T_BY))
1555 if (!ctables_axis_parse (lexer, dataset_dict (ds),
1556 ct, t, PIVOT_AXIS_LAYER))
1561 if (!t->axes[PIVOT_AXIS_ROW] && !t->axes[PIVOT_AXIS_COLUMN]
1562 && !t->axes[PIVOT_AXIS_LAYER])
1564 lex_error (lexer, _("At least one variable must be specified."));
1568 const struct ctables_axis *scales[PIVOT_N_AXES];
1569 size_t n_scales = 0;
1570 for (size_t i = 0; i < 3; i++)
1572 scales[i] = find_scale (t->axes[i]);
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."));
1591 if (lex_token (lexer) == T_ENDCMD)
1593 if (!lex_force_match (lexer, T_SLASH))
1596 while (!lex_match_id (lexer, "TABLE") && lex_token (lexer) != T_ENDCMD)
1598 if (lex_match_id (lexer, "SLABELS"))
1600 while (lex_token (lexer) != T_SLASH)
1602 if (lex_match_id (lexer, "POSITION"))
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;
1613 lex_error_expecting (lexer, "COLUMN", "ROW", "LAYER");
1617 else if (lex_match_id (lexer, "VISIBLE"))
1619 lex_match (lexer, T_EQUALS);
1620 if (!parse_bool (lexer, &t->slabels_visible))
1625 lex_error_expecting (lexer, "POSITION", "VISIBLE");
1630 else if (lex_match_id (lexer, "CLABELS"))
1632 while (lex_token (lexer) != T_SLASH)
1634 if (lex_match_id (lexer, "AUTO"))
1635 t->row_labels = t->col_labels = CTLP_NORMAL;
1636 else if (lex_match_id (lexer, "ROWLABELS"))
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;
1645 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
1649 else if (lex_match_id (lexer, "COLLABELS"))
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;
1658 lex_error_expecting (lexer, "OPPOSITE", "LAYER");
1664 lex_error_expecting (lexer, "AUTO", "ROWLABELS",
1670 else if (lex_match_id (lexer, "CRITERIA"))
1672 if (!lex_force_match_id (lexer, "CILEVEL"))
1674 lex_match (lexer, T_EQUALS);
1676 if (!lex_force_num_range_halfopen (lexer, "CILEVEL", 0, 100))
1678 t->cilevel = lex_number (lexer);
1681 else if (lex_match_id (lexer, "CATEGORIES"))
1683 if (!ctables_table_parse_categories (lexer, dataset_dict (ds), t))
1686 else if (lex_match_id (lexer, "TITLES"))
1691 if (lex_match_id (lexer, "CAPTION"))
1692 textp = &t->caption;
1693 else if (lex_match_id (lexer, "CORNER"))
1695 else if (lex_match_id (lexer, "TITLE"))
1699 lex_error_expecting (lexer, "CAPTION", "CORNER", "TITLE");
1702 lex_match (lexer, T_EQUALS);
1704 struct string s = DS_EMPTY_INITIALIZER;
1705 while (lex_is_string (lexer))
1707 if (!ds_is_empty (&s))
1708 ds_put_byte (&s, ' ');
1709 ds_put_substring (&s, lex_tokss (lexer));
1713 *textp = ds_steal_cstr (&s);
1715 while (lex_token (lexer) != T_SLASH
1716 && lex_token (lexer) != T_ENDCMD);
1718 else if (lex_match_id (lexer, "SIGTEST"))
1722 t->chisq = xmalloc (sizeof *t->chisq);
1723 *t->chisq = (struct ctables_chisq) {
1725 .include_mrsets = true,
1726 .all_visible = true,
1732 if (lex_match_id (lexer, "TYPE"))
1734 lex_match (lexer, T_EQUALS);
1735 if (!lex_force_match_id (lexer, "CHISQUARE"))
1738 else if (lex_match_id (lexer, "ALPHA"))
1740 lex_match (lexer, T_EQUALS);
1741 if (!lex_force_num_range_halfopen (lexer, "ALPHA", 0, 1))
1743 t->chisq->alpha = lex_number (lexer);
1746 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
1748 lex_match (lexer, T_EQUALS);
1749 if (parse_bool (lexer, &t->chisq->include_mrsets))
1752 else if (lex_match_id (lexer, "CATEGORIES"))
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;
1761 lex_error_expecting (lexer,
1762 "ALLVISIBLE", "SUBTOTALS");
1768 lex_error_expecting (lexer, "TYPE", "ALPHA",
1769 "INCLUDEMRSETS", "CATEGORIES");
1773 while (lex_token (lexer) != T_SLASH
1774 && lex_token (lexer) != T_ENDCMD);
1776 else if (lex_match_id (lexer, "COMPARETEST"))
1780 t->pairwise = xmalloc (sizeof *t->pairwise);
1781 *t->pairwise = (struct ctables_pairwise) {
1783 .alpha = { .05, .05 },
1784 .adjust = BONFERRONI,
1785 .include_mrsets = true,
1786 .meansvariance_allcats = true,
1787 .all_visible = true,
1796 if (lex_match_id (lexer, "TYPE"))
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;
1805 lex_error_expecting (lexer, "PROP", "MEAN");
1809 else if (lex_match_id (lexer, "ALPHA"))
1811 lex_match (lexer, T_EQUALS);
1813 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
1815 double a0 = lex_number (lexer);
1818 lex_match (lexer, T_COMMA);
1819 if (lex_is_number (lexer))
1821 if (!lex_force_num_range_open (lexer, "ALPHA", 0, 1))
1823 double a1 = lex_number (lexer);
1826 t->pairwise->alpha[0] = MIN (a0, a1);
1827 t->pairwise->alpha[1] = MAX (a0, a1);
1830 t->pairwise->alpha[0] = t->pairwise->alpha[1] = a0;
1832 else if (lex_match_id (lexer, "ADJUST"))
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;
1843 lex_error_expecting (lexer, "BONFERRONI", "BH",
1848 else if (lex_match_id (lexer, "INCLUDEMRSETS"))
1850 lex_match (lexer, T_EQUALS);
1851 if (!parse_bool (lexer, &t->pairwise->include_mrsets))
1854 else if (lex_match_id (lexer, "MEANSVARIANCE"))
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;
1863 lex_error_expecting (lexer, "ALLCATS", "TESTEDCATS");
1867 else if (lex_match_id (lexer, "CATEGORIES"))
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;
1876 lex_error_expecting (lexer, "ALLVISIBLE",
1881 else if (lex_match_id (lexer, "MERGE"))
1883 lex_match (lexer, T_EQUALS);
1884 if (!parse_bool (lexer, &t->pairwise->merge))
1887 else if (lex_match_id (lexer, "STYLE"))
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;
1896 lex_error_expecting (lexer, "APA", "SIMPLE");
1900 else if (lex_match_id (lexer, "SHOWSIG"))
1902 lex_match (lexer, T_EQUALS);
1903 if (!parse_bool (lexer, &t->pairwise->show_sig))
1908 lex_error_expecting (lexer, "TYPE", "ALPHA", "ADJUST",
1909 "INCLUDEMRSETS", "MEANSVARIANCE",
1910 "CATEGORIES", "MERGE", "STYLE",
1915 while (lex_token (lexer) != T_SLASH
1916 && lex_token (lexer) != T_ENDCMD);
1920 lex_error_expecting (lexer, "TABLE", "SLABELS", "CLABELS",
1921 "CRITERIA", "CATEGORIES", "TITLES",
1922 "SIGTEST", "COMPARETEST");
1927 if (t->row_labels != CTLP_NORMAL && t->col_labels != CTLP_NORMAL)
1929 msg (SE, _("ROWLABELS and COLLABELS may not both be specified."));
1934 while (lex_token (lexer) != T_ENDCMD);
1936 ctables_execute (ct);
1937 ctables_destroy (ct);
1941 ctables_destroy (ct);