file no - rgpvonline.com course file compile…  · web viewtranslation of declaration,...

46
COLLEGE NAME, BHOPAL DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING COURSE FILE Program : B.E. Semester : VII Course Code : Subject Name : Automata and Compiler Design

Upload: buikiet

Post on 06-Feb-2018

218 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

COLLEGE NAME, BHOPAL

DEPARTMENT OF COMPUTER SCIENCE & ENGINEERING

COURSE FILE

Program : B.E.

Semester : VII

Course Code :

Subject Name : Automata and Compiler Design

Prepared By: Approved By

Page 2: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

Index

S.No. Contents Page No.

1. Scheme

2. Syllabus

3. Time Table

4. Lecture Plan

5. List of Books

6. Mid Semester Exam Question Papers

7. RGPV Question Paper

8. Tutorial Questions

9. Assignment Questions

10. Hand-Written Notes

11. Transparencies/PPT Slides

12. Mid Semester Exam Result

13. Attendance Sheet

Page 3: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information
Page 4: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

PROGRAMME: B.E. Information Technology VII SemesterIT 704 Elective –I (IT- 713- Automata and Compiler Design)

Unit I: Introduction: Alphabets, Strings and Languages; Automata and Grammars, Deterministic finite Automata (DFA)-Formal Definition, Simplified notation: State transition graph, Transition table, Language of DFA, Nondeterministic finite Automata (NFA), Equivalence of NFA and DFA, Minimization of Finite Automata, Regular Expressions, Arden’s theorem.

Unit II: Compiler Structure: Compilers and Translators, Various Phases of Compiler, Pass Structure of Compiler, Bootstrapping of Compiler. Lexical Analysis: The role of Lexical Analyzer, A simple approach to the design of Lexical Analyzer, Implementation of Lexical Analyzer. The Syntactic Specification of Programming Languages: CFG, Derivation and Parse tree, Ambiguity, Capabilities of CFG. Basic Parsing Techniques: Top-Down parsers with backtracking, Recursive Descent Parsers, Predictive Parsers,

Unit III: Bottom–up Parsers, Shift-Reduce Parsing, Operator Precedence Parsers, LR parsers (SLR, Canonical LR, LALR) Syntax Analyzer Generator: YACC, Intermediate Code Generation: Different Intermediate forms: three address code, Quadruples & Triples. Syntax Directed translation mechanism and attributed definition. Translation of Declaration, Assignment, Control flow, Boolean expression, Array References in arithmetic expressions, procedure calls, case statements, postfix translation.

Unit IV: Run Time Memory Management: Static and Dynamic storage allocation, stack based memory allocation schemes, Symbol Table management Error Detection and Recovery: Lexical phase errors, Syntactic phase errors, Semantic errors.

Unit V: Code Optimization and Code Generation: Local optimization, Loop optimization, Peephole optimization, Basic blocks and flow graphs, DAG, Data flow analyzer, Machine Model, Order of evaluation, Register allocation and code selection

References:

1.Louden, “Compiler construction”, Cengage learning .2. Alfred V Aho, Jeffrey D. Ullman, “Principles of Compiler Design”, Narosa.3. A.V. Aho, R. Sethi and J.D Ullman, “Compiler: principle, Techniques and Tools”, AW.4. Michal Sipser, “Theory of Computation”, Cengage learning.5. H.C. Holub, “Compiler Design in C”, Prentice Hall Inc.6. Hopcroft, Ullman, “Introduction to Automata Theory, Languages and Computation”, PearsonEducation.

Page 5: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

Time Table

Page 6: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

Department Information Technology Session : 2014Name of Teacher Sem VII

Subject Compiler Design Sub. Code IT-713

(B) TIME SCHEDULE : Total expected periods:___, Extra periods (if required)_____Lecture Plan

Day Mon Tue Wed Thu Fri Sat Max. available

No. ofPeriods

