1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring.

Slides:



Advertisements
Similar presentations
C++ Introduction.
Advertisements

Introduction to Compiler Construction
Chapter 8 ICS 412. Code Generation Final phase of a compiler construction. It generates executable code for a target machine. A compiler may instead generate.
CS 338 Project: Phase 4. Grammar Start  stmt eof Stmt  id = expr | if (expr) then stmt | while (expr) do stmt | begin CS end CS  stmt ; CS |   ote:
Copyright © 2005 Elsevier Imperative languages Group languages as –imperative von Neumann(Fortran, Pascal, Basic, C) object-oriented(Smalltalk, Eiffel,
Introduction to Programming Lecture 2. Today’s Lecture Software Categories Software Categories System Software System Software Application Software Application.
The Functions and Purposes of Translators Code Generation (Intermediate Code, Optimisation, Final Code), Linkers & Loaders.
Systems Software.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 3: Lexical Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 33: Code Generation and Linking COMP 144 Programming Language Concepts Spring 2002.
Software Language Levels Machine Language (Binary) Assembly Language –Assembler converts Assembly into machine High Level Languages (C, Perl, Shell)
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 31: Building a Runnable Program COMP 144 Programming Language Concepts Spring 2002.
Three types of computer languages
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 5: Syntax Analysis COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos.
COP4020 Programming Languages
Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source.
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
ICS611 Introduction to Compilers Set 1. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
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.
Parser-Driven Games Tool programming © Allan C. Milne Abertay University v
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
Introduction to Compiler Construction Robert van Engelen COP5621 Compiler Construction Copyright Robert.
Java Programming Introduction & Concepts. Introduction to Java Developed at Sun Microsystems by James Gosling in 1991 Object Oriented Free Compiled and.
CSC 338: Compiler design and implementation
CISC 471 First Exam Review Game Questions. Overview 1 Draw the standard phases of a compiler for compiling a high level language to machine code, showing.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
Introduction to C++ Programming Language
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
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.
Dr. Mohamed Ben Othman 1 Project phase 3 1)Rewrite the code of the project in the slides, run it: 1)change it so the input should be from file with extension.exp.
CHAPTER 1 INTRODUCTION 1 st Semester H King Saud University College Of Applied Studies and Community Services CSC 1101 Computer Programming-1.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
Introduction to Compilers. Related Area Programming languages Machine architecture Language theory Algorithms Data structures Operating systems Software.
Overview of Previous Lesson(s) Over View  A program must be translated into a form in which it can be executed by a computer.  The software systems.
Compilers: Overview/1 1 Compiler Structures Objective – –what are the main features (structures) in a compiler? , Semester 1,
Chapter 1: Introduction 1 Compiler Designs and Constructions Chapter 1: Introduction Objectives: Course Objectives Introduction Dr. Mohsen Chitsaz.
Compilers I CNS History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language.
1 Compilation and Interpretation (Sections ) Compilation and Interpretation (Sections ) CSCI 431 Programming Languages Fall 2003 A modification.
The Development Process Compilation. Compilation - Dr. Craig A. Struble 2 Programming Process Problem Solving Phase We will spend significant time on.
Dr. Mohamed Ramadan Saady 314ALL CH1.1 Chapter 1: Introduction to Compiling.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
ICS312 Introduction to Compilers Set 23. What is a Compiler? A compiler is software (a program) that translates a high-level programming language to machine.
C Programming Lecture 3 : C Introduction 1 Lecture notes : courtesy of Woo Kyun and Chang Byung-Mo.
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
1 Types of Programming Language (1) Three types of programming languages 1.Machine languages Strings of numbers giving machine specific instructions Example:
Programming Language Theory 2014, 1 Chapter 1 :: Introduction Origin : Michael L. Scott School of Computer & Information Engineering,
Software Engineering Algorithms, Compilers, & Lifecycle.
Introduction To Software Development Environment.
Compiler principles Compiler Jakub Yaghob.
Intro to compilers Based on end of Ch. 1 and start of Ch. 2 of textbook, plus a few additional references.
PRINCIPLES OF COMPILER DESIGN
Introduction to Compiler Construction
Chapter 5- Assembling , Linking, and Executing Programs
CS 326 Programming Languages, Concepts and Implementation
Language Translation Compilation vs. interpretation.
Programming Language Hierarchy, Phases of a Java Program
Compiler Construction (CS-636)
Chapter 1: Introduction to Compiling (Cont.)
Introduction to Compiler Construction
and Executing Programs
Introduction to System Programming
Compilers B V Sai Aravind (11CS10008).
COP4020 Programming Languages
System Programming and administration
Introduction to Compiler Construction
Compiler Structures 1. Overview Objective
System Programming By Prof.Naveed Zishan.
Introduction to Compiler Construction
Presentation transcript:

