Compilers I Chapter 1: Introduction. What is a compiler? Basic compiler structure. Compiler. January 07

Similar documents
CA Compiler Construction

Language Processing Systems

CSCI 3136 Principles of Programming Languages

Compilers I - Chapter 4: Generating Better Code

How to make the computer understand? Lecture 15: Putting it all together. Example (Output assembly code) Example (input program) Anatomy of a Computer

Programming Languages

n Introduction n Art of programming language design n Programming language spectrum n Why study programming languages? n Overview of compilation

Lecture 7: Machine-Level Programming I: Basics Mohamed Zahran (aka Z)

Lecture 27 C and Assembly

CCA CYBER SECURITY TRACK

Introduction. Compiler Design CSE 504. Overview. Programming problems are easier to solve in high-level languages

Compiler and Language Processing Tools

Programming Languages and Compilers

Compiler Construction

Lecture 9. Semantic Analysis Scoping and Symbol Table

Parsing Technology and its role in Legacy Modernization. A Metaware White Paper

Massachusetts Institute of Technology Department of Electrical Engineering and Computer Science

Semantic Analysis: Types and Type Checking

Programming Languages CIS 443

Computer Programming. Course Details An Introduction to Computational Tools. Prof. Mauro Gaspari:

CS 253: Intro to Systems Programming

Compilers. Introduction to Compilers. Lecture 1. Spring term. Mick O Donnell: michael.odonnell@uam.es Alfonso Ortega: alfonso.ortega@uam.

1/20/2016 INTRODUCTION

138 To satisfy a prerequisite, the student must have earned a letter grade of A, B, C or CR in the prerequisite course, unless otherwise stated.

Programming Languages

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Programming Languages & Tools

Computer Science. 232 Computer Science. Degrees and Certificates Awarded. A.S. Degree Requirements. Program Student Outcomes. Department Offices

AQA GCSE in Computer Science Computer Science Microsoft IT Academy Mapping

Compiler I: Syntax Analysis Human Thought

CSC230 Getting Starting in C. Tyler Bletsch

HPC Wales Skills Academy Course Catalogue 2015

Scoping (Readings 7.1,7.4,7.6) Parameter passing methods (7.5) Building symbol tables (7.6)

Computer Science. Computer Science 213. Faculty and Offices. Degrees and Certificates Awarded. AS Computer Science Degree Requirements

CE 504 Computational Hydrology Computational Environments and Tools Fritz R. Fiedler

Sources: On the Web: Slides will be available on:

Instructor Özgür ZEYDAN BEU Dept. of Enve. Eng. CIV 112 Computer Programming Lecture Notes (1)

Topics. Introduction. Java History CS 146. Introduction to Programming and Algorithms Module 1. Module Objectives

Building and Using a Cross Development Tool Chain

Web Services for Management Perl Library VMware ESX Server 3.5, VMware ESX Server 3i version 3.5, and VMware VirtualCenter 2.5

03 - Lexical Analysis

Advanced compiler construction. General course information. Teacher & assistant. Course goals. Evaluation. Grading scheme. Michel Schinz

Chapter 1. Dr. Chris Irwin Davis Phone: (972) Office: ECSS CS-4337 Organization of Programming Languages

CHAPTER 1 ENGINEERING PROBLEM SOLVING. Copyright 2013 Pearson Education, Inc.

Language Evaluation Criteria. Evaluation Criteria: Readability. Evaluation Criteria: Writability. ICOM 4036 Programming Languages

Simple C Programs. Goals for this Lecture. Help you learn about:

Securing PHP Based Web Application Using Vulnerability Injection

Numerical Analysis. Professor Donna Calhoun. Fall 2013 Math 465/565. Office : MG241A Office Hours : Wednesday 10:00-12:00 and 1:00-3:00

DIABLO VALLEY COLLEGE CATALOG

CS 40 Computing for the Web

Computer Science. Computer Science 207. Degrees and Certificates Awarded. A.S. Computer Science Degree Requirements. Program Student Outcomes

C HAPTER E IGHTEEN T HE PGP, MAIL, AND CGI LIBRARIES. PGP Interface Library

Jonathan Worthington Scarborough Linux User Group

Programming Languages

Compiler Construction

Intel Assembler. Project administration. Non-standard project. Project administration: Repository

GUI and Web Programming

Artificial Intelligence. Class: 3 rd

Scanning and parsing. Topics. Announcements Pick a partner by Monday Makeup lecture will be on Monday August 29th at 3pm

[Refer Slide Time: 05:10]

Compiler-Assisted Binary Parsing

Outline. hardware components programming environments. installing Python executing Python code. decimal and binary notations running Sage

