Vous êtes sur la page 1sur 8

c c

c c
c
c
c

c
c
c
Ê Ê 

c

cc
c


 cc c Ê cccc
 cc c

cc
c
c   c
c c
Êc
Q1.Explain Static storage allocation.

ANS:

Ê   

In static allocation, names are bound to storage as the program is compiled, so


there is no need for a run-time support package. Since the bindings do not change
at run time, every time a procedure is activated, its names are bounded to the same
storage locations. This property allows the values of the local names to be retained
across ^ ^ of a procedure. That is, when control returns to a procedure, the
values of the locals are the same as they are when control left the last time.

From the type of a name, the compiler determines the


amount of storage to set aside for that name. The address of this storage consists of
an offset from an end of the activation record for the procedure. The compiler must
eventually decide where the activation records go, relative to the target code and to
one another. Once this decision is made, the position of each activation record, and
hence of the storage for each name in the record is fixed. At compile time we can
therefore fill in the addresses at which the target code can find the data it operates
on. Similarly the addresses at which information is to be saved when a procedure
call occurs are also known at compile time.

However, some limitations go along with using static allocations alone.

1. The size of the data object and constraints on its position in memory must be
known at compile time.

2. Recursive procedures are restricted, because all activations of a procedure use


the same bindings for local names.

3. Data structures cannot be created dynamically, since there is no mechanism for


storage allocation at run time.

 
Fortran was designed to permit static storage allocation. A Fortran program
consists of a main program, subroutines, and functions, as in Fortran 77 program in
figure(a). Using memory organization of figure(b), the layout of the code and the
activation records for this program in figure(c). Within the activation record for
CNSUME, there is space for the locals BUF, NEXT, and C. The storage bound to
BUF holds a string of 50 characters. It is followed by space for holding an integer
value for NEXT and a character value for C. The fact that NEXT is also declared
in PRDUCE presents no problem, because the locals of the two procedures get
space in their respective activation records.

Since the sizes of the executable code and the activation records are known at
compile time, memory organizations other than the one in figure(c) are possible. A
Fortran compiler might place the activation record for a procedure together with
the code for that procedure. On some computer systems, it is feasible to leave the
relative position of the activation records unspecified and allow the link editor to
link activation records and executable code.

//static memory allocation

Void alloc_static()

Static int array[10]; //static allocation

int n;

For (n=0; n<10; n++)

Array[n]=n+1;

Printf (³%d :%d\n´, n, array[n]);

Q2 Dynamic data storage allocation.

ANS:

In dynamic memory allocation (also known as heap-based memory allocation) is


the allocation of memory storage for use in a computer program during the runtime
of that program. It can be seen also as a way of distributing ownership of limited
memory resources among many pieces of data and code.Dynamically allocated
memory exists until it is released either explicitly by the programmer, or by the
garbage collector. This is in contrast to static memory allocation, which has a fixed
duration. It is said that an object so allocated has a dynamic lifetime.

Allocation of memory during execution is called dynamic memory allocation. C


provides library functions to allocate and free up memory dynamically during
program execution. Dynamic memory is allocated on the heap by the system.

If memory is repeatedly allocated, eventually the system will run out of memory.

Two basic operations in dynamic storage management:

d Allocate

d Free.

Dynamic allocation can be handled in one of two general ways:

d Stack allocation (hierarchical): restricted, but simple and efficient.

d Heap allocation: more general, but less efficient, more difficult to implement.
c

Q3. Give the structure of compiler with Syntax analysis.

ANS:

Unlike other aspects of the compiler, the syntax analysis parts are not very
separable, since they are mixed up with calls to all other parts, such as semantic
analysis.
However the method used is that commonly known as ÿ ÿ    This will
not be treated in great detail here - consult any book on compiler theory for details.

The method depends on writing a separate parsing procedure for each kind of
syntactic structure, such as u statement, assignment statement, expression and so
on, and each of these is only responsible for analysing its own kind of structure. If
any structure contains another structure then the parsing procedure can call the
procedure for this contained structure.

Q4 Give the working of code generator.

