Statement Syntax

The text of the statements is assumed to be in columns 1 through 255 inclusive. The remaining data on the line is ignored. Labels always start in column one, must begin with an alphabetic character and end with the first blank. An asterisk (*) or vertical bar (|) in column one indicates that this is a comment line. A plus (+) or period (.) in column one indicates this is a continuation line for the previous statement (ignoring intermediate comment lines). A token (such as a name or string) cannot be split across a line boundary. A minus (-) in column one indicates that a listing control word follows the minus with no blank:

-LIST = turn listing on
-UNLIST = turn listing off
-EJECT = skip to next page

There is actually only one kind of SNOBOL4 statement. It just has many optional parts which cause various actions. Figure 2 gives the syntax for a SNOBOL4 program. The lower case names are metasymbols. The ::= indicates the definition of the metasymbol on the left is given on the right. The brackets [ ] indicate that what is contained within them is optional. Three dots after an item indicates repetition. The vertical bar "|" indicates choice between alternatives. The quoted strings indicate actual terminal text.

programfile ::= [line]... "END" userdatalines line ::= statement [";" statement]... | "*" comment | "|" comment | "-EJECT" | "-LIST" [" LEFT" | " RIGHT"] | "-UNLIST" statement ::= [label] sb [subject [sb pattern] [sb "=" sb [object]]] [ goto] goto ::= sb ":" ob target | sb ":" ob "F" target | sb ":" ob "S" target | sb ":" ob "F" target ob "S" target | sb ":" ob "S" target ob "F" target target ::= lp nameexp ")" | lb codeexp ">" subject ::= expression pattern ::= expression object ::= expression nameexp ::= expression codeexp ::= expression expression ::= variable | literal | prefixop expression | expression " " infixop " " expression | expression sb | expression sb expression | leftparen expression ")" | arrayreference | tablereference | fncreference fncreference ::= functionname lp [expression] ["," ob expression]... ")" arrayreference ::= variable lb [expression] ["," ob expression]... ">" tablereference ::= variable lb [expression] ">" lp ::= "(" ob lb ::= "<" ob ob ::= [" "]... sb ::= " "... infixop ::= op prefixop ::= op op ::= "~" | "?" | "$" | "." | "!" | "**" | "%" | "*" | "/" | "#" | "+" | "-" | "@" | "|" | "&"
Figure 2. SNOBOL4 syntax summary

The label is always optional. The special label END marks the end of the program and must have no statement.

The goto is always optional and specifies where to go after the statement has been executed:

- unconditional goto
- goto only if statement succeeds
- goto only if statement fails
- goto depending on success/failure
If the brackets <> are used instead of parenthesis for the goto, the argument must be of CODE datatype which is run-time compilation output from the CODE() function.

Numeric literals are just a series of digits. Floating point literals contain a decimal point but cannot begin with a decimal point (E notation as in FORTRAN is not valid). Character string literals must either start with a single or double quote and end with the same kind of quote. To embed quotes within character string literals, you must use the other kind of quote. For example the following concatenated literals:

OUTPUT = '"This string' "'s delimiter is a quote" '"' print: "This string's delimiter is a quote"
The null string can be represented by two quote characters with no intervening blanks.

Expressions follow the usual rules of other languages except that binary infix operators must have at least one blank on each side of the operator, and that unary prefix operators must have at least one blank on the left and no blanks on the right. Also a series of one or more blanks with no operator symbol implies the concatenation operator.

The pattern in a statement is optional and is delimited by the first zero level (parenthesis level) blank following the subject. The pattern is matched against the subject and if the match fails, the statement fails. If the match succeeds and there is no = with optional object, the statement succeeds. If the match succeeds and there is an = with optional object, the object is evaluated, and if the evaluation succeeds, the portion of the subject matched by the pattern is replaced by the object and the statement succeeds. If the evaluation of the object fails, the statement fails. If the object is missing but the = is present, then the value of the object is the null string.

If there is no pattern, the subject is evaluated and if it succeeds, then the object is evaluated (if present) and assigned to the subject (if object present). Otherwise the statement fails. If assignment is being performed, the subject must be a name. Note that if there is no pattern, and if the subject consists of concatenated parts, it must be enclosed in parenthesis.

The order of operations within expressions is according to precedence, parenthesis, deferral (see * prefix operator) and otherwise left to right. Expressions in statements are evaluated as follows during execution:

Prior Page, Next Page, First Page of the Minnesota SNOBOL4 Reference