0% found this document useful (0 votes)
71 views14 pages

Compiler Design QB - UNIT1

The document discusses the introduction to compilers including phases of a compiler, lexical analysis, finite automata, derivation trees, and ambiguity. It provides a question bank with multiple choice questions related to compiler design topics at different difficulty levels.

Uploaded by

Saraswathi E
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views14 pages

Compiler Design QB - UNIT1

The document discusses the introduction to compilers including phases of a compiler, lexical analysis, finite automata, derivation trees, and ambiguity. It provides a question bank with multiple choice questions related to compiler design topics at different difficulty levels.

Uploaded by

Saraswathi E
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 14

SRM INSTITUTE OF SCIENCE AND TECHONLOGY

DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERING

18CSC304J COMPILER DESIGN - QUESTION BANK

UNIT I INTRODUCTION TO COMPILERS


Compilers – Analysis of the source program - Phases of a compiler – Cousins of the Compiler Grouping
of Phases – Compiler construction tools - Lexical Analysis – Role of Lexical Analyzer - Input Buffering –
Specification of Tokens- design of lexical analysis (LEX) - Finite automation (deterministic & non
deterministic) - Conversion of regular expression of NDFA – Thompson’s construction - Conversion of NDFA
to DFA- minimization of NDFA - Derivation - parse tree – ambiguity

PART A
Q.no. Question Page No. Level

1. Formulate The number of tokens in the following C statement :printf("i = %d, &i = %x", i, &i);
25 L3
a.3
b.26
c.10
d.21 Answer:c
2. In a compiler, keywords of a language are recognized during 21 L1
a.parsing of the program
b.the code generation
c.the lexical analysis of the program
d.dataflow analysis Answer: c

3. Analyse the lexical analysis for a modern computer language such as Java needs the power of which
one of the
following machine models in a necessary and sufficient sense? web L2
a.Finite state automata
b.Deterministic pushdown automata
c.Non-Deterministic pushdown automata
d.Turing Machine Answer: a
4. The lexical analyzer takes_________as input and produces a stream of_______as output. 21 L1
a) Source program,tokens
b)Token,source program
c)Either A and B
d)None of the above Answer:a
5. Resolution is externally defined symbols is performed by web L1
a)linker
b)loader
c)compiler
d)assembler Answer: a
6. Which of the following is used for grouping of characters into tokens? 21 L1
a) Parser
b) Code optimization
c) Code generator
d) Lexical analyzer Answer:d
7. Decide whether a given pattern constitutes a token or not depends on the 32 L2
a)Source language
b)Target language
c)Compiler
d) All of these Answer: c
8. Choose :A Compiler is 20 L3
a)A program that place program into memory and prepares them for execution
b)A program that automates the translation of assembly language into machine language
c)Aprogram that accepts program written in high level language and produces an object program
d)A program that appears to execute a source program as if it were machine language
Answer:c
9. Advantage of panic mode of error recovery is that 12 L2
a)It never gets into an infinite loop
b)It is simple to implement
c)Both A and B
d) None of these. Answer:c
10. In which way(s) a macroprocessor for assembly language can be implemented ? 14 L3
a) Independent two-pass processor
b) Independent one-pass processor
c)Expandmacrocalls and substitute arguments
d)All of the above Answer:d
11. Which of the following is used for grouping of characters into tokens (in a computer) 14 L1
a)A parser
b) Code optimizer
c) Code generator
d)Scanner Answer:d
12. A simple two-pass assembler does which of the following the first phase 13 L1
a) It allocates space for the literals.
b) It computes the total length of the program.
c) It builds the symbol table for the symbols and their values
d) All of above Answer: d
13. Convince :The translator is best described as 1 L3
a) Application software
b) A system software
c)A hardware component
d) All of the above Answer: b

14. Which of the following are language processors? 3 L1


a) Assembler
b)Compilers
c) interpreters
d) All of these Answer:a
15. Interpret that Relocating bits used by relocating loader are specified by 3 L2
a)Relocating loader itself
b)Linker
c)Assembler
d) Macro processor Answer: b
16. An absolute loading scheme which loader function is accomplished by assembler? 3 L1
a) re-allocation
b) allocation
c) linking
d) Loader Answer: a
17. Categorize :The regular languages are not closed under web
L2
a) Concatenation
b) Union
c) Kleene star
d) Complement Answer: d
18. Given an arbitrary non-deterministic finite automaton (NFA) with N states, the maximum number of
states in
an equivalent minimized DFA is at least. web
L3
(a) N^2
(b) 2^N
(c) 2N
(d) N! Answer: (b)
19. Let S and T be language over ={a,b} represented by the regular expressions (a+b*)* and (a+b)*,
respectively.
Which of the following is true? 38 L3
(a) ScT (S is a subset of T)
(b) TcS (T is a subset of S)
(c) S=T
(d) SnT=Ø Answer: (c).
20. There are ________ tuples in finite state machine. 41 L1
a) 4
b) 5
c) 6
d) unlimited Answer:b.

