You are on page 1of 19

1.What is a Complier?

A Complier is a program that reads a program written in one language-the source


language-and translates it in to an equivalent program in another language-the target language
. As an important part of this translation process, the compiler reports to its user the presence
of errors in the source program.

2. State some software tools that manipulate source program?

i. Structure editors

ii. Pretty printers

iii. Static

iv. checkers

v. Interpreters.

3.What is a Structure editor?

A structure editor takes as input a sequence of commands to build a source program .The
structure editor not only performs the text creation and modification functions of an ordinary
text editor but it also analyzes the program text putting an appropriate hierarchical structure
on the source program.

4. List the operations on languages.

Union - L U M ={s | s is in L or s is in M}

Concatenation – LM ={st | s is in L and t is in M}

Kleene Closure – L* (zero or more concatenations of L)

Positive Closure – L+ ( one or more concatenations of L)

5.Define handle.

A handle of a string is a substring that matches the right side of a production, and whose
reduction to the nonterminal on the left side of the production represents one step along the
reverse of a rightmost derivation.

A handle of a right – sentential form γ is a production A→β and a position of γ where the string
β may be found and replaced by A to produce the previous right-sentential form in a rightmost
derivation of γ. That is , if S =>αAw =>αβw,then A→β in the position following α is a handle of
αβw.

6.define 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.

7.Differentiate compiler and interpreter.

BASIS FOR
COMPILER INTERPRETER
COMPARISON

Input It takes an entire program at a It takes a single line of code o

time. instruction at a time.

Output It generates intermediate object It does not produce any

code. intermediate object code.

Working mechanism The compilation is done before Compilation and execution ta

execution. place simultaneously.

Speed Comparatively faster Slower

Memory Memory requirement is more It requires less memory as it

due to the creation of object not create intermediate objec


BASIS FOR
COMPILER INTERPRETER
COMPARISON

code. code.

Errors Display all errors after Displays error of each line on

compilation, all at the same one.

time.

Error detection Difficult Easier comparatively

Pertaining C, C++, C#, Scala, typescript PHP, Perl, Python, Ruby uses

Programming uses compiler. interpreter.

languages

8.Define annotated parse tree?


A parse tree showing the values of attributes at each node is called an annotated
parse tree. The process of computing an attribute values at the nodes is called
annotating parse tree.
Example: an annotated parse tree for the input 3*5+4n.

9.Differentiate tokens, patterns, lexeme.

 Tokens- Sequence of characters that have a collective meaning.


 Patterns- 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

 Lexeme- A sequence of characters in the source program that is matched by


the pattern for a token.

10.Mention some of the cousins of a compiler.

Cousins of the compiler are:

 Preprocessors

 Assemblers

 Loaders and Link-Editors

11.What are the two main parts of compilation? What are they performing?

Analysis and Synthesis are the two parts of compilation.

The analysis part breaks up the source program into constituent pieces and creates an
intermediate representation of the source program.

The synthesis part constructs the desired target program from the intermediate representation.

12. Write a regular expression for an identifier.

An identifier is defined as a letter followed by zero or more letters or digits.

The regular expression for an identifier is given as

letter (letter | digit)*

13.What are the different notational shorthands available?

 One or more instances (+)


 Zero or one instance (?)

 Character classes ([abc] where a,b,c are alphabet symbols denotes the regular
expressions a | b | c.)

 Non regular sets

14.List the characteristics of peephole optimization.

 Redundant instruction elimination

 Flow of control optimization

 Algebraic simplification

 Use of machine idioms

15.List the various compiler construction tools.

The following is a list of some compiler construction tools:

 Parser generators

 Scanner generators

 Syntax-directed translation engines

 Automatic code generators

 Data-flow engines

16.Define a context free grammar.

A context free grammar G is a collection of the following

 V is a set of non terminals

 T is a set of terminals

 S is a start symbol
 P is a set of production rules

G can be represented as G = (V,T,S,P)

Production rules are given in the following form

Non terminal → (V U T)*

17.What is a flow graph?

A flow graph is a directed graph in which the flow control information is added to the
basic blocks.

 The nodes to the flow graph are represented by basic blocks

 The block whose leader is the first statement is called initial block.

 There is a directed edge from block B1 to block B2 if B2 immediately follows B1 in the given
sequence. We can say that B1 is a predecessor of B2

18.Define symbol table.

