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

Post on 18-Jan-2016

220 Views

Category:

Documents

0 Downloads

Preview:

Click to see full reader

TRANSCRIPT

Expressions

ธนวั�ฒน� แซ่เอี ยบ

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

Levels of abstraction

Hardware architecture

Assembly program for an arithmetic expression

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

Parts of expressions

Types of expressions

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

Application of functions

Application of operators

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

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

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– ( – ) = – =

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– ( – ) = – =

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– – + =

Priorities of operators

• 17 4*3 18*5+ < and 1 1 17418

1 3

Expression trees

• The structure of an expression may be represente d by an expression tree

Expression trees

Expression trees

Expression trees

Expression trees

Expression trees

Expression trees

Expression trees

Recursive algorithm of the factorial

Example

Stacks

Using the evaluation stack

Using the evaluation stack

Using the evaluation stack

Using the evaluation stack

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

top related