+ All Categories
Home > Documents > ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6....

ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6....

Date post: 18-Mar-2020
Category:
Upload: others
View: 7 times
Download: 0 times
Share this document with a friend
16
Transcript
Page 1: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters
Page 2: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters

ELEMENTS OF COMPII_JER DESIGN

By

Dr. M. JOSEPH B.E. , M.E., Ph.D. in Computer Engineering

Principal, Mother Teresa College

of Engineering and Technology

Pudukkottai, Tamil Nadu

UNIVERSITY SCIENCE PRESS(An Imprint of Laxmi Publications Pvt. Ltd.)

An ISO 9001:2008 Company

BENGALURU ● CHENNAI ● COCHIN ● GUWAHATI ● HYDERABADJALANDHAR ● KOLKATA ● LUCKNOW ● MUMBAI ● RANCHI ● NEW DELHI

BOSTON (USA) ● ACCRA (GHANA) ● NAIROBI (KENYA)

Page 3: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters

ELEMENTS OF COMPILER DESIGN

© by Laxmi Publications (P) Ltd. All rights reserved including those of translation into other languages. In accordance with the Copyright (Amendment) Act, 2012, no part of this publication may be reproduced, stored in a retrieval system, or transmitted in any form or by any means, electronic, mechanical, photocopying, recording or otherwise. Any such act or scanning, uploading, and or electronic sharing of any part of this book without the permission of the publisher constitutes unlawful piracy and theft of the copyright holder’s intellectual property. If you would like to use material from the book (other than for review purposes), prior written permission must be obtained from the publishers.

Printed and bound in India Typeset at Monu Printographics, Delhi

First Edition : 2011ISBN 978-93-80856-14-8

Limits of Liability/Disclaimer of Warranty: The publisher and the author make no representation or warranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim all warranties. The advice, strategies, and activities contained herein may not be suitable for every situation. In performing activities adult supervision must be sought. Likewise, common sense and care are essential to the conduct of any and all activities, whether described in this book or otherwise. Neither the publisher nor the author shall be liable or assumes any responsibility for any injuries or damages arising here from. The fact that an organization or Website if referred to in this work as a citation and/or a potential source of further information does not mean that the author or the publisher endorses the information the organization or Website may provide or recommendations it may make. Further, readers must be aware that the Internet Websites listed in this work may have changed or disappeared between when this work was written and when it is read.

All trademarks, logos or any other mark such as Vibgyor, USP, Amanda, Golden Bells, Firewall Media, Mercury, Trinity, Laxmi appearing in this work are trademarks and intellectual property owned by or licensed to Laxmi Publications, its subsidiaries or affiliates. Notwithstanding this disclaimer, all other names and marks mentioned in this work are the trade names, trademarks or service marks of their respective owners.

Published in india by

UNIVERSITY SCIENCE PRESS(An Imprint of Laxmi Publications Pvt. Ltd.)

An ISO 9001:2008 Company113, GOLDEN HOUSE, DARYAGANJ, NEW DELHI - 110002, INDIA Telephone : 91-11-4353 2500, 4353 2501 Fax : 91-11-2325 2572, 4353 2528 C—2441/010/11www.laxmipublications.com [email protected] Printed at: Ajit Prining Press, Delhi

& Bengaluru 080-26 75 69 30

& Chennai 044-24 34 47 26, 24 35 95 07

& Cochin 0484-237 70 04, 405 13 03

& Guwahati 0361-254 36 69, 251 38 81

& Hyderabad 040-27 55 53 83, 27 55 53 93

& Jalandhar 0181-222 12 72

& Kolkata 033-22 27 43 84

& Lucknow 0522-220 99 16

& Mumbai 022-24 91 54 15, 24 92 78 69

& Ranchi 0651-220 44 64

Bran

ches

Page 4: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters

.-------------ll Dedication :~--------------,

To My First Teacher and My Father,

Mr. A Maria Michal, Teacher

And

To Prof M. Maria Louis, BE, M.A. Sc. (Toronto},

W1w ha.'> Lit Up My Life.

Page 5: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters
Page 6: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters

Contents

Fa reward

Preface

Acknowledgements

1. Words, Sentences and Languages

1.1 Word

2.

3.

1.2 Sentence

1. 3 Language

1 .4 Translator

1.5 Compiler

1.6 Interpreter

1. 7 Phases of a Compiler

1 . 8 Phases and Passes

Lexical Analyser

2.1 Tokens

2.2 Languages and Strings

2.3 Input Buffers

2.4 Transition Diagrams

2.5 Regular Expressions (RE)

2.6 Finite Automata

2 .7 Examples

Context Free Grammars

3 .I Context Free Grammars (CFG)

3.2 Derivations and Reductions

3.3 Parse Trees

3.4 Ambiguous Grammar