Symbol table is a data structure used by the compiler to keep track of semantics of the
variables. It stores information about scope and binding information about names.

19.List the different storage allocation strategies.

The strategies are:

 Static allocation

 Stack allocation

 Heap allocation

20.What is meant by handle pruning?

A rightmost derivation in reverse can be obtained by handle pruning.

If w is a sentence of the grammar at hand, then w = γn, where γn is the nth right-
sentential form of some as yet unknown rightmost derivation
S = γ0 => γ1…=> γn-1 => γn = w

21.Define LR(0) items.

An LR(0) item of a grammar G is a production of G with a dot at some position of the


right side. Thus, production A → XYZ yields the four items

A→.XYZ

A→X.YZ

A→XY.Z

A→XYZ.

22.What are the Error-recovery actions in a lexical analyzer?

Possible error recovery actions are:

 Panic mode recovery

 Deleting an extraneous character

 Inserting a missing character

 Replacing an incorrect character by a correct character

 Transposing two adjacent characters

23.What are the disadvantages of operator precedence parsing?

Disadvantages

1. The operator like minus has two different precedence(unary and binary).Hence it is hard to
handle tokens like minus sign.

2. This kind of parsing is applicable to only small class of grammars.

24.What is dangling else problem?

Ambiguity can be eliminated by means of dangling-else grammar which is show below:


stmt → if expr then stmt

| if expr then stmt else stmt

| other

25.How do you calculate the cost of an instruction?

The cost of an instruction can be computed as one plus cost associated with the source
and destination addressing modes given by added cost.

MOV R0,R1 1

MOV R1,M 2

SUB 5(R0),*10(R1) 3

26.List three types of implementations of three address statements?

The three address statements can be implemented using the following types

 Quadruple : a structure with atmost four fields such as


operator(OP),arg1,arg2,result.

 Triples : the use of temporary variables is avoided by referring the pointers in


the symbol table.

 Indirect triples : the listing of triples has been done and listing pointers are
used instead of using statements.

27.What are the various ways to pass a parameter in a function?

 Call by value

 Call by reference

 Copy-restore

 Call by name
28.Mention the general phases of a compiler

The following are the various phases of a compiler:

 Lexical Analyzer
 Syntax Analyzer
 Semantic Analyzer
 Intermediate code generator
 Code optimizer
 Code generator

29.Mention the role of semantic analysis.

The role of the semantic analysis is the phase in which certain checks are performed to ensure
that the components of a program fit together meaningfully.

30.Define Lexical Analysis?

The first phase of compiler is Lexical Analysis. This is also known as linear analysis in which the
stream of characters making up the source program is read from left-toright and grouped into
tokens that are sequences of characters having a collective meaning.

31.What does a semantic analysis do?

Semantic analysis is one in which certain checks are performed to ensure that components of a
program fit together meaningfully. Mainly performs type checking

32. What is a Loader? What does the loading process do?

A Loader is a program that performs the two functions

i.Loading
ii .Link editing
The process of loading consists of taking relocatable machine code, altering the
relocatable address and placing the altered instructions and data in memory at the
proper locations.

33.What is a preprocessor?

A preprocessor is one, which produces input to compilers. A source program may be divided
into modules stored in separate files. The task of collecting the source program is sometimes
entrusted to a distinct program called a preprocessor.
The preprocessor may also expand macros into source language statements.

Skeletal source program

Preprocessor

Source program

34. State some functions of Preprocessors

i) Macro processing

ii) File inclusion

iii) Relational Preprocessors

iv) Language extensions

35. What is an assembler?

Assembler is a program, which converts the source language in to assembly language

36.What is a sentinel? What is its usage?

A Sentinel is a special character that cannot be part of the source program. Normally we use
‘eof’ as the sentinel. This is used for speeding-up the lexical analyzer

37. What is a regular expression? State the rules,which define regular expression?

Regular expression is a method to describe regular language

Rules:

1) ε-is a regular expression that denotes {ε} that is the set containing the empty string

2) If a is a symbol in ∑,then a is a regular expression that denotes {a}

3) Suppose r and s are regular expressions denoting the languages L(r ) and L(s) Then,

a) (r )/(s) is a regular expression denoting L(r) U L(s).

b) (r )(s) is a regular expression denoting L(r )L(s)

c) (r )* is a regular expression denoting L(r)*.

