CS 363 – Chapter 1 What is a programming language? Kinds of languages

Slides:



Advertisements
Similar presentations
Copyright © 2005 Elsevier Imperative languages Group languages as –imperative von Neumann(Fortran, Pascal, Basic, C) object-oriented(Smalltalk, Eiffel,
Advertisements

Programming Languages Marjan Sirjani 2 2. Language Design Issues Design to Run efficiently : early languages Easy to write correctly : new languages.
CPSC Compiler Tutorial 9 Review of Compiler.
PLLab, NTHU Cs2403 Programming Languages Implementation Issues Cs2403 Programming Language Spring 2005 Kun-Yuan Hsieh.
The Analytical Engine Module 6 Program Translation.
CSCE 121, Sec 200, 507, 508 Fall 2010 Prof. Jennifer L. Welch.
Programming Languages Structure
CS 104 Introduction to Computer Science and Graphics Problems Software and Programming Language (2) Programming Languages 09/26/2008 Yang Song (Prepared.
1 Programming Languages Examples: C, Java, HTML, Haskell, Prolog, SAS Also known as formal languages Completely described and rigidly governed by formal.
(1.1) COEN 171 Programming Languages Winter 2000 Ron Danielson.
COP4020 Programming Languages
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.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Unit-1 Introduction Prepared by: Prof. Harish I Rathod
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CSC 8505 Compiler Construction
Objective of the course Understanding the fundamentals of the compilation technique Assist you in writing you own compiler (or any part of compiler)
Dr. Hussien Sharaf Dr Emad Nabil. Dr. Hussien M. Sharaf 2 position := initial + rate * Lexical analyzer 2. Syntax analyzer id 1 := id 2 + id 3 *
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
The language focusses on ease of use
Advanced Computer Systems
Component 1.6.
Compiler Design (40-414) Main Text Book:
Introduction Chapter : Introduction.
Zuse’s Plankalkül – 1945 Never implemented Problems Zuse Solved
Concepts of Programming Languages
Why study programming languages?
CS 3304 Comparative Languages
Introduction to Compiler Construction
Component 1.6.
CS 3304 Comparative Languages
CS 326 Programming Languages, Concepts and Implementation
PROGRAMMING LANGUAGES
CSCI-235 Micro-Computer Applications
Compiler Construction (CS-636)
Introduction.
Ch. 4 – Semantic Analysis Errors can arise in syntax, static semantics, dynamic semantics Some PL features are impossible or infeasible to specify in grammar.
课程名 编译原理 Compiling Techniques
History of compiler development
Compiler Lecture 1 CS510.
Compiler Construction
Chap. 6 :: Control Flow Michael L. Scott.
Course supervisor: Lubna Siddiqui
Compiler Construction
CSCE Fall 2013 Prof. Jennifer L. Welch.
Overview of Compilation The Compiler BACK End
CSCE 121: Simple Computer Model Spring 2015
Chap. 6 :: Control Flow Michael L. Scott.
CMP 131 Introduction to Computer Programming
CSCE Fall 2012 Prof. Jennifer L. Welch.
Principles of Programming Languages
Tonga Institute of Higher Education IT 141: Information Systems
Tonga Institute of Higher Education IT 141: Information Systems
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 10: Compilers and Language Translation
Introduction Chapter : Introduction.
Lecture 18 Compilers and Language Translation (S&G, ch. 9)
CS105 Introduction to Computer Concepts Intro to programming
Presentation transcript:

CS 363 – Chapter 1 What is a programming language? Kinds of languages Some history

Languages Carl Sagan once said that to make a recipe from scratch, __________ Programming is difficult, but the language should help us Human language vs. prog. language No one gets job for knowing 12 prog lang  What languages have you worked with? Language should help us; just like how a car helps us get to work.

Definition A programming language is a language intended for expressing a computer program, and any program can be expressed in it. So conceivably, a program can be written in any language, but it’s important to choose appropriately.

Kinds of languages (1) Most are “imperative” Program = seq of statements: begin & end Variables, memory locations a. Von Neuman (verb centered) * b. Object oriented (noun centered) (for short programs, we tend to think von Neumann)

Kinds of languages (2) 2. Most miscellaneous lang are “declarative” NOT a sequence of statements, no order Variables sometimes not used often interactive a. Logical: enter facts, make queries b. Functional: everything is function call ! With effort, can make a declarative language behave imperatively

Some history Originally, all coding was binary Possible even today Instruction consists of opcode & operands Assembly & “pseudocode” languages Replace opcode with word (add, sub, …) Operands are still numbers Ex. load r1, 16 Ex. add r1, r2, r3 Machine dependent  Note that many operations are opposites + - * / etc.

General-purpose languages Starting around 1957 Our own variable names Mathematical operators “x = y+z” instead of “add r1, r2, r3” Can focus on abstraction/algorithm more *** Needs a compiler to translate to machine code! Each language has its own story…

Some influential languages Year Language Features/purpose 1957 FORTRAN Fast, numerical applications, arrays, mistakes 1960 Algol Nested structures, recursion, tries to do everything 1970 Pascal Simple & efficient; pointers 1972 C economical syntax, lots of pointers 1980 Ada Exceptions, concurrency, packages (classes) We’ll finish chapter 1 tomorrow on compilation

Compilation Read sections 1.4 – 1.6 Most languages are compiled; some are interpreted Compile = translate Input to compiler is source code Output is usually machine code

Interpreting and compiling Phases of a compiler

Interpreting A few languages are interpreted After editing program, can immediately run. Each statement is interpreted one at a time as the program is run. Run-time is slower than compiled language. Every time you run program, everything has to be interpreted from scratch. Interpreter doesn’t know what to expect, such as waste. Examples: SPIM simulator, Web browser, Word Although HTML is not a programming language

Compiling Most languages use a compiler… Step 1: Compile = translate Input is source code Output is machine code or assembly code Step 2: The machine code is decoded by machine (very fast)

Some compiling features Note: Not all compilers have these features. 1. Pre-processor Substitutes constants Expands macros Supports conditional compilation 2. Assembler 3. Linker

C Pre-processing example #define MAX 100 #define SWAP(x,y) {int t=x;x=y;y=t;} … // sort ascending for (i = 0; i < MAX; ++i) for (j = i+1; j < MAX; ++j) if (a[i] > a[j]) SWAP(a[i], a[j]); // sort descending if (a[i] < a[j]) Conditional compilation: for (i = 0; i < MAX; ++i) for (j = i+1; j < MAX; ++j) #ifdef ASCENDING if (a[i] > a[j]) #else if (a[i] < a[j]) #endif SWAP(a[i], a[j]);

Writing a compiler Suppose we have new language – Sumatra 1. Write first compiler in existing language, Java. Now, we have a Sumatra compiler, so any Sumatra program can be translated into machine language. From now on, we don’t need Java anymore. 2. Write next Sumatra compiler in Sumatra. Compile it using the first compiler. Another strategy is bootstrapping (p.21) kind of ugly

Steps in Compilation Front end (machine-independent) Scan Parse Semantic analysis Optimization Back end (machine-dependent) Code generation Minor point: We can do some machine-independent optimizations

Scanning & Parsing Scan = “lexical analysis” Break up code into tokens Ignore whitespace & comments Lexical errors are rare Parse = create a parse tree rep’n (p.30-31) Using tokens, understand program’s structure Enforce grammatical rules of language Generates syntax error messages  Why a tree rep’n? Because program is hierarchical (nested) structure.

Semantic analysis Create symbol table Check to see if variables used correctly All variables declared (exactly once) Check types in expressions & statements Ex. Can’t use ++ on JFrame object Check function parameters If all’s well, create syntax tree (p.34) Notice that many tokens no longer needed There are actually more examples of semantic errors!

Static vs. dynamic semantics Compiler can only check static semantics We are NOT running the program Dynamic semantics = behavior at run-time Arithmetic overflow? Exception? These should be handled by program, or else OS or run-time system might intervene

Code generation From symbol table, allocate registers or memory locations Traverse syntax tree to generate instructions Optimize = make code more efficient Eliminate redundancy or waste Replace long operations with short ones It’s worth it ! Re-state the 5 different phases of the compiler (scan, parse, semantic analysis, code gen, opt)