You are on page 1of 10

Anjalai Ammal Mahalingam Engineering College

Kovilvenni-614 403

Department of Computer Science and Engineering


TWO MARKS QUESTIONS AND ANSWERS

UNIT I INTRODUCTION TO COMPILERS

1. Differentiate compiler with interpreter.


A compiler is a computer 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.
A interpreter is a translator which converts source code into target code. The translation is
performed by the microcode resides in the control memory of a machine. This microcode
generates control signals for conversion.
2. Define pre-processor.
A pre-processor is a program that processes its input data to produce output that is used
as input to another program. The output is said to be a preprocessed form of the input data,
which is often used by some subsequent programs like compilers.
3. Issues in lexical analysis.
Separation of phases
Efficiency
Compiler portability
The lexical analyzer has to recognize the longest possible string
Skipping comments
Symbol table interface
4. List 4 software tools that generate parser.
LEX,YACC,BISON,LEMON
5. Algebraic properties of regular expressions.
R|S=S|R-Commutative
R|(S|T)=(R|S)|T-Associative
(RS)T=R(STA)-Concatenation is associative
R**=R*-* is idempotent
6. Define Recognizer or automata.
A recognizer for a language is a program that takes a string x ad answers yes
if x is a sentence of that language and no otherwise.
UNIT II LEXICAL ANALYSIS

1. Define NFA.
A non-deterministic finite automaton (NFA) is a mathematical model that consists of:
1. S- a set of states
2. -a set of input symbols (alphabet)
3. Move- a transition function move to map state-symbol pairs to sets of states.
4. S0-a start (initial) state
5. F- a set of accepting states (final states)
2. Define DFA.
A deterministic finite automaton (DFA) is a special form of a NFA.
No state has - transition
For each symbol a and state s, there is at most one labelled edge a leaving s. i.e. transition
function is from pair of state-symbol to state (not set of states).
3. What are error recovery actions in lexical analyzer?
1. Deleting an extraneous character.
2. Inserting a missing character.
3. Replacing an incorrect character by a correct character.
4. Transporting two adjacent characters.
4. What is Buffer Pair?
To reduce the amount of overhead required to process an input character, a buffer has
been used. It is divided into 2 N-character halves where N is the number of characters on
one disk block.
5. What is sentinel?
The sentinel is a special character used to represent the end of the buffer which cannot
be a part of the source program.

: : E : : : = : : M : * : eof :c : : : : eof

6. What are the goals of error handler?


a. Should report the presence of errors clearly and accurately
b. Should recover from each error quickly enough to be able to detect subsequent
errors.
c. Should not significantly slow down the processing of correct programs.
7. What are Kernel and Non-kernel items?
E E closure({E .E})=
E E+T { E .E -> kernel items
E T E .E+T
T T*F E .T
F (E) T .T*F non kernel items
F id T .F
F .(E)
F .id}

UNIT III SYNTAX ANALYSIS


1. Define parser.
The parser get valid tokens from the lexical analyzer and extract the constructs of
the language appearing within the sequence.
Its aim is to check whether the input is grammatically correct or not.
token parse tree
Lexical analyzer parser
get next token
syntax tree

2. What are the different kinds of errors faced by a program?


1. Lexical error.
2. Syntactic error.
3. Semantic error.
4. Logical error
3. What are semantic errors?

The errors due to undefined variables incompatible operands, etc., are called
semantic errors.

4. Define derivation.
The process of expanding the start symbol of the grammar to a desired string of
terminals by a sequence of intermediary strings is called derivation.
5. Define ambiguous grammar.
A grammar is said to be ambiguous if there exists more than one parse tree for the
same sentence. It can have more than one leftmost and rightmost derivation.
6. What are the two ways of top-down parsing?
i) Recursive descent parsing.
ii) Predictive parsing
7. Define recursive descent parsing.
The parser consists of a set of manually recursive routines which may require
backtracking to create a parse tree and it also requires repeated scanning of the input.
8. What are the advantages of LR parsing?
i) LR parser can recognize virtually all programming language constructs in
CFG.
ii) It is the most general backtracking technique known.
iii) Can detect syntax errors quickly.
9. Define LL(1) grammar.
A grammar is an LL(1) grammar if all productions satisfies the following conditions.
i) For each production A ->1|2|.....|n ,
FIRST (i) n FIRST(j)= for all ij
ii) If nonterminal X can derive an empty string, then
FIRST(X) n FOLLOW(X) =

