410/510 1 of 31 Week 1 – Lecture 1 Introduction The Textbook Assessment Overview Compiler Construction.

Slides:



Advertisements
Similar presentations
Compilers Course 379K, TTH 9:30-11:00 Instructor: Dr. Doron A. Peled Office Hours: Mon 11:00-12:00.
Advertisements

Introduction to Compiler Construction
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
BİL744 Derleyici Gerçekleştirimi (Compiler Design)1.
1.3 Executing Programs. How is Computer Code Transformed into an Executable? Interpreters Compilers Hybrid systems.
Elements of Computing Systems, Nisan & Schocken, MIT Press, 2005, Chapter 10: Compiler I: Syntax Analysis slide 1www.idc.ac.il/tecs.
410/510 1 of 20 Week 1 – Lecture 1 Introduction The Textbook Assessment Programming & Tools A v. small compiler Compiler Construction.
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
Introduction to Compiler Construction Robert van Engelen COP5621 Compiler Construction Copyright Robert.
Compiler Construction1 COMP Compiler Construction Lecturer: Dr. Arthur Cater Teaching Assistant:
Compiler course 1. Introduction. Outline Scope of the course Disciplines involved in it Abstract view for a compiler Front-end and back-end tasks Modules.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 2.
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.
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.
Introduction Lecture 1 Wed, Jan 12, The Stages of Compilation Lexical analysis. Syntactic analysis. Semantic analysis. Intermediate code generation.
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.
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.
1 Compiler Design (40-414)  Main Text Book: Compilers: Principles, Techniques & Tools, 2 nd ed., Aho, Lam, Sethi, and Ullman, 2007  Evaluation:  Midterm.
Introduction to Compiling
Introduction CPSC 388 Ellen Walker Hiram College.
Compiler Design Introduction 1. 2 Course Outline Introduction to Compiling Lexical Analysis Syntax Analysis –Context Free Grammars –Top-Down Parsing –Bottom-Up.
Compiler Construction (CS-636)
Compiler Introduction 1 Kavita Patel. Outlines 2  1.1 What Do Compilers Do?  1.2 The Structure of a Compiler  1.3 Compilation Process  1.4 Phases.
Chapter 1 Introduction. Chapter 1 -- Introduction2  Def: Compiler --  a program that translates a program written in a language like Pascal, C, PL/I,
The Model of Compilation Natawut Nupairoj, Ph.D. Department of Computer Engineering Chulalongkorn University.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
What is a compiler? –A program that reads a program written in one language (source language) and translates it into an equivalent program in another language.
Dr. Mohamed Ramadan Saady 314ALL CH1.1 Chapter 1: Introduction to Compiling.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
CSC 8505 Compiler Construction
CS 404Ahmed Ezzat 1 CS 404 Introduction to Compiler Design Lecture 1 Ahmed Ezzat.
Presented by : A best website designer company. Chapter 1 Introduction Prof Chung. 1.
CS416 Compiler Design1. 2 Course Information Instructor : Dr. Ilyas Cicekli –Office: EA504, –Phone: , – Course Web.
CS510 Compiler Lecture 1. Sources Lecture Notes Book 1 : “Compiler construction principles and practice”, Kenneth C. Louden. Book 2 : “Compilers Principles,
Objective of the course Understanding the fundamentals of the compilation technique Assist you in writing you own compiler (or any part of compiler)
Compilers Design Chapter1 : Introduction, Structure of a Compiler
CHAPTER 1 INTRODUCTION TO COMPILER SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Advanced Computer Systems
Component 1.6.
Compiler Design (40-414) Main Text Book:
Introduction Chapter : Introduction.
Chapter 1 Introduction.
Introduction to Compiler Construction
Introduction.
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Compiler Lecture 1 CS510.
CS416 Compiler Design lec00-outline September 19, 2018
Introduction to Compiler Construction
Course supervisor: Lubna Siddiqui
Introduction CI612 Compiler Design CI612 Compiler Design.
CS416 Compiler Design lec00-outline February 23, 2019
Introduction to Compiler Construction
Compilers Principles, Techniques, & Tools Taught by Jing Zhang
Chapter 1 Introduction.
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Introduction Chapter : Introduction.
Introduction to Compiler Construction
Presentation transcript:

410/510 1 of 31 Week 1 – Lecture 1 Introduction The Textbook Assessment Overview Compiler Construction

410/510 2 of 31 The Big Picture In this course we will be constructing a compiler! Moving from a High Level Language to a Low Level Language Compilers are complex programs –> 10,000 lines of code Integrate aspects from many different areas of CS –Formal language theory, algorithms, data structures, HLL & LLL (obviously), user interaction (error reporting)

410/510 3 of 31 What is a compiler? A specialization of a language translator Usually in CS: – the Source is a high level programming language –the Target is a machine code for a micro-processor L1L2 SourceTarget Cx86 processor

410/510 4 of 31 Applications of Compiler Techniques Potential Source languages include: –Natural languages (English, French,….) –Circuit layout languages –Mark-up languages (HTML, XML, …) –Command line languages (SQL interface) Potential Target languages include: –Natural languages –Printer drivers –Markup languages e.g. HTML to RTF converter –Could involve many of the aspects we will cover in compiler construction

410/510 5 of 31 Compilers for Programming Languages If we had 1 compiler for each {Source,Target} pair then we would have a lot of compilers! Source LanguagesTarget Languages Compilers C Prolog Java Lisp Haskell C++ C# Fortran Pascal Sather x86 (MMX) JVM PowerPC 750 (G3) ARM SPARC AMD K6

410/510 6 of 31 Modularity for Code Generation Compilers x86 ARM G4 Source Intermediate Representation  Compiler portability (man gcc – lists different target machines)

410/510 7 of 31 Modularity for Source Languages? Compilers Intermediate Representation Sources Targets C Java Prolog Typically compilers only compile one source language – but the techniques used are very similar and are shared across different compilers

410/510 8 of 31 Typical Compiler Intermediate Representation SourceTarget Front-endBack-end Independent of Source and Target languages AnalysisSynthesis For a new Source language – we can add a new front-end to an existing back-end For a new Target language – we can add a new back-end to an existing front-end course nowweek 6  Ideally:

410/510 9 of 31 Front End Knowledge about the source language –Lexical structure (tokens) –Syntax Programming constructs –Conditionals, iteration etc –Semantics Type checking Error-reporting –UI component Often basic (and unhelpful!) May vary if part of an IDE or standalone Source program Lexical analyser Syntax analyser Semantic analyser Symbol table Error Handler

410/ of 31 Lexical Analysis Lexical Tasks the compiler has to perform: group together the 3 characters ‘max’ to form the single variable identifier max group together the 2 characters ‘<=’ to form the single relational operator <= (less than or equal to) int max = 20, x; read(x); if ( x <= max ) print(‘ok’); else print(‘too big’);

410/ of 31 Syntactic Analysis Recognise the if.. then … else structure Group the x <= max into a single expression with a relational operator Recognise the format of the variable declaration list –Such that x is correctly declared to be an int Loops, program blocks (begin…end) Arithmetic expressions, etc

410/ of 31 Semantic analysis Check that x <= max is a sensible thing to do –If x was a boolean and max a string then we would have a type error Check that the ‘20’ is in fact an integer and so can be assigned to an int And also (can be split over several phases) –Keep a note of all the variables used so we make sure they all refer to the same value (in memory)

410/ of 31 Data Structures Stream of text as the source file Group together text into larger units from a limited set Nearly all programming constructs can be represented as tree structures If statement ifBoolean expressionstatementelse statement Relational operator expression

410/ of 31 Data Structures Lexical Analyzer –  Stream of tokens (enumerated type) –NUMBER OPERATOR NUMBER Syntax Analyzer / Parser –  Tree of program structure program if_statementassignmentwhile_loopoutput_statement

410/ of 31 Back-end Knowledge about target processor / virtual machine –Instruction set ‘costs’ of different: –op-codes –instructions –Registers –Memory Semantic analyser Intermediate code generator Code optimiser Code generator Symbol table manager Error handler

