CS 432: Compiler Construction Lecture 1

Slides:



Advertisements
Similar presentations
報告者:會計四 簡思佳 The process of converting a program written in a high-level language into a machine-executable form. language implementation Ex.C++
Advertisements

1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation.
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Compiler Construction
Compiler Construction1 A Compulsory Module for Students in Computer Science Department Faculty of IT / Al – Al Bayt University First Semester 2009/2010.
CS 153: Concepts of Compiler Design August 25 Class Meeting Department of Computer Science San Jose State University Fall 2014 Instructor: Ron Mak
1 Programming Languages Examples: C, Java, HTML, Haskell, Prolog, SAS Also known as formal languages Completely described and rigidly governed by formal.
Invitation to Computer Science 5th Edition
Course Revision Contents  Compilers  Compilers Vs Interpreters  Structure of Compiler  Compilation Phases  Compiler Construction Tools  A Simple.
INTRODUCTION TO COMPUTING CHAPTER NO. 06. Compilers and Language Translation Introduction The Compilation Process Phase 1 – Lexical Analysis Phase 2 –
COP4020 Programming Languages
CS 153: Concepts of Compiler Design August 24 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
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.
CSC 338: Compiler design and implementation
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.
CS 152: Programming Language Paradigms April 2 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak
CS 153: Concepts of Compiler Design August 26 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
CS 153: Concepts of Compiler Design September 16 Class Meeting Department of Computer Science San Jose State University Fall 2015 Instructor: Ron Mak
Compiler design Lecture 1: Compiler Overview Sulaimany University 2 Oct
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
1. 2 Preface In the time since the 1986 edition of this book, the world of compiler design has changed significantly 3.
CS 460/660 Compiler Construction. Class 01 2 Why Study Compilers? Compilers are important – –Responsible for many aspects of system performance Compilers.
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.
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.
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.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
1 Asstt. Prof Navjot Kaur Computer Dept PRESENTED BY.
COMP 3002: Compiler Construction Pat Morin School of Computer Science.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
Lecture 1b- Introduction
Advanced Computer Systems
Component 1.6.
CS 153: Concepts of Compiler Design September 14 Class Meeting
Compiler Design (40-414) Main Text Book:
CS 153: Concepts of Compiler Design August 24 Class Meeting
Chapter 1 Introduction.
Introduction to Compiler Construction
CS 326 Programming Languages, Concepts and Implementation
Introduction to Computer Science
CS 153: Concepts of Compiler Design August 29 Class Meeting
Compiler Construction (CS-636)
Overview of Compilation The Compiler Front End
Chapter 1 Introduction.
CS 153: Concepts of Compiler Design September 7 Class Meeting
课程名 编译原理 Compiling Techniques
History of compiler development
CS 153: Concepts of Compiler Design November 30 Class Meeting
Compiler Lecture 1 CS510.
CMPE 152: Compiler Design December 5 Class Meeting
CMPE 152: Compiler Design January 25 Class Meeting
Compiler Construction
CMPE 152: Compiler Design February 6 Class Meeting
Course supervisor: Lubna Siddiqui
Compiler Construction
CMPE 152: Compiler Design August 21 Class Meeting
CMPE 152: Compiler Design August 23 Class Meeting
CMPE 152: Compiler Design August 21/23 Lab
CMPE 152: Compiler Design January 24 Class Meeting
CMPE 152: Compiler Design January 29 Class Meeting
Chapter 10: Compilers and Language Translation
Compiler Construction
CMPE 152: Compiler Design August 22 Class Meeting
CMPE 152: Compiler Design August 27 Class Meeting
Presentation transcript:

CS 432: Compiler Construction Lecture 1 Department of Computer Science Salisbury University Fall 2017 Instructor: Dr. Sophie Wang http://faculty.salisbury.edu/~xswang 11/8/2017

Goals of the Course Understand the concepts of compilers and interpreters. Parser, scanner, tokens Symbol tables, intermediate code Executors, code generators Compiler-compilers Learn important job skills that employers want. Work as a member of a small programming team. Understand Big Application. Use modern software engineering practices to develop a complex application. 11/8/2017

Course Notes Class website http://faculty.salisbury.edu/~xswang/cosc432/cosc432.htm Latest news Lecture notes and handouts The textbook and Java source code from the textbook: Writing Compilers and Interpreters, 3rd edition http://www.apropos-logic.com/wci/ Second reference textbook: Generating Parsers with JavaCC, 2nd edition 11/8/2017

How to instruct a computer In the beginning… 11/8/2017

How to instruct a computer Programming bit-by-bit doesn’t scale we want to instruct the computer at a higher level of abstraction 11/8/2017

