1. UNIT –1
2 Marks and Answers
1. Define Compiler.
Compiler is a program which takes one language (Source program) as input and translates it
into an equivalent another language (Target program).
Input Output
Compiler
Source program Target program
During this process if some errors are encountered then compiler displays them as error
messages.
2. Describe the analysis-synthesis model of compiler.
In the analysis part the source program is read and broken down into constituent pieces. The
syntax and the meaning of the source program is determined and then the intermediate code is
created.
In the synthesis part this intermediate form of the source program is taken and converted into
a equivalent target program.
Compiler
Source Target
program Analysis synthesis
program
3. Define a symbol table.
The task of symbol table is to store identifiers used in the program. It also stores information
about attributes of each identifier such as its type, its scope, and storage allocated for it.
It also stores information about the subroutines used in the program. In this case the attributes
may be name of the subroutine, number of arguments passed to it, its type and the method of
passing these arguments (call by value or call by reference) and the return type.
Basically it is a data structure which stores information about all tokens.
2. 4. What are the functions of pre-processor?
Pre-processor allow users to use macros in the program. Macros are some set of instructions
which can be repeatedly used in the program. Macro is a small set of instruction, whenever a
macro name is defined. The macro names which are used in the program are expanded by the
macro definition. Example for macro definition
#define PI 3.14
The PI value wherever used in the program are replaced by 3.14 during compilation by the
preprocessor.
#include <stdio.h> - this code will include the header file stdio.h before execution.
5. What is CFG? Give an example.
Context free grammar is a notations used to specify the syntax of programming language and
it consists of
1) Terminals
2) Non-terminals
3) Start symbols
4) Productions
Example-
Expr expr op expr
Expr id
Op +
Op -
Terminlas id, +, -
Non-terminals expr, op
Start symbols expr
Productions 4 productions (given)
3. 6. List out any two compiler construction tools.
1) Scanner generators
2) Parser generators
3) Syntax directed translation engines
4) Data flow engines
5) Automatic code generators
7. Mention some of the cousins of the compiler.
1) Compiler
2) Interpreter
3) Assembler
4) Preprocessor
5) Loader & Linker
8. Define Pre processor.
It is a program which performs some preliminary functions such as macros and file inclusion.
Macros are small programs that are executed frequently. The macro names which are used in
the program are expanded by the macro definition. Example for macro definition
#define PI 3.14
The PI value wherever used in the program are replaced by 3.14 during compilation by the
preprocessor.
#include <stdio.h> - this code will include the header file stdio.h before execution.
Preprocessor
Skeleton program source program
9. Define Tokens, Lexeme and Attributes.
Token - The meaningful collection of characters over the character set of the programming
language is called as the token.
Lexeme - A lexeme is a sequence of tokens in the source program that is matched by the rule
for a token.
Attributes – attributes specify the properties of tokens. The lexical analyzer collects
information about tokens and stores them as their attributes.
4. 10. Define Translator and give example.
A translator is one kind of program that takes one form of program that takes one form of
program as the input and converts it into another form. The input program is called source
language and the output program is called target language.
Source target
Translator
language language
11. Draw the transition diagram for a*/b*.
a
a
S1
S0
b S2
b
12. What is the function of a loader?
Loader is a program which performs two functions: loading and link editing. Loading is a
process in which the relocatable machine code is read and the relocateable addresses are altered.
Then that code with altered instructions and data is placed the memory at proper location. The
job of link editor is to make a single program from several files of relocatable machine code. If
code in on file refers the location in another file then such a reference is called external
reference.
13. What is regular expression? Give an example.
Regular expressions are mathematical symbolisms which describe the set of strings of
language. It provides convenient and useful notation for representing tokens.
Example-
A regular expression (R.E) for a language containing the strings of length two over
∑={0,1}.
R.E = (0/1) (0/1)
5. 14. Check whether the input string abab is accepted by the given production.
S aSbS/bSaS/d
Productions S aSbS, S bSaS, S d To get the input string abab the derivation may
be
S aSbS
S abSabS
S abaSbS
S ababS
S abab
The input string abab is accepted.
15. Difference between Pass and Phase.
PHASE PASS
The process of compilation is carried out in Various phases are logically grouped together
various steps. to form a pass.
These steps are referred as phases. The process of compilation can be carried out
in single pass or in multiple passes.
The phases of compilation are lexical analysis,
syntax analysis, intermediate code,
Generation, code generation, code
optimization.
16. Write short notes on transition table and transition diagram with suitable example.
Transition table-
This is a tabular representation of finite automata. For transition table the transition
function is used.
states A b
q0 q1 -
q1 - q2
q2 q2 -
Transition diagram-
A transition diagram or transition graph can be defined as collection of
6. 1) Finite set of states k
2) Finite set of symbols ∑
3) A non empty set states S of k. It is called start symbol.
4) A set F≤K of final
5) A transition function K*AK with K as states and A as input from ∑*.
Example :
The transition diagram for the input abb is
a b b
S0 S1 S2 S3
17. Write any 3 difference between compiler and interpreter.
INTERPRETER COMPILER
Demerit: Merit:
The source program gets interpreted In the process of compilation the program
every time it is to be executed, and every time is analyzed only once and then the code is
the source program is analyzed. Hence, generated. Hence compiler is efficient than
interpretation is less efficient than compiler. interpreter.
The interpreter s do not produce object code. The compilers produce object code.
Merit: Demerits:
The interpreters can be made portal The compiler has to present on the host
because they do not produce object code. machine when particular needs to be compiled.
Merits: Demerits:
Interpreters are simpler and give us The compiler is a complex program and
improved debugging environment. it requires large amount of memory.
18. Mention some of the issues of lexical analyzer.
The various issues in lexical analysis are
1) The lexical analysis and syntax analysis are separated out for simplifying the task of one
or other phases. This separation reduces the burden on parsing phase.
2) Compiler efficiency gets increased if the lexical analyzer is separated. This is because a
large amount of time is spent on reading the source file. And it would be inefficient if
each phase has to read the source file. Lexical analyzer uses buffering techniques for
efficient scan of source file. The tokens obtained can be stored in input buffer to increase
performance of compiler.
7. 3) Input alphabet peculiarities and other device specific anomalies can be restricted to the
lexical analyzer can be isolated in lexical analyzer. For example, in pascal, ↑ can be
isolated in lexical analyzer.
19. What do you meant by sentinel?
The ‘eof’’ character, which is introduced at the end of the buffer is called sentinel. It is used to
identify the end of the buffer.
Buffer 1 bp
i n t i = i
+ 1 ; J = 1 eof
fp buffer 2 sentinal
20. Define book keeping.
‘Symbol table’ is also called as Book keeping. All the information about the tokens are in
the symbol table.
The task of symbol table is to store identifiers used in the program. It also stores information
about attributes of each identifier such as its type, its scope, and storage allocated for it.
It also stores information about the subroutines used in the program. In this case the attributes
may be name of the subroutine, number of arguments passed to it, its type and the method of
passing these arguments (call by value or call by reference) and the return type.
Basically it is a data structure which stores information about all tokens