01 01 01 01 01 01

Lecture. Topics to be covered PlannedDate of Completion Remarks

UNIT-I

1 Alphabets, Strings and Languages R1:1, R2:1

2 Automata and Grammars R1:3, R2:1

3 Deterministic finite Automata (DFA) R1:10 , R2:5

4 State transition graph, Transition table R1:84,88,R2:74

5 Nondeterministic finite Automata (NFA R1:105, R2:103

6 Equivalence of NFA and DFA, R2:20

7 Minimization of FiniteAutomata

R1:725 ,R2:24

8 Regular Expressions, Arden’s theorem NOTES

Unit-II

9 Compilers and Translators R2:126

10 Various Phases of Compiler R2-147

11 Pass Structure ofCompiler, Bootstrapping of Compiler

R1:215,193, R2:158

12 The role of Lexical Analyzer R1:215,

Page 7: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

193, R2:158

13 A simple approach to the design of Lexical Analyzer, R1:215,193, R2:158

14 CFG, Derivation and Parse tree, R1:52,207, R2:146

15 Ambiguity, Capabilities of CFG. Basic Parsing R1:215,193, R2:158

16 Top-Down parsers with backtracking R1:227,233,R3:148

17 Recursive Descent Parsers R1:257

18 Predictive Parsers R1:299,R5:154

Unit-III

24 Bottom–up Parsers R1:360,R4:83-89

25 Shift-Reduce Parsing R1:-350

26 Operator Precedence Parsers R1-366

27 LR parsers NOTES

28 Syntax Analyzer Generator R1:408,R5:252

29 YACC, Intermediate Code Generation R1:413,R5:278

30 three address code R2:328, R2:336

31 Quadruples & Triples. R1:478,R2:254

32 . Translation of Declaration, Assignment R1:500,R2:271

33 Control flow, Boolean expression NOTES

34 Array References in arithmetic expressions R1:526,R2:521

35 procedure calls R1:28

36 case statements R1:526,

37 postfix translation NOTES

Unit –IV

Page 8: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

39 Run Time Memory Management R2-408

40 Static and Dynamic storage allocation R1:608,R2:410

41 stack based memoryallocation schemes

R1:608,R2:410

42 Symbol Table management Error Detection and Recovery R2-408

43 Lexical phase errors R3:661

44 Syntactic phase errors, Semantic errors R2-408

Unit –V

45 Code Optimization and Code Generation R2-408

46 Local optimization R1:608,R2:410

47 Loop optimization R1:608,R2:410

48 Peepholeoptimization

R2-408

49 Basic blocks and flow graphs R3:661

50 DAG R2-408

51 Data flow analyzer R1:608,R2:410

52 Machine Model R1:608,R2:410

53 Order of evaluation R2-408

54 Register allocation and code selection R3:661

Page 9: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

REFERENCE BOOKS

R1: Compilers Principles, Techniques, and Tools by Aho,Ullman & Sethi, Pearson Education

R2: Principles of Compiler Design by Aho & Ullman, Narosa Publishing House

R3:Compiler Construction 2/e: Dhamdhere

R4: Compiler Design by Santanu Chattopadhyay, PHI

R5: Compilers Construction & Design by Rajni Jindal, Umesh Publications

R6: Compiler Design by O. G. Kakde, Laxmi Publications (p) LTD

Websites:

1. www.cs.uccs.edu/~abudjen/classsnotes.doc 2. www.os.iitb.ac.in/~sri/notes/lexical.pdf 3. www.iitb.ac.in/~sri/notes/compiler/regex.pdf

Page 10: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

List of Books

1. A. V. Aho, R. Sethi, and J. D. Ullman. Compilers: Principles, Techniques and

Tools , Pearson Education

2 Raghavan, Compiler Design, TMH Pub.

3. Louden. Compiler Construction: Principles and Practice, Cengage Learning

4. A. C. Holub. Compiler Design in C , Prentice-Hall Inc., 1993.

5. Mak, writing compiler & Interpreters, Willey Pub.

Page 11: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

COLLEGE NAMEMID SEMESTER I

BRANCH- ITSEMESTER: VII

SUBJECT: COMPILER DESIGNSUBJECT CODE: IT-713

Max. Marks: 40 Time: 2 Hours Note: All questions are compulsory carry equal marks.

1. (a) What is Ad hoc network and how it differs from other network?

(b) Explain various challenges of mobile Ad hoc network.2. What is Source and Receiver oriented MAC Routing Protocol?3. Write the difference between-

a. Cellular and Mobile Ad hoc Networkb. Proactive and Reactive Routing protocol

4. Explain various design issues of Ad hoc Network in brief.5. Explain the Working of DSDV Routing protocol

Page 12: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

COLLEGE NAMEMID SEMESTER II

BRANCH- ITSEMESTER: VII

SUBJECT: COMPILER DESIGNSUBJECT CODE: IT-713

Max. Marks: 40 Time: 2 Hours Note: Attempt any four questions.

1. (a)

(b)

Explain dynamic source routing (DSR) protocols with its advantages and disadvantages.Evaluate the route discovery (RD) time parameter in the communication performance of ad-hoc network.

5

5

2. (a)(b)

Describe the power management at various layers.What are smart batteries? What are its characteristics ?

55

3. (a)

(b)

Explain the ATM cell header at UNI and at NNI.

What are the advantages and disadvantages of packet switching over circuit switching?

5

5

4. (a)

(b)

Draw and discuss the ATM protocol architecture model.

With neat sketch, explain architecture of 802.11 LAN. Also, explain its MAC logic.

5

5

5. Write short note on (any two)i) ZRP protocolii) EED performance in ad-hoc networkiii) X.25iv) AAL

