Languages and Compilers

Slides:



Advertisements
Similar presentations
Semantics Static semantics Dynamic semantics attribute grammars
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #6 Lecture #6 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Introduction to Programming Languages Nai-Wei Lin Department of Computer Science and Information Engineering National Chung Cheng University.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Axiomatic Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Copyright © 2006 Addison-Wesley. All rights reserved. 3.5 Dynamic Semantics Meanings of expressions, statements, and program units Static semantics – type.
Predicate Transformers
Fall Semantics Juan Carlos Guzmán CS 3123 Programming Languages Concepts Southern Polytechnic State University.
CS 330 Programming Languages 09 / 19 / 2006 Instructor: Michael Eckmann.
1 Semantic Description of Programming languages. 2 Static versus Dynamic Semantics n Static Semantics represents legal forms of programs that cannot be.
1/22 Programs : Semantics and Verification Charngki PSWLAB Programs: Semantics and Verification Mordechai Ben-Ari Mathematical Logic for Computer.
CS 355 – Programming Languages
Describing Syntax and Semantics
Concepts of Programming Languages 1 Describing Syntax and Semantics Brahim Hnich Högskola I Gävle
Axiomatic Semantics Dr. M Al-Mulhem ICS
CS 330 Programming Languages 09 / 18 / 2007 Instructor: Michael Eckmann.
ISBN Chapter 3 Describing Syntax and Semantics.
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Axiomatic Semantics ICS 535.
Dec Formal Semantics1 Programming Language Theory Formal Semantics Leif Grönqvist The national Graduate School of Language Technology (GSLT) MSI.
CS 330 Programming Languages 09 / 16 / 2008 Instructor: Michael Eckmann.
Describing Syntax and Semantics
Dr. Muhammed Al-Mulhem 1ICS ICS 535 Design and Implementation of Programming Languages Part 1 Fundamentals (Chapter 4) Denotational Semantics ICS.
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Describing Syntax and Semantics
Chapter 1 Introduction Dr. Frank Lee. 1.1 Why Study Compiler? To write more efficient code in a high-level language To provide solid foundation in parsing.
Copyright © 1998 by Addison Wesley Longman, Inc. 1 Chapter 3 Syntax - the form or structure of the expressions, statements, and program units Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter Describing Syntax and Semantics. Chapter 3 Topics 1-2 Introduction The General Problem of Describing Syntax Formal Methods of Describing.
ISBN Chapter 3 Describing Syntax and Semantics.
ISBN Chapter 3 Describing Semantics -Attribute Grammars -Dynamic Semantics.
Describing Syntax and Semantics
CS 363 Comparative Programming Languages Semantics.
1 Programming Languages Fundamentals Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.
Muhammad Idrees Lecturer University of Lahore 1. Outline Introduction The General Problem of Describing Syntax Formal Methods of Describing Syntax Attribute.
3.2 Semantics. 2 Semantics Attribute Grammars The Meanings of Programs: Semantics Sebesta Chapter 3.
ISBN Chapter 3 Describing Semantics.
Chapter 3 Part II Describing Syntax and Semantics.
ISBN Chapter 3 Describing Syntax and Semantics.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved. 1-2 Chapter 3 Topics Introduction The General Problem.
Semantics In Text: Chapter 3.
Dr. Muhammed Al-MulhemICS (Denotational Semantics)
CCSB 314 Programming Language Department of Software Engineering College of IT Universiti Tenaga Nasional Chapter 3 Describing Syntax and Semantics.
1 / 48 Formal a Language Theory and Describing Semantics Principles of Programming Languages 4.
Semantics (1).
Chapter 3 © 2002 by Addison Wesley Longman, Inc Introduction - Who must use language definitions? 1. Other language designers 2. Implementors 3.
Principle of Programming Lanugages 3: Compilation of statements Statements in C Assertion Hoare logic Department of Information Science and Engineering.
Describing Syntax and Semantics Session 2 Course : T Programming Language Concept Year : February 2011.
Chapter 3 Describing Syntax and Semantics. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 3 Topics Introduction The General Problem.
ISBN Chapter 3 Describing Syntax and Semantics.
Cs7100(Prasad)L18-9WP1 Axiomatic Semantics Predicate Transformers.
Copyright © 2006 Addison-Wesley. All rights reserved.1-1 ICS 410: Programming Languages Chapter 3 : Describing Syntax and Semantics Denotational Semantics.
CSC3315 (Spring 2009)1 CSC 3315 Languages & Compilers Hamid Harroud School of Science and Engineering, Akhawayn University
C HAPTER 3 Describing Syntax and Semantics. D YNAMIC S EMANTICS Describing syntax is relatively simple There is no single widely acceptable notation or.
Dr. M. Al-Mulhem Denotational Semantics 1 Chapter 4 Fundamentals (Denotational Semantics)
Semantics(1). 2 Symantec(1)  To provide an authoritative definition of the meaning of all language constructs for: 1.Programmers 2.Compiler writers 3.Standards.
Advanced programming language theory. week 2. Attribute grammars and semantics.
Describing Syntax and Semantics
Syntax Questions 6. Define a left recursive grammar rule.
Describing Syntax and Semantics
Semantics In Text: Chapter 3.
Predicate Transformers
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Describing Syntax and Semantics
Chapter 3 (b) Semantics.
- Who must use language definitions?
Programming Languages and Compilers (CS 421)
Presentation transcript:

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): {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} 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