Presentation is loading. Please wait.

Presentation is loading. Please wait.

Languages and Compilers

Similar presentations


Presentation on theme: "Languages and Compilers"— Presentation transcript:

1 Languages and Compilers
introduction Languages and Compilers

2 Outline Syntax Semantics Imperative languages
Programming languages Syntax Semantics Paradigms in programming languages Imperative languages Object-oriented languages Logic programs Functional programs Language processors Compilers Linkers Loaders 25/04/60 Chapter 1 Introduction

3 Programming Languages
Syntax Define the form or structure of the language Use context-free grammar Semantics Define the meanings of the language. For programmers and language processors. For checking program correctness Pragmatics Usage Chapter 1 Introduction 25/04/60

4 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. sentence Subject part predicate Chapter 1 Introduction 25/04/60

5 Semantics Operational semantics Axiomatic 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. Chapter 1 Introduction 25/04/60

6 Operational Semantics
Describe the meaning by the sequence of operations executing for programs. Example: A=B+C; move r1, mem[addr B] add r3, r1, r2 move 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. Chapter 1 Introduction 25/04/60

7 Axiomatic Semantics 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): {QxE} 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} Chapter 1 Introduction 25/04/60

8 Axiomatic Semantics 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). Chapter 1 Introduction 25/04/60

9 Axiomatic Semantics An inference rule for sequences
For a sequence S1;S2: {P1} S1 {P2} {P2} S2 {P3} the inference rule is: Chapter 1 Introduction 25/04/60

10 Axiomatic Semantics An inference rule for logical pretest loops
For the loop construct: {P} while B do S end {Q} the inference rule is: where I is the loop invariant (the inductive hypothesis) Chapter 1 Introduction 25/04/60

11 Characteristics of the loop invariant
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) Chapter 1 Introduction 25/04/60

12 Denotational Semantics
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. Chapter 1 Introduction 25/04/60

13 Denotational Semantics
<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 Chapter 1 Introduction 25/04/60

14 Denotational Semantics
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) 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) * ... Chapter 1 Introduction 25/04/60

15 Paradigms in Programming languages
Imperative languages Object-oriented languages Logic programs Functional programs Chapter 1 Introduction 25/04/60

16 Imperative Languages Programs specify steps of what to do.
Major concepts are Variables Control flows Examples: FORTRAN Pascal C Chapter 1 Introduction 25/04/60

17 Object-oriented Languages
Major concepts are Objects Control flows Objects have Attributes Methods Inheritance Chapter 1 Introduction 25/04/60

18 Logic Programs Programs are definitions of what “things” are.
Running the program is to infer from the definitions what the answer are. Chapter 1 Introduction 25/04/60

19 Example of Logic Programs
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). Chapter 1 Introduction 25/04/60

20 Functional Languages 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. Chapter 1 Introduction 25/04/60

21 Functional Languages 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 Chapter 1 Introduction 25/04/60

22 Example of Functional Program
sort [] = [] sort (h : t) = sort [b | b<-t, b<=h] ++ h ++ sort[b | b <-t, b>h]. Chapter 1 Introduction 25/04/60

23 Language Processors Preprocesser Compiler Linker Loader
Find macros or directives and add parts of code for the directives. Translate high-level language programs into object code. Combine object codes and resolve address reference. But this code is not tied to real address. Find real address. Chapter 1 Introduction 25/04/60

24 Phases of Compilers 2301380 Chapter 1 Introduction 25/04/60
Error handler Symbol & literal tables Scanner Parser Semantic analyzer Intermediate code generator Code generator Code optimizer Chapter 1 Introduction 25/04/60

25 intermediate code generator
Compiler Process scanner parser semantic analyzer intermediate code generator code generator Source code Sequence of tokens Parse tree Annotated tree Intermediate code Target code Chapter 1 Introduction 25/04/60


Download ppt "Languages and Compilers"

Similar presentations


Ads by Google