CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.

Slides:



Advertisements
Similar presentations
C O N T E X T - F R E E LANGUAGES ( use a grammar to describe a language) 1.
Advertisements

Copyright © 2005 Elsevier Imperative languages Group languages as –imperative von Neumann(Fortran, Pascal, Basic, C) object-oriented(Smalltalk, Eiffel,
CPSC Compiler Tutorial 9 Review of Compiler.
The Analytical Engine Module 6 Program Translation.
PZ02A - Language translation
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
Chapter 3 Program translation1 Chapt. 3 Language Translation Syntax and Semantics Translation phases Formal translation models.
COP4020 Programming Languages
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Chapter 17 Programming Tools The Architecture of Computer Hardware and Systems Software: An Information Technology Approach 3rd Edition, Irv Englander.
Invitation to Computer Science 5th Edition
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
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.
(1.1) COEN 171 Programming Languages Winter 2000 Ron Danielson.
CS 2104 Prog. Lang. Concepts Dr. Abhik Roychoudhury School of Computing Introduction.
Compiler1 Chapter V: Compiler Overview: r To study the design and operation of compiler for high-level programming languages. r Contents m Basic compiler.
COP4020 Programming Languages
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.
Chapter 10: Compilers and Language Translation Invitation to Computer Science, Java Version, Third Edition.
1 Programming Languages Tevfik Koşar Lecture - II January 19 th, 2006.
Compiler Construction1 COMP Compiler Construction Lecturer: Dr. Arthur Cater Teaching Assistant:
CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.
1 COMP 3438 – Part II-Lecture 1: Overview of Compiler Design Dr. Zili Shao Department of Computing The Hong Kong Polytechnic Univ.
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 Programming Languages Fundamentals Cao Hoaøng Truï Khoa Coâng Ngheä Thoâng Tin Ñaïi Hoïc Baùch Khoa TP. HCM.
CPS 506 Comparative Programming Languages Syntax Specification.
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
Topic #1: Introduction EE 456 – Compiling Techniques Prof. Carl Sable Fall 2003.
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.
Chapter 1 Introduction Study Goals: Master: the phases of a compiler Understand: what is a compiler Know: interpreter,compiler structure.
Introduction to Compiling
Introduction CPSC 388 Ellen Walker Hiram College.
1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
 Fall Chart 2  Translators and Compilers  Textbook o Programming Language Processors in Java, Authors: David A. Watts & Deryck F. Brown, 2000,
Introduction to Language Programming Hierarchy of programming lang. Based on machine independences: 1. Machine language 2. Assembly language 3. Higher.
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.
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.
Overview of Compilation Prepared by Manuel E. Bermúdez, Ph.D. Associate Professor University of Florida Programming Language Principles Lecture 2.
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 3.
COP4020 Programming Languages Introduction Prof. Robert van Engelen (modified by Prof. Em. Chris Lacher)
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
Programming Languages Concepts Chapter 1: Programming Languages Concepts Lecture # 4.
Chapter 1 Introduction.
CS 3304 Comparative Languages
CS 3304 Comparative Languages
CS 326 Programming Languages, Concepts and Implementation
CSCI-235 Micro-Computer Applications
CS 326 Programming Languages, Concepts and Implementation
Compiler Construction (CS-636)
Overview of Compilation The Compiler Front End
Overview of Compilation The Compiler Front End
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Chapter 1 Introduction.
Compiler Lecture 1 CS510.
COP4020 Programming Languages
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
High-Level Programming Language
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
Faculty of Computer Science and Information System
Presentation transcript:

CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2

22/69 A Simple Program Compute greatest common divisor of 2 integers: afbf0015 Machine language −Sequence of bits that directly controls the processor −Add, compare, move data, etc. −Computer’s time more valuable than programmers’ time

33/69 A Simple Program Assembly language −Mnemonic abbreviations, 1-to-1 correspondence to machine language −Translated by an assembler −Still machine-dependent

44/69 Compilation and Interpretation Need for: −Machine-independent language −Resemblance between language and mathematical computation High-level languages (the first: Fortran, Lisp) −Translated by a compiler or interpreter −No more 1-to-1 correspondence to machine language −Accepted when compilers could produce efficient code −Today: efficient compilers, large programs, high maintenance costs, rapid development requirements −Labor cost higher than hardware cost

55/69 Compilation and Interpretation Compiler translates into target language (machine language), then goes away The target program is the locus of control at execution time Interpreter stays around at execution time Implements a virtual machine whose machine language is the high-level language

66/69 Compilation and Interpretation Interpretation −Greater flexibility, better diagnostics −Allow program features (data types or sizes) to depend on the input −Lisp, Prolog: program can write new pieces of itself and execute them on the fly −Java: compilation, then interpretation Compilation −Better performance −Many decisions are made only once, at compile time, not at every run time −Fortran, C

77/69 Programming Environments Set of tools: −Editors −Pretty printers −Style checkers −Preprocessors −Debuggers −Linkers −Module management −Browsers (cross-references) −Profilers −Assemblers Separate programs: Unix Integrated environment: Microsoft Visual C++, Visual Works for Smalltalk

88/69 Overview of Compilation Front end Back end

99/69 Lexical Analysis (Scanner) Compute greatest common divisor, in Pascal: 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. The scanner extracts tokens (smallest meaningful units): programgcd(input,output); var i, j : integer;begin read (i,j);while …………….……………………………………………………………...……….

1010/69 Syntax Analysis (Parser) The parser uses a context-free grammar, as a description of the language syntax: program → PROGRAM identifier ( identifier more_identifiers ) ; block. block → labels constants types variables subroutines BEGIN statement more_statements END more_identifiers →, identifier more_identifiers more_identifiers → ε …………………….. Generates a parse tree, that reflects the structure of the program Shows how the sequence of tokens can be derived under the rules of the grammar

1111/69 Syntax Analysis (Parser) Parse tree:

1212/69 Semantic Analysis Discovers meaning in a program Static semantic analysis (at compile time): −Pointers dereferenced only when refer to a valid object −Array subscript expressions lie within bounds Simplifies the parse tree → syntax tree Maintains symbol table, attaches attributes −Identifiers declared before use −Subroutine calls provide correct number and type of arguments Dynamic semantics (cannot be checked at compile time, so generate code to check at run time):

1313/69 Semantic Analysis Syntax tree:

1414/69 Back end Target code generation −Generate assembly or machine language −Traverses the syntax tree to generate elementary operations: loads and stores, arithmetic operations, tests and branches Code improvement (optional) −A.k.a “optimization” −Transform program into a new version with same functionality, but more efficient (faster, uses less memory)

1515/69 Compilation Review Lexical analysis (scanner) −Break program into primitive components, called tokens (identifiers, numbers, keywords,...) −Formal models: regular grammars and finite automata Syntactic analysis (parser) −Create parse tree of the program −Formal models: context free grammars and pushdown automata Symbol table −Store information about declared objects (identifiers, procedure names,...)

1616/69 Compilation Review Semantic analysis −Understand relationships between tokens in the program Code improvement (machine independent) −Rewrite syntax tree to create a more efficient program Code generation −Convert parsed program into executable form in target (machine) language Code improvement (machine dependent) −Rewrite target code to create a more efficient program Will discuss scanning and parsing

1717/69 Formal Translation Models Formal language: a set of strings of symbols drawn from a finite alphabet Examples: −L 1 = { black, white } −L 2 = { 1, 01, 001, 0001,... } −L 3 = { ab, aabb, aaabbb,... } −L 4 = English

1818/69 Grammars Nonterminals: a finite set of symbols: − Terminals: a finite set of symbols: −the, boy, girl, ran, ate, cake Start symbol: one of the nonterminals: −

1919/69 Grammars Rules (productions): A finite set of replacement rules: → → ran | ate → the → boy | girl | cake

2020/69 Grammars Replacement operator (written =>): Replace a nonterminal by the right hand side of a rule Derivation: => First rule => Second rule => the Fifth rule... => the boy ate the cake Can also derive: =>... => the cake ate the boy Syntax does not imply correct semantics

2121/69 Grammars Concepts: Derivation: series of replacements to obtain string of terminals from start symbol Sentential form: any intermediate string of symbols during derivation Yield: the final sentential form, containing only terminals Language generated by grammar G: the set of all possible yields

2222/69 Grammar: B → 0B | 1B | 0 | 1 Generate 010 Derivation: B => 0B => 01B => 010 Parse Trees Parse tree

2323/69 Parse Trees But derivations may not be unique: Grammar: S → SS | (S) | () Generate (())() Derivation1: S => SS =>(S)S =>(())S =>(())() Derivation2: S => SS => S() =>(S)() =>(())() Different derivations but get the same parse tree Parse tree

2424/69 Parse Trees Ambiguity Grammar: S -> SS | (S) | () Generate ()()() Unique derivation: S => SS => SSS => ()SS => ()()S => ()()() Ambiguous grammar  one string has multiple parse trees

2525/69 Readings Chapter 2, up to 2.2.3