4. Parsers

4.1 Syntax Analyser

4.2 Predictive Parser

4.3 Shift-reduce Parser

4.4 OR Parsers

(vii)

(xi)

(xiii)

(xiv)

1

1

2

3

3

3

5

7

7

8

8

9

10

10

19

42 42

43

44

45

47

47

48

65

71

Page 7: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters

(viii)

5. LR Parsers 90 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158

6. Syntax Directed Translations 174 6.1 Attributes 174

6.2 Syntax Directed Translation 174 6.3 Synthesized Translation 176

6.4 Inherited Translation 176

6.5 Translation of Parse Tree 176

6.6 Translation of Assignment Statements 177

6.7 Translation of the Boolean Functions 178

6.8 Syntax Directed Translation for Postfix 179

7. Type Checking and Run-time Environments 180 7 .I Type Checking 180

7. 2 Run-time Environments 183

8. Symbol Tables and Errors 187 8.1 Symbol Tables 187

8.2 Errors 189

9. Intermediate Code Generation 193 9.1 Postfix 193

9.2 Syntax Trees 193

9.3 Three-address Codes 195

9.4 Types of Three-address Statements 195

9.5 Quadruples, Triples and Indirect Triples 196

10. Code Optimization 202 10.1 Necessity of Code Optimization 202

10.2 Code Improving Transformations 203

10.3 Basic Blocks 205

10.4 Flow Graphs 206

10.5 Loops 207

10.6 Loops in Flow Graphs 209

11. Code Generation 213 II. I Architecture of Machine for Code Generation 214

11.2 Instruction Costs 214

11.3 Code Generation 214

Page 8: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters

(ix)

II .4 DAG (Directed Acyclic Graph)

11 .5 Peephole Optimization

12. Interpreters

12.1 RISC and CISC Machines

12.2 Definitions

12.3 Design of an Interpreter

12.4 Java-An Interpreted Language

APPENDICES

Appendix A-Two Mark Questions

Appendix B-Big Questions

Appendix C-Mini Compiler Source Code

Appendix C.l. Compilation and Execution

Appendix C.2. Sample Inputs and Outputs

Appendix D-Multiple Choice Questions

Appendix E-Lex

Index

217 221

223

223

224 225

226

228

228 231

237 285

286

289

298

304

Page 9: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters
Page 10: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters

Foreword

I am immensely pleased to note that Dr. M. JOSEPH, brings out this book on Compiler Design.

Compiler is a study of Language Processing for computers in a broader sense. This deals with the Characters, Grammars , Syntactical Structures and Semantics of programming languages. This specialized area is a subject of study for the students of Computer Science and Information Technology. Keeping them as target audience, the writer has done the tremendous job of rendering this subject easier and intelligible.

Dr. M. JOSEPH has been working in the field of Compilers with greater zeal and sincerity for a long time . His field of research is High-Level Synthesis (Hardware Compiler) in VLSI CAD. He has developed Technology driven High-Level Synthesis (THLS) strategy and tool (compiler) in C++, modifying Icarus Verilog Compiler, an open source High-Level Synthesis tool for IEEE standard 1364 HDL Verilog.

I feel he has rightly thought of bringing out a book on the elementary concepts on Compi lers for the benefits of students and made it a reality. The uniqueness of this volume is its lucidity and comprehensiveness.

My congratulations to Dr. M. JOSEPH for his scholarly effort and also wish him to bring out many more academic books in the days to come.

I al so appreciate the publi shers for having brought out the slender volume in a better format and presentation.

(xi)

Dr. Kn. K.S.K. Chockalingam,

PRINCIPAL,

National Engineering College,

Koil Patty, Tirunelveli,

Tamil Nadu.

Page 11: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters
Page 12: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters

Preface

The book is mainly written mainly for the beginners. The students of Engineering and other undergraduated, post graduate programs in Computer Science will be finding great help in learning the fundamental concepts of compiler design in this book. This book is written in a very simple and easy to understand manner. The student can also learn the concepts in a self-supporting way.

The book can be a good aide to the teachers, who teach compiler as an introductory course. This book covers syllabi of all Universities for regular and part time courses.

All the concepts (Scanner, Grammar, Parser, Intermediate Code Generator, Syntax Directed Translator, Type Checking, Runtime Storage Management, Code Optimizer, Symbol Table, Error Handler and Code Generator) involved in design and development of compilers are presented from chapter 1 to chapter 11 with examples in very simple style.

A chapter (Chapter 12) on Interpreters is also presented, which will be giving the details about interpreters with an example of an interpreted language (Java).

There are many problems, which are frequently asked in the university exams, solved with detailed explanation. There are more examples, which will be much useful to the students as per the exam point of view.

