There are two types of operators defined in the SNOBOL5 language. These are prefix, unary operators and infix, binary operators. Unary operators are higher in precedence than any binary operator. Binary operators have the associativity and precedence indicated in the following figure:
OP | Definition as unary operator | Definition as binary operator | Binary association | Binary precedence |
~ | negation | not defined | right | 12 |
? | interrogation | not defined | left | 12 |
$ | indirect reference | immediate assignment | left | 11 |
. | name | conditional assignment | left | 11 |
! | not defined | exponentiation | right | 10 |
** | not defined | exponentiation | right | 10 |
^ | not defined | exponentiation | right | 10 |
% | not defined | not defined | left | 9 |
* | unevaluated expression | multiplication | left | 8 |
/ | not defined | division | left | 7 |
# | not defined | not defined | left | 6 |
+ | positive | addition | left | 5 |
- | negative | subtraction | left | 5 |
@ | cursor position | not defined | left | 4 |
blank | not allowed | concatenation | left | 3 |
| | not defined | alternation | left | 2 |
& | keyword | not defined | left | 1 |
addition + binary operator is used to perform ordinary addition among integers and reals. Note that strings which contain numeric values are automatically converted to numeric type first.
alternation | binary operator indicates that the pattern on the left is the first alternative and that the pattern on the right is the second alternative to be tried during pattern matching.
concatenation (blank) binary operator concatenates the string on the left with the one on the right and returns the resulting string. Concatenation is indicated with no intervening operator except for a blank. See the SNOBOL5 syntax summary for statement syntax, because a blank does not always mean concatenation.
conditional assignment . binary operator assigns the portion of the subject string successfully matched by the pattern left of the operator, to the variable on the right, only if the entire pattern match succeeds. For example, the following replaces the first integer in string Q by 3 times its value:
cursor position @ unary operator stores the current pattern matching cursor position into the argument variable when encountered during pattern matching. Cursor position zero is just before the leftmost character of the subject string.
division / binary operator is used to perform ordinary division among integers and reals. Note that strings which contain numeric values are automatically converted to numeric type first.
exponentiation (^ or ! or **) binary operator is used to perform ordinary exponentiation. Note that this operator is right associative which means that the value of 2 ** 3 ** 3 will be 134217728 and not 512.
immediate assignment $ binary operator assigns the portion of the subject string successfully matched by the pattern left of the operator, to the variable on the right, each time the operator is encountered. For example, the following statement will print all initial substrings of the string Q:
Note that if &FULLSCAN is set to 1, the above will print all substrings of Q.
indirect reference $ unary operator takes a string or name data type as an argument and returns a variable with the name specified by the argument. Indirect reference is the inverse of the name operator. Note that the indirect reference operator cannot be used to access keywords.
interrogation ? unary operator is used to test whether an expression evaluation succeeds or not. If the expression succeeds, then the ? prefix will succeed and return the null string. If the expression fails, the ? prefix will fail. The interrogation operator is the inverse of the negation operator ~.
keyword & unary operator is used in front of a variable name to indicate a reference to the keyword and not the natural variable. See the keywords section for a description of all of the SNOBOL5 keywords.
multiplication * binary operator is used to perform ordinary multiplication among integers and reals. Note that strings which contain numeric values are automatically converted to numeric type first.
name . unary operator returns the name of a variable. In the case of a natural variable it returns a string which is the name of the variable. In the case of a created variable, it returns the NAME data type which refers to that variable. The name operator is the inverse of the indirect reference operator $. See the indirect reference unary operator, NRETURN token and the NAME data type descriptions for more information.
negation ~ unary operator is used to test whether an expression evaluation fails or not. If the expression fails, then the ~ prefix will succeed and return the null string. If the expression succeeds, the ~ prefix will fail. The negation operator can be used to complement predicate functions. For example there is a lexically greater than function but not the inverse. "LLE(a,b)" can be obtained with ~LGT(a,b). Note that the original SNOBOL5 language manual uses the character '¬' instead of '~' for the negation operator.
negative - unary operator is used to obtain the additive inverse of a numeric value (integer or real). Note that strings which contain numeric values are automatically converted to numeric type first.
positive + unary operator is the inverse of the negative unary operator. It simply passes on the numeric value of the argument without change. Note that strings which contain numeric values are automatically converted to numeric type first.
subtraction - binary operator is used to perform ordinary subtraction among integers and reals. Note that strings which contain numeric values are automatically converted to numeric type first.
unevaluated expression * unary operator indicates that the argument expression is not to be evaluated until it is encountered in pattern matching or evaluated by the EVAL() function. See the EXPRESSION data type for more information.
Prior Page, Next Page, First Page of the Oregon SNOBOL5 Reference