34
DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING QUESTION BANK PRINCIPLES OF COMPILER DESIGN UNIT I LEXICAL ANALYSIS Introduction to Compiling- Compilers-Analysis of the source program-The phases- Cousins-The grouping of phases-Compiler construction tools. The role of the lexical analyzer- Input buffering-Specification of tokens-Recognition of tokens-A language for specifying lexical analyzer. UNIT II SYNTAX ANALYSIS and RUN-TIME ENVIRONMENTS Syntax Analysis- The role of the parser-Context-free grammars- Writing a grammar-Topdown parsing-Bottom-up Parsing-LR parsers- Constructing an SLR(1) parsing table. Type Checking- Type Systems-Specification of a simple type checker. Run-Time Environments-Source language issues-Storage organization- Storage-allocation strategies. UNIT III INTERMEDIATE CODE GENERATION Intermediate languages-Declarations-Assignment statements - Boolean expressions- Case statements- Backpatching-Procedure calls UNIT IV CODE GENERATION Issues in the design of a code generator- The target machine- Run-time storage management-Basic blocks and flow graphs- Next-use information-A simple code generator-Register allocation and assignment-The dag 1

PCD 2 marks

Embed Size (px)

DESCRIPTION

principle-of-Compiler-Degin

Citation preview

JEPPIAAR ENGINEERING COLLEGE

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

QUESTION BANK

PRINCIPLES OF COMPILER DESIGN

UNIT I LEXICAL ANALYSIS Introduction to Compiling- Compilers-Analysis of the source program-The phases-Cousins-The grouping of phases-Compiler construction tools. The role of the lexicalanalyzer- Input buffering-Specification of tokens-Recognition of tokens-A language forspecifying lexical analyzer.

UNIT II SYNTAX ANALYSIS and RUN-TIME ENVIRONMENTS Syntax Analysis- The role of the parser-Context-free grammars-Writing a grammar-Topdown parsing-Bottom-up Parsing-LR parsers-Constructing an SLR(1) parsing table.Type Checking- Type Systems-Specification of a simple type checker. Run-TimeEnvironments-Source language issues-Storage organization-Storage-allocationstrategies.

UNIT III INTERMEDIATE CODE GENERATION Intermediate languages-Declarations-Assignment statements - Boolean expressions-Case statements- Backpatching-Procedure calls

UNIT IV CODE GENERATION Issues in the design of a code generator- The target machine-Run-time storagemanagement-Basic blocks and flow graphs- Next-use information-A simple codegenerator-Register allocation and assignment-The dag representation of basic blocks -Generating code from dags.

UNIT V CODE OPTIMIZATION Introduction-The principle sources of optimization-Peephole optimization- Optimization ofbasic blocks-Loops in flow graphs- Introduction to global data-flow analysis-Codeimproving transformations. TOTAL: 75 PERIODSTEXT BOOK:1. Alfred V. Aho, Ravi Sethi Jeffrey D. Ullman, Compilers- Principles, Techniques, andTools, Pearson Education Asia, 2007.REFERENCES:1. David Galles, Modern Compiler Design, Pearson Education Asia, 20072. Steven S. Muchnick, Advanced Compiler Design & Implementation,MorganKaufmann Pulishers, 2000.3. C. N. Fisher and R. J. LeBlanc Crafting a Compiler with C, Pearson Education,2000.UNIT I

PART-A

1. What is Error Handler?

The error handler invokes error diagnostic routines and also generates error messages. It works in conjunction with all the phases to handle the errors at the respective phases.

2. Define Tokens. APRIL/MAY 2011The token can be defined as a meaningful group of characters over the character set of the programming language like identifiers, keywords, constants and others.

3. Define Symbol Table.

A Symbol Table is a data structure containing a record for each identifier, with fields for the attributes of the identifier.

4. Name some variety of intermediate forms.

Postfix notation or polish notation.

Syntax tree

Three address code

Quadruple

5. What is a Complier? MAY/JUNE 2007

A Complier is a program that reads a program written in one language-the source language-and translates it in to an equivalent program in another language-the target language . As an important part of this translation process, the compiler reports to its user the presence of errors in the source program.6. What are the cousins of compiler?April/May2004,April/May2005,April/May 2012 The following are the cousins of compilers i. Preprocessors

ii. Assemblers

iii. Loaders

iv. Link editors.7. What are the main two parts of compilation? What are they performing? APRIL/MAY 2010 The two main parts are

Analysis part breaks up the source program into constituent pieces and creates

An intermediate representation of the source program.

Synthesis part constructs the desired target program from the intermediate

Representation.8. State some compiler construction tools? Arpil /May 2008

a. Parse generator

b. Scanner generators

c. Syntax-directed translation engines

d. Automatic code generator

i. Data flow engines.

9. What is a Loader? What does the loading process do?

