Download as pdf or txt
Download as pdf or txt
You are on page 1of 14

IA-II Question Bank

1. Compute First and Follow for the following grammer.


S→Aa
A→BD
B→b|€
D→d|€

2. What are the different ways of intermediate code representation? Explain with example.

Postfix Notation:
• Also known as reverse Polish notation or suffix notation.
• In the infix notation, the operator is placed between operands, e.g., a +
b. Postfix notation positions the operator at the right end, as in ab +.
• For any postfix expressions e1 and e2 with a binary operator (+) , applying
the operator yields e1e2+.
• Postfix notation eliminates the need for parentheses, as the operator’s
position and arity allow unambiguous expression decoding.
• In postfix notation, the operator consistently follows the operand.
Example 1: The postfix representation of the expression (a + b) * c is : ab +
c*
Example 2: The postfix representation of the expression (a – b) * (c + d) +
(a – b) is : ab – cd + *ab -+
2. Three-Address Code:
• A three address statement involves a maximum of three
references, consisting of two for operands and one for
the result.
• A sequence of three address statements collectively
forms a three address code.
• The typical form of a three address statement is
expressed as x = y op z, where x, y, and z represent
memory addresses.
• Each variable (x, y, z) in a three address statement is
associated with a specific memory location.
• While a standard three address statement includes
three references, there are instances where a statement
may contain fewer than three references, yet it is still
categorized as a three address statement.
Example: The three address code for the expression a +
b * c + d : T1 = b * c T2 = a + T1 T3 = T2 + d; T 1 , T2 ,
T3 are temporary variables.
There are 3 ways to represent a Three-Address Code in
compiler design:
i) Quadruples
ii) Triples
iii) Indirect Triples
Read more: Three-address code

3. Syntax Tree:
• A syntax tree serves as a condensed representation of a
parse tree.
• The operator and keyword nodes present in the parse
tree undergo a relocation process to become part of
their respective parent nodes in the syntax tree. the
internal nodes are operators and child nodes are
operands.
• Creating a syntax tree involves strategically placing
parentheses within the expression. This technique
contributes to a more intuitive representation, making it
easier to discern the sequence in which operands should
be processed.
• The syntax tree not only condenses the parse tree but
also offers an improved visual representation of the
program’s syntactic structure,
Example: x = (a + b * c) / (a – b * c)
3. Construct LR(0) parsing table for the following grammar and analyze the contents of stack and
input buffer and action taken after each step while parsing the input ‘abbcbcde’.
S→aCDe
C→Cbc
C→b
D→d
4. Discuss different databases used in direct linking loader.

Loader is the system program which is responsible for preparing the object program for
execution and initiate the execution.
• The loader does the job of coordinating with the OS to get initial loading address for the
.EXE file and load it into the memory.
Function of Loader
Allocation:
• Allocates the space in the memory where the object program would be loaded for
Execution.
Linking:
• It links two or more object codes and provides the information needed to allow references
between them.
Loading:
• It brings the object program into the memory for execution.

Format of Databases:
The assembler provides following types of record in the object file as follows

1. External Symbol Dictionary (ESD)

• ESD record combine information about all the symbol that are defined in this program.
• But may be referenced in the program but defined elsewhere.

1. Text Cards (TXT)

• Text card record control the actual object code translated version of the source program.

1. Relocation and Linkage Directory (RLD):

• The RLD records contains information about location in the program


• Whose contents depend on the address at which the program is placed.

1. END CARD:

• The END card records indicates the end of the object File and specifies the start address for
Execution.
ESD
IT consist of three types of definition
• SD- Segment Definition
• LD – Local Definition
• ER – External Reference

Relocating and Linking directory (RLD)


It includes different operations performed on it.
Flag is important in this table

Note:
• Since all the address cannot be resolved in the starting.
• We required two pass Dynamic linking Loader.
• In pass 1 , it performs defining of a segment and local definition
• In pass 2, we work on with the text and RLD.
END
• Specifies the END of the program. • And total address is generated by PLA + SLength.

5. Generate 3-address code for the following C program and construct flow graph with the help of
basic blocks:
I=1; j=1; x=5;
While(i<3)
{
Switch(i)
{
Case 1:
A[j++]= i+x;
Break;
Case 2:
A[j++]=i-x;
Break;
}
i++;
}
6. What the phases of compiler? Give working of each for the following:
a) P = Q +R-S*3 b) a=b*c+10 where a,b,c are of type real