Two mark questions are given for all the chapters and Big questions are given for all the chapters in Appendices A and B.

A mini Compiler is developed in C++ for the desk top calculator and given in Appendix C. Its source code (C++) is also given.

In most of the competitive exams many questions are asked from compilers. So, a set of multiple choice questions is given in Appendix D. The students, who are preparing for competitive exams can refer this.

A separate chapter on LEX with examples, which will be of great use for the lab course on compilers, is given in Appendix E.

-Author

(xiii)

Page 13: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters

Acknowledgements

I am very much grateful to Dr. Kn. K.S.K. Chockalingam, Principal of National Engineering College-Koil Patty, Tirunelveli for his foreward. He has been my source of inspiration and also my well wisher since I was a student of him during my postgraduate studies.

I am also thankful to Mr. Dhanasekar, Architect, Thanjavur for designing the Cover for the book.

I am grateful to Mrs. A. Punitha for helping me in writing the chapter on LEX tool.

I am thankful Mr. Srinath, Bangalore, for the help in developing the Compiler in C++ for the simple desk top calculator.

I am thankful to Mrs. J. Niraimathi, Vrrudu Nagar for helping me in working out the problems presented in the book.

I am also thankful to all those who have directly or indirectly helped me in bringing out this book.

-Author

(xiv)

Page 14: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters

Chapter 1 WORDS, SENTENCES AND

LANGUAGES

In the beginning was the WORD And the WORD was with God,

and the WORD was God.

-The Bible

A Language is worth knowing unless it teaches you to think differently.

- Larrywall and Randal Schwartz

1.1 WORD

In simpler way a Word can be defined as a set of characters, which gives a meaning. For example : "boy" is a word defined from the set of characters from the alphabets available in English Language.

It denotes masculine gender child.

1.2 SENTENCE

A sentence is a set of words which gives a meaning.

For example : The boy is praying. The sentence should follow the grammar. Here the grammar defines the way in which the sentence can be formed. If a sentence does not follow any grammar, then it can be called a grammatically incorrect sentence.

1.3 lANGUAGE

The language is a tool used to communicate with others. It is defined over the sentences in turn words, which in turn defined over the characters. The language is mainly used for communicating with the external world.

The following example will help us in understanding concept of compiler Construction. Consider the following sentence, in English.

I AM GOING TO MARKET

I ~ Subject

Am ~ Auxiliary Verb

Go+ ing ~ Verb+ ing

To Market ~ Object. 1

Page 15: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters

2 ELEMENTS OF COMPILER DESIGN

This simple sentence is Syntactically, Grammatically correct.

There are some sentences which are not syntactically correct, for Example :

The hoi is going to hastel.

We can now understand the sentence should be written error free, i.e., syntactically correct in broader sense.

There are also some sentences which are not Grammatically correct, for Example :

The boy is go to hostel.

But the correct sentence is : The boy is going to hostel.

We can also understand the sentence should be written without any grammatical errors.

Okay, we have a sentence which is syntactically and grammatically Correct. Now What is to be done ? The answer is, The boy must do some action, i.e., The boy must go to the hostel. This action is attached to the sentence. It is clear that an action is associated with a sentence which is Syntactically and Grammatically correct.

The above simple analogies will explain how the compiler works in very simpler terms. The above discussion can be interpreted in the following way.

Language ~ Any HLL

Word ~ String.

Sentence --~ Statement in a HLL.

Set of Sentences ~ Set of statements i.e., a Program in a HLL.

To carryout certain task a sentence must be written, which should satisfy the following :

1. Syntactically Correct

2. Grammatically correct

3. An action must also be associated. Then

4. It must be understood by the Executor for execution (must be made ready for execution).

In a very similar way, the statement of a program must satisfy the following in a compiler.

1. Syntactically correct (Lexical Anayser)

2. Grammatically correct (Syntax Anayser)

3. An action must also be associated (Syntax Directed Translation)

4. It must be understood by the Executor for execution. (Code generation and Execution)

NOTE~ There is another phase called code optimization which is an optional phase of the compiler).

The reader can relate the above analogy when learning the phases of the compiler in the subsequent sections.

1.4 TRANSLATOR

Translator is a program, which converts a program written in any Source Language into any other Destination language. The source and destination languages may be any High Level Language (HLL) or low level languages (LLL) like assembly language or machine language.

Page 16: ELEMENTS OF COMPII JER DESIGN · 5.1 SLR Parser 90 5. 2 CLR Parser 119 5. 3 LALR Parser 158 6. Syntax Directed Translations 174 6.1 Attributes 174 6.2 ... details about interpreters

Elements Of Compiler Design

Publisher : Laxmi Publications ISBN : 9789380856148 Author : M Joseph

Type the URL : http://www.kopykitab.com/product/11679

Get this eBook

40%OFF


Recommended