Advanced Topics in Parsing

This section includes parsing several strings and flow charts illustrating a conceptual view of parsing.

Parsing Several Strings

Only ARG and PARSE ARG can have more than one source string. To parse several strings, you can specify several comma-separated templates. Here is an example:

parse arg template1, template2, template3

This instruction consists of the keywords PARSE ARG and three comma-separated templates. For an ARG instruction, the source strings to be parsed come from arguments you specify when you call a program or CALL a subroutine or function. Each comma is an instruction to the parser to move on to the next string.

Example:

/* Parsing several strings in a subroutine                       */
num="3"
musketeers="Porthos Athos Aramis D'Artagnan"
CALL Sub num,musketeers  /* Passes num and musketeers to sub     */
SAY total; say fourth /* Displays: "4" and " D'Artagnan"         */
EXIT

Sub:
  parse arg subtotal, . . . fourth
  total=subtotal+1
  RETURN

Note that when a Rexx program is started as a command, only one argument string is recognized. You can pass several argument strings for parsing if:

If there are more templates than source strings, each variable in a leftover template receives a null string. If there are more source strings than templates, the language processor ignores leftover source strings. If a template is empty (two subsequent commas) or contains no variable names, parsing proceeds to the next template and source string.

Combining String and Positional Patterns

There is a special case in which absolute and relative positional patterns do not work identically. Parsing with a template containing a string pattern skips the data in the source string that matches the pattern (see Templates Containing String Patterns). But a template containing the sequence string pattern, variable name, and relative position pattern does not skip the matching data. A relative positional pattern moves relative to the first character matching a string pattern. As a result, assignment includes the data in the source string that matches the string pattern.

/* Template containing string pattern, then variable name, then  */
/*  relative positional pattern does not skip any data.          */
string="REstructured eXtended eXecutor"
parse var string var1 3 junk "X" var2 +1 junk "X" var3 +1 junk
say var1||var2||var3 /* Concatenates variables; displays: "Rexx" */

Here is how this template works:

Conceptual Overview of Parsing

The following figures are to help you understand the concept of parsing.

The figures include the following terms:

string start

is the beginning of the source string (or substring).

string end

is the end of the source string (or substring).

length

is the length of the source string.

match start

is in the source string and is the first character of the match.

match end

is in the source string. For a string pattern, it is the first character after the end of the match. For a positional pattern, it is the same as match start.

match position

is in the source string. For a string pattern, it is the first matching character. For a positional pattern, it is the position of the matching character.

token

is a distinct syntactic element in a template, such as a variable, a period, a pattern, or a comma.

value

is the numeric value of a positional pattern. This can be either a constant or the resolved value of a variable.

Figure 9-1. Conceptual Overview of Parsing

Figure 9-2. Conceptual View of Finding Next Pattern

Figure 9-3. Conceptual View of Word Parsing

Note: The figures do not include error cases.