d) (r) is a regular expression denoting L(r ).


38.What is recognizer?

Recognizers are machines. These are the machines which accept the strings belonging to
certain language. If the valid strings of such language are accepted by the machine then it is
said that the corresponding language is accepted by that machine, otherwise it is rejected.

39. How many phases does analysis consists?

Analysis consists of three phases

i .Linear analysis

ii. Hierarchical analysis

iii. Semantic analysis

40.Define static allocations and stack allocations

Static allocation is defined as lays out for all data objects at compile time. Names are bound to
storage as a program is compiled, so there is no need for a run time support package.

Stack allocation is defined as process in which manages the run time as a Stack. It is based on
the idea of a control stack; storage is organized as a stack, and activation records are pushed
and popped as activations begin and end.

41.Define code generation?

It is the final phase in compiler model and it takes as an input an intermediate representation of
the source program and output produces as equivalent target programs. Then intermediate
instructions are each translated into a sequence of machine instructions that perform the same
task.

42.What do you mean by algebraic transformations.

It can be used to change the set of expressions computed by a basic blocks into A algebraically
equivalent sets. The useful ones are those that simplify the Expressions place expensive
operations by cheaper ones.

X = X+ 0

X=X*1
43.What is meant by code optimization?

The term code-optimization refers to techniques a compiler can employ in an attempt to


produce a better object language program than the most obvious for a given source program.

44.What is dependency graph?

dependency graph is a directed graph representing dependencies of several objects


towards each other. It is possible to derive an evaluation order or the absence of an
evaluation order that respects the given dependencies from the dependency graph.

45.Define Loader

It loads the executable code into memory; program and data stack are created, register
gets initialized.

46.What are rational preprocessors?

Rational preprocessor: thesepreprocessors augment older languages with more


modern flow-of-control and data structuring facilities.

47.What is interpreter?

An interpreter translates high-level instructions into an intermediate form, which it then


executes.

48.What is Transition diagram

Transition diagram is a special kind of flowchart for language analysis. In transition


diagram the boxes of flowchart are drawn as circle and called as states. ... The label or
weight on edge indicates the input character that can appear after that state.

49.Define activation record.

The activation record is a block of memory used for managing the information needed by a
single execution of a procedure

50.What are the issues in the design of code generator

Input to the generator

Target programs

Memory management

Instruction selection
Register allocation

Choice of evaluation order

Approaches to code generation.

51.Define syntax tree representation?

syntax tree, is a tree representation of the abstract syntactic structure of source


code written in a programming language. Each node of the tree denotes a construct
occurring in the source code.

52.What is meant by left most derivation?

The leftmost derivation is the one in which you always expand the leftmost non-
terminal.

53.Define left factoring

Left Factoring is a grammar transformation technique. It consists in "factoring out"


prefixes which are common to two or more productions. For example, going from: A →
αβ|αγ

54.What is data flow analysis?

It is the analysis of flow of data in control flow graph, i.e., the analysis that determines
the information regarding the definition and use of data in program. With the help of this
analysis optimization can be done. In general, its process in which values are computed
using data flow analysis.The data flow property represents information which can be
used for optimization.

55.What is inherited attributes?

Inherited Attribute: An attribute that gets its values from the attributes attached to the
parent (or siblings) of its non-terminal. As you can see from the given
grammar(productions), inh is an inherited attribute.

56.Define Debugger?

As its name suggests the debugger is used to test and debug programs. The debugger
allows a user to test a program step by step, so that the problem points or steps can be
identified and rectified. It allows the user to inspect the registers and
the memory locations after a program has executed.
57.Write a short note on LEX.

 Lex is officially known as a "Lexical Analyser".


 Its main job is to break up an input stream into more usable elements. Or in,
other words, to identify the "interesting bits" in a text file.
 For example, if you are writing a compiler for the C programming language, the
symbols { } ( ); all have significance on their own.
 Instead, we are interested in the whole word. Spaces and newlines are
completely uninteresting, and we want to ignore them completely, unless they
appear within quotes "like this"
 All of these things are handled by the Lexical Analyser.

58.What are semantic errors? Give example

These are valid code the compiler understands, but they do not what you, the
programmer, intended.
These may be using the wrong variable, the wrong operation, or operations in the wrong
order. There is no way for the compiler to detect them.

example : Use of a non-initialized variable:

