UNIT I - SNS Courseware

advertisement
SNS COLLEGE OF ENGINERRING
DEPARTMENT OF CSE
UNIT I
UNIT I - INTRODUCTION TO COMPILERS
PART A - 2 MARKS
1. Compare compiler & interpreter. [ Nov/Dec 2012, Apr/May 2008]
S.NO
COMPILER
1
Program is analyzed only once
& the code is generated.
2
Compilers produce object code.
INTERPRETER
The source program gets interpreted every
time so that it can be executed.
Interpreters do not produce object code.
3
More efficient than interpreter.
Less efficient than compiler.
4
It is a complex program &
requires high memory.
It is simpler & requires less memory.
2. What is meant by a cross - compiler? [Nov/Dec 2012]
The compiler which run on one machine and produces the target code for another
machine. Such a compiler is called cross compiler from which the platform independency is
achieved.
e.g. For the first version of EQN compiler , the compiler is written in C & the command
are generated for TROFF is shown as
EQN
TROFF
C
The cross compiler for EQN can be obtained by running it on PDP -11 through C compiler
which produces output for PDP-11 is shown as
TROFF PDP-11
PDP-11
3. How a source program is analyzed? [May/Jun 2012]
Analysis part breaks up the source program into constituent pieces and creates an intermediate
representation of the source program using three phases,
 Linear analysis: Stream of characters making up the source program is read from
left to right and grouped into tokens.
 Hierarchical analysis: Tokens are grouped hierarchically into nested collections
with collective meaning.
 Semantic analysis: Checks are performed to ensure that the components of a
program fit together meaningfully.
CS6660 COMPILER DESIGN
2 MARKS (Q&A)
SNS COLLEGE OF ENGINERRING
DEPARTMENT OF CSE
4. What are the cousins of compiler? [May/Jun 2012, Apr/May 2004 & 2005,Nov/Dec 2013]
 Pre processor – Macro processing , File inclusion , Rational Preprocessor & Language
extension
 Assembler – Two pass Assembly
 Loaders & Link – Editors
5. What is an interpreter? [Apr/May 2011]
An interpreter is a language translator like a compiler. It differs from a compiler thus it
executes the source code immediately rather than generating object code. Frequently used to
execute command languages, since it is a complex routine for editor or compiler.
e.g, For an assignment statement, an interpreter builds a tree and then carry out the operations at
the node as it “walks” the tree.
6. What are the functions of preprocessors? [Nov/Dec 2007]
 Macro processing
 File inclusion
 Rational preprocessors
 Language extensions
7. Define a symbol table. [Nov/Dec 2007]
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. When an identifier in the source
program is detected by the lexical analyzer, the identifier is entered into the symbol table.
8. List four software tools that analyses the source program. [May/Jun 2006]
 Structure editors
 Pretty printers
 Static checkers
 Interpreters
9. What are compiler construction tools? [Nov/Dec 2004,2006]
 Parser generator
 Scanner generator
 Syntax directed translation engine
 Automatic code generation
 Data flow engine
10.What is the need for separating analysis phase into lexical & parsing? [Nov/Dec 06]
Based on software engineering concerns, lexical analysis is separated from parsing for
the following reasons,
 Simplicity of design – well defined sub task modularity
 Efficiency – easy to apply specialized techniques for both phases
 Portability – can use peculiarities & other device specific anomalies
CS6660 COMPILER DESIGN
2 MARKS (Q&A)
SNS COLLEGE OF ENGINERRING
DEPARTMENT OF CSE
11. What is a preprocessor?[ Nov/Dec 2004]
Preprocessor is a separate program that is called by the compiler before actual translation
begins. Such a processor can delete comments, include other files, and perform macro
substitutions. Preprocessors produce input to compilers.
12.What are machine dependant & machine independent phases?
The machine dependant phases are code generation and code optimization phases. The
machine independent phases are lexical analyzers, syntax analyzers, semantic analyzers.
13. What are the factors affecting number of passes in compiler?
Various factors are,
 Forward reference
 Storage limitation
 Optimization
