Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the...
Transcript of Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the...
![Page 1: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/1.jpg)
Expressions
ธนวั�ฒน� แซ่เอี ยบ
![Page 2: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/2.jpg)
Historical remark
FORTRAN - was one of the first high level programmi ng languages
• Developed at the end of the 50´s• Evolved through multiple versions• - Still in wide spread use today (in scientific applicat
ions) FORTRAN stands for FOR mula TRANslater
• One of its most important contributions is that the programmer may write formulas (expressions) clo
se to the way it is done in mathematics• The programmer need not program formulas in ter
ms of instructions written in an assembly language (LOAD, STORE, ADD, etc.)
• The compiler performs this translation
![Page 3: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/3.jpg)
Levels of abstraction
![Page 4: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/4.jpg)
Hardware architecture
![Page 5: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/5.jpg)
Assembly program for an arithmetic expression
![Page 6: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/6.jpg)
What is an expression?
Expression• A formula which is composed of operators and ope
rands (and other parts, e.g., parentheses) and who se 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
![Page 7: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/7.jpg)
Parts of expressions
![Page 8: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/8.jpg)
Types of expressions
![Page 9: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/9.jpg)
Operations, operators, and functions
An operation takes a sequence of arguments and ret urns 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 o r more special characters
Examples: +, *, <, <= A function is an operation which is denoted by an identifier
Examples: fac, max
![Page 10: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/10.jpg)
Application of functions
![Page 11: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/11.jpg)
Application of operators
![Page 12: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/12.jpg)
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
![Page 13: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/13.jpg)
Composition and evaluation of expressions
An expression is evaluated as follows:• For an identifier denoting a variable: take the valu
e 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
![Page 14: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/14.jpg)
Controlling the evaluation of expressions
Priorities• Operators of higher priority are applied before ope
rators 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 l eft to right
• Example:
18 7 5 18 7 5 11 5 6– – = ( – ) – = – =≠
18 7 5 18 2 16– ( – ) = – =
![Page 15: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/15.jpg)
Controlling the evaluation of expressions
Parentheses• An expression enclosed in parentheses is evaluate
d before being applied as an argument
• - - -In combination with priorities and left to right eval uation, parentheses are
only needed where the standard evaluation order is not desired
• Example: 18 7 5 18 2 16– ( – ) = – =
![Page 16: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/16.jpg)
Adding parentheses to expressions
• We may add parentheses to expressions to make t he evaluation order explicit
• The meaning (semantics) of expressions is not cha nged by this transformation if it conforms with prio - - -rities and left to right evaluation
• Example: 17 4 5*9 32*3– – + =
![Page 17: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/17.jpg)
Priorities of operators
• 17 4*3 18*5+ < and 1 1 17418
1 3
![Page 18: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/18.jpg)
Expression trees
• The structure of an expression may be represente d by an expression tree
![Page 19: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/19.jpg)
Expression trees
![Page 20: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/20.jpg)
Expression trees
![Page 21: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/21.jpg)
Expression trees
![Page 22: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/22.jpg)
Expression trees
![Page 23: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/23.jpg)
Expression trees
![Page 24: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/24.jpg)
Expression trees
![Page 25: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/25.jpg)
Expression trees
![Page 26: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/26.jpg)
Recursive algorithm of the factorial
![Page 27: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/27.jpg)
Example
![Page 28: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/28.jpg)
Stacks
![Page 29: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/29.jpg)
Using the evaluation stack
![Page 30: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/30.jpg)
Using the evaluation stack
![Page 31: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/31.jpg)
Using the evaluation stack
![Page 32: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/32.jpg)
Using the evaluation stack
![Page 33: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved.](https://reader035.fdocuments.in/reader035/viewer/2022062315/5697bfeb1a28abf838cb7cb5/html5/thumbnails/33.jpg)
Literature
• Bernhard Westfechtel : RWTH Aachen University• A.V. Aho, R. Sethi, J.D. Ullman: Compilers: Principles, Techniq
- ues, and Tools, Chapter 2, Addison Wesley, 1986