10

Page 13: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

COLLEGE NAME, BhopalDepartment of Information Technology

Assignment-1

Subject: Compiler Design Subject Code: IT-713

Unit-1

1. Give the reasons for the separation of scanner and parser in separate phases of compiler.2. Describe the role of lexical analyzer in recognizing tokens.3. Explain the concept of input buffering.4. Explain how tokens are recognized.5. What is simple approach to design of lexical analyzer for an identifier.6. What’s LEX? Describe auxiliary definitions and translation rules for LEX with suitable

example.7. What are the tasks performed by the compiler in lexical and syntax analysis phases.

Explain with help of examples.

8. Explain role of symbol table in various phases of compiler

COLLEGE NAME, BhopalDepartment of Information Technology

Page 14: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

Assignment-2

Subject: Compiler Design Subject Code: IT-713

Unit-2

1. Write in brief about the error recovery procedure in LL and LR parsing.2. Write short note on automatic parser generator.3. Describe the function of LALR parser generator YACC.4. What is meant by syntax directed translation?5. Explain. Give the parse tree and translation for expression 23* 5+4 according to the syntax

directed translation scheme.6. Differentiate between synthesized translation and inherited translations.7. Let the synthesized attribute “val”, give the integer value associated with non terminals in

following grammar-

L→ E E →E +T | T T→ T*F | F F→(E)| digit

8. Write a brief note on syntax tree.9. For the following grammar find FIRST and FOLLOW sets for each non terminal-

S→aAB |bA|€ A→aAb|€ B→bB|€

Where € is Null string.

