Languages and Compilers introduction Languages and Compilers
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 2301380 Chapter 1 Introduction
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 2301380 Chapter 1 Introduction 25/04/60
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 2301380 Chapter 1 Introduction 25/04/60
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. 2301380 Chapter 1 Introduction 25/04/60
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. 2301380 Chapter 1 Introduction 25/04/60
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): {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} 2301380 Chapter 1 Introduction 25/04/60
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). 2301380 Chapter 1 Introduction 25/04/60
Axiomatic Semantics An inference rule for sequences For a sequence S1;S2: {P1} S1 {P2} {P2} S2 {P3} the inference rule is: 2301380 Chapter 1 Introduction 25/04/60
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) 2301380 Chapter 1 Introduction 25/04/60
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) 2301380 Chapter 1 Introduction 25/04/60
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. 2301380 Chapter 1 Introduction 25/04/60
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 2301380 Chapter 1 Introduction 25/04/60
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) * ... 2301380 Chapter 1 Introduction 25/04/60
Paradigms in Programming languages Imperative languages Object-oriented languages Logic programs Functional programs 2301380 Chapter 1 Introduction 25/04/60
Imperative Languages Programs specify steps of what to do. Major concepts are Variables Control flows Examples: FORTRAN Pascal C 2301380 Chapter 1 Introduction 25/04/60
Object-oriented Languages Major concepts are Objects Control flows Objects have Attributes Methods Inheritance 2301380 Chapter 1 Introduction 25/04/60
Logic Programs Programs are definitions of what “things” are. Running the program is to infer from the definitions what the answer are. 2301380 Chapter 1 Introduction 25/04/60
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). 2301380 Chapter 1 Introduction 25/04/60
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. 2301380 Chapter 1 Introduction 25/04/60
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 2301380 Chapter 1 Introduction 25/04/60
Example of Functional Program sort [] = [] sort (h : t) = sort [b | b<-t, b<=h] ++ h ++ sort[b | b <-t, b>h]. 2301380 Chapter 1 Introduction 25/04/60
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. 2301380 Chapter 1 Introduction 25/04/60
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 2301380 Chapter 1 Introduction 25/04/60
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 2301380 Chapter 1 Introduction 25/04/60