A Loader is a program that performs the two functions

i. Loading

ii .Link editing

The process of loading consists of taking relocatable machine code, altering the relocatable address and placing the altered instructions and data in memory at the proper locations.

10. What is a preprocessor? Nov/Dev 2004

A preprocessor is one, which produces input to compilers. A source program may be divided into modules stored in separate files. The task of collecting the source program is sometimes entrusted to a distinct program called a preprocessor.

The preprocessor may also expand macros into source language statements.

Skeletal source program

Preprocessor

Source program

11. What is an assembler and interpreter? APRIL/MAY 2011Assembler is a program, which converts the assembly language in to machine language.

Interpreter is a program which converts source language into machine language line by line.

12. What is a lexeme? Define a regular set. APRIL/MAY 2011 A Lexeme is a sequence of characters in the source program that is matched by the pattern for a token.

A language denoted by a regular expression is said to be a regular set

13. What is a sentinel? What is its usage? April/May 2004

A Sentinel is a special character that cannot be part of the source program. Normally we use eof as the sentinel. This is used for speeding-up the lexical analyzer.

14. What is a regular expression? State the rules, which define regular expression? MAY/JUNE 2007

Regular expression is a method to describe regular Language Rules:1) 2) -is a regular expression that denotes {} that is the set containing the empty string

3) If a is a symbol in ,then a is a regular expression that denotes {a}

4) Suppose r and s are regular expressions denoting the languages L(r ) and L(s) Then,

a) (r )/(s) is a regular expression denoting L(r) U L(s).

b) (r )(s) is a regular expression denoting L(r )L(s)

c) (r )* is a regular expression denoting L(r)*.

d) (r) is a regular expression denoting L(r ).

15. What are the Error-recovery actions in a lexical analyzer? April/May 2012, APRIL/MAY 2005 Deleting an extraneous character

Inserting a missing character

Replacing an incorrect character by a correct character

Transposing two adjacent characters16. Draw a transition diagram to represent relational operators. NOV/DEC 2007 Start < = return( relop, LE)

>

Other return(relop, NE)

= *

return(relop,EQ) return(relop,LT)

> =

return(relop,GE)

Other *

Return(relop, GT)

17. What are the issues to be considered in the design of lexical analyzer? MAY/JUNE 2009

How to Precisely Match Strings to Tokens

How to Implement a Lexical Analyzer18. Define concrete and abstract syntax with example. MAY/JUNE 2009The grammar is mainly used for parsing only - the key is to resolve all ambiguities. This grammar is called Concrete Syntax. Abstract Syntax is used to characterize the essential structure of the program -

the key is to be as simple as possible; it may contain ambiguities.

Traditional Compilers do semantic analysis on Concrete Syntax

Modern Compilers do the semantic analysis on Abstract Syntax tree

19. Differentiate compiler and interpreter APRIL/MAY 2008An interpreter is atranslator used to convert high level program into machine code. These translators translate the program line by line. A Compiler is also a translator which is used to translate the whole source of a program at a time.

20. Write short notes on buffer pair. APRIL/MAY 2008Lexical analyzer will detect the tokens from the source language with the help of input buffering. The reason is, the lexical analyzer will scan the input character by character, it will increase the cost file read operation.. So buffering is used. The buffer is a pair in which each half is equal to system read command.

21. How is the token structure is specified? APRIL/MAY 2010.

Token structure is specified with the help of Pattern. The pattern can be described with the help of Regular Expression

22. What is the role of lexical analyzer? NOV/DEC 2011Its main task is to read input characters and produce as output a sequence of tokens that parser uses for syntax analysis. Additionally task is removing blank, new line and tab characters

23. Give the transition diagram for an identifier. NOV/DEC 2011

.

PART B [Text Book:Alfred Aho,Ravi Sethi,V.Jeffery Ullman]

1. What are the various phases of a compiler. Explain each phase in detail. (Page No.10) April/May 2011, April/May 20122. Explain the various Compiler Construction Tools. (Page No.22) April/May 2011, April/May 20123. Explain Input Buffering with example. (Page No.88) November/December 20114. Explain the role Lexical Analyzer. (Page No.84) April/May 20115. Explain the specification of tokens. (Page No.92) APRIL/MAY 20086. Elaborate in detail the recognition of tokens. (Page No.98) April/May 20127. What are the issues in Lexical analysis. (Page No.84) April/May 20128. Draw the transition diagram for relational operators and unsignednumbers in Pascal. (Page No.101 & 102) April/May 2011 UNIT II

PART A