10. What is Shift-Reduce and Reduce-Reduce conflict? How these can be resolved? With examples explain in which condition S-R and R-R conflict can occur in SLR, Canonical LR and LALR parsers. (Make use of LR(0), LR(1) items.

Page 15: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

COLLEGE NAME, BhopalDepartment of Information Technology

Assignment-3

Subject: Compiler Design Subject Code: IT-713

Unit-3

1. What do you mean by heap allocation? Explain the following terms related to heap allocation-

(i) Fragmentation (ii) Free list(iii) Reference counts

2. Explain the difference between static, stack and heap allocation.

3. What is the difference between dynamic and static storage management?

4. What are different parameter passing mechanisms?

5. Explain with a suitable example, mechanisms, used by the compiler to handle procedure parameters.

6. Write short note on symbol table organization.

7. Explain various symbol table management techniques.

8. Explain various data structures used for implementing the symbol table and compare them.

9. What is hashing? What are different types of hashing techniques available?

COLLEGE NAME, Bhopal

Page 16: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

Department of Information Technology

Assignment-4

Subject: Compiler DesignSubject Code: IT-713

Unit-4

1. Define Leaders.2. Explain DAG construction.3. What are applications of DAGs?4. Write advantages of DAG.5. Write short note on application of DAG in code generation.6. Discuss the various methods of translating Boolean expression.7. Construct DAG of basic block after converting code in 3-address representation-

BeginProd:=0;i :=1;dobeginprod:= prod+a[i]*b[i];i:=i+1;endwhile i<=20end

8. Translate the following expression into quadruples, triples and indirect triples.

Page 17: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

COLLEGE NAME, BhopalDepartment of Information Technology

Assignment-5

Subject: Compiler DesignSubject Code: IT-713

Unit-5

1. What is global data flow analysis? What is its use in code optimization?2. Describe global data flow analysis.3. Write the criteria for code improving transformations. Explain the principal sources of

optimization.4. Define dominators and write short note on loops in flow graph.

Page 18: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

COLLEGE NAME, BhopalDepartment of Information Technology

Tutorial-1

Subject: Compiler DesignSubject Code: IT-713

Topic to be covered: Unit-1

9. Explain why one should study about the compilers.

10. Explain working of compilers drawing its block diagram.

11. What are the features of good compiler?

12. Compare and contrast the features of single pass compiler with multi pass compiler.

13. Write short note on bootstrapping.

14. Compiler uses two passes scheme of compilation, performing analysis, in first pass and

synthesis in second pass. Explain what use is made of the symbol table in two passes of

compiler.

15. Draw and explain various phases of compiler

16. Draw the block diagram of lexical and syntax analyzer and state clearly input , output and

task performed by them.

Page 19: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

COLLEGE NAME, BhopalDepartment of Information Technology

Tutorial-2

Subject: Compiler DesignSubject Code: IT-713

Unit-2

1. Write short note on grammar. What do you mean by ambiguity of grammar?2. What is top down parsing? What are the difficulties encountered in this and how they

overcome?3. Write short note on recursive decent parser.4. What is predictive parser? How a parser is controlled by a program?5. Differentiate top down and bottom up parsing. Give examples of method of each type.6. What is operator precedence grammar?7. What Describe operator precedence parsing algorithm.8. What are the limitations of operator precedence parsing?9. What do you understand by LR(k) grammar?10. Write the Algo/ Procedure for construction of canonical LR parsing table.

COLLEGE NAME, BhopalDepartment of Information Technology

Page 20: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

Tutorial-3

Subject: Compiler DesignSubject Code: IT-713

Unit-3

1. Give a brief description of type checking.

2. Give the difference between implicit type conversion and explicit type conversion with

help of an example.

3. Differentiate between static and dynamic binding.

4. Explain the importance of run time storage management in compiler.

5. What do you mean by activation record? Why this record is maintained by the compiler?

6. Explain various fields of activation record.

7. Describe storage allocation strategies.

8. What do you mean by static allocation? What are its drawback?

Page 21: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

COLLEGE NAME, BhopalDepartment of Information Technology

Tutorial-4

Subject: Compiler DesignSubject Code: IT-713

Unit-4

9. Write a translation scheme to generate intermediate code for assignment statement with array references.

10. Write syntax directed definition to translate ‘switch’ statement. With a suitable example, show translation of the source language ‘switch’ statement.

11. Write short note on back patching.12. Write short note on code generation.13. What are general issues in designing a code generator?14. Explain code generation algorithm.15. What is basic block? With suitable example discuss various transformations on the basic

blocks.16. What are the different types of intermediate codes? Explain in brief.

COLLEGE NAME, Bhopal

Page 22: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

Department of Information Technology

Tutorial-5

Subject: Compiler DesignSubject Code: IT-713

Unit-5

1. Explain why code optimization is called optional phase?2. Explain the principal sources of optimization with suitable example.3. Describe the necessary and sufficient condition performing constant propagation and

dead code eliminations.4. Explain the following optimizing transformations with suitable examples-

(i) Code movement(ii) Common sub-expression elimination

5. Define dominators.

Page 23: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

Unit wise blow- Up

UNIT – I

A language translator is a program which translates programs from source language in an equivalent program in an object language. The source language is usually a high-level programming language and the object language is usually the machine language of an actual computer. A compiler is a translator whose source language is a high-level language and whose object language is close the machine language of an actual computer. The typical compiler consists of an analysis phase and a synthesis phase. In contrast with compilers an interpreter is a program which simulates the execution of programs written in a source language. Interpreters may be used either at the source program level or an interpreter may be used it interpret an object code for an idealized machine. The typical compiler consists of several phases each of which passes its output the next phase

(i) The lexical phase (scanner) groups characters in lexical units or kens. The input the lexical phase is a character stream. The output is a stream of kens. Regular expressions are used define the kens recognized by a scanner (or lexical analyzer). The scanner is implemented as a finite state machine.

(ii) The parser groups kens in syntactical units. The output of the parser is a parse tree representation of the program. Context-free grammars are used define the program structure recognized by a parser. The parser is implemented as a push-down automata.

(iii) The contextual analysis phase analyzes the parse tree for context-sensitive information often called the static semantics. The output of the contextual analysis phase is an annotated parse tree. Attribute grammars are used describe the static semantics of a program.

(iv) The optimizer applies semantics preserving transformation the annotated parse tree simplify the structure of the tree and facilitate the generation of more efficient code.

(v) The code general transforms the simplified annotated parse tree in object code using rules which denote the semantics of the source language.

(vi) The peephole optimizer examines the object code, a few instructions at a time, and attempts do machine dependent code improvements.

Types of Compiler-

1. One-pass compiler2. Multi-pass Compiler3. Load & Go Compiler4. Optimized Compiler

A one-pass compiler reads the program only once, and translates it at the same time as it is reading. A multi-pass compiler reads the program several times, each time transforming it in a different form and usually in a different data structure.

Cross Compiler-A cross compiler compiles a target language different from the language of the machine it runs on

Page 24: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

Bootstrapping- Bootstrapping describe the techniques involved in writing a compiler or assembler) in the target programming language which it is intended compile.

