25
INTRODUCTION Languages and Compilers

INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Embed Size (px)

Citation preview

Page 1: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

INTRODUCTION

Languages and Compilers

Page 2: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Outline

180423/ /

2

2301380 Chapter 1 Introduction

Page 3: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Programming Languages

180423/ /

3

2301380 Chapter 1 Introduction

Page 4: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Syntax

Describe correct forms of language constructs. In English, a sentence is composed of the

subject part and the verb part, as shown below.

In a programming language, the syntax describes what a program, a statement, an expression, etc. are composed of.

180423/ /

4

2301380 Chapter 1 Introduction

Page 5: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Semantics

Operational semantics Describe the meaning of a program by telling how

a computer executes the program. Based on the model of computers.

Axiomatic semantics Describe the meaning of a program by telling the

“condition” before and after the execution of the program.

Conditions are expressed in predicates or assertions.

Denotational semantics Meaning of a program is a function from a

language construct to a value.180423/ /

5

2301380 Chapter 1 Introduction

Page 6: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Operational Semantics

180423/ /2301380 Chapter 1 Introduction

6

Describe the meaning by the sequence of operations executing for programs.

Example: A=B+C; move r1, mem[addr B] add r3, r1, r2move r2, mem[addr C] load mem[addr A], r3

Operations are based on some machine. Good for programmers to understand

programs. Difficult to define in detail, due to machine

dependency.

Page 7: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Axiomatic Semantics

180423/ /2301380 Chapter 1 Introduction

7

Pre-post form: {P} statement {Q} An example: a = b + 1 {a > 1}

One possible precondition: {b > 10}

Weakest precondition: {b > 0} An axiom for assignment statements (x

= E): {QxE} x = E {Q}

Example: {y*y-9>0} x=y*y-9 {x>0}

{y>3 or y<-3} x=y*y-9 {x>0}

Page 8: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Axiomatic Semantics

180423/ /2301380 Chapter 1 Introduction

8

Inference rule:

{P} S {Q} and P’=> P and Q => Q’

Then, {P’} S {Q’}.

Example:

Let {y > 3 or y < -3} x=y*y-9 {x > 0}.

Then, {y < -3} x=y*y-9 {x> -9} (because y < -3 implies y>3 or y < -3 and x> 0 implies x>-9).

Page 9: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Axiomatic Semantics

180423/ /2301380 Chapter 1 Introduction

9

An inference rule for sequencesFor a sequence S1;S2:

{P1} S1 {P2}

{P2} S2 {P3}

the inference rule is:

Page 10: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Axiomatic Semantics

180423/ /2301380 Chapter 1 Introduction

1

0

An inference rule for logical pretest loopsFor the loop construct:

{P} while B do S end {Q} the inference rule is:

where I is the loop invariant (the inductive hypothesis)

Page 11: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Characteristics of the loop invariant

180423/ /2301380 Chapter 1 Introduction

1

1

Loop invariant I must meet the following conditions: P => I (the loop invariant must be true initially) {I} B {I}

Evaluation of Boolean must not change the validity of I

{I and B} S {I}

I is not changed by executing the body of the loop (I and (not B)) => Q

If I is true and B is false, Q is implied. The loop terminates (this can be difficult to

prove)

Page 12: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Denotational Semantics

180423/ /2301380 Chapter 1 Introduction

1

2

The state of a program is the values of all its current variables

s = {<i1, v1>, <i2, v2>, …, <in, vn>} Let VARMAP be a function that, when given

a variable name and a state, returns the current value of the variable

VARMAP(ij, s) = vj

The meaning of a program is a function mapping the program construct to the state of the program.

Page 13: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Denotational Semantics

180423/ /2301380 Chapter 1 Introduction

1

3

<dec_num> 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | <dec_num> (0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9)

Mdec('0') = 0,

Mdec ('1') = 1, …,

Mdec ('9') = 9

Mdec (<dec_num> '0') = 10 * Mdec (<dec_num>)

Mdec (<dec_num> '1’) = 10 * Mdec (<dec_num>) + 1…

Mdec (<dec_num> '9') = 10 * Mdec (<dec_num>) + 9

Page 14: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Denotational Semantics

180423/ /2301380 Chapter 1 Introduction

1

4

Me(<expr>, s) : case <expr> of <dec_num> => Mdec(<dec_num>, s) <var> => if VARMAP(<var>, s) == undef then error else VARMAP(<var>, s) <binary_expr> => if (Me(<binary>.<left_expr>, s)==undef OR Me(<binary_expr>.<right_expr>, s)==undef) then error

else if (<binary_expr>.<operator> == ‘+’) then Me(<binary_expr>.<left_expr>, s) + Me(<binary_expr>.<right_expr>, s) else Me(<binary_expr>.<left_expr>, s) * Me(<binary_expr>.<right_expr>, s)...

Page 15: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Paradigms in Programming languages

180423/ /

1

5

2301380 Chapter 1 Introduction

Page 16: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Imperative Languages

180423/ /2301380 Chapter 1 Introduction

1

6

Programs specify steps of what to do. Major concepts are

Variables Control flows

Examples: FORTRAN Pascal C

Page 17: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Object-oriented Languages

180423/ /2301380 Chapter 1 Introduction

1

7

Major concepts are Objects Control flows

Objects have Attributes Methods Inheritance

Page 18: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Logic Programs

180423/ /2301380 Chapter 1 Introduction

1

8

Programs are definitions of what “things” are.

Running the program is to infer from the definitions what the answer are.

Page 19: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Example of Logic Programs

180423/ /2301380 Chapter 1 Introduction

1

9

sort([],[]).sort(A,B) :- half(A,X,Y), sort(X,M), sort(Y,N),

merge(M,N,B).half([],[],[]).half([A], [A],[]).half([A,B|R], [A|R1], [B|R2]) :- half(R, R1, R2).merge(A, [], A).merge([], A, A).merge([X|A], [Y|B], [X|Z]) :- X<Y, merge(A, [Y|B], Z).merge([X|A], [Y|B], [Y|Z]) :- X>=Y, merge([X|A], B, Z).

Page 20: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Functional Languages

180423/ /2301380 Chapter 1 Introduction

2

0

A program is a collection of mathematical functions.

A function is a composition of functions. The execution of a program is the application

of a function (mostly) on some data. Functions produce results which are used by

other functions. There is no variable in pure functional

languages. No side effect.

Page 21: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Functional Languages

180423/ /2301380 Chapter 1 Introduction

2

1

Functions are a basic data type. Functions can be passed as parameters. Examples:

LISP: a functional language with list as a basic data type

Scheme: a variation of LISP ML Haskell

Page 22: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Example of Functional Program

180423/ /2301380 Chapter 1 Introduction

2

2

sort [] = []sort (h : t) = sort [b | b<-t, b<=h] ++ h ++ sort[b | b <-t, b>h].

Page 23: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Language Processors

180423/ /

2

3

2301380 Chapter 1 Introduction

Page 24: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Phases of Compilers

180423/ /2301380 Chapter 1 Introduction

2

4

Page 25: INTRODUCTION Languages and Compilers. Outline Syntax Semantics Programming languages Imperative languages Object-oriented languages Logic programs Functional

Compiler Process

180423/ /

2

5

2301380 Chapter 1 Introduction