Understanding and Writing Compilers

Similar documents
A Structured Programming Approach to Data

Fundamentals of Operating Systems

Understanding and Writing Compilers

About the Authors... iii Introduction... xvii. Chapter 1: System Software... 1

New Syllabus Mathematics for 0-Level 1

Section A. A grammar that produces more than one parse tree for some sentences is said to be ambiguous.

Fundamentals of Computer Science

Assembly Language Assembled for the Sinclair ZX81

Macmillan Computer Science Series

Let us construct the LR(1) items for the grammar given below to construct the LALR parsing table.

CJT^jL rafting Cm ompiler

SYLLABUS UNIT - I UNIT - II UNIT - III UNIT - IV CHAPTER - 1 : INTRODUCTION CHAPTER - 4 : SYNTAX AX-DIRECTED TRANSLATION TION CHAPTER - 7 : STORA

Compiler Design Aug 1996

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING Subject Name: CS2352 Principles of Compiler Design Year/Sem : III/VI

Gujarat Technological University Sankalchand Patel College of Engineering, Visnagar B.E. Semester VII (CE) July-Nov Compiler Design (170701)

SYED AMMAL ENGINEERING COLLEGE (An ISO 9001:2008 Certified Institution) Dr. E.M. Abdullah Campus, Ramanathapuram

Appendix A The DL Language

List of Figures. About the Authors. Acknowledgments

The analysis part breaks up the source program into constituent pieces and creates an intermediate representation of the source program.

Torben./Egidius Mogensen. Introduction. to Compiler Design. ^ Springer

PRINCIPLES OF COMPILER DESIGN

About the Tutorial. Audience. Prerequisites. Copyright & Disclaimer. Compiler Design

Syntax Analysis Part I

1. The output of lexical analyser is a) A set of RE b) Syntax Tree c) Set of Tokens d) String Character

LECTURE NOTES ON COMPILER DESIGN P a g e 2

Appendix Set Notation and Concepts

An Object Oriented Programming with C

KINGS COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING ACADEMIC YEAR / EVEN SEMESTER

Context-free grammars

Compiler Construction Using

Appendix A The BCPL Language

VIVA QUESTIONS WITH ANSWERS

Compiler Construction: Parsing

Question Bank. 10CS63:Compiler Design

1. Explain the input buffer scheme for scanning the source program. How the use of sentinels can improve its performance? Describe in detail.

Compilation 2012 Context-Free Languages Parsers and Scanners. Jan Midtgaard Michael I. Schwartzbach Aarhus University

A Structured Programming Approach to Data

Principles of Programming Languages [PLP-2015] Detailed Syllabus

INSTITUTE OF AERONAUTICAL ENGINEERING

LOGIC AND DISCRETE MATHEMATICS

VALLIAMMAI ENGINEERING COLLEGE

Syntax Analysis. Chapter 4

SEMANTIC ANALYSIS TYPES AND DECLARATIONS

Chapter 3: Describing Syntax and Semantics. Introduction Formal methods of describing syntax (BNF)

EDAN65: Compilers, Lecture 06 A LR parsing. Görel Hedin Revised:

SYSTEMS PROGRAMMING. Srimanta Pal. Associate Professor Indian Statistical Institute Kolkata OXFORD UNIVERSITY PRESS

CST-402(T): Language Processors

Programming Languages Third Edition. Chapter 7 Basic Semantics

COMPILER DESIGN LECTURE NOTES

Syllabi of the Comprehensive Examination in Computer Science

UNIT III & IV. Bottom up parsing

Compiler Theory. (Semantic Analysis and Run-Time Environments)

COMPUTATIONAL SEMANTICS WITH FUNCTIONAL PROGRAMMING JAN VAN EIJCK AND CHRISTINA UNGER. lg Cambridge UNIVERSITY PRESS

A Programming Approach with C DHARMENDER SINGH KUSHWAHA