10. Define operator grammar.

A grammar is said to be operator grammar if there does not exist and production
rule with right hand side.

i) As
ii) Two non terminals appearing consecutively
Eg. E E+E
E id
11. Immediate Left Recursive Grammar
A grammar is left recursive if it has a non-terminal A such that there is a
derivation.
A A
Top-down parser techniques cannot handle left-recursive grammars
12. Left Factoring
A predictive parser ( a top-down parser without backtracking) insists that the
grammar must be left-factored
A 1|2 where is non-empty and the first symbols of 1 and 2 (if they have
one ) are different.
Rewrite the grammar as follows
A A
A 1|2 so we can immediately expand A to A
13. Eliminate left recursion for the following grammar
A A|
Eliminate as
A A
A aA
Given: E E+T
E TE
E TE|?
14. What is Recursive Descent Parsing?
It is a top-down parser which requires backtracking. (if a choice of a production
rule does not work, we backtrack to try other alternatives). But it is not efficient.
15. What is predictive parser?
It is a recursive descent top down parser without backtracking.
It consists of stack, input buffer and finite control and tries to find the leftmost
derivation.
Recursive Predictive Parsing is a special form of Recursive Descent parsing
without backtracking.
Non-Recursive (Table Driven) predictive parser is also called as LL(1) parser.
16. How do you find FIRST and FOLLOW?
FIRST () is a set of the terminal symbol which occur as first symbols in strings
derived from where is any string of grammar symbols. If derives to , then is
also in FIRST ().
FOLLOW(A) is the set of the terminals which occur immediately after (follow) the
non-terminal A in the strings derived from the starting symbol.
- A terminal a is in FOLLOW (A) if S Aa
- $ is in FOLLOW(A) if S A
17. Write an algorithm for finding FOLLOW.
i) If S is the start symbol $ is in FOLLOW(S)
ii) If A B is a production rule
Everything in FIRST() is FOLLOW(B) except
iii) If (A B is a production rule ) or
(A B is a production rule and is in FIRST())
Everything in FOLLOW(A) is in FOLLOW(B)
18. What is operator precedence parser?
Parse all LR(1) grammars where two consecutive non terminals never appear in
the right-hand side of any rule and no epsilon present in the right side.
19. Define Handle.
Handle of a string is the substring that matches right side of a production and
whose reduction to the non terminal on the left side of the production represents one
step along the reverse of a right most derivation.
20. What is LL(1) grammar?
A grammar whose parsing table has no multiply-defined entries is said to be LL(1)
grammar.
L- left most derivation
L-input scanned from left to right
1- One input symbol used as a look-head symbol to determine parser action
21. Examples for LL(1) grammar.
i) Left Recursive Grammar
ii) Ambiguous Grammar
22. What are the Error Recovery techniques?
Panic-Mode error recovery
- Skipping the input symbols until a synchronizing token is found

Phrase-Level error recovery

- Each empty entry in the parsing table is filled with a pointer to a specific error
routine to take care that error case.
23. What is Bottom-up parser?
A bottom-up parser creates the parse tree of the given input starting from leaves
towards the root.
A bottom-up parser tries to find the right-most derivation of the given input in the
reverse order.
24. Define Shift Reduce Parser.
Bottom-up parsing is also known as shift-reduce parsing because its two main
actions are shift and reduce.
- At each shift action, the current symbol in the input string is pushed to a stack.
- At each reduction step, the symbols at the top of the stack (this symbol
sequence is the right side of a production) will replaced by the non-terminal at
the left side of that production.
- There are also two more actions: accept and error.
25. What are the conflicts encountered while parsing?
Stack contents and the next input symbol may not decide action:
- Shift/reduce conflict : whether make a shift operation or a reduction.
- Reduce/reduce conflict: the parser cannot decide which of several reductions
to make.
26. What are the categories of shift reduce parser?
There are two main categories of shift reduce parser.
1. Operator-precedence parser
- Simple, but only a small class of grammars.
2. LR-parsers
- Covers wide range of grammars.
1. SLR- simple LR parser
2. LR- most general LR parser
3. LALR-intermediate LR parser (lookhead LR parser)
27. What is LR(k) parsing?
L-left to right canning
R- right most derivation
K- lookhead
28. Write short note on closure operation.

