1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2011, 2012, 2013, 2019 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/case.h"
20 #include "data/casegrouper.h"
21 #include "data/casereader.h"
22 #include "data/dataset.h"
23 #include "data/dictionary.h"
24 #include "data/format.h"
25 #include "data/variable.h"
27 #include "language/lexer/lexer.h"
28 #include "language/lexer/variable-parser.h"
30 #include "libpspp/pool.h"
34 /* Parse the /TABLES stanza of the command. */
36 parse_means_table_syntax (struct lexer *lexer, const struct means *cmd,
39 memset (table, 0, sizeof *table);
41 /* Dependent variable (s) */
42 if (!parse_variables_const_pool (lexer, cmd->pool, cmd->dict,
43 &table->dep_vars, &table->n_dep_vars,
44 PV_NO_DUPLICATE | PV_NUMERIC))
47 /* Factor variable (s) */
48 while (lex_match (lexer, T_BY))
50 struct layer *layer = pool_zalloc (cmd->pool, sizeof *layer);
53 pool_nrealloc (cmd->pool, table->layers, table->n_layers + 1,
54 sizeof *table->layers);
55 table->layers[table->n_layers] = layer;
58 if (!parse_variables_const_pool
59 (lexer, cmd->pool, cmd->dict,
61 &layer->n_factor_vars,
70 If the match succeeds, the variable will be placed in VAR.
71 Returns true if successful */
73 lex_is_variable (struct lexer *lexer, const struct dictionary *dict,
76 if (lex_next_token (lexer, n) != T_ID)
79 const char *tstr = lex_next_tokcstr (lexer, n);
80 return dict_lookup_var (dict, tstr) != NULL;
83 static const struct cell_spec *
84 match_cell (struct lexer *lexer)
86 for (size_t i = 0; i < n_MEANS_STATISTICS; ++i)
88 const struct cell_spec *cs = &cell_spec[i];
89 if (lex_match_id (lexer, cs->keyword))
96 add_statistic (struct means *means, int statistic)
98 if (means->n_statistics >= means->allocated_statistics)
99 means->statistics = pool_2nrealloc (means->pool, means->statistics,
100 &means->allocated_statistics,
101 sizeof *means->statistics);
102 means->statistics[means->n_statistics++] = statistic;
106 means_set_default_statistics (struct means *means)
108 means->n_statistics = 0;
109 add_statistic (means, MEANS_MEAN);
110 add_statistic (means, MEANS_N);
111 add_statistic (means, MEANS_STDDEV);
115 means_parse (struct lexer *lexer, struct means *means)
117 /* Optional TABLES=. */
118 if (lex_match_id (lexer, "TABLES") && !lex_force_match (lexer, T_EQUALS))
121 /* Parse the "tables" */
124 means->table = pool_realloc (means->pool, means->table,
125 (means->n_tables + 1) * sizeof *means->table);
127 if (!parse_means_table_syntax (lexer, means,
128 &means->table[means->n_tables]))
132 /* Look ahead to see if there are more tables to be parsed */
133 if (lex_next_token (lexer, 0) != T_SLASH
134 || !lex_is_variable (lexer, means->dict, 1))
136 lex_match (lexer, T_SLASH);
139 /* /MISSING subcommand */
140 while (lex_token (lexer) != T_ENDCMD)
142 lex_match (lexer, T_SLASH);
144 if (lex_match_id (lexer, "MISSING"))
146 /* If no MISSING subcommand is specified, each combination of a
147 dependent variable and categorical variables is handled
149 lex_match (lexer, T_EQUALS);
150 if (lex_match_id (lexer, "INCLUDE"))
152 /* Use the subcommand "/MISSING=INCLUDE" to include user-missing
153 values in the analysis. */
155 means->ctrl_exclude = MV_SYSTEM;
156 means->dep_exclude = MV_SYSTEM;
158 else if (lex_match_id (lexer, "DEPENDENT"))
159 /* Use the command "/MISSING=DEPENDENT" to include user-missing
160 values for the categorical variables, while excluding them for
161 the dependent variables.
163 Cases are dropped only when user-missing values appear in
164 dependent variables. User-missing values for categorical
165 variables are treated according to their face value.
167 Cases are ALWAYS dropped when System Missing values appear in
168 the categorical variables. */
170 means->dep_exclude = MV_ANY;
171 means->ctrl_exclude = MV_SYSTEM;
175 lex_error_expecting (lexer, "INCLUDE", "DEPENDENT");
179 else if (lex_match_id (lexer, "CELLS"))
181 lex_match (lexer, T_EQUALS);
183 /* The default values become overwritten */
184 means->n_statistics = 0;
185 while (lex_token (lexer) != T_ENDCMD && lex_token (lexer) != T_SLASH)
187 if (lex_match (lexer, T_ALL))
189 means->n_statistics = 0;
190 for (int i = 0; i < n_MEANS_STATISTICS; ++i)
191 add_statistic (means, i);
193 else if (lex_match_id (lexer, "NONE"))
194 means->n_statistics = 0;
195 else if (lex_match_id (lexer, "DEFAULT"))
196 means_set_default_statistics (means);
199 const struct cell_spec *cs = match_cell (lexer);
201 add_statistic (means, cs - cell_spec);
204 const char *keywords[n_MEANS_STATISTICS];
205 for (int i = 0; i < n_MEANS_STATISTICS; ++i)
206 keywords[i] = cell_spec[i].keyword;
207 lex_error_expecting_array (lexer, keywords,
216 lex_error_expecting (lexer, "MISSING", "CELLS");