Operator precedence: Difference between revisions
m (Added link to relevant Perl information) |
(Added zkl) |
||
Line 1,274: | Line 1,274: | ||
| '''if-then-else''' |
| '''if-then-else''' |
||
| If expression |
| If expression |
||
|} |
|||
=={{header|zkl}}== |
|||
All operations of equal precedence are evaluated (associate) left-to-right. |
|||
If shared with C it has the same precedence except for logical and/or, which have the same precedence. |
|||
{| class="wikitable" |
|||
|- |
|||
! Precedence |
|||
! Operator |
|||
! Description |
|||
|- |
|||
! highest |
|||
| '''() .(dot) [],''' |
|||
| Grouping/function call, .resolve, subscripting |
|||
|- |
|||
! |
|||
| '''- not''' |
|||
| Unary minus, logical not |
|||
|- |
|||
! |
|||
| '''* / %''' |
|||
| Multiply, divide and modulo |
|||
|- |
|||
! |
|||
| '''+ -''' |
|||
| Add and subtract |
|||
|- |
|||
! |
|||
| '''< <= > >=''' |
|||
| Comparisons: equal, not equal, less than, etc. |
|||
|- |
|||
! |
|||
| '''== !=''' |
|||
| Comparisons: equal, not equal |
|||
|- |
|||
! |
|||
| '''and or''' |
|||
| logical AND and logical OR |
|||
|- |
|||
! |
|||
| ''':''' |
|||
| compose (nest expressions) |
|||
|- |
|||
! lowest |
|||
| ''':= =''' |
|||
| Assignment |
|||
|} |
|} |
Revision as of 02:30, 10 July 2014
You are encouraged to solve this task according to the task description, using any language you may know.
This page uses content from Wikipedia. The original article was at Operators in C and C++. The list of authors can be seen in the page history. As with Rosetta Code, the text of Wikipedia is available under the GNU FDL. (See links for details on variance) |
Provide a list of precedence and associativity of all the operators and constructs that the language utilizes in descending order of precedence such that an operator which is listed on some row will be evaluated prior to any operator that is listed on a row further below it. Operators that are in the same cell (there may be several rows of operators listed in a cell) are evaluated with the same level of precedence, in the given direction. Stating whether arguments are passed by value or by reference
ALGOL 68
The coder may define new operators and both those and the pre-defined ones may be overloaded and their priorities may be changed.
Array, Procedure, Dereference, Selection and Generator operations
priority | Operation | +Algol68Rev0 | +Algol68G |
---|---|---|---|
Effectively 12 (Primary) |
dereferencing, deproceduring(~,~), subscripting[~], rowing[~,] & slicing[~:~] | currying(~,), diag, trnsp, row, col | |
Effectively 11 (Secondary) |
of (selection), loc & heap (generators) | → | new (generator) |
These are technically not operators, rather they are considered "units associated with names"
Monadic operators
priority (Tertiary) |
Algol68 "Worthy characters" | +Algol68Rev0&1 | +Algol68C,G | +Algol68Rev0 |
---|---|---|---|---|
10 | not, up, down, lwb, upb,
-, abs, arg, bin, entier, leng, level, odd, repr, round, shorten |
¬, ↑, ↓, ⌊, ⌈ | ~, norm, trace, t, det, inv | lws, ups, ⎩, ⎧, btb, ctb |
Standard dyadic operators with associated priorities
priority (Tertiary) |
Algol68 "Worthy characters" | +Algol68Rev0&1 | +Algol68C,G | +Algol68Rev0 |
---|---|---|---|---|
9 | +*, i | +×, ⊥ | ! | |
8 | shl, shr, **, up, down, lwb, upb | ↑, ↓, ⌊, ⌈ | lws, ups, ⎩, ⎧ | |
7 | *, /, %, over, %*, mod, elem | ×, ÷, ÷×, ÷*, %×, □ | ÷: | |
6 | -, + | |||
5 | <, lt, <=, le, >=, ge, >, gt | ≤, ≥ | ||
4 | =, eq, /=, ne | ≠ | ~= | |
3 | &, and | ∧ | /\ | |
2 | or | ∨ | \/ | |
1 | minusab, plusab, timesab, divab, overab, modab, plusto,
-:=, +:=, *:=, /:=, %:=, %*:=, +=: |
×:=, ÷:=, ÷×:=, ÷*:=, %×:= | minus, plus, div, overb, modb, ÷::=, prus |
Note: Tertiaries include names nil and ○.
Assignation and identity relations etc
Again, these are technically not operators, rather they are considered "units associated with names"
priority (Quaternaries) |
Algol68 "Worthy characters" | +Algol68Rev0&1 | +Algol68C | +Algol68Rev0 |
---|---|---|---|---|
Effectively 0 | :=, =:, = , :=:, :/=:, is, isnt, at , @ | :≠:, : | :~=: | ct, ::, ctab, ::=, .., is not |
Note: Quaternaries include names skip and ~.
Algol 68 also includes (something like) C's ternary conditions, e.g.:
- case ~ in ~ ouse ~ in ~ out ~ esac or simply "( ~ | ~ |: ~ | ~ | ~ )",
- if ~ then ~ elif ~ then ~ else ~ fi or simply "( ~ | ~ |: ~ | ~ | ~ )",
And (unlike C's comma operator) the ";" can be used to indicate statements are done sequentially, where as the "," indicates that the statements can be done "collaterally", e.g. in parallel. Or a parallel clause can be used to force statements to be executed in parallel, e.g. par( ~, ~, ... )
Key: The super scripts indicate the following:
- ALGOL 68Rev0 indicates Algol 68 Final Report (Essentially Revision 0)
- ALGOL 68Rev0&1 indicates Algol 68 Revised Report (Essentially Revision 1)
- ALGOL 68C indicates Cambridge University Algol 68[1].
- ALGOL 68G indicates Algol 68 Genie[2]
bc
From the POSIX Standard, ordered by decreasing precedence:
Precedence | Operator(s) | Description | Associativity |
---|---|---|---|
Highest | ++, -- | Prefix/Postfix Increment/Decrement | n/a |
unary - | Negation | n/a | |
^ | Exponentiation | Right to left | |
*, /, % | Multiplication, Division, Remainder | Left to right | |
+, binary - | Addition, Subtraction | Left to right | |
=, +=, -=, *=, /=, %=, ^= | Assignment | Right to left | |
==, <=, >=, !=, <, > | Comparison | None | |
! | Logical Not | n/a | |
&& | Logical And | Left to right | |
Lowest | || | Logical Or | Left to right |
C
[C++.]
C++
The following is a table that lists the precedence and associativity of all the operators in the C and C++ languages. An operator's precedence is unaffected by overloading.
Precedence | Operator | Description | Associativity |
---|---|---|---|
1
highest |
::
|
Scope resolution (C++ only) | Left-to-right |
2 | ++
|
Suffix increment | |
--
|
Suffix decrement | ||
()
|
Function call | ||
[]
|
Array subscripting | ||
.
|
Element selection by reference | ||
->
|
Element selection through pointer | ||
typeid()
|
Run-time type information (C++ only) (see typeid) | ||
const_cast
|
Type cast (C++ only) (see const cast) | ||
dynamic_cast
|
Type cast (C++ only) (see dynamic cast) | ||
reinterpret_cast
|
Type cast (C++ only) (see reinterpret cast) | ||
static_cast
|
Type cast (C++ only) (see static cast) | ||
3 | ++
|
Prefix increment | Right-to-left |
--
|
Prefix decrement | ||
+
|
Unary plus | ||
-
|
Unary minus | ||
!
|
Logical NOT | ||
~
|
Bitwise NOT | ||
(type)
|
Type cast | ||
*
|
Indirection (dereference) | ||
&
|
Address-of | ||
sizeof
|
Size-of | ||
new , new[]
|
Dynamic memory allocation (C++ only) | ||
delete , delete[]
|
Dynamic memory deallocation (C++ only) | ||
4 | .*
|
Pointer to member (C++ only) | Left-to-right |
->*
|
Pointer to member (C++ only) | ||
5 | *
|
Multiplication | |
/
|
Division | ||
%
|
Modulo (remainder) | ||
6 | +
|
Addition | |
-
|
Subtraction | ||
7 | <<
|
Bitwise left shift | |
>>
|
Bitwise right shift | ||
8 | <
|
Less than | |
<=
|
Less than or equal to | ||
>
|
Greater than | ||
>=
|
Greater than or equal to | ||
9 | ==
|
Equal to | |
!=
|
Not equal to | ||
10 | &
|
Bitwise AND | |
11 | ^
|
Bitwise XOR (exclusive or) | |
12 | |
|
Bitwise OR (inclusive or) | |
13 | &&
|
Logical AND | |
14 | ||
|
Logical OR | |
15 | ?:
|
Ternary conditional (see ?:) | Right-to-left |
16 | =
|
Direct assignment | |
+=
|
Assignment by sum | ||
-=
|
Assignment by difference | ||
*=
|
Assignment by product | ||
/=
|
Assignment by quotient | ||
%=
|
Assignment by remainder | ||
<<=
|
Assignment by bitwise left shift | ||
>>=
|
Assignment by bitwise right shift | ||
&=
|
Assignment by bitwise AND | ||
^=
|
Assignment by bitwise XOR | ||
|=
|
Assignment by bitwise OR | ||
17 | throw
|
Throw operator (exceptions throwing, C++ only) | |
18 | ,
|
Comma | Left-to-right |
For quick reference, see also this equivalent, color-coded table.
COBOL
The following data was derived from the 2009 draft COBOL 20XX Standard.
Arithmetic Expressions
Precedence | Operator(s) | Description |
---|---|---|
Highest | + - |
Unary plus and minus |
** |
Exponentiation | |
* / |
Multiplication and Division | |
Lowest | + - |
Addition and Subtraction |
Boolean Expressions
Precedence | Operator | Description |
---|---|---|
Highest | B-NOT |
Negation |
B-AND |
Conjunction | |
B-XOR |
Exclusive disjunction | |
Lowest | B-OR |
Inclusive disjunction |
Concatenation Expressions
The &
operator is the only operator used in concatenation expressions.
Logical Expressions
Precedence | Operator | Description |
---|---|---|
Highest | NOT |
Logical negation |
AND |
Logical conjunction | |
Lowest | OR |
Logical inclusive OR |
D
Priority | Description | Operators | Comments | |
---|---|---|---|---|
15 | Template instantiation | ! | Top-level ',' in rhs expression treated specially. Cannot be chained | |
14.5 | Lambda abstraction | => | Not a real operator, occurs twice, this is binding power to the left. | |
14 | Postfix operators | . ++ -- ( [ | ( and [ treat top-level ',' in rhs expression specially and require balanced ) or ] in order to be completed | |
13 | Power operator | ^^ | Right-associative | |
12 | Unary operators | & ++ -- * + - ! ~ | ||
11 | - | * / % | ||
10 | - | + - ~ | Binary '~' is the concatenation operator | |
9 | Bit shift operators | << >> >>> | ||
6a | Comparison operators | == != > < >= <= !> !< !>= !<= <> !<> <>= !<>= in !in is !is | Unordered with respect to bitwise operators, cannot be chained. | |
8b | Bitwise AND | & | Unordered with respect to comparison operators | |
7b | Bitwise XOR | ^ | Unordered with respect to comparison operators | |
6b | Bitwise OR | | | Unordered with respect to comparison operators | |
5 | Logical AND | && | Short-circuit | |
4 | Logical OR | || | Short-circuit | |
3 | Conditional operator | ?: | Right-associative | |
2 | Assignment operators | = -= += <<= >>= >>>= = *= %= ^= ^^= ~= | Right-associative | |
1.5 | Lambda abstraction | => | Not a real operator, occurs twice, this is binding power to the right | |
1 | Comma operator | , | Not to be confused with other uses of ',', though their precedence is the same | |
0 | Range separator | .. | Not a real operator, hardwired into syntax at specific points |
Erlang
Official documentation table: [[3]], see "Operator Precedence" towards the end.
Fortran
Operators | Details |
---|---|
Function calls | |
** |
Numeric |
* , / |
Numeric |
+ , - |
Unary numeric operators |
+ , - |
Binary numeric operators |
// |
String |
FROM , TO |
|
.EQ. , .GE. , .GT. , .LE. , .LT. , .NE. , == , >= , > , <= , < , /= |
Relational |
.NOT. , # |
Logical |
.AND. , & |
Logical |
.OR. , | |
Logical |
.EQV. , .NEQV. |
Logical |
, |
|
(, ) |
|
Start and End of an expression |
Go
Precedence | Operators |
---|---|
Highest | Unary operators: +, -, !, ^, *, &, <- |
5 | *, /, %, <<, >>, &, &^ |
4 | +, -, |, ^ |
3 | ==, !=, <, <=, >, >= |
2 | && |
1 | || |
Binary operators of the same precedence associate from left to right. Associativity has no meaning for unary operators.
Syntactic elements not in the list are not considered operators in Go; if they present ambiguity in order of evaluation, the ambiguity is resolved by other rules specific to those elements.
Icon and Unicon
Taken from http://www.cs.arizona.edu/icon/refernce/exprlist.htm#expressions (blank lines separate groups of operators with equal precedence):
(expr) # grouping {expr1;expr2;...} # compound x(expr1,expr2,...) # process argument list x{expr1,expr2,...} # process co-expression list [expr1,expr2,...] # list expr.F # field reference expr1[expr2] # subscript expr1[expr2,expr3,...] # multiple subscript expr1[expr2:expr3] # section expr1[expr2+:expr3] # section expr1[expr2-:expr3] # section not expr # success/failure reversal | expr # repeated alternation ! expr # element generation * expr # size + expr # numeric value - expr # negative . expr # value (dereference) / expr # null \ expr # non-null = expr # match and tab ? expr # random value ~ expr # cset complement @ expr # activation ^ expr # refresh expr1 \ expr2 # limitation expr1 @ expr2 # transmission expr1 ! expr2 # invocation expr1 ^ expr2 # power expr1 * expr2 # product expr1 / expr2 # quotient expr1 % expr2 # remainder expr1 ** expr2 # intersection expr1 + expr2 # sum expr1 - expr2 # numeric difference expr1 ++ expr2 # union expr1 -- expr2 # cset or set difference expr1 || expr2 # string concatenation expr1 ||| expr2 # list concatenation expr1 < expr2 # numeric comparison expr1 <= expr2 # numeric comparison expr1 = expr2 # numeric comparison expr1 >= expr2 # numeric comparison expr1 > expr2 # numeric comparison expr1 ~= expr2 # numeric comparison expr1 << expr2 # string comparison expr1 <<= expr2 # string comparison expr1 == expr2 # string comparison expr1 >>= expr2 # string comparison expr1 >> expr2 # string comparison expr1 ~== expr2 # string comparison expr1 === expr2 # value comparison expr1 ~=== expr2 # value comparison expr1 | expr2 # alternation expr1 to expr2 by expr3 # integer generation expr1 := expr2 # assignment expr1 <- expr2 # reversible assignment expr1 :=: expr2 # exchange expr1 <-> expr2 # reversible exchange expr1 op:= expr2 # (augmented assignments) expr1 ? expr2 # string scanning expr1 & expr2 # conjunction Low Precedence Expressions break [expr] # break from loop case expr0 of { # case selection expr1:expr2 ... [default:exprn] } create expr # co-expression creation every expr1 [do expr2] # iterate over generated values fail # failure of procedure if expr1 then exp2 [else exp3] # if-then-else next # go to top of loop repeat expr # loop return expr # return from procedure suspend expr1 [do expr2] # suspension of procedure until expr1 [do expr2] # until-loop while expr1 [do expr2] # while-loop
J
Precedence | Grammatical classification | Associativity |
---|---|---|
highest | conjunctions | long left scope |
adverbs | ||
lowest | verbs | long right scope |
See http://www.jsoftware.com/help/dictionary/partsofspeech.htm for tokens in each grammatical class.
Note that other parts of speech do not have any precedence, because they are not "operators".
Note that this is an imprecise statement of the grammatical rules. For a complete treatment, see http://www.jsoftware.com/help/dictionary/dicte.htm
Here's an informal treatment of the grammar:
Conjunctions require a left and right argument, either of which may be a noun or a verb. If one argument is omitted the conjunction is curried with the remaining argument, forming an adverb (if the right argument is omitted, the precedence of the conjunction is treated as lower than the precedence of a verb).
Adverbs require a single left argument, which may be a noun or a verb.
Verbs require a right argument which must be a noun and may accept an optional left argument (which must also be a noun). Unless we're working with a dangling (rightmost) conjunction, verbs have lower precedence than adverbs and conjunctions.
Nouns are not operators and accept no arguments.
The result of a verb must be a noun.
The result of an adverb or a conjunction can have any one of these grammatical classifications, and verb results are typical (and, thus, the result of an adverb or a conjunction may accept further arguments). Adverbs and conjunctions serve a role analogous to that of macros in other languages.
Java
This is well-documented on the Oracle website.
Lua
Table available here. That table does not contain all operators, however.
Precedence | Operator | Description |
---|---|---|
lowest | or | Boolean OR |
and | Boolean AND | |
<, <=, >, >=, ~=, == | Comparisons | |
.. | Concatenation [1] | |
+, - | Addition and subtraction | |
*, /, % | Multiplication, division, modulo | |
not, -, # | Boolean NOT, negation, length | |
^ | Exponentiation [1] | |
highest | x[index], x.index, x(arguments...), x:m(arguments...) | Generic index, string index, function call, method index+call [2] |
Notes:
- Concatenation and exponentiation are right-associative, all other binary operators are left-associative
- Binding is done at the call site; therefore, method lookup is syntactically part of the call
Mathematica
Here is an outline:
Precedence | Class | Examples |
---|---|---|
highest | Extensions of symbol names | x_, #2 , e::s, etc. |
Function application variants | e[e], e@@e, etc. | |
Power-related operators | √e, e^e, etc. | |
Multiplication-related operators | ∇e, e/e, e⊗e, e e, etc. | |
Addition-related operators | e⊕e, e+e, e⋃e, etc. | |
Relational operators | e==e, e∼e, e∈e, etc. | |
Arrow and vector operators | e↗e, e⇌e, etc. | |
Logic operators | e&&e, e∨e, e⊢e, etc. | |
Pattern and rule operators | e, e->e, e/.e, etc. | |
Pure function operator | e& | |
Assignment operators | e=e, e:=e, etc. | |
lowest | Compound expression | e;e |
There is a table of precedence of all operators on the page "tutorial/OperatorInputForms" in Mathematica help.
OCaml
This table contains the precedence and associativity of operators and other expression constructs in OCaml, including user-defined operators.
Perl
See the relevant documentation for a table of Perl 5 operators ordered by precedence level.
Perl 6
See this table for a list of the precedence levels. Perl 6 is an operator-rich language (and users may define more operators at will), so instead of listing all the operators in the table, representative operators are listed for some of the precedence levels; see later in the same file for a more complete list of predefined operators at each precedence level.
PHP
PARI/GP
Unless otherwise stated, operators at a given level are applied left-to-right.
Precedence | Operator | Description |
---|---|---|
highest | :
|
Type information for gp2c .
|
()
|
Postfix function call notation. | |
++ , --
|
Postfix increment or decrement operators. (Note that these work like the prefix ++ and -- operators in C.)
| |
.
|
Member operator (as in m.mod ).
| |
' , ~ , !
|
Postfix derivative, transpose, and factorial; prefix negation. | |
#
|
Prefix cardinality operator (like length() ).
| |
^
|
Infix exponentiation operator, evaluated right-to-left. | |
+ , -
|
Prefix sign operators. | |
* , / , \ , \/ , << , >>
|
Infix multiplication, division, integer division, rounded quotient, left shift, and right shift. | |
+ , -
|
Infix addition and subtraction. | |
< , <= , > , >= , != or <> , == , ===
|
Infix comparison operators. === tests whether two objects are identical component-wise and is stricter than == .
| |
&& , ||
|
Infix shortcut logical AND and OR. | |
= , += , -= , *= , %= , /= , \= , \/= , <<= , >>=
|
Infix assignment, evaluated right-to-left. | |
lowest | ->
|
Infix function definition. |
Python
See this table and the whole page for details on Python version 3.x An excerpt of which is this table:
Precedence | Operator | Description |
---|---|---|
lowest | lambda | Lambda expression |
if – else | Conditional expression | |
or | Boolean OR | |
and | Boolean AND | |
not x | Boolean NOT | |
in, not in, is, is not, <, <=, >, >=, !=, == | Comparisons, including membership tests and identity tests, | |
| | Bitwise OR | |
^ | Bitwise XOR | |
& | Bitwise AND | |
<<, >> | Shifts | |
+, - | Addition and subtraction | |
*, /, //, % | Multiplication, division, remainder [1] | |
+x, -x, ~x | Positive, negative, bitwise NOT | |
** | Exponentiation [2] | |
x[index], x[index:index], x(arguments...), x.attribute | Subscription, slicing, call, attribute reference | |
highest | (expressions...), [expressions...], {key:datum...}, {expressions...} | Binding or tuple display, list display, dictionary display, set display |
- Footnotes
- The
%
operator is also used for string formatting; the same precedence applies. - The power operator
**
binds less tightly than an arithmetic or bitwise unary operator on its right, that is,2**-1
is0.5
.
Racket
Racket uses S-expr for its syntax, operators and functions show no precedences as all code is written as: <lang Racket>(function arguments ...)</lang>
function being any function or operator (language or user defined) and arguments are the arguments passed to it.
REALbasic
All operators are left-associated except exponentiation and Pair creation which are right-associated.
Operator(s) | Description |
---|---|
. (dot) | Object member access |
AddressOf, WeakAddressOf | Delegate creation |
IsA | Compare an object to a type |
^ | Exponentiation |
- | Negation and unary minus |
Not | Logical not |
* / \ Mod | Multiplication, floating-point division, integer division, modulo |
+, - | Addition, subtraction |
=, <, >, <>, <=, >= | Equals (comparison and assignment), less-than, greater-than, not equal, less-than or equal, greater-than or equal |
And | Logical and bitwise And |
Or, Xor | Or, Exclusive-Or (Logical and bitwise |
: | Pair creation |
REXX
<lang rexx>/*
╔══════════════════════════════════════════════════════════════════════╗ ║ ║ ║ The following is a table that lists the precedence and associativity ║ ║ of all the operators in the (classic) REXX language. ║ ║ ║ ║ 1 is the highest precedence. ║ ║ ║ ╠══════════╤════════╤══════════════════════════════════════════════════╣ ║ │ │ ║ ║precedence│operator│ description ║ ║ │ │ ║ ╟──────────┼────────┼──────────────────────────────────────────────────╢ ║ 1 │ - │ unary minus ║ ║ │ + │ unary plus ║ ║ │ \ │ logical not ║ ║ │ ├──(the following aren't supported by all REXXes)──╢ ║ │ ¬ │ logical not ║ ║ │ ~ │ logical not ║ ║ │ ^ │ logical not ║ ╟──────────┼────────┼──────────────────────────────────────────────────╢ ║ 2 │ ** │ exponentiation (integer power) ║ ╟──────────┼────────┼──────────────────────────────────────────────────╢ ║ 3 │ * │ multiplication ║ ║ │ / │ division ║ ║ │ % │ integer division ║ ║ │ // │ modulus (remainder division, sign of dividend)║ ║ │ / / │ modulus (any 2 or 3 character operators may ║ ║ │ │ have whitespace between characters.) ║ ╟──────────┼────────┼──────────────────────────────────────────────────╢ ║ 4 │ + │ addition ║ ║ │ - │ subtraction ║ ╟──────────┼────────┼──────────────────────────────────────────────────╢ ║ 5 │ || │ concatenation ║ ╟──────────┼────────┼──────────────────────────────────────────────────╢ ║ 6 │ & │ logical AND ║ ║ │ | │ logical OR (inclusive OR) ║ ║ │ && │ logical XOR (exclusive OR) ║ ╟──────────┼────────┼──────────────────────────────────────────────────╢ ║ 7 │[blank] │ concatenation ║ ║ │abuttal │ concatenation ║ ╟──────────┼────────┼──────────────────────────────────────────────────╢ ║ 8 │ = │ equal to ║ ║ │ == │ exactly equal to (also, strictly equal to) ║ ║ │ \= │ not equal to ║ ║ │ <> │ not equal to (also, less than or greater than) ║ ║ │ >< │ not equal to (also, greater than or less than) ║ ║ │ > │ greater than ║ ║ │ >= │ greater than or equal to ║ ║ │ < │ less than ║ ║ │ <= │ less than or equal to ║ ║ │ >> │ exactly greater than ║ ║ │ << │ exactly less than ║ ║ │ <<= │ exactly less than or equal to ║ ║ │ >>= │ exactly greater than or equal to ║ ║ │ ├──(the following aren't supported by all REXXes)──╢ ║ │ /= │ not equal to ║ ║ │ ¬= │ not equal to ║ ║ │ ~= │ not equal to ║ ║ │ ^= │ not equal to ║ ║ │ /== │ not exactly equal to ║ ║ │ \== │ not exactly equal to ║ ║ │ ¬== │ not exactly equal to ║ ║ │ ~== │ not exactly equal to ║ ║ │ ^== │ not exactly equal to ║ ║ │ /< │ not less than ║ ║ │ ~< │ not less than ║ ║ │ ¬< │ not less than ║ ║ │ ^< │ not less than ║ ║ │ /> │ not greater than ║ ║ │ ¬> │ not greater than ║ ║ │ ~> │ not greater than ║ ║ │ ^> │ not greater than ║ ║ │ /<= │ not less than or equal to ║ ║ │ ¬<= │ not less than or equal to ║ ║ │ ~<= │ not less than or equal to ║ ║ │ ^<= │ not less than or equal to ║ ║ │ />= │ not greater than or equal to ║ ║ │ ¬>= │ not greater than or equal to ║ ║ │ ~>= │ not greater than or equal to ║ ║ │ ^>= │ not greater than or equal to ║ ║ │ \>> │ not exactly greater than ║ ║ │ ¬>> │ not exactly greater than ║ ║ │ ~>> │ not exactly greater than ║ ║ │ ^>> │ not exactly greater than ║ ║ │ \<< │ not exactly less than ║ ║ │ ¬<< │ not exactly less than ║ ║ │ ~<< │ not exactly less than ║ ║ │ ^<< │ not exactly less than ║ ╚══════════╧════════╧══════════════════════════════════════════════════╝ */</lang>
Ruby
Ruby operators, by precedence (high to low), with arity (N), associativity (A), and definability (D)
Operator(s) | N | A | D | Operation |
---|---|---|---|---|
! ~ + | 1 | R | Y | Boolean NOT, bitwise complement, unary plus |
** | 2 | R | Y | Exponentiation |
- | 1 | R | Y | Unary minus (define with -@) |
* / % | 2 | L | Y | Multiplication, division, modulo (remainder) |
+ - | 2 | L | Y | Addition (or concatenation), subtraction |
<< >> | 2 | L | Y | Bitwise shift-left (or append), bitwise shift-right |
& | 2 | L | Y | Bitwise AND |
^ | 2 | L | Y | Bitwise OR, bitwise XOR |
< <= >= > | 2 | L | Y | Ordering |
== === != =~ !~ <=> | 2 | N | Y | Equality, pattern matching, comparison |
&& | 2 | L | N | Boolean AND |
2 pipes | 2 | L | N | Boolean OR |
.. ... | 2 | N | N | Range creation and Boolean flip-flops |
?: | 3 | R | N | Conditional |
rescue | 2 | L | N | Exception-handling modifier |
= **= *= / = %= += -= <<= >>= &&= &= ^= | 2 | R | N | Assignment |
defined? | 1 | N | N | Test variable definition and type |
not | 1 | R | N | Boolean NOT (low precedence) |
and or | 2 | L | N | Boolean AND, Boolean OR (low precedence) |
if unless while until | 2 | N | N | Conditional and loop modifiers |
Seed7
Seed7 supports user defined operators with priority and associativity.
This includes user defined operator symbols. Priority and associativity are defined with
the Seed7 Structured Syntax Description (S7SSD)
A S7SSD statement like
<lang seed7>$ syntax expr: .(). + .() is -> 7;</lang>
specifies the syntax of the +
operator.
The right arrow ->
describes the associativity:
Binding of operands from left to right. With 7
the priority
of the +
operator is defined. The syntax pattern
<lang seed7>.(). + .()</lang>
is introduced and delimited with dots (.
). Without dots the pattern is
() + ()
The symbol ()
is a nonterminal symbol and +
is a terminal symbol.
The S7SSD does not distinguish between different nonterminal symbols.
Instead it only knows one nonterminal symbol: ()
.
The include file syntax.s7i contains the syntax of the predefined operators. The table below is extracted from syntax.s7i:
Priority | infix/prefix | Left associative | Right associative | Not associative |
---|---|---|---|---|
1 | infix | conv varConv cast value parse | ||
1 | prefix | { | getfunc getobj [ | |
2 | infix | . [ ^ -> | ||
3 | prefix | & | ||
4 | infix | ! | ** | |
4 | prefix | ! | ||
5 | prefix | + - conj | ||
6 | infix | * / div rem mdiv mod | ||
7 | infix | + - | ||
8 | infix | mult find | times | |
9 | infix | << >> | ||
10 | infix | & | ||
11 | infix | |||
12 | infix | = <> < > <= >= in not in | ||
13 | prefix | new sub | not subtype subrange set array hash | |
14 | infix | and | ||
15 | infix | or | ||
16 | infix | val radix RADIX digits sci | ||
17 | infix | exp lpad rpad lpad0 | ||
18 | infix | <& | ||
20 | infix | := +:= -:= *:= /:= <<:= >>:= &:= @:= |
Tcl
Tcl only supports operators within an expression context (such as the expr
command, which lists the operators with more detail):
Precedence | Operator | Description |
---|---|---|
highest | - + ~ ! | Unary minus, unary plus, bit-wise NOT, logical NOT. |
** | Exponentiation. Right-to-left associative. | |
* / % | Multiply, divide, remainder. | |
+ - | Add and subtract. | |
<< >> | Left and right shift. | |
< > <= >= | Boolean less, greater, less than or equal, and greater than or equal. | |
== != | Boolean equal and not equal. | |
eq ne | Boolean string equal and string not equal. | |
in ni | List containment and negated list containment. | |
& | Bit-wise AND. | |
^ | Bit-wise exclusive OR. | |
| | Bit-wise OR. Valid for integer operands only. | |
&& | Logical AND. Evaluates its second operand lazily. | |
|| | Logical OR. Evaluates its second operand lazily. | |
lowest | x ? y : z | If-then-else, as in C. Evaluates its second and third operands lazily. |
XPL0
All operations of equal precedence are evaluated (associate) left-to-right.
Precedence | Operator | Description |
---|---|---|
highest | () , | Grouping, comma separator (constructs) |
- + addr @ | Unary minus, unary plus, address of a variable | |
<< >> ->> | Left and right logical shifts, arithmetic shift right | |
* / | Multiply and divide | |
+ - | Add and subtract | |
= # < <= > >= | Comparisons: equal, not equal, less than, etc. | |
~ not | Bitwise NOT | |
& and | Bitwise AND | |
! or | xor | Bitwise OR and exclusive OR | |
lowest | if-then-else | If expression |
zkl
All operations of equal precedence are evaluated (associate) left-to-right. If shared with C it has the same precedence except for logical and/or, which have the same precedence.
Precedence | Operator | Description |
---|---|---|
highest | () .(dot) [], | Grouping/function call, .resolve, subscripting |
- not | Unary minus, logical not | |
* / % | Multiply, divide and modulo | |
+ - | Add and subtract | |
< <= > >= | Comparisons: equal, not equal, less than, etc. | |
== != | Comparisons: equal, not equal | |
and or | logical AND and logical OR | |
: | compose (nest expressions) | |
lowest | := = | Assignment |