If I is a set of LR(0) items for a grammar G, then closure(I) is the set of LR(0) items
constructed from I by the two rules:

1. Initially, every LR(0) item in I is added to closure(I).


2. If A .B is in closure(I) and B is a production rule of G; then B
B . will be in the closure(I). Apply this rule until no more new LR(0) items can be
added to closure(I).
29. Write short note on GOTO function?
If I is a set of LR(0) items and X is a grammar symbol(terminal or non-terminal), then
goto (I,X) is defined as follows:
- If A .X in I then every item in closure( { A X.}) will be in goto (I,X).
30. What is LR(1) item?
A LR(1) item is:
A ., a where a is the look-head of the LR(1) item (a is a terminal or end-
marker)
31. Compare SLR and CLR.
SLR is smaller, uses FOLLOW function, computes LR(0) items
CLR is larger, uses only lookheads, computes LR(1) items.
32. What is CLR canonical LR?
In order to overcome the conflicts like shift-reduce and reduce-reduce conflicts,
CLR parser as been constructed.
Algorithm:
C is { closure({ S .S,$ })}
Repeat the followings until no more set of LR(1) items can be added to C.
For each I in C and each grammar symbol X.
If goto (I,X) is not empty and not in C Add goto (I,X) to C.

UNIT IV SYNTAX DIRECTED TRANSLATION & RUN TIME ENVIRONMENT

1. What is syntax directed definition?


A syntax directed definition is a generalization of a context-free grammar in which
each grammar symbol has an associated set of attributes, partitioned into two
subsets called the synthesized and inherited attributes of that grammar symbol.
2. What are inherited attributes?
A inherited attribute is one whose value at a node in a parse tree is defined in
terms of attributes at the parent and/or siblings of that node.
3. What is Annotated Parse Tree?
A parse tree showing the values of attributes at each node.
4. How do you evaluate semantic rules?
a) May generate intermediate codes
b) May put information into the symbol table
c) May perform type checking
d) May issue error messages
e) May perform some other activities
f) In fact, they may perform almost any activities.
5. What are translation schemes?
a)Indicate the order of evaluation of semantic actions associated with a
production rule
b)In other words, translation schemes give a little bit information about
implementation details
6. What are the different representations of intermediate languages?
a) Syntax tree
b) Postfix
c) Three address code
7. What are the implementations of three-address statements?
a) Quadruples
b) Triples
c) Indirect triples
8. Give syntax directed translation for the following statement call p1 ( int a, int b).
S call id (Elist) generate a param statement for each item on queue.
Causing these statements to follow the statements evaluating the
argument expressions {for each item p on queue do emit (paramp);
Emit (callid.place)
Elist Elist,E {append E.place to the end of the queue}
Elist E {initialize queue to contain only E.place}
9. What is backpatching?
The labels left unspecified in the jump statements will be filled in when the
proper label can be determined. This process of filling-in of labels is backpatching.
Source:
If a or b then
goto L3
if c then L3:x=y+1
x=y+1 L3:
Translation : After Backpatching
if a goto L 100: if a goto 103
if b goto L1 101:if b goto 103
goto L3 goto 106
L1:if c goto L2
10. List of functions used to manipulate list of labels in backpatching.
i) Makelist(i) creates a newlist and put i in the list
ii) Merge(p1,p2)-merges two lists pointed by p1 and p2
iii) Back patch(p,j) inserts the target label j for each list pointed by p.

UNIT V CODE OPTIMIZATION AND CODE GENERATION

1. What are the factors affecting code generation?


i) Input
ii) Target code structure
iii) Instruction selection
iv) Register allocation
v) Evaluation order

2. Define register interference graph.