9. Differentiate Top Down Parser And Bottom Up Parser? Give Example for each. APRIL/MAY 2010.Top Down Parser are the parsers which constructs the parse tree from the root to the leaves in pre- order for the given input string. Predictive Parser, Recursive Descendent Parser.Bottom Up Parser are the parsers which constructs the parse tree from the leaves to the root for the given input string. LR Parser, SLR Parser.10. Define Handles. MAY/JUNE 2007, APRIL/MAY 2005A handle of a string is a substring that matches the right side of a production. This reduction helps in constructing the parse tree or right most derivation.

11. Define ambiguous grammar with an example, and specify it demerits

April/May 2012 , NOV/DEC 2007If a grammar produces more than one parse tree for the given input

string then it is called ambiguous grammar. It's demerit is

It is difficult to select or determine which parse tree is suitable for an input string.

Ex: E E+E / E*E / id

12. Mention the properties of parse tree.

The root is labeled by the start symbol.

Each leaf is labeled by a token or by ( Each interior node is labeled by a non terminal

If A is the Non terminal, labeling some interior node and x1, x2, x3 ( .xn

are the labels of the children.

13. What do you mean by a syntax tree?

Syntax tree is a variant of a parse tree in which each leaf represents an operand and each interior node represents an operator.

14. Define Handle pruning. NOVEMBER/DECEMBER 2011, APRIL/MAY 2011 A technique to obtain the rightmost derivation in reverse (called canonical reduction sequence) is known as handle pruning (i.e.) starting with a string of terminals w to be parsed. If w is the sentence of the grammar then (=(n where (n is the nth right sentential form of unknown right most derivation.

15. What are the goals of error handler in a parser? APRIL/MAY 2005, NOV/DEC 2007 The error handler in a parser has simple-to-state goals:

It should report the presence of errors clearly and accurately.

It should recover from each error quickly enough to be able to detect subsequent errors.

It should not significantly slow down the processing of correct programs.16. How will you define a context free grammar?

A context free grammar consists of terminals, non-terminals, a start symbol, and productions.

i. Terminals are the basic symbols from which strings are formed. Token is a synonym for terminal. Ex: if, then, else. ii. Nonterminals are syntactic variables that denote sets of strings, which help define the language generated by the grammar. Ex: stmt, expr.

iii. Start symbol is one of the nonterminals in a grammar and the set of strings it denotes is the language defined by the grammar. Ex: S.

iv.The productions of a grammar specify the manner in which the terminals and nonterminals can be combined to form strings Ex: expr-> id

17. Differentiate sentence and sentential form. SentenceSentential form

If S=>w then the string w is called Sentence of G.If S=>a then a is a sentential form of G.

Sentence is a string of terminals. Sentence is a sentential form with no nonterminals. Sentential form may contain non terminals

18. What is meant by Predictive parsing? Nov/Dec 2007 A special form of Recursive Descent parsing, in which the look-ahead symbol unambiguously determines the procedure selected for each nonterminal, where no backtracking is required.

19. What do you mean by viable prefixes? The set of prefixes of right sentential forms that can appear on the stack of a shift-reduce parser are called viable prefixes.

A viable prefix is that it is a prefix of a right sentential form that does not continue the past the right end of the rightmost handle of that sentential form.

20. What are the conflicts during Shift reduce parsing?There are context free grammars for which shift reduce parsing cannot be used. Every shift reduce parser for such a grammar can reach a configuration in which the parser knowing the entire stack contents and the next input symbol cannot decide whether to shift or to reduce a shift/reduce conflict or cannot decide which of several reductions to make a reduce/reduce conflict.

21. When procedure call occurs, what are the steps to be takes placed? NOV/DEC 2007 State of the calling procedure must be saved, so that it can resume after completion of procedure.

Return address is saved, in this location called routine must transfer after completion of

procedure

22. List out the actions involved in shift reduce parsing. MAY/JUNE 2007 Shift.

Reduce.

Accept. Error.23. What are the algebraic properties of regular expressions? APRIL/MAY 2005 Union

Kleene Closure

Positive Closure

24. What is left factoring? Give an example. NOV/DEC 2007

Left factoring is a grammar transformation that is useful for producing a grammar suitable for predictive parsing.

25. Derive the string and construct a syntax tree for the input string ceaedbe using

the grammar S->SaA|A,A->AbB|B,B->cSd|e MAY/JUNE 2009

Refer class notes.

26. List the factors to be considered for top-down parsing. MAY/JUNE 2009

We begin with the start symbol and at each step,

expand one of the remaining nonterminals by replacing it with the right side of one of its

productions. We repeat until only terminals remain. The top-down parse produces a

leftmost derivation of the sentence

27. Construct a parse tree of (a+b)*c for the grammer E->E+E/E*E/(E)/id. APRIL/MAY 2008

Refer class notes.

28. Eliminate immediate left recursion for the following grammer

E->E+T/T,T->T*F,F->(E)/id. APRIL/MAY 2008

Refer class notes

29. Eliminate Left Recursion for the given grammar. MAY/JUNE 2007E ( E + T | T T ( T * F | F F ( ( E )| id

E (TE'

E' ( +TE' |

T ( FT'

T' (*FT' |

F (( E ) | id

30. What are the limitations of static allocation? APRIL/MAY 20111. The size of the data object and constraints on its position in memory must be known

at compile time.

2. Recursive procedures are restricted, because all activations of a procedure use the

same bindings for local names.

3. Data structures cannot be created dynamically, since there is no mechanism for

storage allocation at run time.

31. Mention the two rules for type checking. Nov/Dec 2011

type checker for a language is based on information about the syntactic constructs in the language, the notion of types, and the rules for assigning types to language constructs.

32. What is dangling reference APRIL/MAY 2012

Adangling referenceoccurs when there is a reference to storage that has been deallocated. It is a logical error to use dangling references, since the value of deallocated storage is undefined according to the semantics of most languagesPART B [Text Book:Alfred Aho,Ravi Sethi,V.Jeffery Ullman]

1. Explain Top- Down Parsing and Bottom up Parsing. C MAY/JUNE 2007

2. Explain Error Recovery in Predictive Parsing. (Page No.192) MAY/JUNE 2007 NOV/DEC 2007 APRIL/MAY 2005

3. Distinguish between the source text of a procedure and its activation at run time.

(Page No.389) April/May 2011 , Nov/Dec 2011, NOV/DEC 20074. Discuss the various storage allocation strategies in detail. (Page No.401) April/May 2011 , Nov/Dec 2011, NOV/DEC 20075. i) Construct an SLR parsing table for the above grammar.

E -> E + T

E -> T

T -> T * F

T -> F

F -> (E)

F-> id (Refer Notes) MAY/JUNE 2009, April/May 2011, APRIL/MAY 20086. Construct the predictive parser for the following grammar: S -> (L) | a

L -> L,S | S.

Construct the behavior of the parser on the sentence (a, a) using the grammar

specified above. (Refer Notes) April/May 2012 , MAY/JUNE 2007,

APRIL/MAY 2005,

7. Give an algorithm for finding the FIRST and FOLLOW positions for a given non-terminal. MAY/JUNE 2009 APRIL/MAY 20088. Consider the grammar,

E -> E + T

E -> T

T -> T * F

T -> F

F -> (E)

F-> id

Construct a predictive parsing table for the grammar given above. Verify whether the input string id + id * id is accepted by the grammar or not. (Refer Notes)

MAY/JUNE 2009 APRIL/MAY 20089. Check whether the following grammar is a LL(1) grammar

S-> iEtS | iEtSeS | a

E-> b

Also define the FIRST and FOLLOW procedures. (Refer Notes) APRIL/MAY 2005

10. Consider the grammar E -> E + E | E * E | (E) | id

Show the sequence of moves made by the shift-reduce parser on the input id1 + id2 * id3 and determine whether the given string is accepted by the parser or not.

(Refer Notes) MAY/JUNE 2007

11. What is a shift-reduce parser? Explain in detail the conflicts that may occur during shift-reduce parsing. (Page No.201) April/May 2012

12. Consider the grammar given below.

E -> E + T

E -> T

T -> T * F

T -> F

F -> (E)

F-> id

Construct an LR parsing table for the above grammar. Give the moves of LR parser

on id*id+id (Refer Notes) MAY/JUNE 2007

13. Explain in detail about the specification of a simple type checker. (Page No.348)

April/May 2012 14. How to subdivide a run time memory into code and data areas. Explain. (Page No.396) April/May 2012 15. Explain the non-recursive predictive parsing with its algorithm. (Page No.190) NOV/DEC 200716. Explain the LR parsing algorithm in detail. (Page. No. 218) NOV/DEC 2007 APRIL/MAY 2005UNIT III

PART A

1. What is Data flow engine? APRIL/MAY 2008.

Much of the information needed to perform good code optimization involves data flow analysis, the gathering of information about how values are transmitted from one part of a program to each other part. This is handled by the data flow engines.

2. What are Syntax directed translation engines? MAY/JUNE 2007These produce collections of routines that walk the parse tree generating intermediate code.

3. What do you mean by DAG? NOVEMBER/DECEMBER 2011.It is Directed Acyclic Graph. In this common sub expressions are

eliminated. So it is a compact way of representation.

4. Define quadruple and give one example.

A quadruple is a data structure with 4 fields like operator, argument-1 ,

argument-2 and result.

Example: a=b*-c

OperatorArgument -1Argument -1Result

(0)Uminus c T1

(1)* b T1T2

(2)=a

5. What is the merit of quadruples? APRIL/MAY 2012If we move a statement computing a variable, A then the statements using 'A'

requires no change. That is A can be computed anywhere before the utilization of

A.

6.Generate three address code for " if A