LEX- LEX is a program general designed for lexical processing of character input streams. It accepts a high-level, problem oriented specification for character string matching, and produces a program in a general purpose language which recognizes regular expressions. The regular expressions are specified by the user in the source specifications given LEX. The LEX written code recognizes these expressions in an input stream and partitions the input stream in strings matching the expressions

Page 25: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

UNIT II

The parsing is a process of finding a parse tree for a string of kens. Equivalently, it is a process of determining whether a string of kens can be generated by a grammar. There are two types of Parsing

Top-down Parsing (start from start symbol and derive string)A Top-down parser builds a parse tree by starting at the root and working down wards the leaves.

o Easy generate by hand. o Examples are- Recursive- descent, Predictive.

Bottom-up Parsing (start from string and reduce start symbol)A bottom- up parser builds a parser tree by starting at the leaves and working up wards the root.

o Not easy handle by hands, usually compiler- generating software generate bottom up parser

o But handles larger class of grammar o Example is LR parser.

Operator precedence parsing- Bottom-up parsers for a large class of context-free grammars can be easily developed using operator grammars. Operator grammars have the property that no production right side is empty or has two adjacent non terminals. This property enables the implementation of efficient operator-precedence parsers

SLR parser - Simple LR parser or SLR parser is an LR parser or which the parsing tables are generated as for an LR(0) parser except that it only performs a reduction with a grammar rule A → w if the next symbol on the input stream is in the follow set of A. Such a parser can prevent certain shift-reduce and reduce-reduce conflicts. That occur in LR (0) parsers and it can therefore deal with more grammars. However, it still cannot parse all grammars that can be parsed by an LR(1) parser. A grammar that can be parsed by an SLR parser is called a SLR grammar.