410/ of 31 Putting it together Source program Lexical analyser Syntax analyser Semantic analyser Symbol table Error Handler Intermediate code generator Code optimiser Code generator Compiler Skeletal source program preprocessor compiler assembler Loader link-editor Target asse mbly program Relocatable machine code Absolute machine code Source program A language-processing system

410/ of 31 Grammars We define/describe HL languages with grammars A Grammar consists of: –T, set of Terminals –N, set of Non-terminals N  T =  –P, set of Productions    Where  and  are members of T  N –S, special member of N, the Start symbol G = {T, N, P, S}

410/ of 31 Chomsky’s Grammar Hierarchy Type 3 Regular Grammar Type 2 Context Free Grammar Type 1 Context-Sensitive Grammar Type 0 Unrestricted Grammar

410/ of 31 Grammars Type 0 (unrestricted) –   , –  and  are unrestricted sequences,  is not null –languages formed from Type 0 grammars can be recognised by non-deterministic Turing machines Type 1 (context sensitive) –  A    B  –A becomes B in the context of  …  –Complex for computer analysis

410/ of 31 Grammars Type 2 (context free) –A   A is a Non-terminal  is a member of T  N   (can be empty) –Equivalent to a push-down automaton Type 3 (regular) –A  wB, A  w (right linear) w is a string of Terminals A and B are Non-Terminals –Finite state automata

410/ of 31 In a compiler Use the minimum complexity grammars that let us successfully cope with HL programming languages (and process them efficiently) Regular grammars (=regular expressions) in the Lexical Analysis phase –‘recognise the words’ Context-free grammars in the Syntax Analysis phase –’recognise the phrases’ –  define our HLL as a grammar based on the output of the Lexical Analysis Deal with context sensitivity in the Semantic Analysis phase

410/ of 31 Overall Front-End View Source program Text file Lexical Analyser Syntax Analyser tokens Semantic Analyser Tree structure Intermediate Representation Type-safe Tree structure Back-end Tree / Linearized tree Context-free grammar Regular grammar Flex Bison

410/ of 31 The Textbook Compilers: principles, techniques & tools Aho, Sethi & Ullman Addison-Wesley {‘The Dragon Book’}

410/ of 31 Assessment Building a compiler for a new language Front-end –Lexical analysis –Parsing Back end –Generating assembler code Some formal and some practical –Formal more at the front-end

410/ of 31 Programming & Tools Lexical analysis generator – lex / flex Parser generator – yacc / bison C / C++ –To implement the remainder of the compiler Unix environment –make files will be useful for coordinating lex and yacc

410/ of 31 Instant Compilation Consider the program: main() { int a = 3; a = a + 1; } Given a reasonably sensible assembly language a hand- compilation might be: LDA #3 STA 1 LDA 1 ADD a, #1 STA 1

410/ of 31 & an Instant Compiler could look like … Switch( source_code_construct ) { case INT_DEC: print( “LDA #”, INT.value) print(“STA 1”) break case INT_ADD: print(“LDA 1”) print(“ADD a,#”, ADD.value) print(“STA 1”) break } /* end switch */

410/ of 31 The Problems …. Not efficient, (LDA #4; STA 1) Only works for 1 variable Only works at one location in memory –(usually let assembler deal with symbolic addresses) Only has 2 programming constructs! Not even slightly portable: – 1 instruction set & 1 source language

410/ of 31 More problems… No error reporting –type checking? Assumes: –Program is correct –Recognition of programming language constructs int a = 3  INT_DEC –Access to values INT.value, ADD.value –1:1 relationship between integers and memory locations

410/ of 31 Solutions We can view compilers as a solution to all of these problems E.g. –Only compile correct programs to object code –Recognise all constructs in the language –Improve the efficiency of code Execution speed Memory usage –Meaningful error messages to the user –Cope with different target architectures

410/ of 31 Why are compilers called compilers? In early compilers one of the main tasks was connecting object program to –standard library functions, I/O devices collecting information from different sources(e.g. libraries) –OS and processor dependent This is now performed by ‘linkers’ Compile – ‘construct by collecting from different sources’