14. What is difference between compiler and assembler?[Nov/Dec 2013]
S.NO
COMPILER
ASSEMBLER
1
Compiler is a computer
Assembler is a computer program that
program that reads code (like
reads code written in one language which
C,C++ code )and translates into is called Assembly language and
machine language.
translates into machine language.
2
Compiler is used in high-level
Assembler is used in low-level Assembly
language.
language.
3
Compiler is more popular than
Assembler is less popular than compiler.
Assembler.
15. What is the role of lexical analyzer?[May/Jun 2013]
Upon receiving a get-next-token command from the parser, the lexical analyzer reads input
characters until it can identify the next token.
The lexical analyzer collects also information about tokens into their associated attributes:
 the tokens influence parsing decisions,
 the attributes influence the translation of tokens
CS6660 COMPILER DESIGN
2 MARKS (Q&A)
SNS COLLEGE OF ENGINERRING
DEPARTMENT OF CSE
16. Write about the tool for generating lexical analyzer. [May/June 2012]
For an efficient design of compiler, various tools have been built for constructing lexical
analyzers using the special purpose notations called regular expressions.
 LEX Tool – is a Unix utility which works faster in finding the tokens.
 YACC Tool – Yet Another Compiler is another automated tool used to specify the
grammar for realizing the source programming construct.
17. What is meant by token? Give example. [May/June 2012]
Tokens describe the class or category of input string such as identifiers, constant,
keywords.
Example: int MAX(int a, int b)
Here, int – keyword
MAX – identifier
( - operator
int – keyword
a – identifier
, - operator
Int – keyword
b – identifier
) – operator
18. Mention some lexical errors. [May/June 2012]
Lexical errors are:
 Misspelling an identifier keyword or an operator.
 None of the patterns for tokens matches a prefix of remaining input.
 An undeclared function identifier.
19. What is meant by token? How it can be recognized? [May/June 2012]
A recognizer for a language is a program that takes as input a string “X” and answers
“yes” if “X” is sentence of the language and “no” otherwise.
Finite automaton – a generalized transition diagram for a regular expression is a recognizer.
20. Define token & lexeme. [April/May 2011]
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. The pattern is said
to match each string in the set. A lexeme is a sequence of characters in the source program that is
matched by the pattern for a token.
21. What are the issues to be considered in the design of lexical analyzer?
[May/June 09, Nov/Dec 04]
There are several reasons for separating the analysis phase of compiling into lexical analysis and
parsing. There are,
 Simpler design
 Compiler efficiency
 Compiler portability is enhanced
CS6660 COMPILER DESIGN
2 MARKS (Q&A)
SNS COLLEGE OF ENGINERRING
DEPARTMENT OF CSE
22. Define concrete and abstract syntax with example. [May/June 2009]
Concrete Syntax: It is part of the definition of the language and is defined by a context
free grammar. It consists of a set of productions that define programs.
(e.g), stmt -> if expr then stmt |
if expr then stmt else start | a
expr | b
Abstract Syntax: It is part of definition of a particular implementation of a language. It
is the set of trees used to represent programs in the implementation.
(e.g), Parse tree
23. Write short notes on buffer pair. [April/May 2008]
Buffering techniques have been developed to reduce the amount of overhead required to
process an input character. A buffer is divided into two N- character halves. N is the number of
characters on one disk block. (e.g) 1024 or 4096
E
=
M
*
C
*
*
Lexeme beginning
2
EOF
Forward pointer
24. What are the error recovery actions in a lexical analyzer? [Apr/may 2005]
 Panic mode error recovery
 Deleting an extraneous character
 Inserting a missing character
 Replacing an incorrect character by a correct character
 Transposing two adjacent characters.
CS6660 COMPILER DESIGN
2 MARKS (Q&A)
Download