Yacc-YACC stands for “Yet Another Compiler-Compiler”. this is because this kind of analysis of text files is normally associated with writing compilers. Yacc provides a general ol for imposing structure on the input a computer program. The Yacc user prepares a specification of the input process; this includes rules describing the input structure, code be invoked when these rules are recognized, and a low-level routine do the basic input. Yacc then generates a function control the input process

Page 26: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

UNIT III

A syntax- directed translation is used define the translation of a sequence of kens some other value, based on a CFG for the input. A syntax- directed translation is defined by associating a translation rule with each grammar rule. A translation rule defines the translation of the left- hand -side conterminal as a function of eight-hand- side no terminals' translations, and the values of the right-hand-side terminals. compute the translation of a string, build the parse tree, and use the translation rules compute the translation of each conterminal in the tree, bottom-up; the translation of the string is the translation of the root conterminal. There is no restriction on the type of converted actions that manipulate the parser's semantic stack. Each action must pop all right-hand-side non terminals' translations from the semantic stack, then compute and push the left-hand-side non terminal's translation. Next, the actions are incorporated (as action numbers) in the grammar rules. Finally, the grammar is converted LL (1) form (treating the action numbers just like terminal or conterminal symbols).

Intermediate Code- The semantic phase of a compiler first translates parse trees in an intermediate representation (IR), which is independent of the underlying computer architecture, and then generates machine code from the IRs. This makes the task of retargeting the compiler another computer architecture easier handle

1. High-Level Intermediate Languages

Abstract syntax tree. Postfix notation is a linearized representation of an abstract syntax tree.

EXAMPLE- a = b*c + d is a b c * d + assign

Dependence graphs.

Medium-Level Intermediate Language

Reflect a range of features in a language-independent way. Represent source variables, temporaries, and registers. Appropriate for most of the optimizations done in compilers such as common-sub expression elimination, code motion, and algebraic simplification.

Three address code (x = y op z), o assignment statements x := y op z o assignment statements x := op y o copy x := y o unconditional jump go L o conditional jump if re loop go L

Page 27: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

o param x and call p, n for procedure calls and return y -- where y representing a return value is optional

o indexed statements of the form x:= y[i], x[i] := y o address and pointer assignments of the form x := &y and x := *y

Low-Level Intermediate Languages

one--one correspondence target machine instructions, and are usually architecture dependent. Usually deviate only when there are alternatives for the most effective code generate for them. E.g., multiply instruction or more complex addressing modes.

Page 28: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

UNIT IV

Activation records Created every time a procedure is called Must be accessible both the caller and the callee Allocates space for

o Parameterso Local variableso Return addresso Other links and pointers provide access non-local data

Other issueso Initializing local variableso Stack vs. heap allocatedo Optimizing activation records by coalescing

Symbol Table keeps track of scope and other attributes of named program entities key operations

o void insert(symbol s); o symbol lookup(string name); o void enter_scope(void); o void exit_scope(void);

implementations o list o hash table o stack of tables

for some languages, the symbol table must handle overloading o each identifier contains a list of symbols o when entering new scope, chain symbols with same name in previous scope

Page 29: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

UNIT V

