Download presentation
Presentation is loading. Please wait.
Published byNaomi Fox Modified over 9 years ago
1
Expressions ธนวัฒน์ แซ่เอียบ
2
Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved through multiple versions Still in wide-spread use today (in scientific applications) FORTRAN stands for FORmula TRANslater One of its most important contributions is that the programmer may write formulas (expressions) close to the way it is done in mathematics The programmer need not program formulas in terms of instructions written in an assembly language (LOAD, STORE, ADD, etc.) The compiler performs this translation
3
Levels of abstraction
4
Hardware architecture
5
Assembly program for an arithmetic expression
6
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
7
Parts of expressions
8
Types of expressions
9
Operations, operators, and functions An operation takes a sequence of arguments and returns a value The arity of an operation is the number of its arguments Unary operations: one argument Examples: fac, sin, cos Binary operations: two arguments Examples: +, *.... An operator is an operation which is denoted by one or more special characters Examples: +, *, <, <= A function is an operation which is denoted by an identifier Examples: fac, max
10
Application of functions
11
Application of operators
12
Composition and evaluation of expressions 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
13
Composition and evaluation of expressions 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
14
Controlling the evaluation of expressions 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
15
Controlling the evaluation of expressions 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
16
Adding parentheses to expressions We may add parentheses to expressions to make the evaluation order explicit The meaning (semantics) of expressions is not changed by this transformation if it conforms with priorities and left-to-right-evaluation Example: 17 – 4 – 5 * 9 + 32 * 3 =
17
Priorities of operators 17 + 4 * 3 < 18 * 5 and 17 + 4 = 18 + 3
18
Expression trees The structure of an expression may be represented by an expression tree
19
Expression trees
26
Recursive algorithm of the factorial
27
Example
28
Stacks
29
Using the evaluation stack
33
Literature Bernhard Westfechtel : RWTH Aachen University A.V. Aho, R. Sethi, J.D. Ullman: Compilers: Principles, Techniques, and Tools, Chapter 2, Addison-Wesley, 1986
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.