i) Each node corresponds to a unique variable
ii) An edge exists between two nodes if and only if the corresponding variables are
live simultaneously at some point in the program.
3. Write the pseudocode of graph colouring problem.
i/p: interference graph, k colors
the graph is k-colourable
i) Pick a node t with fewer than k neighbours.
ii) Push the variable t onto stack and remove it from the graph along with all adjoining
edges.
iii) Repeat the step 1 and 2 until no node is left.
iv) While stack is not empty do
Pop a node, color the node with a color different from those end do assigned to
already coloured neighbours of x.
4. What are the objects to be maintained during runtime?
i) Generated code
ii) Data objects
iii) Stack
5. What are the fields in an activation record?
i) Parameters passed to the procedure
ii) Book keeping information includes return address
iii) Storage for local variables
iv) Storage for local temporaries
6. Define register descriptor.
A register descriptor keeps track of what is currently in each register.
7. What is address descriptor?
An address descriptor keeps track of the location where the current value of the name
can be found at run time.
8. What are the storage allocation strategies during runtime?
i) Static allocation
ii) Stack allocation
iii) Heap allocation
9. What are the different ways of parameters to a procedure?
i) Call-by-value
ii) Call-by-reference
iii) Copy restore
iv) Call-by-name
v) Macro expansion
10. What is Basic Block?
A basic block is a sequence of consecutive statements in which flow of control enters at
the beginning and leaves at the end without halt or possibility of branching except at the
end.
Flow graph:
The basic block with flow-of-control information making up a program by constructing a
directed graph.
11. What is Activation tree?
A tree used to represent the control enters and leaves activations where the activation
is the execution of a procedure.
12. What are the limitations of static allocation?
i) Size of a data object and constraints must be known at compile time
ii) Recursive procedures are restricted
iii) Data structures cannot be created dynamically
13. What is peephole optimization?
A simple technique for locally improving target code ( can also be applied o intermediate
code) . The peephole is a small, moving window on the target program.
14. What are the different ways to pass parameters in a function?
a) Call-by-value
b) Call-by-reference
c) Copy-restore
d) Call-by-name
15. How would you calculate the cost of an instruction?
The cost of an instruction to be one plus the costs associated with the source and
destination address modes. This cost corresponds to the length of the instruction.
Eg. MOV R0,R1 - cost=1
MOV b,R0 - cost=2
16. How would you map names to values?
An address descriptor keeps track of the location where the current value of the name
can be found at run time.

17. Define DAG


A DAG for a basic block is a directed acyclic graph with the following labels on nodes:
i) Leaves are labelled by unique identifiers,either vaiable names or constants.
ii) Interior nodes are labelled by an operator symbol.
iii) Nodes are also optionally given a sequence of identifiers for labels
Applications:
i) Detect common sub expression
ii) Identify the values of an identifier in the block
iii) Determine which statements compute values and could be used.
18. What are the features to be maintained during optimization?
Semantic equivalence with the program has to be maintained.
The transformation must speed up program to a measurable amount.
19. What are the factors influencing the optimization?
Characteristics of the target machine
Number of CPU registers
Number of functional units
Cache size
20. What is dead code?
The portion of the program that can never get executed for any control flow through the
program.
21. Define program point
A program point j is the instant between the end of execution of instruction i j And the
beginning of the execution of the instruction i j+1 .
22. What is code motion?
Transformation takes an expression that yields the same result independent of the number
of times a loop is executed.
i. Eg. while(i<limit-2)
ii. {
iii. ;;;;
iv. }
v. Can be restated ad
vi. T=limit-2;
vii. While(i<t)
viii. {
ix. ;;;;
x. }
23 How do you find leader in basic block?

a) First statement is a leader


b) Any statement that is the target of a conditional or unconditional goto
c) Any statement that immediately follows a goto or conditional goto statement

24 What is constant folding?

Deducing at compile time that the value of an expression is a constant and using the constant
instead.

Eg. x :=32 becomes x:=64


X:=x +32

25 What is LR(K) parsing?

L-Left to Right Parsing


R-Rightmost derivation
K-lookheads

26 What is global data-flow analysis?

In order to perform code optimization and code generation in a good manner, a compiler
needs to collect information about the program as a whole and to distribute this
information to each block in the flow graph. This can be performed by Global Data-flow
analysis.

You might also like