21. Transition function maps. 41 L1


a) Σ * Q -> Σ
b) Q * Q -> Σ
c) Σ * Σ -> Q
d) Q * Σ -> Q Answer:d
22. Evaluate the number of states require to accept string ends with 10. 76 L3
a) 3
b) 2
c) 1
d) can’t be represented. Answer:a.
23. Regular expression for all strings starts with ab and ends with bba is. web L3
a) aba*b*bba
b) ab(ab)*bba
c) ab(a+b)*bba
d) All of the mentioned Answer:c.
24. How many DFA’s exits with two states over input alphabet {0,1} ? 41 L3
a) 16
b) 26
c) 32
d) 64 Answer:d
25. Grammar that produce more than one Parse tree for same sentence is: web L1
a.Ambiguous
b.Unambiguous
c.Complementation
d.Concatenation Intersection Answer:a
26. Automaton accepting the regular expression of any number of a ‘ s is: web L3
a.a*
b.ab*
c.(a/b)*
d.a*b*c Answer:a
27. Grammar is checked by which component of compiler 14 L1
a) Scanner
b) Parser
c) Semantic Analyzer
d) None of the mentioned Answer:b
28. The number of leaves in a parse tree with expression E*(E) where * and () are operators
web L3
a) 5
b) 2
c) 4
d) 3 Answer: a
29. Which among the following is the root of the parse tree? 132 L1
a) Production P
b) Terminal T
c) Variable V
d) Starting Variable S Answer: d
30. An NFA’s transition function returns 41 L1
a) A Boolean value
b) A state
c) A set of states
d) An edge Answer: c
31. Compiler translates the source code to _______? 20 L1
a)Executable code
b) Machine code
c)Binary Code
d)Both B and C Answer:D
32. Which of the following groups is/are token together into semantic structures? 72 L1
A) Syntax analyzer
B) Intermediate code generation
C) Lexical analyzer
D) Semantic analyzer Answer:C
33. Compiler should report the presence of __________ in the source program, in translation process.20
L2
A. Classes
B. Objects
C. Errors
D. Text Answer: C

34. What is the output of lexical analyzer? 31 L1


A. A parse tree
B. A list of tokens
C. Intermediate code
D. Machine code Answer: B
35. Summarizing the overall progress how many parts of compiler are there? 4 L2
A. 1
B. 2
C. 4
D. 8 Answer: B

36. Grammar of the programming is checked at _______ phase of compiler. 31 L1


A. Semantic analysis
B. Syntax analysis
C. Code optimization
D. Code generation Answer: B

37. What is the action of parsing the source program into proper syntactic classes? 5 L1
A. Lexical analysis
B. Syntax analysis
C. General syntax analysis
D. Interpretation analysis Answer: A

38. Compiler can check ________ error. web L2


A. Logical
B. Syntax
C. Content
D. Both A and B Answer : B
39. ______ is the most general phase structured grammar. web L1
A. Context sensitive
B. Regular
C. Context free
D. All of these Answer: A
40. Which of the following are language processor? 3 L1
a.) Assembler
b) Compiler
c)Interpreter
d) All the above Answer: A
41. Relate :The linker is defined as 3 L2
a) is same as the loader
b) is require to create a load module
c)user source code as input
d) is always used before programs executed Answer: A
42. Point out :what is the permanent database in the general model of compiler ? web L2
a) Literal table
b)Identifier table
c)Terminal tale
d)Source code Answer: D
43. Guess :Which of the system software resides in main memory always ? 4 L2
a) text editor
b) assembler
c) linker
d) loader Answer: C
44. Grade :The translator is best described as web L3
a) Application software
b) System Software
c) Hardware component
d) Software module Answer: B
45. When is the type checking usually done? web L1
a)Sysntax directed translation
b)lexical analysis
c)code optimization
d) syntax analysis Answer: A
46. How many tokens are there in following statement ?Printf(“j=%d,&j=%x”,j&j)
25 L3
a)4
b) 5
c) 9
d) 10 Answer : D