Directed Acyclic Graph (DAG)– Directed acyclic graph DAG-• Contraction of AST that avoids duplication- identicalsub trees are reused.• exposes redundancies- changes (assignments, calls) ?• smaller memory footprint

o Example- a x (a-b) + c x (a-b)

Code optimization together with code generation form the back end of the compiler.  In compilers with a very extensive optimization, the optimization phase is distinguished as a middle end.

The goal of the compiler's optimizer is transform the IR program created by the front end in an IR program that computes the same results in a better way. Here "better" can take on many meanings. It usually implies faster code, but it might imply more compact code, less power when it runs or costs less run under some model.

Ideally, compilers should produce target code that is as good as can be written by hand. The reality is that this goal is achieved only in limited cases, and with difficulty. However, the code produced by straightforward compiling algorithms can often be made run faster or take less space, or both.  This improvement is achieved by program transformations that are traditionally called optimizations, although the term "optimization" is a misnomer because is rarely a guarantee that the resulting code is the best possible. Most of the compilers involve some optimization. Compilers that apply code-improving transformations are called optimizing compilers.

Code generation is the final phase in compilation. It takes as input an intermediate representation of the source program and produces as output an equivalent target program. The code generation techniques can be used whether or not an optimization phase occurs before code generation.

The requirements traditionally imposed on a code generar are severe. The output code must be correct and of high quality, meaning that it should make effective use of the resources of the target machine. Moreover, the code generar itself should run efficiently. 

Page 30: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

Mathematically, the problem of generating optimal target code is (TM) undecidable!

In practice, we must be content with heuristic techniques that generate good, but not necessarily optimal, code. The choice of heuristics is important, because a carefully designed code generation algorithm can easily produce code that is several times faster that produced with ad hoc code generation techniques.

As code generation begins, the program exists in IR form. The code generar must convert the IR program (perhaps, already optimized) in code that can run on the target machine.

The code generation is performed typically as a sequence- instruction selection, instruction scheduling and register allocation-

o Instruction selection - selecting a sequence of target-machine operations that implement the IR operations.

o Instruction scheduling - choosing an order in which the operations should execute.

o Register allocation - deciding which values should reside in registers at each point in the program

Most compilers handle each of these three processes separately. The term code generation is often used refer instruction selection only.

When the level of abstraction of the IR and the target machine differ significantly, or the underlying computation models differ, instruction selection can play a critical role in bridging the gap. The extent which instruction selection can map the computation in the IR program efficiently the target machine will often determine the efficiency of the generated code. For example, consider three scenarios for generating code from an IR-

o A simple, scalar RISC machine - the mapping from IR assembly is straightforward. The code generar might consider only one or two assembly-language sequences for each IR operation.

o A CISC processor - make effective use of a CISC's instruction set, the compiler may need aggregate several IR operations in a single target-machine operation.

o A stack machine - the code generar must translate from the register--register computational style of IR a stack based style with its implicit names and, in some cases, destructive operations.

As the gap in abstraction between the IR and the target ISA grows, so does the need for ols help build code generars.

While instruction selection can play an important role in determining code quality, the compiler writer must keep in mind the enormous size of the search space that the instruction select might explore. As we shall see, even moderately sized instruction sets can produce search spaces that contain hundreds of millions of states. Clearly, the compiler cannot afford explore such spaces in either a careless or an exhaustive way.

Previous Univ. Exam Paper

Page 31: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

CS – 701B.E. (Seventh Semester ) EXAMINATION , June, 2009

(Common for CS & IT Engg.)COMPILER DESIGN

Time - Three HoursMaximum Marks - 100

Minimum Pass Marks - 35

Note - Attempt all questions. All question carry equal marks.

1. (a) Consider the following LEX program -

Auxiliary Definitions(none)

Translation RulesToken Pattern Action

Key wordIdentifierConstantOperator

{Return KW}(Return id}

{Return const.}{Return OP}

