Complier Design
Complier Design
Compiler Design
Preliminary Level
1. A ……… is a software utility that translates code 7. In translation process, complier should report the
written in higher language into a low level language. presence of ………… in the source program.
a. converter b. compiler a. classes b. text
c. text editor d. code optimizer c. objects d. errors
e. None of these e. None of these
2. Compiler translates the source code to ……… . 8. A compiler for a high level language that runs on one
a. Machine code b. Binary code machine and produce code for different machine is
c. Executable code d. Both (a) and (b) called
e. None of these a. optimizing compiler b. cross compiler
c. one pass compiler d. multipass compiler
3. How many parts of compiler are there? e. None of these
a. 1 b. 2
c. 3 d. 4 9. Which of the following is not a phase of compiler?
e. 5 a. Lexical analyser b. Syntax analyser
c. Code optimization d. Code generation
4. Which of the following is/are part(s) of the compiler? e. None of these
a. Analysis part b. Synthesis part
c. Execution part d. Both (a) and (b) 10. In a compiler, linear analysis is called ……… .
e. None of these a. lexical analysis b. scanning
c. testing d. Both (a) and (b)
5. Which of the following part of the compiler breaks up e. None of these
the source program into constituent pieces and
11. ……… or scanning is the process, where the stream of
creates an intermediate representation of the source
characters making up the source program is read from
program?
left-to-right and grouped into tokens.
a. Analysis part b. Synthesis part
a. Lexical analysis b. Diversion
c. Either (a) or (b) d. Both (a) and (b)
c. Modeling d. All of these
e. None of these
e. None of these
6. ………… constructs the desired target program from the 12. Lexical analysis is about breaking a sequence of
intermediate representation of the source program. characters into ……… .
a. Analysis part b. Synthesis part
a. tokens b. packets
c. Lexical part d. Both (a) and (b)
c. groups d. lines
e. None of these
e. None of these
KEY Zone
1. b 2. d 3. b 4. d 5. a 6. b 7. d 8. b 9. e 10. d
11. a 12. a
Computer Awareness Question Bank | 308
Phases of Compiler 16. The process of searching for matched tokens is typically
Phase Description described using ……… .
a. finite automata b. context free grammar
Lexical ●
This is the initial part of reading and c. regular expressions d. Both (a) and (c)
Analysis or analyzing the program text (source e. None of these
scanning code). The text is read and divided into
tokens, each of which corresponds to a 17. Grammar of the programming is checked at ……… phase of
symbol in the programming language. compiler.
Syntax ●
This phase takes the list of tokens
a. semantic analysis b. code generation
Analysis produced by the lexical analysis and c. syntax analysis d. code optimization
arranges these in a tree-structure e. None of these
(called the syntax tree) that reflects the
structure of the program. This phase is
18. Compiler can check
often called parsing. a. logical error
b. syntax error
Semantic ●
A semantic analyzer takes its input from c. Both (a) and (b)
Analysis the syntax analysis phase in the form of d. content error
a parse tree and a symbol table. Its e. None of the above
purpose is to determine if the program
violates certain consistency 19. ……… groups token together into syntactic structures.
requirements. a. Lexical analyzer b. Syntax analyzer
c. Semantic analyzer d. Intermediate code generation
Intermediate ●
The program is translated to a simple
Code machine independent intermediate e. None of these
Generation language. 20. A grammar that produces more than one parse tree for
●
Following are the types of intermediate some sentence is called
code a. ambiguous b. unambiguous
(i) Postfix notation or reverse polish c. regular d. All of these
notation
e. None of these
(ii) Four Address Code or Quadruple
(iii) Three Address Code or Triple 21. Which of the following grammars are not
(iv) Syntax Tree phase-structured?
Code ●
An optimizer attempts to improve the a. Regular b. Context-free
Optimization time and space requirements of a c. Context-sensitive d. All of these
program. There are many ways in which e. None of these
code can be optimized, but most are
expensive in terms of time and space to 22. Which of the following is the most general
implement. phase-structured grammar?
a. Regular b. Context-free
Code ●
The final phase of the compiler is to
Generation generate code for a specific machine. c. Context-sensitive d. All of these
e. None of these
13. The output of a lexical analyzer is 23. Representing the syntax by a grammar is advantageous
a. machine code b. intermediate code because
c. a list of tokens d. a parse tree a. it is concise
e. None of these b. it is accurate
c. automation becomes easy
14. The lexical analyzer takes ……… as input and d. intermediate code can be generated easily and efficiently
produces a list of ……… as output. e. All of the above
a. source code, tokens b. tokens, source code
24. CFG (Context Free Grammar) can be recognized by a
c. machine code, mnemonic d. Both (a) and (b)
a. push-down automata b. 2-way linear bounded automata
e. None of these
c. finite state automata d. Both (a) and (b)
15. The action of parsing the source program into e. None of these
proper syntactic classes is called 25. CSG (Context Sensitive Grammar) can be recognized by
a. syntax analysis a. push-down automata
b. lexical analysis b. 2-way linear bounded automata
c. interpretation analysis c. finite state automata
d. general syntax analysis d. All of the above
e. None of the above e. None of the above
Compiler Design | 309
26. The phase “Syntax Analysis” is modeled on the Types of Parser
basis of which of the following? Parser Description
a. Regular grammar
Top-down Parser A top-down parser starts with the root of the
b. High-level language
parse tree. It is labeled with the start symbol
c. Low-level language or goal symbol of the grammar.
d. Context-free grammar
e. None of the above
● Recursive It consists of a set of functions that
descent parser construct a leftmost derivation of the input.
27. In compiler, the phase “Semantic Analysis” is The functions don’t actually have to be
recursive, but they can be. Each function
responsible for implements a grammar rule.
a. check semantics b. error reporting ●
LL Parser It is a predictive parser that reads the input
c. static checking d. type checking from left to right and constructs a leftmost
e. All of these derivation on LL(k) grammars. An LL(k)
grammar must not be ambiguous or
28. Which of the following is considered as a sequence contain left recursion.
of characters in a token?
Bottom-up Parser The basic idea of a bottom-up parser is that
a. Hexeme b. Lexeme we use grammar productions in the
c. Pattern d. Rexeme opposite way (from right to left). Bottom-up
e. None of these parsing is also known as shift-reduce
parsing.
29. Which of the following is a process of finding a These are a class of parsers that scan the
● LR Parsers
parse tree for a string of tokens? input from left to right, but constructs a
a. Analysing b. Recognizing rightmost derivation
c. Parsing d. Tokenizing Simple LR parser (SLR), Look-ahead LR
e. None of these parser (LALR) and canonical LR parser are
types of LR parser.
30. The process of determining whether a string of Conocial LR parser works on more
tokens can be generated by a grammar is known grammarsthan LALR parsers, and LALR
as …………… . parsers works on more grammars than SLR
a. parsing b. translating parsers.
c. analysing d. recognizing ● GLR Parsers These are an extension of LR parsers that
e. None of these use a form of backtracking to handle
ambiguous grammars. They work by forking
31. ……… is a graph representation of a derivation. sub-parsers at any point in the parse where
a. The parse tree they can’t decide which transition to take.
b. The oct tree
c. The binary tree 34. Which of the following parser is most powerful?
d. All of the above a. Operator precedence b. Canonical LR
e. None of the above c. LALR d. SLR
e. None of these
32. A top-down parser generates
a. left-most derivation 35. Shift reduce parsers are
b. right-most derivation a. top down parser
c. right-most derivation in reverse b. bottom up parser
d. left-most derivation in reverse c. right-most parser
e. None of the above d. left-most parser
e. centered parser
33. A bottom-up parser generates
a. left-most derivation 36. Which of the following functions is/are performed by the
b. right-most derivation parser?
c. right-most derivation in reverse a. Shift b. Reduce
d. left-most derivation in reverse c. Accept d. Reject
e. None of the above e. All of these
KEY Zone
13. c 14. a 15. b 16. d 17. c 18. b 19. b 20. a 21. e 22. c
23. e 24. d 25. b 26. d 27. e 28. b 29. c 30. a 31. a 32. a
33. c 34. b 35. b 36. e
Computer Awareness Question Bank | 310
37. Type checking is normally done during 45. An intermediate code form is
a. Lexical analysis a. Postfix notation b. Syntax trees
b. Syntax analysis c. Three address code d. Four address code
c. Syntax-directed translation e. All of these
d. Code optimization
e. None of the above 46. Intermediate code generation phase gets input
from
38. Which of the following walks the parse tree and as a a. Lexical analyzer b. Syntax analyzer
result generate intermediate code? c. Semantic analyzer d. Error handling
a. Scanner generator e. None of these
b. Syntax-directed translation
c. Automatic code generators 47. Which of the following is not an intermediate code
d. Data-flow engines form?
e. None of the above a. Reverse polish notation
b. Syntax trees
39. In a syntax-directed translation scheme, if the value of c. Three address codes
an attribute of a node is a function of the values of the d. Quadruples
attributes of its children, then it is called a e. None of the above
a. synthesized attribute
b. inherited attribute 48. Symbol table can be used for
c. canonical attribute a. checking type compatibility
d. All of the above b. suppressing duplicate error messages
e. None of the above c. storage allocation
d. All of the above
40. Synthesized attribute can be easily simulated by a e. None of the above
a. LL grammar
b. Ambiguous grammar 49. The best way to compare the different
c. LR grammar implementations of symbol table is to compare the
d. All of the above time required to
e. None of the above a. add a new name
b. make an inquiry
41. Syntax-directed translation scheme is desirable c. add a new name and make an inquiry
because d. All of the above
a. it is based on the syntax e. None of the above
b. its description is independent of any implementation
c. it is easy to modify 50. Which of the following symbol table implementation
d. Both (a) and (c) is based on the property of locality of reference?
e. All of the above a. Linear list
b. Search tree
42. A syntax tree c. Hash table
a. is another name for a parse tree d. Self-organizing list
b. is a condensed form of parse tree e. None of the above
c. should not have keywords as leaves
d. Both (b) and (c) 51. Which of the following symbol table implementation
e. None of the above is best suited if access time is to be minimum?
a. Linear list
43. A parse tree with attribute ........... at each node is known b. Search tree
as an annotated parse tree. c. Hash table
a. name b. value d. Self organization list
c. label d. node e. None of the above
e. edge
52. Which of the following symbol table implementation,
44. ……… does code optimization using data-flow analysis. makes efficient use of memory?
a. Scanner generator a. List
b. Syntax-directed translation b. Search tree
c. Automatic code generators c. Hash table
d. Data-flow engine d. Self-organizing list
e. None of the above e. None of the above
Compiler Design | 311
53. In code optimization phase, an compiler 59. Select the machine independent phase of the compiler.
a. is optimized to occupy less space a. Syntax analysis
b. is optimized to take less time for execution b. Intermediate code generation
c. optimizes the code c. Lexical analysis
d. All of the above d. All of the above
e. None of the above e. None of the above
54. Local and loop optimization in turn provide 60. Relocation bits used by relocating loader are specified
motivation for by
a. Data flow analysis a. relocating loader itself
b. Constant folding b. linker
c. Pee hole optimization c. assembler
d. DFA and constant folding d. macro processor
e. None of the above e. None of the above
55. The optimization which avoids test at every iteration 61. An ideal compiler should
is a. be smaller in size
a. Loop unrolling b. take less time for compilation
b. Loop jamming c. be written in a high level language
c. Constant folding d. produce object code that is smaller in size and executes
d. All of the above faster
e. None of the above e. All of the above
56. The optimization technique which is typically applied 62. Task of the lexical analysis is
on loops is a. to parse the source program into the basic elements or
a. removal of invariant computation tokens of the language
b. peephole optimization b. to build a literal table and an identifier table
c. constant folding c. to build a uniform symbol table
d. All of the above d. All of the above
e. None of the above e. None of the above
57. Concept which can be used to identify loops is 63. Which statement is valid about interpreter?
a. dominators a. It translates one instruction at a time
b. reducible graphs b. Object code is saved for future use
c. depth first ordering c. Repeated interpretation is not necessary
d. All of the above d. All of the above
e. None of the above e. None of the above
58. Which of the following are not loop optimization 64. Whether a given pattern constitutes a token or not
techniques? depends on the
a. Jamming a. source language
b. Unrolling b. target language
c. Induction variable elimination c. compiler
d. All of the above d. All of the above
e. None of the above e. None of the above
KEY Zone
37. c 38. b 39. a 40. c 41. c 42. d 43. b 44. d 45. e 46. c
47. e 48. d 49. c 50. d 51. c 52. a 53. d 54. a 55. a 56. a
57. d 58. e 59. d 60. b 61. e 62. d 63. a 64. c
Advanced Level
1. Compiler is a program that 6. The cost of developing a compiler is proportional to the
a. places programs into memory and prepares a. complexity of the source language
them for execution b. complexity of the architecture of the target machine
b. automates the translation of assembly c. flexibility of the available instruction set
language into machine language d. All of the above
c. accepts a program written in a high level e. None of the above
language and produces an object program
d. appears to execute a source program as if it 7. Loader is a program that
were machine language a. places programs into memory and prepares them for execution
e. None of the above b. automates the translation of assembly language into machine
language
2. For which of the following reasons, an c. accepts a program written in a high level language and produces an
interpreter is preferred to a compiler? object program
a. It takes less time to execute d. appears to execute a source program as if it were machine
b. It is much helpful in the initial stages of language.
program development e. None of the above
c. Debugging can be faster and easier
8. A grammar is meaningless
d. It needs less computer resources
a. if terminal set and non-terminal set are not disjoint
e. Both (b) and (c)
b. if left hand side of a production is a single terminal
3. For which of the following reasons, a c. if left hand side of a production has no non-terminal
compiler is preferable to an interpreter? d. All of the above
a. It can generate stand-alone programs that e. None of the above
often take less time for execution
9. A given grammar is said to be ambiguous if
b. It is much helpful in the initial stages of
a. two or more productions have the same non-terminal on the left
program development
hand side
c. Debugging can be faster and easier
b. a derivation tree has more than one associated sentence
d. If one changes a statement, only that
c. there is a sentence with more than one derivation tree
statement needs recompilation
corresponding to it.
e. None of the above
d. parenthesis are not present in the grammar
4. The only difference between a e. None of the above
cross-compiler and a normal compiler in 10. A parser with the valid prefix property is advantageous because
terms of it
a. execution time a. detects error as soon as possible
b. programmer friendly b. detects errors as and when they occur
c. efficiency c. limits the amount of erroneous output passed to the text phase
d. code generated by it d. All of the above
e. All of the above e. None of the above
5. Incremental-compiler is a compiler 11. Which of the following is not true for syntax-directed translation
a. which is written in a language that is different schemes?
from the source language. a. It is a CFG with program fragments embedded within production
b. that generates object code for its host machine bodies
c. which is written in a language that is same as b. The semantic actions appear at a fixed position within a production
the source language body
d. that allows a modified portion of a program to c. They can be considered as a complementary notation to
be recompiled syntax-directed definitions
e. None of the above d. All of the above
e. None of the above
KEY Zone
1. c 2. e 3. a 4. d 5. d 6. d 7. a 8. d 9. c 10. c
11. b
Explanations for Selected Questions
Preliminary Level 28. Lexeme A lexeme is a sequence of characters from the input that match a
pattern (and hence constitute an instance of a token).
1. Compiler A compiler is a special program that
processes statements written in a particular 29. Parsing Parsing or syntactic analysis is the process of analysing a string
programming language and turns them into machine of symbols, either in natural language or in computer languages according
language or “code” that a computer’s processor to the rules of a formal grammar.
uses. 31. Parse Tree A parse tree is an ordered, rooted tree that represents the
syntactic structure of a string according to some context-free grammar.
3. Parts of compiler There are two major parts of a
compiler 36. Functions of parser Parser performs one of the following functions
(a) Analysis part An analysis divides program into (a) Shift Push the current input symbol onto the stack and go onto the next
components and creates the intermediate input symbol.
representation. (b) Reduce A grammar rule’s right hand side is on the top of the stack; pop
This part consists of three phases. Lexical analysis, it off and push the grammar rule’s left-hand-side non-terminal.
Syntactic analysis and Semantic analysis. (c) Accept Accept the input (parsing has finished successfully)
(b) Synthesis part A synthesis requires more (d) Reject Input is not syntactically correct.
specified methods. It involves converting
intermediate representation in the executing 39. Synthesized attribute Attribute of a node (non-terminal) that depends on
program. This part consists of intermediate code the value of attributes of children nodes in the parse tree.
generator, code generator and code optimizer 40. LR grammar An LR grammar is one for which we can construct a
phase. shift/reduce parser that reads input left-to-right and builds an rightmost
8. Cross compiler A cross compiler is a compiler derivation is reverse.
capable of creating executable code for a platform 48. Symbol table A symbol table is a data structure used by a language
other than the one on which the compiler is running. translator such as a compiler or interpreter, where each identifier in a
16. Finite automata A finite automaton (FA) is a simple program’s source code is associated with information relating to its
idealized machine used to recognize patterns within declaration or appearance in the source.
input taken from some character set. The job of an 54. Data flow analysis It is a technique for gathering information about the
FA is to accept or reject an input depending on possible set of values calculated at various points in a computer program.
whether the pattern defined by the FA occurs in the 60. Linkers A linker combines object code produced from compiling and
input. assembling many source programs, as well as standard library functions
Regular expression In computer science, a regular and resources supplied by the operating system.
expression is a sequence of characters that forms a
63. Interpreter An interpreter is a computer program that directly executes
search pattern, mainly for use in pattern matching
instructions written in a programming or scripting language, without
with strings, or string matching.
previously compiling them into a machine language program.
18. Syntax error In computer science, a syntax error is
an error in the syntax of a sequence of characters or
tokens that is intended to be written in a particular
Advanced Level
programming language. 5. Incremental compiler It is one that can recompile only those portions of a
program that have been modified. Ordinary compilers must process entire
21. Regular grammar A regular grammar is a formal
modules or programs.
grammar that describes a regular language.
Context-free grammar A context-free grammar is a 7. Loader A loader is the part of an operating system that is responsible for
formal grammar in which every production rule is the loading programs. It is one of the essential stages in the process of starting
form V → w where V is a single non-terminal symbol, a program, as it places programs into memory and prepares them for
and w is a string of terminals and/or non-terminals. execution.
Context-sensitive grammar A context-sensitive 8. Terminals A terminal is a symbol which does not appear on the left-hand
grammar is a formal grammar in which the left-hand side of any production. A grammar contains a set of symbols such as +, *
sides and right-hand sides of any production rules and other token defined by the lexical analyzer such as identifiers.
may be surrounded by a context of terminal and Non-terminals Non-terminals are the non-leaf nodes in a parse tree.
non-terminal symbols. For example, E, T and F are non-terminals. Sometimes, these are enclosed
24. Push-down automaton It is a finite automaton that between angle brackets to distinguish them from terminals.
can make use of a stack containing data. 9. Ambiguity In computer science, a grammar is said to be an ambiguous
Linear bounded automaton It is a multi-track turing grammar if there is some string that it can generate in more than one way
machine which has only one tape, and this tape is (i.e., the string has more than one parse tree or more than one leftmost
exactly the same length as the input. derivation). A language is inherently ambiguous if it can only be generated
by ambiguous grammars.