47. In a compiler, the data structure responsible for the management of information about variables and
their
attributes is 9 L1
a) Semantic stack
b) Parser table
c) Symbol table
d)Syntax tree Answer: C
48. The lexical analysis for java needs _____ in a necessary and sufficient sense. web L1
a)Turing machine
b)NPDA
c)DPDA
d)Finite state Automata Answer : D
49. Three Address code involves web L1
a) exactly 3 address
b)atleast 3 address
c)atmost 3 address
d)all the above Answer: C
50. Relocation bits used by relocating loader are specified by 3 L1
a)relocating loader itself
b)linker
c)assembler
d)macro processor Answer: B

PART B
Q.no. Question Page No. Level

1. What is translator? 1 L1
 Translator is a kind of program which converts one form of source language into another
form of the target language.
 Assembler and compiler are the two commonly used translators in operating systems.

2. What is an interpreter? 2 L1
 An interpreter is a kind of translator which produces the results directly when the source
language and data is given to it as input.
 It does not produce the object code rather each time the program needs execution

3. What is compiler? 20 L1
 A compiler is a program that reads a program written in one language –the source language
and translates it into an equivalent program in another language-the target language.

 The compiler reports to its user the presence of errors in the source program

4. What is the difference between compiler and interpreter? 2 L2

S.N
O COMPILER INTERPERTER
A compiler reads the whole source An interpreter reads a statement from
1. code at the
once, creates tokens, checks input, converts it to an intermediate
semantics, code,
generates intermediate code, executes executes it, then takes the next
the statement in
whole program and may involve many sequence.
passes.
A compiler reads the whole program
2. even If an error occurs, an interpreter stops
if it encounters several errors. execution and reports it.
The interpreters do not produce object
3. The compilers produce object code code
Interpretation is less efficient than
4. Compiler is efficient than interpreter compiler

5. Define token, pattern and lexeme. 24 L1


 Tokens- Sequence of characters that have a collective meaning.
 Patterns- There is a set of strings in the input for which the same token is produced as output.
This set of strings is described by a rule called a pattern associated with the token
 Lexeme- A sequence of characters in the source program that is matched by the pattern for a
token.

6. Mention few cousins of compiler. 3 L1


Some of the cousins of compilers are
 Preprocessor
 Macro processor
 Assembler
 Loaders and Linkers

7. State any two reasons as to why phases of compiler should be grouped. 12 L3


Different phases of compiler can be grouped together to form a front end and back end. The
front end and back end model of the compiler is very much advantages because of following
reasons.
 By keeping the same front end and attaching different back ends one can produce a compiler
for same source language on different machines.
 By keeping different front end and same back ends one can compile several different
languages on same machine.
8. What do you meant by cross compiler? 24 L1
A compiler that runs on one computer but produces object code for a different type of
computer. Cross compilers are used to generate software that can run on computers with a new
architecture or on special-purpose devices that cannot host their own compilers.

9. What are the phases of compiler? 20 L1

The following are the various phases of a compiler:


 Lexical Analyzer
 Syntax Analyzer
 Semantic Analyzer
 Intermediate code generator
 Code optimizer
 Code generator

10. Justify , how will you group the phases of compiler? 12 L3

Different phases of compiler can be grouped together to form a front end and back end.
 Front end consist of those phases that primarily dependent on the source language and
independent on the target language.
 The front end consists of analysis part which includes lexical analysis, syntax analysis and
semantic analysis.
 Back end consists of those phases that are totally dependent upon the target language and
independent on the source language.
 It includes code generation and code optimization.
11. Classify the two parts of a compilation? Explain briefly. 4 L2

Analysis and Synthesis are the two parts of compilation.


 The analysis part breaks up the source program into constituent pieces and creates an
intermediate representation of the source program.
 The synthesis part constructs the desired target program from the intermediate representation.

12. Define a preprocessor. What are the functions of preprocessor? 3 L1


A preprocessor, generally considered as a part of compiler, is a tool that produces input
for compilers. It deals with macro-processing, augmentation; file inclusion, language extension,
etc.

13. Draw the diagram of language processing system. 3 L3


14. List four software tools that analyses the source program(or) What are compiler
construction tools?
14 L1

