CD Questions With Answers
CD Questions With Answers
a. What is the difference between parse tree and abstract syntax tree ?
Ans.
S. No. Parse tree Abstract syntax tree
It contains records of the rules (tokens) to match It contains records of the syntax of programming
2.
input texts. language.
c. What are the various errors that may appear in compilation process?
Ans. Types of errors that may appear in compilation process are:
a. Lexical phase error
b. Syntactic phase error
c. Semantic phase error
Given grammar
S→aAd/bBd/aBe/bAe
A→f B→f
Construct LR(1) Parsing table .Also draw the LALR Table from the derived LR(1)parsing table.
d. What are the two types of attributes that are associated with a grammar symbol?
Ans. Two types of attributes that are associated with a grammar symbol are:
a. Synthesized attributes
b. Inherited attributes
Ans. Left recursion: A grammar G(V, T, P, S) is said to be left recursive if it has a production in the
form of:
Numerical:
Q. Write down the quadruple, triple, indirect triple for the following expression
(x + y)*(y + z).
Table. 2
Since, LALR table does not contain any conflict. So, it is also LALR(1).
DFA:
b. What is an activation record ? Explain how it is related with runtime -storage organization
?
Ans. Activation record:
1. An activation record is used to organize the data required for a particular procedure run.
2. When a procedure is called, an activation record is pushed onto the stack, and it is popped when
control is transferred back to the calling function.
Format of activation records in stack allocation:
Sub-division of run-time memory into codes and data areas is shown in Fig.
1. Code: It stores the executable target code which is of fixed size and do not change during
compilation.
2. Static allocation:
a. The static allocation is for all the data objects at compile time.
b. The size of the data objects is known at compile time.
c. This allocation of data objects is done via static allocation, and the names of these objects are only
bound to storage at compile time.
d. For static allocation, the compiler can estimate how much storage each data item needs. A
compiler can easily locate the location of these data in the activation record as a result.
e. The locations at which the target code can locate the data on which it works can be filled in by the
compiler at the time of compilation.
3. Heap allocation: There are two methods used for heap management:
a. Garbage collection method:
i. An object is considered to be garbaged when all access paths to it are eliminated but the data object
remains.
ii. One method for reusing that object space is garbage collection.
iii. Any components with the trash collection bit set to “on” are garbaged during garbage collection
and returned to the free space list.
b. Reference counter:
i. Reference counters make an effort to reclaim each heap storage item as soon as it becomes
inaccessible.
ii. Each memory cell on the heap is connected to a reference counter that keeps track of the values
that point to it.
iii. The count is raised whenever a new value points to the cell and lowered whenever a value stops
doing so.
4. Stack allocation:
a. Activation record data structures are stored using stack allocation.
b. As activations start and terminate, the activation records are pushed and popped, respectively.
c. The record for each call in the activation procedure contains storage for the locals in that call.
Locals are therefore guaranteed access to new storage in each activation because each call causes a
new activation record to be pushed onto the stack.
d. When the activation is finished, these local settings are removed.
Ans.
The canonical collection of sets o LR(0) items for grammar are as follows:
Let us numbered the production rules is the grammar as
Static allocation allocates memory on the basis of size of Heap allocation makes use of heap for managing the
data objects. allocation of memory at run time.
In static allocation, there is no possibility of creation of In heap allocation, dynamic data structures and objects
dynamic data structures and objects. are created.
In static allocation, the names of the data objects are Heap allocation allocates contiguous block of memory
fixed with storage for addressing. to data objects.
Static allocation is simple, but not efficient memory Heap allocation does memory management in efficient
management technique. way.
Static allocation strategy is faster in accessing data as Heap allocation is slow in accessing as there is chance
compared to heap allocation. of creation of holes in reusing the free space.
a. Construct predictive parse table for the following grammar. E → E + T/T T →T *F/F F →F /a/b
Section 4: Peephole Optimization
a. Write syntax directed definition for a given assignment statement:
By using panic mode it skips a = b + c without checking the error in the code.
2. Phrase-level recovery:
a. When a parser finds an error, it may locally repair the remaining input.
b. It might substitute a string that enables the parser to proceed for a prefix of the remaining input.
c. A typical local modification might remove an unnecessary semicolon, insert a needed semicolon,
and replace a comma with a semicolon.
For example:
Let consider a piece of code
while (x > 0) y = a + b;
In this code local correction is done by phrase-level recovery by adding ‘do’ and parsing is
continued.
3. Error production: If error production is used by the parser, we can generate appropriate error
message and parsing is continued.
For example:
Let consider a grammar
When error production encounters *A, it sends an error message to the user asking to use ‘*’ as
unary or not.
4. Global correction:
a. Global correction is a theoretical concept.
b. This method increases time and space requirement during parsing.
b. Discuss how induction variables can be detected and eliminated from the given intermediate
code
Ans. In the given question, there is no induction variable.
If we replace j with i. then the block B2 becomes
In the above block B2 value of i is incremented by every time in the loop and value of t 1 is
incremented 4 times the value of i.
So, in the given block i and t 1 are two induction variables.
In this case, if we want to remove the induction variables we use reduction is strength technique.
In this technique, we remove same line of code out of the block or modify the computation code in
the block.
Modification:
Parsing table:
Since, no column has more than one value. So the given grammar is LL(1) grammar.
b. Distinguish between static scope and dynamic scope. Briefly explain access to non local
names in static scope.
Ans. Difference:
No. Lexical scope Dynamic scope
The binding of name occurrences to declarations is done At runtime, name occurrences are
statistically at compile time. dynamically bound to declarations.
The environment in which a process is defined determines the value A free variable gets its value from where
of any free variables used within that procedure. the procedure is called.
Access to non-local names in static scope:
1. The method used to implement non-local names (variable) access in static scope is called static
chain.
2. An activation record instance in the stack that is connected by a static chain consists of static links.
3. An activation record instance for subprogram A points to one of the activation record instances of
A’s static parent via the static ink, or static scope pointer.
4. When an object declared in a static parent at the surrounding scope nested at level k is referenced
by a subroutine at the nesting level, J-k static links generate a static chain that must be navigated to
reach the frame containing the object.
5. In order to reach non-local names, the compiler generates code that performs these frame
traversals.
For example: Subroutine A is at nesting level 1 and Cat nesting level 3. When C accesses an object
of A, 2 static links are traversed to get to A’s frame that contains that object
Section 7: Code Generator and Code Loop Optimization
a. What are the various issues in design of code generator and code loop optimization ?
Ans. Various issues of code generator:
1. Input to the code generator:
a. The source program’s intermediate representation created by the front end, combined with data
from the symbol table, are the inputs to the code generator.
b. Graphical representations and three address representations are both included in IR.
2. The target program:
a. The complexity of building a decent code generator that creates high quality machine code is
significantly influenced by the target machine’s instruction set architecture.
b. The most popular target machine architectures are stack based, CISC (Complex Instruction Set
Computer), and RISC (Reduced Instruction Set Computer).
3. Instruction selection:
a. The target machine must be able to execute the code sequence that the code generator creates from
the IR program.
b. If the IR is high level, the code generator may use code templates to convert each IR statement
into a series of machine instructions.
4. Register allocation:
a. A key problem in code generation is deciding what values to hold in which registers on the target
machine do not have enough space to hold all values.
b. Values that are not held in registers need to reside in memory. Instructions involving register
operands are invariably shorter and faster than those involving operands in memory, so efficient
utilization of registers is particularly important.
c. The use of registers is often subdivided into two subproblems:
i. Register allocation, during which we select the set of variables that will reside in registers at each
point in the program.
ii. Register assignment, during which we pick the specific register that a variable will reside in.
5. Evaluation order:
a. The order in which computations are performed can affect the efficiency of the target code.
b. Some computation orders require fewer registers to hold intermediate results than others.
Various issues of code loop optimization:
1. Function preserving transformation: The function preserving transformations are basically
divided into following types:
a. Common sub-expression elimination:
i. A common sub-expression is just an expression that has previously been computed and is utilized
repeatedly throughout the programme.
ii. We stop computing the same expression repeatedly if the outcome of the expression is unchanged.
For example:
Before common sub-expression elimination:
iii. In given example, the equation a = t * 4 – b + c is occurred most of the times. So it is eliminated
by storing the equation into temp variable.
b. Dead code elimination:
i. Dead code means the code which can be emitted from program and still there will be no change in
result.
ii. A variable is live only when it is used in the program again and again. Otherwise, it is declared as
dead, because we cannot use that variable in the program so it is useless.
iii. The dead code occurred during the program is not introduced intentionally by the programmer.
For example:
iv. If false becomes zero, is guaranteed then code in ‘IF’ statement will never be executed. So, there
is no need to generate or Write code tor this statement because it is dead code.
c. Copy propagation:
i. Copy propagation is the concept where we can copy the result of common sub-expression and use
it in the program.
ii. In this technique the value of variable is replaced and computation of an expression is done at the
compilation time.
For example:
Here at the compilation time the value of pi is replaced by 3.14 and r by 5.
d. Constant folding (compile time evaluation):
i. Constant folding is defined as replacement of the value of one constant in an expression by
equivalent constant value at the compile time.
ii. In constant folding all operands in an operation are constant. Original evaluation can also be
replaced by result which is also constant.
For example: a = 3.14157/2 can be replaced by a = 1.570785 thereby eliminating a division
operation.
2. Algebraic simplification:
a. Peephole optimization is an effective technique for algebraic simplification.
b. The statements such as
b. Generate the three address code for the following code fragment.
Ans.
1. High level language is transformed into machine language. It transforms machine language f
Ans.
S.
Compiler Interpreter
No.
It scans through the entire source code and lists each It scans each line individually, and if there
1.
syntax error as it is found. programme immediately stops running.
The compiler’s output is saved in a file. Hence, the file The machine code that an interpreter gene
2.
need not be continuously compiled. As a result, each time, we must interpret th
Ans. A cross compiler is a compiler that has the ability to generate executable code
for platforms other than the one it is currently executing on.
Ans. 1. YACC is a tool which will produce a parser for a given grammar.
Ans. Language of DFA which accept even number of a and b is given by:
Regular expression for above DFA:
Ans.
1. Backtracking
2. Left recursion
3. Left factoring
4. Ambiguity
i. Input buffer
ii. Stack
iii. Parsing table
i. SLR parser
ii. LALR parser
iii. Canonical LR parser
Ans. To parse the input string using the shift-reduce approach, we require LR
parsing tables.
Ans. Three address code is an abstract form of intermediate code that can be
implemented as a record with the address fields. The general form of three address
code representation is :
a =b op c.
Ans. Postfix notation is the type of notation in which operator are placed at the right
end of the expression. For example, to add A and B, we can write as AB+ or BA+.
2. The outcome of any given operation in the triples notation is referenced to by its
position, thus if one instruction is relocated, modifications must be made to all
references that lead to that result.
Ans.
Q8. What is a syntax tree ? Draw the syntax tree for the following statement: c
bcba–*+–*=
Syntax tree of c b c b a – * + – * = :
In the given statement, number of alphabets is less than symbols. So, the syntax
tree drawn will be incomplete.
Q9. Define backpatching.
i. Boolean expressions
ii. Flow of control statements
Ans. SDD’s primary benefit is that it aids in selecting the evaluation order. The
assessment of semantic SDDD operations may result in the generation of code, the
saving of data to a symbol table, or the issuance of error signals.
Ans. Quadruples are structure with at most four fields such as op, arg1, arg2,
result.
Ans.
S.
Quadruples Triples
No.
Quadruple represent three address code by using four fields: OP, Triples represent three addres
1.
ARG1, ARG2, RESULT. fields:OP, ARG1, ARG 2.
Ans. A compiler uses a symbol table as a data structure to store details on the
scope, life, and binding of names. The various programme elements, such as
variables, constants, procedures, and the labels of statements, are identified by
these names in the source code.
1. Unordered list
2. Ordered list
3. Search tree
4. Hash tables and hash functions
Ans. Activation record is a type of data structure that holds crucial state details for a
certain function call instance.
Ans. By inserting references to error routines into the empty rows of the predictive
parsing table, phase level error recovery is performed. These procedures have the
ability to modify, add, or remove symbols from the input while displaying the proper
error warnings. They could possibly fall out of the stack.
Q5. List the various error recovery strategies for a lexica analysis.
Ans. Error recovery strategies are:
i. Static allocation
ii. Stack allocation
iii. Heap allocation
Ans. The storage is arranged as a stack in a stack allocation approach. The control
stack is another name for this stack. This stack uses push and pop operations to
manage the available storage for local variables.
Ans. In order to store data objects during runtime, the heap allocation allocates and
deallocates a continuous block of memory as needed.
Ans. The demerit of heap allocation is that it causes memory gaps during allocation.
Unit-V: Code Generation (Short Question)
Q1. What do you mean by code optimization ?
Ans. The approach employed by the compiler to improve the execution efficiency of
the generated object code is referred to as code optimisation.
i. Dominators
ii. Natural loops
iii. Inner loops
iv. Pre-header
v. Reducible flow graph
Ans.
i. Code motion
ii. Induction variable and strength reduction
iii. Loop invariant method
iv. Loop unrolling
v. Loop fusion
Ans.
Ans. Global register allocation first allocates registers for variables in inner loops
because that is where a programme spends the most of its time, and the same
register is utilized for variables if they appear in an outer loop.
Ans. Peephole optimisation is a type of code optimisation that only affects a small
portion of the code. It is used to a very tiny collection of instructions in a code
segment. Peephole refers to the limited set of instructions or small portion of code on
which peephole optimisation is conducted.
Impotent Topic:
DAG.
Constant folding.
Assembler
Hashing