Introduction to Programming Languages and Compilers

Slides:



Advertisements
Similar presentations
Prof Fateman CS 164 Lecture 371 Review: Programming Languages and Compilers CS AM MWF 10 Evans.
Advertisements

UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics Fall 2005.
1 Introduction to Compilation Cheng-Chia Chen. 2 What is a compiler? l a program that translates an executable program in one language into an executable.
Reasons to study concepts of PL
Programming Languages Structure
Prof. Hilfinger CS 164 Lecture 11 Introduction to Programming Languages and Compilers CS164 11:00-12:00 MWF 10 Evans Notes by G. Necula, with additions.
Introduction and Syntax. Course objectives Discuss features of programming languages. Discuss how the features are implemented in a simple computer architecture.
Summer 02-03Programming Language Concepts1 Programming Language Concepts (CS 360) Lecture 1: Overview, Grammars, and Little Languages Jeremy R. Johnson.
CS 104 Introduction to Computer Science and Graphics Problems Software and Programming Language (2) Programming Languages 09/26/2008 Yang Song (Prepared.
CS 415: Programming Languages Chapter 1 Aaron Bloomfield Fall 2005.
1 How are Languages Implemented? Two major strategies: –Interpreters (older, less studied) –Compilers (newer, more extensively studied) Interpreters run.
(1.1) COEN 171 Programming Languages Winter 2000 Ron Danielson.
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
Chapter 1. Introduction.
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.
Software Overview. Why review software? Software is the set of instructions that tells hardware what to do The reason for hardware is to execute a program.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
Copyright © 2007 Addison-Wesley. All rights reserved.1-1 Reasons for Studying Concepts of Programming Languages Increased ability to express ideas Improved.
1.  10% Assignments/ class participation  10% Pop Quizzes  05% Attendance  25% Mid Term  50% Final Term 2.
Compilation in More Detail Asst. Prof. Dr. Ahmet Sayar Spring-2012 Kocaeli University Computer Engineering Department Principles of Programming Languages.
CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.
Programming Languages
Prof. Necula CS 164 Lecture 11 Introduction to Programming Languages and Compilers CS164 11:00-12:30 TT 10 Evans.
UPRM ICOM 4029 (Adapted from: Prof. Necula UCB CS 164) 1 Introduction to Programming Languages and Compilers CS164 11:00-12:30 TT 10 Evans.
How to execute Program structure Variables name, keywords, binding, scope, lifetime Data types – type system – primitives, strings, arrays, hashes – pointers/references.
Programming Language Concepts (CIS 635) Elsa L Gunter 4303 GITC NJIT,
History. Development Driven by Function Functions of a Programming Language –To describe computation for use by computers –To describe computation and.
Chapter 1. Introduction.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
The language focusses on ease of use
Lecture 1b- Introduction
Component 1.6.
Introduction Chapter : Introduction.
Zuse’s Plankalkül – 1945 Never implemented Problems Zuse Solved
Concepts of Programming Languages
Basic 1960s It was designed to emphasize ease of use. Became widespread on microcomputers It is relatively simple. Will make it easier for people with.
Concepts of Programming Languages
Why study programming languages?
Chapter 1 Introduction.
Computer Programming (BCT 1113)
CS 326 Programming Languages, Concepts and Implementation
PROGRAMMING LANGUAGES
Problem Solving Using C: Orientation & Lecture 1
CS 363 – Chapter 1 What is a programming language? Kinds of languages
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
1.1 Reasons to study concepts of PLs
History of compiler development
Compiler Lecture 1 CS510.
CS 3304 Comparative Languages Fall 2011
Course supervisor: Lubna Siddiqui
Compiler Construction
Compiler 薛智文 TH 6 7 8, DTH Spring.
Problem Solving.
Compilers CSCI/CMPE 3334 David Egle.
Problem Solving Using C: Orientation & Lecture 1
Ada – 1983 History’s largest design effort
Lecture 15 (Notes by P. N. Hilfinger and R. Bodik)
Chapter 1 Preliminary. Chapter 1 Preliminary 1.1 Reasons for Studying Concepts of Programming Languages Increased capacity to express ideas Improved.
COP4020 Programming Languages
CSCE 330 Programming Language Structures Ch.2: Syntax and Semantics
Compiler 薛智文 TH 6 7 8, DTH Spring.
Problem Solving Using C: Orientation & Lecture 1
Introduction to Computer Programming
Von Neumann Architecture
Principles of Programming Languages
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Programming Languages, Preliminaries, History & Evolution
Reasons To Study Programming Languages
Compiler 薛智文 M 2 3 4, DTH Spring.
Presentation transcript:

Introduction to Programming Languages and Compilers CS164 11:00-12:00 MWF 306 Soda Notes by G. Necula, with additions by P. Hilfinger Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 Administrivia Course home page: http://www-inst.eecs.berkeley.edu/~cs164 Concurrent enrollment: see me after class Pick up class accounts at the end of lecture today. Pick a partner Must move Tu 11:00 section. Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 Course Structure Course has theoretical and practical aspects: analysis and translation of programming languages uses both. Regular homework = theory, should be individual work. Programming assignments = practice, in teams All submissions will be electronic Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 Academic Honesty Don’t use work from uncited sources Including old code We use plagiarism detection software 6 cases in last few semesters PLAGIARISM Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 The Course Project Course has hidden agenda: programming design and experience. Substantial project in parts. Provides example of how complicated problem might be approached. Validation (testing) is part of the project. Also a chance to introduce important tool: version control, which we’ll use to monitor your progress General rule: start early! Prof. Hilfinger CS 164 Lecture 1

How are Languages Implemented? Two major strategies: Interpreters (older, less studied) Compilers (newer, more extensively studied) Interpreters run programs “as is” Little or no preprocessing Compilers do extensive preprocessing Most implementations use compilers New trend is hybrid: “Just-In-Time” compilation, interpretation+compilation Prof. Hilfinger CS 164 Lecture 1

(Short) History of High-Level Languages Initially, programs “hard-wired” or entered electro-mechanically: Analytical Engine, Jacquard Loom, ENIAC, punched-card-handling machines Programs encoded as numbers (machine language) stored as data: Manchester Mark I, EDSAC. 1953 IBM develops the 701 All programming done in assembly Problem: Software costs exceeded hardware costs! John Backus: “Speedcoding” An interpreter Ran 10-20 times slower than hand-written assembly Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 FORTRAN I 1954 IBM develops the 704 John Backus Idea: translate high-level code to assembly Many thought this impossible 1954-7 FORTRAN I project By 1958, >50% of all software is in FORTRAN Cut development time dramatically (2 wks to 2 hrs) Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 FORTRAN I The first compiler Produced code almost as good as hand-written Huge impact on computer science Led to an enormous body of theoretical work Modern compilers preserve the outlines of FORTRAN I Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 After FORTRAN Lisp, late 1950s: dynamic, symbolic data structures. Algol 60: Europe’s answer to FORTRAN: modern syntax, block structure, explicit declaration. Set standard for language description. Dijkstra: “A marked improvement on its successors.” COBOL: late 1950’s. Business-oriented. Records. Prof. Hilfinger CS 164 Lecture 1

The 60s Language Explosion APL (arrays), SNOBOL (strings), FORMAC (formulae), and many more. 1967-68: Simula 67, first “object-oriented” language. Algol 68: Combines FORTRANish numerical constructs, COBOLish records, pointers, all described in rigorous formalism. Remnants remain in C, but Algol68 deemed too complex. 1968: “Software Crisis” announced. Trend towards simpler languages: Algol W, Pascal, C Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 The 1970s Emphasis on “methodology”: modular programming, CLU, Modula family. Mid 1970’s: Prolog. Declarative logic programming. Mid 1970’s: ML (Metalanguage) type inference, pattern-driven programming. Late 1970’s: DoD starts to develop Ada to consolidate >500 languages. Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 And on into the present Complexity increases with C++. Then decreases with Java. Then increases again (C#). Proliferation of little or specialized languages and scripting languages: HTML, PHP, Perl, Python, Ruby, … Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 Problems to address How to describe language clearly for programmers, precisely for implementors? How to implement description, and know you’re right? Automatic conversion of description to implementation Testing How to save implementation effort? Multiple languages to multiple targets: can we re-use effort? How to make languages usable? Handle errors reasonably Detect questionable constructs Compile quickly Prof. Hilfinger CS 164 Lecture 1

The Structure of a Compiler Lexical Analysis Parsing Semantic Analysis Optimization Code Generation The first 3, at least, can be understood by analogy to how humans comprehend English. Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 Lexical Analysis First step: recognize letters and words. Words are smallest unit above letters This is a sentence. Note the Capital “T” (start of sentence symbol) Blank “ “ (word separator) Period “.” (end of sentence symbol) Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 More Lexical Analysis Lexical analysis is not trivial. Consider: ist his ase nte nce Plus, programming languages are typically more cryptic than English: *p->f+=-.12345e-5 Prof. Hilfinger CS 164 Lecture 1

And More Lexical Analysis Lexical analyzer divides program text into “words” or “tokens” if x == y then z = 1; else z = 2; Tokens: if, x, ==, y, then, z, =, 1, ;, else, z, =, 2, ; Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 Parsing Once words are understood, the next step is to understand sentence structure Parsing = Diagramming Sentences The diagram is a tree Prof. Hilfinger CS 164 Lecture 1

Diagramming a Sentence This line is a longer sentence verb article noun adjective subject object sentence Prof. Hilfinger CS 164 Lecture 1

Parsing program expressions is the same Consider: Parsing Programs Parsing program expressions is the same Consider: If x == y then z = 1; else z = 2; Diagrammed: if-then-else x y z 1 2 == assign relation predicate else-stmt then-stmt Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 Semantic Analysis Once sentence structure is understood, we can try to understand “meaning” But meaning is too hard for compilers Compilers perform limited analysis to catch inconsistencies Some do more analysis to improve the performance of the program Prof. Hilfinger CS 164 Lecture 1

Semantic Analysis in English Example: Jack said Jerry left his assignment at home. What does “his” refer to? Jack or Jerry? Even worse: Jack said Jack left his assignment at home? How many Jacks are there? Which one left the assignment? Prof. Hilfinger CS 164 Lecture 1

Semantic Analysis in Programming Programming languages define strict rules to avoid such ambiguities This C++ code prints “4”; the inner definition is used { int Jack = 3; int Jack = 4; cout << Jack; } Prof. Hilfinger CS 164 Lecture 1

More Semantic Analysis Compilers perform many semantic checks besides variable bindings Example: Jack left her homework at home. A “type mismatch” between her and Jack; we know they are different people Presumably Jack is male Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 Optimization No strong counterpart in English, but akin to editing Automatically modify programs so that they Run faster Use less memory In general, conserve some resource Not an emphasis in the project. Prof. Hilfinger CS 164 Lecture 1

Optimization Example (in C) for (int i = 0; i < N; i += 1) { for (int j = 0; j < N; j += 1) { A[i][j] += B[i]*C[j] } for (i = 0; i < N; i += 1) { double tmp1 = B[i]; double* tmp2 = &A[i]; for (int j = 0; j < N; j +=1) tmp2[j] += tmp1*C[j]; } Prof. Hilfinger CS 164 Lecture 1

Optimization is tricky for (i = 0; i < N; i += 1) A[i] *= D/A[0]; is NOT tmp1 = D/A[0]; A[i] *= tmp1; Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 Code Generation Having understood the program, translate it into something useful (executable) Classical compilers produce machine code or assembly. Target: bare hardware. Java implementations, many “scripting languages” produce virtual machine code (“bytecode”). Target: interpreter or JIT (Just In Time) compiler. Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 Issues Compiling is almost this simple, but there are many pitfalls. Example: How are erroneous programs handled? Language design has big impact on compiler Determines what is easy and hard to compile Course theme: many trade-offs in language design Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 Compilers Today The overall structure of almost every compiler adheres to our outline The proportions have changed since FORTRAN Early: lexing, parsing most complex, expensive Today: optimization dominates all other phases, lexing and parsing are cheap Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 Trends in Compilation Optimization for speed is less interesting. But: scientific programs advanced processors (Digital Signal Processors, advanced speculative architectures) Small devices where speed = longer battery life Ideas from compilation used for improving code reliability: memory safety detecting concurrency errors (data races) ... Prof. Hilfinger CS 164 Lecture 1

Prof. Hilfinger CS 164 Lecture 1 Trends, contd. Parallelism and parallel architectures (esp., multicore: see Berkeley ParLab) Dynamic features: dynamic types, dynamic loading. Compilers More needed and more complex Driven by increasing gap between new languages new architectures Venerable and healthy area Prof. Hilfinger CS 164 Lecture 1

Why Study Languages and Compilers ? Increase capacity of expression Improve understanding of program behavior Increase ability to learn new languages Learn to build a large and reliable system See many basic CS concepts at work Prof. Hilfinger CS 164 Lecture 1