Professional Documents
Culture Documents
Expressions
Bernhard Westfechtel
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 2
Fundamentals of Computer Engineering 2 Summer 2004
Historical remark
Bernhard Westfechtel 3
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 4
Fundamentals of Computer Engineering 2 Summer 2004
Hardware architecture
LOAD STORE
Accumulator
Memory
Arithmetic-Logical
Unit
(ADD, MULT ,etc.)
...
Bernhard Westfechtel 5
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 6
Fundamentals of Computer Engineering 2 Summer 2004
What is an expression?
Expression
» A formula which is composed of operators and operands (and other
parts, e.g., parentheses) and whose evaluation yields a value from a
certain domain (e.g., an integer value, a real value, etc.)
Examples
» 17 + 4
» a + b*2
» 14 – (5 – c)
» a+b=c–d
» 56 < 11
» (a or b) and d
Bernhard Westfechtel 7
Fundamentals of Computer Engineering 2 Summer 2004
Parts of expressions
n
Identifier Name of variable or function
fac
237
Constant value from a
Literal certain domain (e.g., integer 0.7
or real)
14.7E-4
Bernhard Westfechtel 8
Fundamentals of Computer Engineering 2 Summer 2004
Types of expressions
Expression whose
Arithmetic expression (a + b) * (c + 87)
evaluation yields a number
Bernhard Westfechtel 9
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 10
Fundamentals of Computer Engineering 2 Summer 2004
Application of functions
Comma-separated list of
Most wide-spread notation
arguments, enclosed in max(x, y)
for function applications
parentheses
Bernhard Westfechtel 11
Fundamentals of Computer Engineering 2 Summer 2004
Application of operators
Bernhard Westfechtel 12
Fundamentals of Computer Engineering 2 Summer 2004
An expression may be
» An identifier
» A literal
» A function applied to its arguments
» An operator applied to its arguments
An argument is a subexpression which is
» Smaller than its enclosing expression, but
» Obeys the same rules as its enclosing expression
Î Expressions are composed recursively
An expression is evaluated as follows:
» For an identifier denoting a variable: take the value of that variable
» For a literal: take the value denoted by that literal
» For a function applied to its arguments:
Ö Evaluate the arguments
Ö Apply the function to its arguments
» Likewise for an operator applied to its arguments
Bernhard Westfechtel 13
Fundamentals of Computer Engineering 2 Summer 2004
Priorities
» Operators of higher priority are applied before operators of lower priority
» Examples:
Ö Multiplication has a higher priority than addition
Ö Logical and has a higher priority than logical or
Left-to-right evaluation
» Operators of the same priority are evaluated from left to right
» Example:
Ö 18 – 7 – 5 = (18 – 7) – 5 = 11 – 5 = 6 ≠
18 – (7 – 5) = 18 – 2 = 16
Parentheses
» An expression enclosed in parentheses is evaluated before being
applied as an argument
» In combination with priorities and left-to-right-evaluation, parentheses are
only needed where the standard evaluation order is not desired
» Example:
Ö 18 – (7 – 5) = 18 – 2 = 16
Bernhard Westfechtel 14
Fundamentals of Computer Engineering 2 Summer 2004
13 45 96
-32
64
Bernhard Westfechtel 15
Fundamentals of Computer Engineering 2 Summer 2004
*, /
Arithmetic operators
+, –
not
or
Bernhard Westfechtel 16
Fundamentals of Computer Engineering 2 Summer 2004
Example
17 + 4 * 3 < 18 * 5 and 17 + 4 = 18 + 3 =
((17 + (4 * 3)) < (18 * 5)) and ((17 + 4) = (18 + 3)) =
((17 + 12) < 90) and (21 = 21) =
(29 < 90) and (21 = 21) =
true and true =
true
Bernhard Westfechtel 17
Fundamentals of Computer Engineering 2 Summer 2004
Expression trees
Bernhard Westfechtel 18
Fundamentals of Computer Engineering 2 Summer 2004
Example
Root Inner
Expression tree and node node
+ * + +
17 * 18 5 17 4 18 3
Leaf
4 3 node
Bernhard Westfechtel 19
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 20
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 21
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 22
Fundamentals of Computer Engineering 2 Summer 2004
Example (1)
and
< =
+ * + +
17 * 18 5 17 4 18 3
4 3
Bernhard Westfechtel 23
Fundamentals of Computer Engineering 2 Summer 2004
Example (2)
and
< =
+ * + +
17 12 18 5 17 4 18 3
Bernhard Westfechtel 24
Fundamentals of Computer Engineering 2 Summer 2004
Example (3)
and
< =
29 * + +
18 5 17 4 18 3
Bernhard Westfechtel 25
Fundamentals of Computer Engineering 2 Summer 2004
Example (4)
and
< =
29 90 + +
17 4 18 3
Bernhard Westfechtel 26
Fundamentals of Computer Engineering 2 Summer 2004
Example (5)
and
true =
+ +
17 4 18 3
Bernhard Westfechtel 27
Fundamentals of Computer Engineering 2 Summer 2004
Example (6)
and
true =
21 +
18 3
Bernhard Westfechtel 28
Fundamentals of Computer Engineering 2 Summer 2004
Example (7)
and
true =
21 21
and
true true
true
Bernhard Westfechtel 29
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 30
Fundamentals of Computer Engineering 2 Summer 2004
Example (1)
*
*
fac 6 fac
6 fac
6 -
5
6 1
*
*
* 6 *
6 *
6 * 5 *
5 fac
5 fac 4 fac
-
4 -
5 1
4 1
Bernhard Westfechtel 31
Fundamentals of Computer Engineering 2 Summer 2004
Example (2)
* 6 *
6 * 5 *
5 * 4 *
4 fac 3 fac
3 -
3 1
Bernhard Westfechtel 32
Fundamentals of Computer Engineering 2 Summer 2004
Example (3)
* 6 *
6 * 5 *
5 * 4 *
4 * 3 *
3 fac 2 fac
2 -
2 1
Bernhard Westfechtel 33
Fundamentals of Computer Engineering 2 Summer 2004
Example (4)
*
6 *
5 *
6 *
4 *
5 *
3 *
4 *
2 *
3 *
1 fac
2 fac
1 1
Bernhard Westfechtel 34
Fundamentals of Computer Engineering 2 Summer 2004
Example (5)
6 *
6 *
5 *
5 *
4 *
4 *
3 *
3 *
2 *
2 *
1 fac
1 1
Bernhard Westfechtel 35
Fundamentals of Computer Engineering 2 Summer 2004
Example (6)
6 *
6 *
5 *
5 *
4 *
4 *
3 *
3 2
2 1
Bernhard Westfechtel 36
Fundamentals of Computer Engineering 2 Summer 2004
Example (7)
6 *
6 *
5 *
5 24
4 6
720
6 120
Bernhard Westfechtel 37
Fundamentals of Computer Engineering 2 Summer 2004
and
< =
Expression
+ * + +
tree
17 * 18 5 17 4 18 3
4 3
Post-order linearization
Postfix
17 4 3 * + 18 5 * < 17 4 + 18 3 + = and
expression
Bernhard Westfechtel 38
Fundamentals of Computer Engineering 2 Summer 2004
Post-order linearization
Bernhard Westfechtel 39
Fundamentals of Computer Engineering 2 Summer 2004
12 90 21 21
29
true true
true
Notes
No priorities required
No parentheses required
Since the operands are placed before the operator, all operators may be
applied immediately
Bernhard Westfechtel 40
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 41
Fundamentals of Computer Engineering 2 Summer 2004
Bernhard Westfechtel 42
Fundamentals of Computer Engineering 2 Summer 2004
push(17)
17 4 3 * + 18 5 * < 17 4 + 18 3 + = and 17
push(4)
4
17 4 3 * + 18 5 * < 17 4 + 18 3 + = and 17
push(3)
Bernhard Westfechtel 43
Fundamentals of Computer Engineering 2 Summer 2004
12
17 4 3 * + 18 5 * < 17 4 + 18 3 + = and 17
Bernhard Westfechtel 44
Fundamentals of Computer Engineering 2 Summer 2004
17 4 3 * + 18 5 * < 17 4 + 18 3 + = and 29
push(18)
18
17 4 3 * + 18 5 * < 17 4 + 18 3 + = and 29
push(5)
5
18
17 4 3 * + 18 5 * < 17 4 + 18 3 + = and 29
second := top; pop;
first := top; pop;
push (first * second)
Bernhard Westfechtel 45
Fundamentals of Computer Engineering 2 Summer 2004
push(17)
17
17 4 3 * + 18 5 * < 17 4 + 18 3 + = and true
push(4)
Bernhard Westfechtel 46
Fundamentals of Computer Engineering 2 Summer 2004
push(18)
18
21
17 4 3 * + 18 5 * < 17 4 + 18 3 + = and true
push(3)
Bernhard Westfechtel 47
Fundamentals of Computer Engineering 2 Summer 2004
21
21
17 4 3 * + 18 5 * < 17 4 + 18 3 + = and true
Bernhard Westfechtel 48
Fundamentals of Computer Engineering 2 Summer 2004
true
17 4 3 * + 18 5 * < 17 4 + 18 3 + = and true
Bernhard Westfechtel 49
Fundamentals of Computer Engineering 2 Summer 2004
Literature
Bernhard Westfechtel 50