expressions ธนวัฒน์ แซ่เอียบ. historical remark fortran was one of the...

33
Expressions ธธธธธธธ ธธธ ธธธธธ

Upload: naomi-fox

Post on 18-Jan-2016

220 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Expressions ธนวัฒน์ แซ่เอียบ. Historical remark FORTRAN was one of the first high-level programming languages Developed at the end of the 50´s Evolved

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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