1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 2: Compilation and Interpretation COMP 144 Programming Language Concepts Spring 2002 Felix Hernandez-Campos Jan 11 The University of North Carolina at Chapel Hill

2 COMP 144 Programming Language Concepts Felix Hernandez-Campos From Source Code to Executable Code program gcd(input, output); var i, j: integer; begin read(i, j); while i <> j do if i > j then i := i – j; else j := j – i; writeln(i)end. program gcd(input, output); var i, j: integer; begin read(i, j); while i <> j do if i > j then i := i – j; else j := j – i; writeln(i)end. Compilation

3 COMP 144 Programming Language Concepts Felix Hernandez-Campos Compilation and Interpretation A compiler is a program that translates high-level source programs into target programA compiler is a program that translates high-level source programs into target program An interpreter is a program that executes another programAn interpreter is a program that executes another program

4 COMP 144 Programming Language Concepts Felix Hernandez-Campos Mixing Compilation and Interpretation Fuzzy difference:Fuzzy difference: –A language is interpreted when the initial translation is simple –A language is compiled when the translation process is complicated

5 COMP 144 Programming Language Concepts Felix Hernandez-Campos Preprocessing MacrosMacros –#define –#define –#define FALSE 0 –#define max(A,B) ( (A) > (B) ? (A):(B))

6 COMP 144 Programming Language Concepts Felix Hernandez-Campos Linking Libraries of subroutinesLibraries of subroutines

7 COMP 144 Programming Language Concepts Felix Hernandez-Campos Portability Assembly language instead of machine languageAssembly language instead of machine language Intermediate source codeIntermediate source code

8 COMP 144 Programming Language Concepts Felix Hernandez-Campos Programming Environments Much more than compilers and interpretersMuch more than compilers and interpreters –Assemblers, debuggers, preprocessors and linkers –Editors –Pretty printers –Style Checkers –Version management –Profilers Integrated environmentsIntegrated environments –Beyond a simple bus error –Emacs

9 COMP 144 Programming Language Concepts Felix Hernandez-Campos Overview of Compilation program gcd(input, output); var i, j: integer; begin read(i, j); while i <> j do if i > j then i := i – j; else j := j – i; writeln(i)end. program gcd(input, output); var i, j: integer; begin read(i, j); while i <> j do if i > j then i := i – j; else j := j – i; writeln(i)end. Compilation

10 COMP 144 Programming Language Concepts Felix Hernandez-Campos Phases of Compilation

11 COMP 144 Programming Language Concepts Felix Hernandez-Campos Example From Scott’s class notesFrom Scott’s class notes Desk calculator languageDesk calculator language Example program:Example program: read A read A read B read B sum := A + B sum := A + B write sum write sum write sum / 2 write sum / 2

12 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lexical Analysis Tokens:Tokens: id = letter ( letter | digit ) * [ except "read" and "write" ] literal = digit digit * ":=", "+", "-", "*", "/", "(", ")“ $$$ [end of file]

13 COMP 144 Programming Language Concepts Felix Hernandez-Campos Syntax Analysis Grammar in EBNFGrammar in EBNF -> $$$ -> $$$ -> | E -> | E -> id := | read | write -> id := | read | write -> | -> | -> -> <factor -> ( ) | id | literal -> ( ) | id | literal -> + | - -> + | - -> * | / -> * | /

14 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation Intermediate code:Intermediate code: read read pop A pop A read read pop B pop B push A push A push B push B add add pop sum pop sum push sum push sum write write push sum push sum push 2 push 2 div div write write

15 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation Target code:Target code:.data.data A:.long 0 A:.long 0 B:.long 0 B:.long 0 sum:.long 0 sum:.long 0.text.text main: jsr read main: jsr read movl d0,d1 movl d0,d1 movl d1,A movl d1,A jsr read jsr read movl d0,d1 movl d0,d1 movl d1,B movl d1,B movl A,d1 movl A,d1

16 COMP 144 Programming Language Concepts Felix Hernandez-Campos Code Generation movl B,d2 movl B,d2 addl d1,d2 addl d1,d2 movl d1,sum movl d1,sum movl sum,d1 movl sum,d1 movl d1,d0 movl d1,d0 jsr write jsr write movl sum,d1 movl sum,d1 movl #2,d2 movl #2,d2 divsl d1,d2 divsl d1,d2 movl d1,d0 movl d1,d0 jsr write jsr write