ANS: ANS: code generation is the process by which a compiler's code generator
converts some internal representation of source code into a form (e.g., machine
code) that can be readily executed by a machine .
Sophisticated compilers typically perform multiple passes over various
intermediate forms. This multi-stage process is used because many algorithms for
code optimization are easier to apply one at a time, or because the input to one
optimization relies on the processing performed by another optimization. This
organization also facilitates the creation of a single compiler that can target
multiple architectures, as only the last of the code generation stages (the ·^ )
needs to change from target to target.

The input to the code generator typically consists of a parse tree or an abstract
syntax tree. The tree is converted into a linear sequence of instructions, usually in
an intermediate language such as three address code.

Tasks which are typically part of a sophisticated compiler's "code generation"


phase include:

Instruction selection: which instructions to use.

Instruction scheduling: in which order to put those instructions. Scheduling is a


speed optimization that can have a critical effect on pipelined machines.

Xc The evaluation order depends on operator precedence¶s ±an operator which


precedes its left and right neighbors must be evaluated before either of them.
hence ,a feasible evaluation order is the order in which operators are reduced
during bottom up parse ,or the reverse of the order in which operators are
reduced during bottom up parse.
Xc The choice of an instruction to be used depends upon the type and length of
each operand and the addressability of each operand, i.e where the operand
is located and how it can be accessed.
Xc An important issue in code generation is when and how to partial results
between memory and CPU register, and how to know which partial results
are contained in a register.
Q5.Eplain data descriptors.

ANS:

In computer programming, a file descriptor is an abstract indicator for accessing a


file. The term is generally used in POSIX operating systems. In Microsoft
Windows terminology and in the context of the C standard I/O library, "file
handle" is preferred, though the latter case is technically a different object (see
below).

In POSIX, a file descriptor is an integer, specifically of the C type int. There are 3
standard POSIX file descriptors which presumably every process (save perhaps a
daemon) should expect to have:

Generally, a file descriptor is an index for an entry in a kernel-resident data


structure containing the details of all open files. In POSIX this data structure is
called a file descriptor table, and each process has its own file descriptor table. The
user application passes the abstract key to the kernel through a system call, and the
kernel will access the file on behalf of the application, based on the key. The
application itself cannot read or write the file descriptor table directly.

In Unix-like systems, file descriptors can refer to files, directories, block or


character devices (also called "special files"), sockets, FIFOs (also called named
pipes), or unnamed pipes.

The FILE * file handle in the C standard I/O library routines is technically a
pointer to a data structure managed by those library routines; one of those
structures usually includes an actual low level file descriptor for the object in
question on Unix-like systems. Since 
^
refers to this additional layer, it is
not interchangeable with 
  ÿ ÿ.

To further complicate terminology, Microsoft Windows also uses the term 

^
to refer to the more low-level construct, akin to POSIX's file descriptors.
Microsoft's C libraries also provide compatibility functions which "wrap" these
native handles to support the POSIX-like convention of integer file descriptors as
detailed above.
Q6.Differentiate top down and bottom up parsing.

Ê
 
u  according to a grammar G attempts to derive a string
matching a source string through a sequence of derivations starting with the
distinguished symbol G. Top-down parsing is determining how to interpret a
particular piece of data by splitting around non terminals first and sorting these into
a hierarchy that ends with terminals.

An LL parser, also called a £


 ££ 
 or £
 
, applies each
production rule to the incoming symbols by working from the left-most symbol
yielded on a production rule and then proceeding to the next production rule for
each non-terminal symbol encountered. In this way the parsing starts on the Left of
the result side (right side) of the production rule and evaluates non-terminals from
the Left first and, thus, proceeds down the parse tree for each new non-terminal
before continuing to the next symbol for a production rule.

   ££  

 constructs a parse tree for a source string through a
sequence of reductions. The source string is valid if it can be reduced to S ,the
distinguished symbol of G. Bottom up parsing proceeds in a left to right manner ,
ie. Attempts at reduction start with the first symbol in the string and proceed to the
right. Bottom-up parsing is determining how to interpret a particular piece of data
by resolving sets of terminals into terminals themselves, and branching up until the
whole set has been resolved

Vous aimerez peut-être aussi