1. EXPRESSIONS
An expression is a sequence of operands and operators that reduces to a single
value. Expressions can be simple or complex. An operator is a syntactical token
that requires an action to be taken. An operand is an object on which an
operation is performed; it receives an operator's action.
A simple expression contains only one operator.
For example 2+5 is a simple expression whose value is 7; similarly, -a is a
simple expression.
A complex expression contains more than one operator.
An example of a complex expression is 2 + 5 * 7. To evaluate a complex
expression, we reduce it to a series of simple expressions.
We first evaluate the simple expression (5 * 7) and then the expression 2 + 35,
giving a result of 37.
Every language has operators whose actions are clearly specified in the
language syntax. The order in which the operators in a complex expression are
evaluated is determined by a set of priorities known as precedence; the
higher the precedence, the earlier the expression containing the operator is
evaluated.
2.
3. If two operators with same precedence occur in a complex expression, another
attribute of an operator, its associativity, takes control.
Associativity
is the parsing direction used to evaluate an expression. It can be either left-to-
right or right-to-left. When two operators with the same precedence occur in an
expression and their associativity is left-to-right, the left operator is evaluated
first.
For example, in the expression 3 * 4 / 6, there are two operators, multiplication
and division, with the same precedence and left-to-right associativity. Therefore,
the multiplication is evaluated before the division.
An expression always reduces to a single value.
6*2/( 2+1 * 2/3 +6) +8 * (8/4)
Example:
4. Infix, prefix and postfix are three different but equivalent notations of writing
algebraic expressions.
a. INFIX NOTATION
The traditional method of writing mathematical expressions is called as
the infix expressions.
It is of the form <operand><operator><operand>.
As the name suggests, here the operator is fixed inside between the
operands. e.g. A+B here the plus operator is placed inside between the
two operators, (A*B)/Q.
Such expression are easy to understand and evaluate for human beings.
However computer finds it difficult to parse - Information is needed about
operator precedence and associativity rules, and brackets which override
these rules.
Hence we have postfix and prefix notations which make the computer take
less effort to solve the problem.
5. b. POSTFIX NOTATION
The postfix expression as the name suggests has the operator placed right
after the two operands.
It is of the form <operand><operand><operator>
In the infix expressions, it is difficult to keep track of the operator
precedence whereas here the postfix expression itself determines the
precedence of operators (which is done by the placement of operators)
i.e. the operator which occurs first operates on the operand.
E.g. PQ-C/, here – operation is done on P and Q and then / is applied on C
and the previous result.
A postfix expression is parenthesis-free expression. For evaluation, we
evaluate it from left-to-right.
Infix expression Postfix expression
(P+Q)*(M-N) PQ+MN-*
(P+Q) / (M-N) - (A*B) PQ+MN-/AB*-
6. c. PREFIX NOTATION
The prefix expression as the name suggests has the operator placed before
the operand is specified.
It is of the form < operator > < operand > < operand >.
It works entirely in same manner as the postfix expression.
While evaluating a prefix expression, the operators are applied to the
operands immediately on the right of the operator.
For evaluation, we evaluate it from left-to-right. Prefix expressions are also
called as polish notation.
Infix expression Postfix expression
(P+Q)*(M-N) *+PQ-MN
(P+Q) / (M-N) - (A*B) -/+PQ-MN*AB
7. A + B * C + D
(A + B) * (C + D)
A * B + C * D
A + B + C + D