The following is a list of some compiler construction tools:


 Parser generators
 Scanner generators
 Syntax-directed translation engines
 Automatic code generators
 Data-flow engines

15. What are the factors affecting number of passes in compiler? 13 L1

Various factors affecting the number of phases in compiler are,


 Forward reference
 Storage limitations
 Optimization.

16. Order the functions performed in analysis phase? 4 L2


 In analysis part the source program is read and broken into constituent pieces.
 The syntax and the meaning of the source string is determined and then an intermediate code
is created from the input source program.

17. Order the functions performed in synthesis phase? 4 L2

 In synthesis part the intermediate form of the source language is taken and converted into an
equivalent target program.
 During this process if certain code is to be optimized.

18. What is a symbol table? 9 L1

 A symbol table is a data structure containing a record for each identifier, with fields for the
attributes of the identifier.
 The data structure allows us to find the record for each identifier quickly and to store or
retrieve data from that record quickly.
 Whenever an identifier is detected by a lexical analyzer, it is entered into the symbol table.
 The attributes of an identifier cannot be determined by the lexical analyzer.

19. List the phases that constitute the front end of a compiler 12 L1
The front end consists of those phases or parts of phases that depends primarily on the source
language and is largely independent of the target machine. These include
 Lexical and Syntactic analysis
 The creation of symbol table
 Semantic analysis
 Generation of intermediate code
A certain amount of code optimization can be done by the front end as well. Also includes error
handling that goes along with each of these phases.

20. Mention the back-end phases of a compiler. 12 L1

The back end of compiler includes those portions that depend on the target machine and
generally those portions do not depend on the source language, just the intermediate language.
These include
 Code optimization
 Code generation, along with error handling and symbol- table operations.
21. What does a semantic analysis do? 7 L1

 Semantic analysis is one in which certain checks are performed to ensure that components of
a program fit together meaningfully.
 Mainly performs type checking.

22. Show how the source program is analyzed? web L2

The source program can be analyzed in three phases

 Linear analysis: The source string is read from left to right and grouped into tokens.
 Hierarchical analysis: Characters or tokens are grouped hierarchically into nested collections
for checking them syntactically
 Semantic analysis: It ensures the correctness of meaning of the program.

23. What is meant by assembler? 21 L1

 The assembler is a kind of translator which takes the assembly program as input and
produces the machine code as output.
 The assembler converts the assembly program to low level machine language using two
passes.
 A pass means one complete scan of the input program. The end of second pass is the
relocatable machine code.
24. Differentiate and describe loaders and link editors? 3 L2

 Loader is a part of operating system and is responsible for loading executable files into
memory and executes them. It calculates the size of a program (instructions and data) and
creates memory space for it. It initializes various registers to initiate execution.
 Linker is a computer program that links and merges various object files together in order to
make an executable file. All these files might have been compiled by separate assemblers.
25. Indicate the role of lexical analyzer? 32 L1

The lexical analyzer scans the source program and separates out the tokens from it.
26. Interpret the functions of lexical analyzer? 32 L2
 It produces stream of tokens
 It eliminates blank and comments
 It generates symbol table which stores the information about identifiers , constants
encountered in the input.
 It keep track of line numbers.
 It reports the error encountered while generating the tokens.

27. What are the issues of the lexical analyzer? 33 L1


 Separation of lexical and syntax analysis reduces the bourdon on parsing phase.
 Representation of non standard symbol can be isolated in lexical analyzer.
 Large amount on time is spent on reading the source file

28. What are sentinels? What is its usage? 36 L1


Sentinels are special string or characters that are used to indicate an end of buffer. The
sentinel is not a part of source language. One can avoid a test on end of buffering using sentinels
at the end of buffer. For example: ‘eof’.

29. Write short note on token specification. 37 L3


 To specify tokens regular expressions are used.
 When a pattern is matched by some regular expression then token can be recognized

30. What is recognizer? web L1


 Recognizer is machines. These are the machines which accept the strings belonging to certain
language.
 If the valid strings of such languages are accepted by the machine then it is said that the
corresponding language is accepted by that machine, otherwise it is rejected.
For example,
 The finite state machines are recognizers for regular expressions or regular languages
 The push down automata is recognizers for context free languages.

31. Manipulate the transition diagram for an identifier. L2