The compilation process contains the sequence of various phases. Each phase takes
source program in one representation and produces output in another
representation. Each phase takes input from its previous stage.

There are the various phases of compiler:

Fig: phases of compiler

Lexical Analysis:
Lexical analyzer phase is the first phase of compilation process. It takes source code
as input. It reads the source program one character at a time and converts it into
meaningful lexemes. Lexical analyzer represents these lexemes in the form of
tokens.

Syntax Analysis
Syntax analysis is the second phase of compilation process. It takes tokens as input
and generates a parse tree as output. In syntax analysis phase, the parser checks
that the expression made by the tokens is syntactically correct or not.

Semantic Analysis
Semantic analysis is the third phase of compilation process. It checks whether the
parse tree follows the rules of language. Semantic analyzer keeps track of
identifiers, their types and expressions. The output of semantic analysis phase is
the annotated tree syntax.

Intermediate Code Generation


In the intermediate code generation, compiler generates the source code into the
intermediate code. Intermediate code is generated between the high-level
language and the machine language. The intermediate code should be generated
in such a way that you can easily translate it into the target machine code.

Code Optimization
Code optimization is an optional phase. It is used to improve the intermediate code
so that the output of the program could run faster and take less space. It removes
the unnecessary lines of the code and arranges the sequence of statements in
order to speed up the program execution.

Code Generation
Code generation is the final stage of the compilation process. It takes the optimized
intermediate code as input and maps it to the target machine language. Code
generator translates the intermediate code into the machine code of the specified
computer.

7. Explain dynamic linking loader in detail.

8. Explain different code optimization techniques in detail.


9. What is 3-address code ? Generate 3 address code for
While(a<b)
{
If(c<d)
X=y+z;
Else X=y-z;
10. Explain absolute loader . State its advantages and disadvantages.

Role of Loader in Compilation


In the compilation process, the Loader is responsible for bringing the
machine code into memory for execution. It performs the following key
functions:
• Loading the executable program into memory from the
secondary storage device.
• Allocating memory space to the program and its data.
• Resolving external references between different parts of the
program.
• Setting up the initial values of the program counter and stack
pointer.
• Preparing the program for execution by the CPU.

Advantages of Loader

There are several advantages of using a loader in compiler design:


1. Relocation: The loader can relocate a program to any memory
location, which allows for efficient use of memory and prevents
conflicts with other programs.
2. Modularity: The loader makes it possible to develop and use
separate modules or components, which can be linked together to
form a complete program. This allows for a more modular design,
which can make programs easier to maintain and update.
3. Reusability: As the program is separated into different modules,
it can be reused in other programs. The loader also allows the use
of shared libraries, which can be used by multiple programs.
4. Ease of use: The loader provides a simple and consistent
interface for loading and executing programs, which makes it
easier for users to run and manage programs on their system.
5. Error handling:
6. Dynamic linking:
7. Memory management:

Disadvantages of Loader

There are several disadvantages of using a loader in compiler design:


1. Complexity: Loaders can be complex to implement and
maintain, as they need to perform a variety of tasks such as
memory management, symbol resolution, and relocation.
2. Overhead: Loaders introduce additional overhead in terms of
memory usage and execution time, as they need to read the object
code from storage and perform various operations before the
program can be executed.
3. Size limitations: Loaders have limitations on the size of the
program that can be loaded and might not be able to handle large
programs or programs with a lot of external references.
4. Limited Flexibility: Loaders are typically specific to a particular
operating system or architecture, and may not be easily portable
to other systems.
5. Security:
6. Error handling: .
7. Overlapping Memory:
8. Dependency issues:

11. Construct SLR parser for the grammar and parse the input ‘()()’
S→(S)S|€
12. Explain the basic blocks and flow graph with example the 3-address code.

Basic Block is a straight line code sequence that has no branches in


and out branches except to the entry and at the end respectively.
Basic Block is a set of statements that always executes one after
other, in a sequence.
The first task is to partition a sequence of three-address codes into
basic blocks. A new basic block is begun with the first instruction and
instructions are added until a jump or a label is met
Basic Blocks-

Basic block is a set of statements that always executes in a sequence one after the other.

The characteristics of basic blocks are-


• They do not contain any kind of jump statements in them.
• There is no possibility of branching or getting halt in the middle.
• All the statements execute in the same order they appear.
• They do not lose lose the flow control of the program.

Example Of Basic Block-

Three Address Code for the expression a = b + c + d is-

Here,
• All the statements execute in a sequence one after the other.
• Thus, they form a basic block.

Also Read- Three Address Code

Example Of Not A Basic Block-

Three Address Code for the expression If A<B then 1 else 0 is-
Here,
• The statements do not execute in a sequence one after the other.
• Thus, they do not form a basic block.

Partitioning Intermediate Code Into Basic Blocks-

Any given code can be partitioned into basic blocks using the following rules-

Rule-01: Determining Leaders-

Following statements of the code are called as Leaders–


• First statement of the code.
• Statement that is a target of the conditional or unconditional goto statement.
• Statement that appears immediately after a goto statement.

Rule-02: Determining Basic Blocks-

• All the statements that follow the leader (including the leader) till the next leader
appears form one basic block.
• The first statement of the code is called as the first leader.
• The block containing the first leader is called as Initial block.

Flow Graphs-
A flow graph is a directed graph with flow control information added to the basic blocks.

• The basic blocks serve as nodes of the flow graph.

13. Explain with example the following code optimization technique


a) Common subexpression elimination b) code motion
b) Dead code elimination
c) Constant propogation
14. Explain direct linking loader with example.