Lecture 1: Introduction

A deeper look at Inline functions

1001ICT Introduction To Programming Lecture Notes

Chapter 13 Computer Programs and Programming Languages. Discovering Computers Your Interactive Guide to the Digital World

Interpreters and virtual machines. Interpreters. Interpreters. Why interpreters? Tree-based interpreters. Text-based interpreters

The programming language C. sws1 1

Course Descriptions. preparation.

How To Write Portable Programs In C

Computer Programming Tutorial

GENERIC and GIMPLE: A New Tree Representation for Entire Functions

Managing Variability in Software Architectures 1 Felix Bachmann*

CARPS: An integrated proposal and data collection system

Assembly Language: Function Calls" Jennifer Rexford!

2. Advance Certificate Course in Information Technology

1 Introduction. 2 An Interpreter. 2.1 Handling Source Code

Time Limit: X Flags: -std=gnu99 -w -O2 -fomitframe-pointer. Time Limit: X. Flags: -std=c++0x -w -O2 -fomit-frame-pointer - lm

Computer Science Course Descriptions Page 1

Computational Mathematics with Python

ADVANCED SCHOOL OF SYSTEMS AND DATA STUDIES (ASSDAS) PROGRAM: CTech in Computer Science

C Compiler Targeting the Java Virtual Machine

Table of Contents. Java CGI HOWTO

Technical paper review. Program visualization and explanation for novice C programmers by Matthew Heinsen Egan and Chris McDonald.

Chapter 13: Program Development and Programming Languages

Testing for Security

Overview. Open source toolchains. Buildroot features. Development process

CS 51 Intro to CS. Art Lee. September 2, 2014

CURRICULUM VITAE EDUCATION:

Axivion Bauhaus Suite Technical Factsheet

What is a programming language?

Efficiency of Web Based SAX XML Distributed Processing

Chapter 12 Programming Concepts and Languages

6.S096 Lecture 1 Introduction to C

Practical Programming, 2nd Edition

Implementing Programming Languages. Aarne Ranta

C Programming Review & Productivity Tools

Masters in Information Technology

Transcription:

Compilers I Chapter 1: Introduction Lecturers: Part I: Paul Kelly (phjk@doc.ic.ac.uk) Office: room 423 Part II: Naranker Dulay (nd@doc.ic.ac.uk) Office: room 562 Materials: Textbook Course web pages (http://www.doc.ic.ac.uk/~phjk/compilers) Course mailing list (221-compilers-2006@doc.ic.ac.uk) This course is about a particular class of programs called language processors, of which the best example is a compiler. What is a compiler? A program which processes programs, written in some programming language. A program which writes programs (in some language). A compiler translates programs written in one language into equivalent programs in another language. January 07 1 January 07 2 Source program Compiler Target program Error/warning messages Input Basic compiler structure In some language Eg Visual Basic For example: Visual basic program Compiler Error/warning messages Intel x86 assembly code Construct an internal representation of the source language structure, and hence its meaning Usually start by building a tree representation, but may build graph, eg to represent control flow Translates from one language into another Or: Output a low-level program which behaves as specified by the input, higher-level program. That is: Mediate between higher-level human concepts, and the word-by-word data manipulation which the machine performs. January 07 3 Synthesis Output Use this internal representation to construct target language version In the target language Analyse and transform the internal representation, then traverse it to produce output Eg in Intel x86 assembler January 07 4

Source Language Program (char string) Lexical Syntax (further decomposition) Semantic (internal representation) Synthesis Intermediate Optimisation Code Code Generator Generation Target Language Program (char string) The phases of a compiler Source program Lexical analysis Syntax analysis Symbol table Semantic analysis Code generation Abstract Syntax Tree Symbol Table Compiler structure in more detail January 07 5 Target program Information from declarations is gathered in the symbol table, and is used to check how each variables is used, to reserve memory for it, and to generate code to access it January 07 6 Phases of a compiler - example Input file test.c : int A; int B; test_fun() { A = B+123; } Output file test.s : _test_fun: pushl %ebp movl %esp,%ebp movl _B,%eax addl $123,%eax movl %eax,_a movl %ebp,%esp popl %ebp Command: gcc -S O test.c ret (the flag -S tells the compiler to produce.comm _A, 16 # 4 assembly code, -O turns optimisation.comm _B, 16 # 4 on). January 07 7 Introduction to lexical analysis INPUT: sequence of characters OUTPUT: sequence of tokens: INTtok INTtok LBRACKETtok RBRACKETtok LCURLYtok EQtok PLUStok CONSTtok 123 RCURLYtok A B Symbol Table main User identifiers like A, B and main are all represented by the same lexical token (IDENTtok), which includes a pointer to a symbol table record giving the actual name. January 07 8

Introduction to Syntax (also known as parsing ) Programming languages have grammatical structure specified by grammatical rules in a notation such as BNF (Backus-Naur Form) Example: stat print expression while expression do stat expression = expression The function of syntax analysis is to extract the grammatical structure to work out how the BNF rules must have been applied to yield the input program. The output of the syntax analyser is a data structure representing the program structure: the Abstract Syntax Tree (AST). January 07 9 Returning to our C example: Input characters:...a = B+123... Lexical tokens: [IDENTtok A, EQtok, IDENTtok B, PLUStok, CONSTtok 123] Abstract syntax tree: Assign Ident A Plus Ident B Const 123 January 07 10 AST for whole C example: IntDecl A DeclarationList void (result type) IntDecl B main (function name) FunctionDecl void (formal parameters) Ident A Assign The AST is implemented as a tree of linked records. The compiler writer must design the AST data structure carefully so that it is easy to build (during syntax analysis), and easy to use (e.g. during code generation). January 07 11 Plus Ident B Const 123 You try: experimenting with real compilers Create a file file.c containing a simple C function Under Linux, type the command: gcc O S file.c This tells Gnu C compiler gcc to produce optimised translation of the C program and leave result in file.s Examine file.s You might try gcc O S fomit-frame-pointer file.c (This simplifies the output slightly) On Windows using Microsoft Visual Studio, try cl /Fa /Ox /c test.c (The output is written to test.asm ) January 07 12

Compilers are just one kind of language processor: Really useful software tools are useful because they are programmable If it s programmable it must have some kind of programming language Examples: Word processors, Spreadsheets (eg Word, Excel + Visual Basic) Web server scripting and active web content (eg PHP, ASP, JSP) Linux /etc/fstab, XFree86 config, Makefile, expect network gateways, routers and firewalls electronic mail processors (eg EXIM s, SendMail s configuration language, spamassassin) XML (may be used to provide a common language for many of examples above) Scripting languages (eg Visual Basic/Windows Script Host, Linux Bash shell, Perl, Python, PHP, Ruby) computer algebra systems (eg MatLab, Maple, Mathematica) Textbook - philosophy There are many textbooks on compilers, some good Purpose of lecture course is to give you enough understanding of the area to be able to use a serious textbook effectively Textbook should be worth more to you after the course than during it! Choose an authoritative book that goes substantially beyond this course January 07 13 January 07 14 Recommended textbooks Three (of the several) excellent runners-up: Modern Compiler Implementation in Java, by Andrew Appel, second edition (Cambridge University Press, 2002). About 30. Recommended book for this course until 2005 Compilers: Principles, Techniques, and Tools (second edition) by Alfred V. Aho, Ravi Sethi, Jeffrey D. Ullman, Monika Lam. The new edition of the definitive book by pioneers in the subject. Often called the Dragon book because of the picture on the front. Compiler engineers regularly refer to standard Dragon book stuff. Advanced Compiler Design and Implementation by Steven Muchnick The most thorough and authoritative advanced textbook. Almost all the material lies beyond what we can do in this course! January 07 15 The recommended textbook Engineering a compiler, by Keith Cooper and Linda Torczon, Morgan Kaufmann/Elsevier (2004). About 30. In my opinion, this book is the best compiler engineering guide ever I read. (review, Amazon.com) This book has a good introduction guiding the beginning compiler student into understanding basic concepts and gradually revealing the more intimidating stuff, but the authors took great care not to scare the beginners away and instead offers great indepth explanations into how concepts and implementation merge. Its an overall good book! (review, Amazon.com) Three things stand out. First, all the algorithms are consistent with the latest research. Second, the explanations are exceptionally clear, especially compared to other recent books. Third, there's always enough extra context presented so that you understand the choices you have to make, and understand how those choices fit with the structure of your whole compiler. (review, Jeff Kenton, comp.compilers 3/12/03) If you are a beginner "do not buy this book (review, Amazon.com) January 07 16

How to enjoy, learn from and pass this course: Textbook Linking and loading lab exercise Make handwritten notes during lectures Tutorial exercises are used to introduce new examinable material Assessed coursework is designed to reinforce and integrate lecture material; it s designed to help you pass the exam You are assumed to have studied the past exam papers Substantial lab exercise should bring it all together Ask questions! Use the course mailing list Chapter 1 the main points again Credit will be given in coursework assessment and exam marking for evidence that you have used a textbook to expand on the material in the notes. The sooner you get the book and read the first couple of chapters the more worthwhile it will be. Some of the later chapters offer far more detail than is needed for this course. January 07 17 January 07 18