32. Classify the possible error recovery actions in lexical analyzer? (Various possible error
recovery actions in lexical analyzer are, 34 L2

 Deleting extra characters that might appear in the source statement.


 Inserting missing character.
 Interchanging two adjacent characters.
 Replacing an incorrect character by the correct character.

33. What is the input to lexical analyzer generator? What is its output? web L1

The lexical analyzer generator takes the source program as input and generates the stream
of tokens as output.

34. Summarize the algebraic properties of regular expressions? 40 L3

S.N
O Properties Meaning
1 r1 |r2=r2|r1 | is commutative
2 r1|(r2|r3)=(r1|r2)|r3 | is associative
Concatenation is
3 (r1r2)r3=r1(r2r3) associative
Concatenation is
4 R1(r2|r3)=r1r2|r1r3 distributive
(r2|r3)r1=r2r1|r3r1
5 Єr=Єr=r Є is identity
6 r*=(r|Є)* Relation between Є and *
7 r**=r* *Is idempotent

35. Write the regular expressions for identifier and number. 41 L3

 Regular expression for identifier is R.E.=letter(letter + digit)*


 Regular expression for integer number is R.E.=digit. digit*

36. Write regular expression to describe the language consists of strings made of even numbers
a and b
Web L3
(11+00+(10+01)(11+00)\*(10+01))

37. Illustrate why buffering used in lexical analysis? What is the commonly used buffering
method?
35 L2
Because of the amount of time taken to process characters and the large number of characters
that must be processed during the compilation of a large source program, specialized buffering
techniques have been developed to reduce the amount of overhead required to process a single
input character.
Method:
 One buffer scheme
 Two buffer scheme

38. Define Lexeme 31 L1

It represents the sequence of characters in the source program that are matched with pattern of
token.
39. Compare the features of DFA and NFA. 73 L2

S.NO DFA NFA


Non-deterministic Finite
1 Deterministic Finite Automata Automata
In DFA, for the given state on given In NFA , we can have more than one
2 input next
we can reach to unique next state. states for the same input
3 Hard to design Easy to design

 Lexical analyzers store the input in input buffer.


 The token is read from input buffer and regular expressions are built for corresponding token.
 From these regular expressions finite automata (NFA) is built.
 For each state of NFA, a function is designed and each input along the transitional edges
correspond to input parameters of these functions
 The set of each function ultimately creates lexical analyzer program.
40. What is a finite automaton? web L1
A finite automata is a collection of 5-tuples (Q,∑,δ,q0,F) Where,
Q is a finite set of states, which is non empty
∑ is input alphabet, indicates input set qo is an initial state and qo is in Q
F is a set oof final states
There are two types of finite automata
 Deterministic finite automata
 Non deterministic finite automata

PART C
Q.no. Question Page No. Level
1. Describe What is compiler? Explain the various phases of compiler in detail. 4 L1
2. Decide the output for the various phases of compiler with respect to following statements.
a=b+c*50.
a:=b+c*4. 26 L3
3. Discuss the various phases of a compiler in detail. Also write down the output for the following
expression after each phase. a:=b*c-d 10 L3
4. Predict and Explain in detail about the phases of compiler and translate the statement
pos:=init+rate*60
26 L3
5. Breakdown the grouping of phases (or) Describe how various phases could be combined as a
pass in a compiler. 12 L1
6. Summarize and explain the compiler construction tools. 14 L3
7. Write down the exclusive functions carried out by the analysis phases of any compiler. 5 L3
8. Briefly explain the cousins of compiler. 3 L1
9. Discuss the role of lexical analyzer in detail. 32 L1
10. Explain input buffering in detail (or) Write briefly on the issues in input buffering and how they
are
handled (or) Explain briefly about input buffering in reading the source program for finding the
tokens.
35 L1
11. Explain briefly the design of lexical analyzer. 65 L1
12. Draw the transition diagram for the following 71 L3
To represent relational operators.
To represent unsigned numbers.

13. Give the minimized DFA for the following expression. (a/b)*abb 44 L3
14. Prove the following two regular expressions are equivalent by showing the minimum state
DFA’s are same.
i) (a/b)* ii) (a*/b*) 46 L3

15. Illustrate Thomson’s construction by drawing a NFA for the regular expression (a/b)*a. Convert
this NFA to DFA using subset construction. Write the algorithm for subset construction.
46 L3
16.Construct the minimized DFA for the regular expression (0+1)*(0+1)10. web L3
17. Explain the design of lexical analyzer using LEX. 63 L1
18. Explain specification and recognition of tokens. 37 L1

You might also like