int i;

i++; // the variable i is not initialized

59.What is synthesized attributes?

A Synthesized attribute is an attribute of the nonterminal on the left-hand side of a


production. All of the attributes that we have used so far have
been synthesized.Synthesized attributes represent information that is being passed
up the parse tree.

60.Define Boolean expression?

Boolean Expressions are used to either compute logical values or as

conditional expressions in flow-of-control statements.

• We consider Boolean Expressions with the following grammar:

E ! E or E | E and E | not E | (E) | id relop id | true | false

• There are two methods to evaluate Boolean Expressions

1. Numerical Representation. Encode true with ’1’ and false with ’0’ and

we proceed analogously to arithmetic expressions.


2. Jumping Code. We represent the value of a Boolean Expression by a

position reached in a program.

61.Define Dead-code elimination with example?

Code that is unreachable or that does not affect the program (e.g. dead
stores) can be eliminated.

Example:

In the example below, the value assigned to i is never used, and the dead
store can be eliminated. The first assignment to global is dead, and the third
assignment to global is unreachable; both can be eliminated.

int global;
void f ()
{
int i;
i = 1; /* dead store */
global = 1; /* dead store */
global = 2;
return;
global = 3; /* unreachable */
}

62.Define Ambiguity with an example

A grammar is said to be ambiguous if there exists more than one leftmost


derivation or more than one rightmost derivative or more than one parse
tree for the given input string. If the grammar is not ambiguous then it is
called unambiguous.

Example:

1. S = aSb | SS
2. S = ∈
For the string aabb, the above grammar generates two parse trees:

63.What is mean by syntax directed definition?

Syntax directed definition specifies the values of attributes by associating semantic


rules with the grammar productions. It is a context free grammar with attributes and
rules together which are associated with grammar symbols and productions
respectively.

64.What do you mean by DAG

Directed acyclic graph(DAG) is a useful data structure for implementing transformations


on basic blocks.
DAG is used in

 · Determining the common sub-expressions.


 · Determining which names are used inside the block and computed outside the
block.
 · Determining which statements of the block could have their computed value
outside the block.

65.Define triple and give one example

This representation doesn’t make use of extra temporary variable to represent a single
operation instead when a reference to another triple’s value is needed, a pointer to that
triple is used. So, it consist of only three fields namely op, arg1 and arg2.

example – Consider expression a = b * – c + b * – c


66.Define quadruple and give one example

It is structure with consist of 4 fields namely op, arg1, arg2 and result. op denotes the
operator and arg1 and arg2 denotes the two operands and result is used to store the
result of the expression.

Example – Consider expression a = b * – c + b * – c.


The three address code is:
t1 = uminus c
t2 = t1 * b
t3 = uminus c
t4 = t3 * b
t5 = t2 + t4
a = t5
67.What is activation tree?

When a called procedure is executed, it returns the control back to the caller. This type
of control flow makes it easier to represent a series of activations in the form of a tree,
known as the activation tree.

68.What are the components of LR parser?

LR parser components: input, stack (strings of grammar symbols and states), driver
routine, parsing tables.

69.Write the algebraic laws obeyed by the regular expression

Algebraic laws of regular expressions

Law Description

r|s=s|r | is commutative

r | (s | t) = (r | s ) | t | is associative

r (st) = (rs)t Concatenation is associative

r(s|t) = rs | rt; (s|t)r = sr | tr Concatenation is distributive


Ɛr = rƐ = r Ɛ is identity for concatenation

r* = (r | Ɛ)* Ɛ is guaranteed in closure

r** = r* * is idempotent

70.What is meant by shift-reduce parsing?

Shift-reduce parsing attempts to construct a parsetree for an input string beginning at


the leaves and working up towards the root. In other words, it is a process of “reducing”
(opposite of deriving a symbol using a production rule) a string w to the start symbol of a
grammar.

71.What you mean by non-recursive predictive parser

Non-recursive predictive parser uses explicit stack data structure.


This prevents implicit recursive calls.
It can also be termed as table-driven predictive parser
72.State error recovery in operator-Precedence Parsing.

Error Recovery in operator-precedence parsing:


o There are two points in the parsing process at which an operator-precedence parser can
discover syntactic error:
 If no precedence relation holds between the terminal on top of the stack and the current
input.
 If a handle has been found, but there is no production with this handle as a right side.

You might also like