High-level Abstract Description to Low-level Implementation Details President My poll ratings are low, lets invade a small nation General Cross the river and take defensive positions Sergeant Forward march, turn left Stop!, Shoot Foot Soldier 11/8/2017

How to instruct a computer Programming bit-by-bit doesn’t scale we want to instruct the computer at a higher level of abstraction Natural Languages: Powerful, but… Ambiguous Same expression describes many possible action 11/8/2017

The computer understands you as well as your mother understands you. “At last, a computer that understands you like your mother” -- 1985 McDonnell-Douglas Ad The computer understands you as well as your mother understands you. The computer understands that you like your mother. The computer understands you as well as it understands your mother. 11/8/2017

“ I know you believe you understood what you think I said, but I am not sure you realize that what you heard is not what I meant.” S. I. Hayakawa (1906-1992), U.S. senator from California 11/8/2017

George Romney (1907-1995), U.S. presidential candidate “I didn’t say that I didn’t say it. I said that I didn’t say I said it. I want to make that very clear.” 11/8/2017

Programming Languages We want a high level of abstraction We also want precision (avoid ambiguity) conciseness expressiveness modularity The goal is to achieve: Programmer Productivity Also Efficiency and Performance 11/8/2017 11

The role of the compiler Translate a program from high-level description to low-level implementation High-level of Abstraction Low-level of Abstraction Program written in a Programming Languages Assembly Language Translation Compiler 11/8/2017

What is a Compiler? A software utility that is extremely important for developing applications … … but is otherwise overlooked and taken for granted UNLESS … you can’t get your program to compile! _ 11/8/2017

A Compiler is a Translator A compiler translates a program you’ve written ... in a high-level language C, C++, Java, Pascal, etc. ... into a low-level language assembly language or machine language ... that a computer can understand and eventually execute. 11/8/2017

More Definitions source program: the program (application) you write in a high-level language that the compiler will translate Usually stored in a source file. source language: the high-level language in which you write your source program Pascal object language: the low-level language (AKA target language) into which the compiler translates the source program Do not confuse object language with object-oriented language. Jasmin assembly language 11/8/2017

Still More Definitions object program: your program after it has been translated into the object language target machine: the computer that will eventually execute the object program Java Virtual Machine (JVM) The JVM runs on your workstation or laptop (any computer that supports Java) implementation language: the language that the compiler itself is written in Java _ 11/8/2017

Overview of the Compilation Process Sort.pas Sort.java What’s in this box? Pascal compiler (you will write this in Java) Java compiler translation: javac Sort.java ... Sort.asm Sort.class assembly language object program Jasmin assembler (provided for you) translation: Sort.class Java linker & loader (provided for you) Java linker & loader java Sort ... Java Virtual Machine (provided for you) Java Virtual Machine execution: 11/8/2017

What is Conceptual Design? The conceptual design of a program is a high-level view of its software architecture. It includes the primary components of the program, how they’re organized, and how they interact with each other. It does not necessarily say how these components will be implemented. Rather, it allows you to examine and understand the components first without worrying about how you’re eventually going to develop them. 11/8/2017

Conceptual Design (Version 1) Parser Controls the translation process. Repeatedly asks the scanner for the next token. Scanner Repeatedly reads characters from the source to construct tokens for the parser. Token A source language element identifier (name) number special symbol (+ - * / = etc.) reserved word Also reads from the source Source The source program 11/8/2017

Token A low-level element of the source language. AKA lexeme Pascal language tokens Identifiers names of variables, types, procedures, functions, enumeration values, etc. Numbers integer and real (floating-point) Reserved words BEGIN END IF THEN ELSE AND OR NOT etc. Special symbols + - * / := < <= = >= > . , .. : ( ) [ ] { } ′ 11/8/2017

Parser Controls the translation process. Repeatedly asks the scanner for the next token. Knows the syntax (“grammar”) of the source language’s statements and expressions. Analyzes the sequence of tokens to determine what kind of statement or expression it is translating. Verifies that what it’s seeing is syntactically correct. Flags any syntax errors that it finds and attempts to recover from them. What the parser does is called parsing – it parses the source program in order to translate it. AKA syntax analyzer 11/8/2017

Scanner Reads characters sequentially from the source in order to construct the next token whenever requested by the parser. Knows the syntax of the source language’s tokens. What the scanner does is called scanning – it scans the source program in order to extract tokens. AKA lexical analyzer _ 11/8/2017

Conceptual Design (Version 2) We can architect a compiler with three major parts: 11/8/2017