Principle of Compilers Lecture IV Part 4: Syntactic Analysis. Alessandro Artale

MIDTERM EXAM (Solutions)

Review main idea syntax-directed evaluation and translation. Recall syntax-directed interpretation in recursive descent parsers

CA Compiler Construction

Syntax Analysis. Amitabha Sanyal. ( as) Department of Computer Science and Engineering, Indian Institute of Technology, Bombay

CIS 1.5 Course Objectives. a. Understand the concept of a program (i.e., a computer following a series of instructions)

St. MARTIN S ENGINEERING COLLEGE Dhulapally, Secunderabad

3. Syntax Analysis. Andrea Polini. Formal Languages and Compilers Master in Computer Science University of Camerino

Introduction to Programming Using Java (98-388)

Lexical and Syntax Analysis. Bottom-Up Parsing

Parsing II Top-down parsing. Comp 412

Formal Languages and Compilers Lecture VII Part 4: Syntactic A

Workbook for C++ THIRD EDITION. Yedidyah Langsam, Ph.D. Brooklyn College of the City University of New York. Custom Publishing

Formal Languages and Compilers Lecture I: Introduction to Compilers

CS606- compiler instruction Solved MCQS From Midterm Papers

Programming Languages, Summary CSC419; Odelia Schwartz


Lecture 8: Deterministic Bottom-Up Parsing

The basic operations defined on a symbol table include: free to remove all entries and free the storage of a symbol table

CSE P 501 Compilers. LR Parsing Hal Perkins Spring UW CSE P 501 Spring 2018 D-1

G.PULLAIH COLLEGE OF ENGINEERING & TECHNOLOGY

This book is licensed under a Creative Commons Attribution 3.0 License

CS5363 Final Review. cs5363 1

Roll No. :... Invigilator's Signature :. CS/B.Tech(CSE)/SEM-7/CS-701/ LANGUAGE PROCESSOR. Time Allotted : 3 Hours Full Marks : 70

Lecture 7: Deterministic Bottom-Up Parsing

PSD3A Principles of Compiler Design Unit : I-V. PSD3A- Principles of Compiler Design

CS453 : JavaCUP and error recovery. CS453 Shift-reduce Parsing 1

VETRI VINAYAHA COLLEGE OF ENGINEERING AND TECHNOLOGY

4. An interpreter is a program that

Part III : Parsing. From Regular to Context-Free Grammars. Deriving a Parser from a Context-Free Grammar. Scanners and Parsers.

Parsing Wrapup. Roadmap (Where are we?) Last lecture Shift-reduce parser LR(1) parsing. This lecture LR(1) parsing

CSE 401 Compilers. LR Parsing Hal Perkins Autumn /10/ Hal Perkins & UW CSE D-1

UNIT 3

Syntax Analysis, VII One more LR(1) example, plus some more stuff. Comp 412 COMP 412 FALL Chapter 3 in EaC2e. target code.

INSTITUTE OF AERONAUTICAL ENGINEERING (AUTONOMOUS)

x64 Windows Debugging

Formal Languages and Compilers Lecture VII Part 3: Syntactic A

COP 3402 Systems Software Syntax Analysis (Parser)

DEPARTMENT OF INFORMATION TECHNOLOGY AUTOMATA AND COMPILER DESIGN. B.Tech-IT, III Year -I Sem

Comp 204: Computer Systems and Their Implementation. Lecture 22: Code Generation and Optimisation

COMP-421 Compiler Design. Presented by Dr Ioanna Dionysiou

Anatomy of a Compiler. Overview of Semantic Analysis. The Compiler So Far. Why a Separate Semantic Analysis?

4. Lexical and Syntax Analysis

CS 415 Midterm Exam Spring 2002

An Introduction to Object-Oriented Programming

Chapter 4 :: Semantic Analysis

Transcription:

Understanding and Writing Compilers