15. Test whether following grammar is LL(1) or not. If it is LL(1) ,construct parse table for the
grammar:
a)
S→1A|B|€
A→1AC|0C
B→0S
C→1
b)
S→Ad
A→aB|BC
B→b
C→e|€
c)
S→(A) | 0
S→AB
B→,SB|€

b)

S→aBDh

B→ cC

C→ bC|€

D→ EF

E→ g|€

F→ f|€ check whether the string ‘acbgh’ is valid or not.

16. What are different issues in code generation phase of compiler.

17. Differentiate between top down and bottom up parsing.

Aspect Top-Down Parsing Bottom-Up Parsing


Starts from the top (start symbol) of the
Starts from the input tokens and works
Approach grammar and tries to match it to the input
upwards to construct the parse tree.
string.
Aspect Top-Down Parsing Bottom-Up Parsing
Recursive descent parsing is a common Shift-reduce parsing is a common
approach, where each nonterminal in the approach, where tokens are shifted onto
Process
grammar has a corresponding parsing a stack and then reduced according to
function. grammar rules.
Used for a broader range of grammars,
Grammar Often used for LL(k) grammars, where k is the
including LR(k) grammars, where k is
Restrictions lookahead size.
the lookahead size.
Sometimes limited due to the need for Generally more predictable and can
Predictability
lookahead and potential left recursion. handle a wider range of grammars.
Typically offers better error messages, as Error messages might be less
Error Handling parsing functions are closely tied to grammar descriptive, as errors are detected
rules. during the reduction phase.
Constructs a parse tree from bottom to
Constructed Constructs a parse tree from top to bottom,
top, which might differ from the input's
Tree which can closely resemble the input structure.
visual structure.
LR(0), SLR(1), LALR(1), LR(1)
Examples Recursive descent parsing, LL(k) parsers.
parsers.
Often more complex to implement but
Can be simpler to implement for certain
Complexity tends to be more efficient for larger
grammars but might be less efficient.
grammars.

18. State the difference between LL parser and LR parser.

LL Parser LR Parser

First L of LL is for left to right and L of LR is for left to right and R


second L is for leftmost derivation. is for rightmost derivation.

It follows reverse of right most


It follows the left most derivation.
derivation.

Using LL parser parser tree is Parser tree is constructed in


constructed in top down manner. bottom up manner.

In LL parser, non-terminals are In LR parser, terminals are


expanded. compressed.

Starts with the start symbol(S). Ends with start symbol(S).

Ends when stack used becomes


Starts with an empty stack.
empty.

Post-order traversal of the


Pre-order traversal of the parse tree.
parser tree.
LL Parser LR Parser

Terminal is read after popping out of Terminal is read before pushing


stack. into the stack.

It may use backtracking or dynamic


It uses dynamic programming.
programming.

LL is easier to write. LR is difficult to write.

Example: LR(0), SLR(1),


Example: LL(0), LL(1)
LALR(1), CLR(1)

19 What do you mean by operator precedence grammar.

20 Write functions of loaders.

You might also like