Introduction CPSC 388 Ellen Walker Hiram College.

Slides:



Advertisements
Similar presentations
Software & Services Group, Developer Products Division Copyright© 2010, Intel Corporation. All rights reserved. *Other brands and names are the property.
Advertisements

CPSC Compiler Tutorial 9 Review of Compiler.
Lecture 01 - Introduction Eran Yahav 1. 2 Who? Eran Yahav Taub 734 Tel: Monday 13:30-14:30
1 Semantic Processing. 2 Contents Introduction Introduction A Simple Compiler A Simple Compiler Scanning – Theory and Practice Scanning – Theory and Practice.
PZ02A - Language translation
Reference Book: Modern Compiler Design by Grune, Bal, Jacobs and Langendoen Wiley 2000.
Compiler Design Lexical Analysis Syntactical Analysis Semantic Analysis Optimization Code Generation.
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.
Lecture 2 Phases of Compiler. Preprocessors, Compilers, Assemblers, and Linkers Preprocessor Compiler Assembler Linker Skeletal Source Program Source.
CS 3240: Languages and Computation Course Overview Sasha Boldyreva.
Invitation to Computer Science 5th Edition
September 7, September 7, 2015September 7, 2015September 7, 2015 Azusa, CA Sheldon X. Liang Ph. D. Computer Science at Azusa Pacific University.
2.2 A Simple Syntax-Directed Translator Syntax-Directed Translation 2.4 Parsing 2.5 A Translator for Simple Expressions 2.6 Lexical Analysis.
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.
CSC 338: Compiler design and implementation
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.
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
Chapter 1 Introduction. Chapter 1 - Introduction 2 The Goal of Chapter 1 Introduce different forms of language translators Give a high level overview.
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.
Introduction to Code Generation and Intermediate Representations
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.
Introduction to Compiling
Chapter 1 Introduction Major Data Structures in Compiler
1 Language translation Programming Language Design and Implementation (4th Edition) by T. Pratt and M. Zelkowitz Prentice Hall, 2001 Sections
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.
Compilers I CNS History Wires Wires Machine Language Machine Language FFBA FFBA No Translation necessary No Translation necessary Assembly Language.
Compiler Construction By: Muhammad Nadeem Edited By: M. Bilal Qureshi.
1 Compiler & its Phases Krishan Kumar Asstt. Prof. (CSE) BPRCE, Gohana.
Code Generation CPSC 388 Ellen Walker Hiram College.
Compiler Construction CPCS302 Dr. Manal Abdulaziz.
CSC 4181 Compiler Construction
LECTURE 3 Compiler Phases. COMPILER PHASES Compilation of a program proceeds through a fixed series of phases.  Each phase uses an (intermediate) form.
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.
Sung-Dong Kim Dept. of Computer Engineering, Hansung University
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,
Prologue Sung-Dong Kim, Dept. of Computer Engineering, Hansung University.
LECTURE 10 Semantic Analysis. REVIEW So far, we’ve covered the following: Compilation methods: compilation vs. interpretation. The overall compilation.
Chapter 1 Introduction Samuel College of Computer Science & Technology Harbin Engineering University.
CHAPTER 1 INTRODUCTION TO COMPILER SUNG-DONG KIM, DEPT. OF COMPUTER ENGINEERING, HANSUNG UNIVERSITY.
Compiler Design (40-414) Main Text Book:
Introduction Chapter : Introduction.
PRINCIPLES OF COMPILER DESIGN
Chapter 1 Introduction.
Introduction to Compiler Construction
CS 3304 Comparative Languages
A Simple Syntax-Directed Translator
Compiler Construction (CS-636)
Introduction.
Chapter 1 Introduction.
课程名 编译原理 Compiling Techniques
Chapter 1: Introduction to Compiling (Cont.)
Compiler Lecture 1 CS510.
Introduction CI612 Compiler Design CI612 Compiler Design.
CSE401 Introduction to Compiler Construction
Lec00-outline May 18, 2019 Compiler Design CS416 Compiler Design.
Introduction Chapter : Introduction.
Presentation transcript:

Introduction CPSC 388 Ellen Walker Hiram College

Why Learn About Compilers? Practical application of important computer science theory Ties together computer architecture and programming Useful tools for developing language interpreters –Not just programming languages!

Computer Languages Machine language –Binary numbers stored in memory –Bits correspond directly to machine actions Assembly language –A “symbolic face” for machine language –Line-for-line translation High-level language (our goal!) –Closer to human expressions of problems, e.g. mathematical notation

Assembler vs. HLL Assembler Ldi $r1, 2 -- put the value 2 in R1 Sto $r1, x -- store that value in X HLL X = 2;