Macmillan Computer Science Series Consulting Editor Professor F. H. Sumner, University of Manchester G. M. Birtwistle, Discrete Event Modelling on Simula Richard Bornat, Understanding and Writing Compilers J. K. Buckle, The ICL 2900 Series Derek Coleman, A Structured Programming Approach to Data* Andrew J. T. Colin, Programming and Problem-solving in Algol 68* S. M. Deen, Fundamentals of Data Base Systems* David Hopkin and Barbara Moss, Automata* H. Kopetz, Software Reliability A. Learner and A. J. Powell, An Introduction to Algol 68 through Problems* A. M. Lister, Fundamentals of Operating Systems, second edition* Brian Meek, Fortran, PL!Iand the Algols Derrick Morris and Roland N. lbbett, The MU5 Computer System I. R. Wilson and A. M. Addyman, A Practical Introduction to Pascal *The titles marked with an asterisk were prepared during the Consulting Editorship of Professor J. S. Rohl, University of Western Australia.

Understanding and Writing Compilers A do-it-yourself guide Richard Bornat Department of Computer Science and Statistics, Queen Mary College, University of London M

Richard Bornat 1979 Softcover reprint of the hardcover 1st edition 1979 All rights reserved. No part of this publication may be reproduced or transmitted, in any form or by any means, without permission. First published 1979 by THE MACMILLAN PRESS LTD London and Basingstoke Associated companies in Delhi Dublin Hong Kong Johannesburg Lagos Melbourne New York Singapore and Tokyo ISBN 978-0-333-21732-0 ISBN 978-1-349-16178-2 (ebook) DOI 10.1007/978-1-349-16178-2 This book is sold subject to the standard conditions of the Net Book Agreement. The paperback edition of this book is sold subject to the condition that it shall not, by way of trade or otherwise, be lent, resold, hired out, or otherwise circulated without the publisher's prior consent in any form of binding or cover other than that in which it is published and without a similar condition including this condition being imposed on the subsequent purchaser.

Co~e~s Introduction................... Host, Object and Source Language in Examples. 2 Acknowledgements 3 Section I Modular Organisation of Compilers...... 5 Phases and Passes.... 8 Tasks and Sub-tasks. 9 Translation and Optimisation 12 Object Descriptions in the Symbol Table 13 Run-time Support. 13 Source Program Errors.. 15 Two-pass Compilation 17 An Example of Compilation.. 18 2 Introduction to Translation 22 Phrases and Trees.. 23 Tree Walking 25 Linear Tree Representations. 27 Improving the Tree Walker.. 29 Using the Symbol Table Descriptors.. 33 Translation Error Handling 35 3 Introduction to Syntax Analysis 38 Language Descriptions (Grammars). 40 Bottom-up Analysis of Expressions 42 Top-down Analysis of Statements 48 Building a Node of the Tree 50 Syntax Error Handling 52 4 Lexical Analysis and Loading 56 Reading the Source Program... 57 Output for a Loader 66 After the Loader. 72

vi Contents Section II Translation and Crucial Code Fragments...... 73 Communication between Translation Procedures 75 Node format 76 5 Translating Arithmetic Expressions. 77 Reverse Operations 80 Register Dumping 81 Tree Weighting 84 Avoiding Reverse Operations 87 Function Calls and Register Dumping 88 Other Forms of the Tree 90 Combinations of Arithmetic Types. 91 6 Translating Boolean Expressions 94 Evaluating a Relational Expression... 94 Boolean or Conditional Operators? 95 Jumping Code for Boolean Expressions 97 7 Translating Statements and Declarations 105 Assignment Statement. 105 'While' Statement 107 BCPL 'for' Statement. 109 FORTRAN 'DO' Statement 111 Compound Statements.. 114 ALGOL-60-like Blocks 114 Procedure Declarations 116 8 Creating and Using the Symbol Table 118 Table Lookup 119 Hash Addressing 123 Object Descriptions in the Symbol Table 128 Single Names with Multiple Descriptors 131 9 Accessing an Element of a Data Structure 136 Accessing an Element of a Vector 136 Accessing an Element of an Array 141 Record Data Structures. 146 10 Code Optimisation 153 Net Effect and Order of Computation 158 Optimisation within a Basic Block 162 Loops and Code Motion 168 Hardware Design and Optimisation 171 Language Design and Optimisation 173