Major Parts of a Compiler Front end Parser, Scanner, Source, Token Intermediate tier Intermediate code (icode) “Predigested” form of the source code that the back end can process efficiently. Example: parse trees AKA intermediate representation (IR) Symbol table (symtab) Stores information about the symbols (such as the identifiers) contained in the source program. Back end Code generator Processes the icode and the symtab in order to generate the object code. Only the front end needs to be source language-specific. The intermediate tier and the back end can be language-independent! 11/8/2017

What Else Can Compilers Do? Compilers allow you to program in a high-level language and think about your algorithms, not about machine architecture. Compilers provide language portability. You can run your C++ and Java programs on different machines because their compilers enforce language standards. Compilers can optimize and improve how well your programs execute. Optimize the object code for speed. Optimize the object code for size. Optimize the object code for power consumption. 11/8/2017

What about Interpreters? An interpreter executes a source program instead of generating object code. It executes a source program using the intermediate code and the symbol table. It shares many of the components of a compiler. Instead of a code generator in the back end, it has an executor. _ 11/8/2017

Conceptual Design (Version 3) A compiler and an interpreter can both use the same front end and intermediate tier. 11/8/2017

Comparing Compilers and Interpreters A compiler generates object code, but an interpreter does not. Executing the source program from object code can be several orders of magnitude faster than executing the program by interpreting the intermediate code and the symbol table. But an interpreter requires less effort to get a source program to execute = faster turnaround time. An interpreter maintains control of the source program’s execution. Interpreters often come with interactive source-level debuggers that allow you to refer to source program elements, such as variable names. AKA symbolic debugger _ 11/8/2017

Compilers and Interpreters, cont’d Therefore ... Interpreters are useful during program development. Compilers are useful to run released programs in a production environment. In this course, you will ... Develop a compiler/an interpretor for a language of your choice. 11/8/2017

A Pascal Interpreter The interpreter parses and executes a Pascal program without first generating object code. The parser generates intermediate code (icode) and a symbol table (symtab). The executor interprets the icode and the symtab in order to execute the Pascal program. _ 11/8/2017

Sample Pascal Program PROGRAM Newton; CONST epsilon = 1e-6; VAR number : integer; root, sqroot : real; BEGIN REPEAT writeln; write('Enter new number (0 to quit): '); read(number); IF number = 0 THEN BEGIN writeln(number:12, 0.0:12:6); END ELSE IF number < 0 THEN BEGIN writeln('*** ERROR: number < 0'); ELSE BEGIN sqroot := sqrt(number); writeln(number:12, sqroot:12:6); root := 1; root := (number/root + root)/2; writeln(root:24:6, 100*abs(root - sqroot)/sqroot:12:2, '%') UNTIL abs(number/sqr(root) - 1) < epsilon; UNTIL number = 0 END. read, write, and writeln are standard (built-in) Pascal procedures. abs, sqr, and sqrt are standard Pascal functions. Command line: java –classpath classes Pascal execute newton.pas 11/8/2017

Why Study Compilers and Interpreters? Compilers and interpreters are complex programs. The subject matter has a well-established body of theory and techniques. Don’t take compilers and interpreters for granted! Understand how they work and you’ll become a better programmer. _ 11/8/2017

Why Study Compilers and Interpreters? Programming challenge. This is one of the largest, most complex programs you’ll ever write. Develop it incrementally and have working code at each step. Use good object-oriented design and software engineering practices. Useful and fun. Create a compiler or an interpreter for a programming language that you invent that’s suitable for your application domain. a procedure-oriented language like Pascal a scripting language like the Linux shell Learn programming techniques and algorithms that you can apply to other applications. _ 11/8/2017

Small Teams Compilers and interpreters are generally written by small teams. Form your own teams of 2 or 3 students each. Choose your team members wisely! Be sure you’ll be able to meet and communicate with each other and work together well. Each team member will receive the same score on the team assignments and on the team compiler project. Email me your team name and the list of team members by Tuesday, September 5. 11/8/2017

An Open Source Pascal IDE You can use Lazarus for Pascal development. http://wiki.lazarus.freepascal.org/ Runs on Windows, Mac OS X, and Linux. It’s an integrated development environment (IDE). Your task # 1. Use Lazarus to develop your first Pascal program that displays “Hello World!”. Crash course to learning Pascal. 11/8/2017

Compiler Project Each team develops a working compiler for a procedure-oriented source language. A subset of c. A subset of any other procedure-oriented languages. Your own procedure-oriented language. Use the JavaCC compiler-compiler to generate a parser and a scanner for your source language. You will need to provide a grammar for the language. Re-use any other compiler code from the class. Start thinking about and planning for your project early in the semester. 11/8/2017

Postmortem Report At the end of the semester, each student will individually turn in a short (1 or 2 pp.) report: A brief description of what you learned in the course. An assessment of your personal accomplishments for your project team. An assessment of each of your other project team members. _ 11/8/2017