compiler java

Download compiler java

Post on 24-Oct-2015

207 views

Category:

Documents

4 download

Embed Size (px)

DESCRIPTION

designing compiler in java

TRANSCRIPT

  • Compiler Design:Theory, Tools, and Examples

    Java Edition

    Seth D. Bergmann

    Rowan University

    2007

  • PrefaceCompiler design is a subject which many believe to be fundamental and vital to computerscience. It is a subject which has been studied intensively since the early 1950's andcontinues to be an important research field today. Compiler design is an important partof the undergraduate curriculum for many reasons: (1) It provides students with a betterunderstanding of and appreciation for programming languages. (2) The techniques usedin compilers can be used in other applications with command languages. (3) It providesmotivation for the study of theoretic topics. (4) It is a good vehicle for an extendedprogramming project.

    There are several compiler design textbooks available today, but most have beenwritten for graduate students. Here at Rowan University, our students have had difficultyreading these books. However, I felt it was not the subject matter that was the problem,but the way it was presented. I was sure that if concepts were presented at a slower pace,with sample problems and diagrams to illustrate the concepts, that our students would beable to master the concepts. This is what I have attempted to do in writing this book.

    This book is a revision of earlier editions that were written for Pascal and C++based curricula. As many computer science departments have moved to Java as theprimary language in the undergraduate curriculum, I have produced this edition toaccommodate those departments. This book is not intended to be strictly an object-oriented approach to compiler design. Though most Java compilers compile to anintermediate form known as Byte Code, the approach taken here is a more traditional onein which we compile to native code for a particular machine.

    The most essential prerequisites for this book are courses in Java applicationprogramming, Data Structures, Assembly Language or Computer Architecture, andpossibly Programming Languages. If the student has not studied formal languages andautomata, this book includes introductory sections on these theoretic topics, but in thiscase it is not likely that all seven chapters will be covered in a one semester course.Students who have studied the theory will be able to skip the preliminary sections (2.0,3.0, 4.0) without loss of continuity.

    The concepts of compiler design are applied to a case study which is an imple-mentation of a subset of Java which I call Decaf. Chapters 2, 4, 5, and 6 include asection devoted to explaining how the relevant part of the Decaf compiler is designed.This public domain software is presented in full in the appendices and is available on theInternet. Students can benefit by enhancing or changing the Decaf compiler provided.

    Chapters 6 and 7 focus on the back end of the compiler (code generation andoptimization). Here I rely on a fictitious computer, called Mini, as the target machine. Iuse a fictitious machine for three reasons: (1) I can design it for simplicity so that thecompiler design concepts are not obscured by architectural requirements, (2) It isavailable to anyone who has a C compiler (the Mini simulator, written in C, is availablealso), and (3) the teacher or student can modify the Mini machine to suit his/her tastes.

  • Chapter 7 includes only a brief description of optimization techniques sincethere is not enough time in a one semester course to delve into these topics, and becausethese are typically studied in more detail at the graduate level.

    To use the software that accompanies this book, you will need access to theworld wide web. The source files can be accessed athttp://www.rowan.edu/~bergmann/books/decaf

    These are plain text files which can be saved from your internet browser. Additionaldescription of these files can be found in Appendix B.

    I wish to acknowledge the people who participated in the design of this book.The reviewers of the original Pascal version James E. Miller of Transylvania Univer-sity, Jeffrey C. Chang of Garner-Webb University, Stephen J. Allan of Utah StateUniversity, Karsten Henckell of the New College of USF, and Keith Olson of MontanaTechnical College all took the time to read through various versions of the manuscriptof the original edition and provided many helpful suggestions. My students in theCompiler Design course here at Rowan University also played an important role intesting the original version and subsequent versions of this book. Support in the form oftime and equipment was provided by the administration of Rowan University.

    The pages of this book were composed entirely by me using Adobe Pagemaker,and diagrams were drawn with Microsoft Excel and Powerpoint.

    Finally, I am most grateful to my wife Sue for being so understanding during thetime that I spent working on this project.

    Seth D. Bergmannbergmann@rowan.edu

    ii

  • iiiContents

    Table of Contents

    Preface ............................................................................................................ iTable of Contents ....................................................................................... iii

    Chapter 1: Introduction ..............................................................................11.1 What is a Compiler? ................................................................................................... 11.2 The Phases of a Compiler .......................................................................................... 91.3 Implementation Techniques ..................................................................................... 191.4 Case Study: Decaf .................................................................................................... 261.5 Chapter Summary ..................................................................................................... 29

    Chapter 2: Lexical Analysis .................................................................... 302.0 Formal Languages .................................................................................................... 302.1 Lexical Tokens .......................................................................................................... 402.2 Implementation with Finite State Machines .......................................................... 442.3 Lexical Tables ........................................................................................................... 502.4 Lexical Analysis with SableCC ............................................................................... 542.5 Case Study: Lexical Analysis for Decaf ................................................................. 652.6 Chapter Summary ..................................................................................................... 69

    Chapter 3: Syntax Analysis ..................................................................... 703.0 Grammars, Languages, and Pushdown Machines .................................................. 713.1 Ambiguities in Programming Languages ............................................................... 893.2 The Parsing Problem ................................................................................................ 943.3 Chapter Summary ..................................................................................................... 95

  • Contentsiv

    Chapter 4: Top Down Parsing ................................................................ 964.0 Relations and Closure .............................................................................................. 974.1 Simple Grammars ................................................................................................... 1004.2 Quasi-Simple Grammars ........................................................................................ 1094.3 LL(1) Grammars ..................................................................................................... 1164.4 Parsing Arithmetic Expressions Top Down .......................................................... 1264.5 Syntax-Directed Translation .................................................................................. 1364.6 Attributed Grammars ............................................................................................. 1434.7 An Attributed Translation Grammar for Expressions .......................................... 1494.8 Decaf Expressions .................................................................................................. 1534.9 Translating Control Structures ............................................................................. 1584.10 Case Study: A Top Down Parser for Decaf ......................................................... 1644.11 Chapter Summary ................................................................................................ 168

    Chapter 5: Bottom Up Parsing ............................................................. 1715.1 Shift Reduce Parsing .............................................................................................. 1715.2 LR Parsing With Tables ......................................................................................... 1785.3 SableCC .................................................................................................................. 1835.4 Arrays ...................................................................................................................... 1995.5 Case Study: Syntax Analysis for Decaf ................................................................. 2055.6 Chapter Summary .................................................................................................... 208

    Chapter 6: Code Generation ..................................

Recommended

View more >