Contents vii Section III Run-time Support 176 ~1~1 P~r~o~c~e~d~u~r~e~Ca~ll~a~n~d~R~e~tu~r~n. 180 The Tasks of Procedure Call and Return Fragments 180 Layout of Procedure Call and Return Fragments 183 Recursion and the 'Efficiency of FORTRAN. 184 Simple Stack Handling 188 12 Arguments and Parameters 195 Different Kinds of Argument Information 196 Passing Argument Information 197 Generating Code for a Single Argument 200 Checking Stack Limits 202 The Evils of Run-time Argument Checking 203 13 Environments and Closures 210 An Explicit Representation of the Environment 213 The Display Vector Mechanism 219 ALGOL 60's call by name 213 The Environment Link Mechanism 216 Block Structure and Data Frames 223 Non-local goto Statements 226 14 Efficiency, Heaps and Lifetimes 228 Procedure Call with PUSH and POP Instructions 229 Addressing the Stack with a Single Register 231 Heap Storage 234 ALGOL 68, Lifetimes and Pointers 239 SIMULA 67 'classes 244 Section IV Parsing Algorithms............. 248 The Dangers of Backtracking. 251 15 Notation and Formal Language Theory 253 Languages and Sentences. 254 Generating a Sentence 255 Grammars and Productions 259 The Chomsky Hierarchy 259 The Parsing Problem 261 Derivations and Sentential Forms 262 Equivalence and Ambiguity 263 Lexical Analysis and type 3 grammars 268 Warshall's Closure Algorithm. 272 Context Dependency and Two-level Grammars 274

viii Contents ~1~6 T~o~p~-~d~o~w~n~S~xn~t~a~x~A~n~a~L4Y~s~i~s 277 Factoring to Reduce Backtracki~g.. 279 Removing Left Recursion 281 One-symbol-Look-ahead and One-track Analysers 285 Context Clashes and Null Symbols 290 A One-track Grammar is Unambiguous. 295 Error Detection and Processing 295 Pragmatics and Tricks...,. 300 Interpretive Top-down Analysis 303 Automatic Generation of Top-down Analysers.. 306 17 Operator Precedence Analysis of Expressions 308 Determining the Precedence of Operators.. 310 Numerical Operator Priorities 314 Reducing a Phrase-pattern to a Single Symbol 316 An Operator-precedence Grammar is Unambiguous 318 Input States, Error Detection and Unary Operators. 318 ~1~8~L~R~(~1~>~S~x~n~t~ax~A~n~a~L~x~s~i~s. 324 Analyser States and the Stack. 327 Why It Works: Non-deterministic Syntax Analysis 330 Building an SLR(1) Analyser.... 335 Constructing an LALR(1) Analyser 340 Error Detection and Tree-building Actions 344 Error Recovery 346 What are the True Advantages of LR(1) Analysis? 346 Section V Interpreting and Debugging 350 19 Interpreters and Interpretation. 354 Interpreting the parse tree 355 Imitative Interpretation 357 Linearising or Virtual-machine Interpreters 359 The Design of Languages for Interpretation 365 Debugging with an Interpreter 366 Combining Interpretation and Compilation 369 20 Run-time Debugging Aids 373 Off-Line debugging and the panic dump. 375 Interactive debugging. 377 Run-time event tracing 378 Break points 378 Producing Debuggable Code 379 Doing Without the Debugger 380

Contents ix Appendix A: The BCPL language.... 381 Appendix B: Assembly code used in examples 385 Bibliography.... 388 Index....... 390