Implement the LEX program as DFA.

(b) Describe the role of a lexical analyzer and also explain the concept of Input Buffering.OR

2. (a) Consider a finite – state automata in which -

S = {q0, q1, q2, q3}, Σ = {0, 1} S0 = q0 F = {q0}

And the transition function δ is given in the following table -

State 0 1q0q1q2q3

q2q3q0q1

q1q0q3q2

a) Give the entire sequence of states for the input string 101101.

Page 32: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

b) Find out the string among the following string which are accepted by the given finite- state automation -

c) 101101d) 11111e) 000000

(b) Explain why should one study about compilers.

3. (a) What do you mean by ambiguous and unambiguous grammar? Explain with example.(b) Construct the parsing table for the following grammar.

S aXYbX c/ €Y d/€

OR4. (a) Consider the grammar -

S ACB/ C b B/ B aA da /BCB g / €C h / €

Calculate FIRST and Follow.

(b) Show that the following grammar is U (1), by constructing its parse table-S a AB / b A / € A a A b / €B b B / €

5. (a) Translate the following statement to quadruple -

If a > b then x = a + b else x = a - b

(b) Translate the following expression to quadruple, triple-

- (x + y) * (z + c) – (x + y + z)

OR

6. Write short notes on the following -

7. S- attributed definitions8. L- Attributed definition9. Dependency graph

Page 33: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

10. (a) Explain the role of symbol table in various phases of compiler.

(b) What do you mean by heap allocation? Explain the following terms related to heap allocation-

11. Fragmentation 12. Free list13. Bit map14. Reference counts

OR

15. (a) Explain various storage allocation strategies. Which storage allocation model is to be used if a language permits recursion ?

(b) Describe parameter passing mechanisms for a procedure call.

16. (a) Describe the necessary and sufficient conditions for performing constant propagation and dead code eliminations.

(b) Write and explain data flow equations.

OR17. Explain the following -

18. Loop invariant conditionals 19. Conditional depending on loop index20. Independent loop conditionals21. Reduction loops

***********************************************************************

Model Paper

CS-701

Page 34: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

COMPILER DESIGNTime - 3 Hrs

Max. Marks-100Minimum Pass Marks - 35

Note - Solve any five questions. All questions carry equal marks.

1.(a) What is the basic task of scanning. What are the difficulties faced in delimeter oriented scanning. How can this be removed.

(b) What is the structure of a compiler. Discuss phases of a compiler briefly.

2. (a) What are errors can be encountered by virtually all the phases of a compiler.(b) Explain the concept of transition diagram and its use in building a lexical analyzer.

3 (a) Why are multiple passes required in a compiler. Describe strategies for reducing the number of passes.(b) What are data structures used for symbol tables management. Explain each of them in brief.

4(a) Comment on the need for indirection in symbol table. (b) What is p-down parsing. What are the difficulties encountered in it and how are they overcome.

5(a) Write the quadruples, triples and indirection triples for the expression -

A B + C*D | E

(b) Why is the dynamic srage allocation strategy particularly amendable the run-time usage requirements of block structured languages. Write the procedure for run-time address calculation in our dynamic srage allocation mode.

6. (a) Describe Global Data Flow analysis. How is it useful in code optimization.(b) Define the ambiguity of grammer. Suggest some possible solutions remove the ambiguity of a grammer.

7. Given a grammer with the following productions. - S EE T | E – TT F | T * FF i | (E)

Generate the sets of LR (1) items Give a trtace of the Parse of the following input string (i – i) * i.

8. What is operar precedence grammer? For given the grammer:

Page 35: FILE NO - rgpvonline.com course file compile…  · Web viewTranslation of Declaration, Assignment, Control flow ... DAG, Data flow analyzer, Machine Model, ... Department of Information

E E + T | TT T * F | FF (E) | id

Obtain the following -

Operator precedence matrix The parse for input string id + id