The structure of an expression
Expressions are combinations of variable references, function calls, and
constant values. They are combined with operators such as
"+", "||", and "!".
Operators come in three basic classes: unary, binary, and tertiary.
Unary operators perform an operation on one operand, binaries on two, and
tertiaries on three.
Let's look at each in turn...
- Unary:
A typical unary operator is the C logical-NOT operator, "!", which
takes the logical-NOT of its operand. The C-S tree for this looks like:
-------
| |
| ! |--->NULL
| |
-------
|
|
\|/
---*---
| |
| opr |--->NULL
| |
-------
- Binary:
A typical binary operator is the C addition operator, "+", which
adds its two operands based on type. The C-S tree for this looks like:
-------
| |
| + |--->NULL
| |
-------
|
|
\|/
---*---- --------
| | | |
| opr1 |------->| opr2 |
| | | |
-------- --------
- Trinary:
A typical trinary operator is the C trinary operator, "?:", which
evaluates its first operand, then evaluates the second operand if the first
was non-zero, or the third operand if the first was zero. The C-S tree for
this looks like:
-------
| |
| + |--->NULL
| |
-------
|
|
\|/
---*---- -------- --------
| | | | | |
| opr1 |------->| opr2 |------->| opr3 |
| | | | | |
-------- -------- --------
Note that, in general, each of these operands may actually be an arbitrarily
large tree, which must be evaluated down to a single value before being used by
the operation.
This page was last modified
.