Characteristics of HLL’s Easier to learn (and remember) Machine independent –No knowledge of architecture needed –… as long as there is a compiler for that machine!

Early Milestones FORTRAN (Formula Translation) –IBM (John Backus) –First High-level language, and first compiler Chomsky Hierarchy (1950’s) –Formal description of natural language structure –Ranks languages according to the complexity of their grammar

Chomsky Hierarchy Type 3: Regular languages –Too simple for programming languages –Good for tokens, e.g. numbers Type 2: Context Free languages –Standard representation of programming languages Type 1: Context Sensitive Languages Type 0: Unrestricted

CSL Another View of the Hierarchy CFL RL

Formal Language & Automata Theory Machines to recognizes each language class –Turing Machine (computable languages) –Push-down Automaton (context-free languages) –Finite Automaton (regular languages) Use machines to prove that a given language belongs to a class Formally prove that a given language does not belong to a class

Practical Applications of Theory Translate from grammar to formal machine description Implement the formal machine to parse the language Tools: –Scanner Generator (RL / FA): LEX, FLEX –Parser Generator (CFL / FA): YACC, Bison

Beyond Parsing Code generation Optimization –Techniques to “mindlessly” improve code –Usually after code generation –Rarely “optimal”, simply better

Phases of a Compiler Scanner -> tokens Parser -> syntax tree Semantic Analyzer -> annotated tree Source code optimizer -> intermediate code Code generator -> target code Target code optimizer -> better target code

Additional Tables Symbol table –Tracks all variable names and other symbols that will have to be mapped to addresses later Literal table –Tracks literals (such as numbers and strings) that will have to be stored along with the eventual program

Scanner Read a stream of characters Perform lexical analysis to generate tokens Update symbol and literal tables as needed Example: Input: a[j] = Tokens: ID Lbrack ID Rbrack EQL NUM PLUS NUM

Parser Performs syntax analysis Relates the sequence of tokens to the grammar Builds a tree that represents this relationship, the parse tree

Partial Grammar assign-expr -> expr = expr array-expr -> ID [ expr ] expr -> array-expr expr -> expr + expr expr -> ID expr -> NUM

Example Parse assign-expression expression add-expressionarray-expression ID[ ] = NUM + expression

Abstract Syntax Tree assign-expression expression add-expressionarray-expression ID NUM expression

Semantic Analyzer Determine the meaning (not structure) of the program This is “compile-time” or static semantics only Example; a[j] = –a refers to an array location –a contains integers –j is an integer –j is in the range of the array (not checked in C) Parse or Syntax tree is “decorated” with this information

Source Code Optimizer Simplify and improve the source code by applying rules –Constant folding: replace “4+2” by 6 –Combine common sub-expressions –Reordering expressions (often prior to constant folding) –Etc. Result: modified, decorated syntax tree or Intermediate Representation

Code Generator Generates code for the target machine Example: –MOV R0, jvalue of j into R0 –MUL R0, 22*j in R0 (int = 2 wds) –MOV R1, &avalue of a in R1 –ADD R1, R0a+2*j in R1 (addr of a[j]) –MOV *R1, 66 into address in R1

Target Code Optimizer Apply rules to improve machine code Example: –MOV R0, j –SHL R0(shift to multiply by 2) Use more complex –MOV &a[R0], 6machine instruction to replace simpler ones

Major Data Structures Tokens Syntax Tree Symbol Table Literal Table Intermediate Code Temporary files

Structuring a Compiler Analysis vs. Synthesis –Analysis = understanding the source code –Synthesis = generating the target code Front end vs. Back end –Front end: parsing & intermediate code generation (target machine-independent) –Back end: target code generation Optimization included in both parts

Multiple Passes Each pass process the source code once –One pass per phase –One pass for several phases –One pass for entire compilation Language definition can preclude one- pass compilation

Runtime Environments Static (e.g. FORTRAN) –No pointers, no dynamic allocation, no recursion –All memory allocation done prior to execution Stack-based (e.g. C family) –Stack for nested allocation (call/return) –Heap for random allocation (new) Fully dynamic (LISP) –Allocation is automatic (not in source code) –Garbage collection required

Error Handling Each phase finds and handles its own types of errors –Scanning: errors like: 1o1 (invalid ID) –Parsing: syntax errors –Semantic Analysis: type errors Runtime errors handled by the runtime environment –Exception handling by programmer often allowed

Compiling the Compiler Using machine language –Immediately executable, hard to write –Necessary for the first (FORTRAN) compiler Using a language with an existing compiler and the same target machine Using the language to be compiled (bootstrapping)

Bootstrapping Write a “quick & dirty” compiler for a subset of the language (using machine language or another available HLL) Write a complete compiler in the language subset Compile the complete compiler using the “quick & dirty” compiler