+/* Check that OLD_NAME can be renamed to NEW_NAME in DICT. */
+static bool
+check_rename (const struct dictionary *dict, const char *old_name, const char *new_name)
+{
+ if (dict_lookup_var (dict, new_name) != NULL)
+ {
+ msg (SE, _("Cannot rename %s as %s because there already exists "
+ "a variable named %s. To rename variables with "
+ "overlapping names, use a single RENAME subcommand "
+ "such as `/RENAME (A=B)(B=C)(C=A)', or equivalently, "
+ "`/RENAME (A B C=B C A)'."),
+ old_name, new_name, new_name);
+ return false;
+ }
+ return true;
+}
+
+/* Parse a "VarX TO VarY" sequence where X and Y are integers
+ such that X >= Y.
+ If successfull, returns a string to the prefix Var and sets FIRST
+ to X and LAST to Y. Returns NULL on failure.
+ The caller must free the return value. */
+static char *
+try_to_sequence (struct lexer *lexer, const struct dictionary *dict,
+ int *first, int *last)
+{
+ /* Check that the next 3 tokens are of the correct type. */
+ if (lex_token (lexer) != T_ID
+ || lex_next_token (lexer, 1) != T_TO
+ || lex_next_token (lexer, 2) != T_ID)
+ return NULL;
+
+ /* Check that the first and last tokens are suitable as
+ variable names. */
+ const char *s0 = lex_tokcstr (lexer);
+ if (!id_is_valid (s0, dict_get_encoding (dict), true))
+ return NULL;
+
+ const char *s1 = lex_next_tokcstr (lexer, 2);
+ if (!id_is_valid (s1, dict_get_encoding (dict), true))
+ return NULL;
+
+ int x0 = strcspn (s0, "0123456789");
+ int x1 = strcspn (s1, "0123456789");
+
+ /* The non-digit parts of s0 and s1 must be the same length. */
+ if (x0 != x1)
+ return NULL;
+
+ /* Both s0 and s1 must have some digits. */
+ if (strlen (s0) <= x0)
+ return NULL;
+
+ if (strlen (s1) <= x1)
+ return NULL;
+
+ /* The non-digit parts of s0 and s1 must be identical. */
+ if (0 != strncmp (s0, s1, x0))
+ return NULL;
+
+ /* Both names must end with digits. */
+ int len_s0_pfx = strspn (s0 + x0, "0123456789");
+ if (len_s0_pfx + x0 != strlen (s0))
+ return NULL;
+
+ int len_s1_pfx = strspn (s1 + x1, "0123456789");
+ if (len_s1_pfx + x1 != strlen (s1))
+ return NULL;
+
+ const char *n_start = s0 + x0;
+ const char *n_stop = s1 + x1;
+
+ /* The first may not be greater than the last. */
+ if (atoi (n_start) > atoi (n_stop))
+ return NULL;
+
+ char *prefix = xstrndup (s0, x1);
+
+ *first = atoi (n_start);
+ *last = atoi (n_stop);
+
+ return prefix;
+}
+
+