COMPILER LAB VIVA QUETIONS AND ANSWERS What Is A Compiler?
A compiler is a program that reads a program written in one language –the source language and translates it into into an equivalent equivalent program in another language-the target language. The The compiler reports to its user the presence of errors in the source program.
What Are The To Parts O! A Compilatio"? E#plai" Brie!l
%$
Analysis and Synthesis are the two parts of compilation. o
o
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.
List The S&' Parts Or Phases O! A"al$sis A"al$sis Part%
Analysis consists of three phases:
2.
inear Analysis. !ierarchical Analysis.
3.
Semantic Analysis Analysis..
1.
S&((est A S&ita'le Approa)h Approa)h *or Comp&ti"( +ash *&")tio"%
o
"sing hash function fu nction we should obtain e#act e#act locations of name in symbol table.
o
The hash function should result in uniform distribution of names in symbol table.
o
The hash function should be such that there will be minimum number of collisions.
o
$ollision is such a situation where hash function results in same location for storing the names.
What Is Co,e Motio"?
$ode motion is an optimi%ation technique in which amount of code in a loop is decreased. This transformation is applicable to the e#pression that yields the same result independent of the number of times the loop is e#ecuted. Such an e#pression is placed before the loop.
What Are The Properties O! Optimi-i"( Compiler?
The source code should be such that it should produce minimum amount of target code. There should not be any unreachable code. &ead code should be completely removed from source language. The optimi%ing compilers should apply following code improving transformations on source language.
2.
common sube#pression elimination dead code elimination
3.
code movement
4.
strength reduction
1.
What Are The Vario&s Wa$s To Pass A Parameter I" A *&")tio"?
o
$all by value
o
$all by reference
o
$opy-restore
o
$all by name
Me"tio" The Iss&es To Be Co"si,ere, While Appl$i"( The Te)h"i.&es *or Co,e Optimi-atio"%
o
The semantic equivalence of the source program must not be changed.
o
The improvement over the program efficiency must be achieved without changing the algorithm of the program.
o
The machine dependent optimi%ation is based on the characteristics of the target machine for the instruction set used and addressing modes used for the instructions to produce the efficient target code.
o
The machine independent optimi%ation is based on the characteristics of the programming languages for appropriate programming structure and usage of efficient arithmetic properties in order to reduce the e#ecution time.
o
Available e#pressions.
o
'eaching definitions.
o
ive variables.
o
(usy variables.
What Are The Basi) /oals O! Co,e Mo0eme"t? )To reduce the si%e of the code i.e. to obtain the space comple#ity. )To reduce the frequency of e#ecution of code i.e. to obtain the time comple#ity.
List The Di!!ere"t Stora(e Allo)atio" Strate(ies% The strategies are:
o
Static allocation Stack allocation
o
!eap allocation
o
What Are The Co"te"ts O! A)ti0atio" Re)or,? The activation record is a block of memory used for managing the information needed by a single e#ecution of a procedure. *arious fields f activation record are:
o
Temporary variables ocal variables
o
Saved machine registers
o
$ontrol link
o
Access link
o
Actual parameters
o
'eturn values
o
What Is D$"ami) S)opi"(? +n dynamic scoping a use of non-local variable refers to the non-local data declared in most recently called and still active procedure. Therefore each time new findings are set up for local names called procedure. +n dynamic scoping symbol tables can be required at run time.
De!i"e S$m'ol Ta'le% Symbol table is a data structure used by the compiler to keep track of semantics of the variables. +t stores information about scope and binding information about names.
What Is Li"ear A"al$sis? inear analysis is one in which the stream of characters making up the source program is read from left to right and grouped into tokens that are sequences of characters having a collective meaning.Also called le#ical analysis or scanning.
List The Vario&s Phases O! A Compiler% The following are the various phases of a compiler:
o
e#ical Analy%er Synta# Analy%er
o
Semantic Analy%er
o
+ntermediate code generator
o
o
$ode optimi%er
o
$ode generator
What Are The Classi!i)atio"s O! A Compiler? $ompilers are classified as:
o
Single- pass ,ulti-pass
o
oad-and-go
o
&ebugging or optimi%ing
o
What Is A S$m'ol Ta'le? A symbol table is a data structure containing a record for each identifier with fields for the attributes of the identifier. The data structure allows us to find the record for each identifier quickly and to store or retrieve data from that record quickly. henever an identifier is detected by a le#ical analy%er it is entered into the symbol table. The attributes of an identifier cannot be determined by the le#ical analy%er.
Me"tio" Some O! The Co&si"s O! A Compiler% $ousins of the compiler are:
o
/reprocessors Assemblers
o
oaders and ink-0ditors
o
List The Phases That Co"stit&te The *ro"t E", O! A Compiler% The front end consists of those phases or parts of phases that depend primarily on the source language and are largely independent of the target machine. These include · e#ical and Syntactic analysis · The creation of symbol table · Semantic analysis · 1eneration of intermediate code
A certain amount of code optimi%ation can be done by the front end as well. Also includes error handling that goes along with each of these phases.
Me"tio" The Ba)12e", Phases O! A Compiler% The back end of compiler includes those portions that depend on the target machine and generally those portions do not depend on the source language 2ust the intermediate language. These include · $ode optimi%ation · $ode generation along with error handling and symbol- table operations.
De!i"e Compiler2)ompiler% Systems to help with the compiler-writing process are often been referred to as compiler-compilers compiler-generators or translatorwriting systems. argely they are oriented around a particular model of languages and they are suitable for generating compilers of languages similar model.
List The Vario&s Compiler Co"str&)tio" Tools% The following is a list of some compiler construction tools:
o
/arser generators Scanner generators
o
Synta#-directed translation engines
o
Automatic code generators
o
&ata-flow engines
o
Di!!ere"tiate To1e"s3 Patter"s3 Le#eme% Tokens- Sequence of characters that have a collective meaning. /atterns- 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 e#eme- A sequence of characters in the source program that is matched by the pattern for a token.
List The Operatio"s O" La"(&a(es%
o
"nion – " , 34s 5 s is in or s is in ,6
o
$oncatenation – , 34st 5 s is in and t is in ,6
o
7leene $losure – 8 9%ero or more concatenations of
o
/ositive $losure – ; 9 one or more concatenations of
Write A Re(&lar E#pressio" *or A" I,e"ti!ier% An identifier is defined as a letter followed by %ero or more letters or digits. The regular e#pression for an identifier is given as letter 9letter 5 digit8
Me"tio" The Vario&s Notatio"al Short +a",s *or Represe"ti"( Re(&lar E#pressio"s%
o
o
=ero or one instance 9>
o
o
$haracter classes 9?abc@ where abc are alphabet symbols denotes the regular e#pressions a 5 b 5 c. on regular sets
What Is The *&")tio" O! A +ierar)hi)al A"al$sis? !ierarchical analysis is one in which the tokens are grouped hierarchically into nested collections with collective meaning.Also termed as /arsing.
What Does A Sema"ti) A"al$sis Do? Semantic analysis is one in which certain checks are perf ormed to ensure that components of a program fit together meaningfully.,ainly performs type checking.
List The Vario&s Error Re)o0er$ Strate(ies *or A Le#i)al A"al$sis%
/ossible error recovery actions are:
o
/anic mode recovery &eleting an e#traneous character
o
+nserting a missing character
o
'eplacing an incorrect character by a correct character
o
Transposing two ad2acent characters
o
Me"tio" The Basi) Iss&es I" Parsi"(%
There are two important issues in parsing. 1. 2.
Specification of synta# 'epresentation of input after parsing.
Wh$ Le#i)al A", S$"ta# A"al$sers Are Separate, O&t? 'easons for separating the analysis phase into le#ical and synta# analy%ers:
o
Simpler design. $ompiler efficiency is improved.
o
$ompiler portability is enhanced.
o
De!i"e A Co"te#t *ree /rammar% A conte#t free grammar 1 is a collection of the following · * is a set of non terminals · T is a set of terminals · S is a start symbol · / is a set of production rules 1 can be represented as 1 3 9*TS/ /roduction rules are given in the following form on terminal 4 9* " T8
Brie!l$ E#plai" The Co")ept O! Deri0atio"% &erivation from S means generation of string w from S. Bor constructing derivation two things are important. i $hoice of non terminal from several others.
ii $hoice of rule from production rules for corresponding non terminal. +nstead of choosing the arbitrary non terminal one can choose i either leftmost derivation – leftmost non terminal in a sentinel form. ii or rightmost derivation – rightmost non terminal in a sentinel form.
De!i"e Am'i(&o&s /rammar% A grammar 1 is said to be ambiguous if it generates more than one parse tree for some sentence of language 91. i.e. both leftmost and rightmost derivations are same for the given sentence.
What Is A Operator Pre)e,e")e Parser? A grammar is said to be operator precedence if it possess the following properties: C. o production on the right side is 5. D. There should not be any production rule possessing two ad2acent non terminals at the right hand side.
List The Properties O! Lr Parser% C. ' parsers can be constructed to recogni%e most of the programming languages for which the conte#t free grammar can be written. D. The class of grammar that can be parsed by ' parser is a superset of class of grammars that can be parsed using predictive parsers. E. ' parsers work using non backtracking shift reduce technique yet it is efficient one.
Me"tio" The T$pes O! Lr Parser% · S' parser- simple ' parser · A' parser- lookahead ' parser · $anonical ' parser
What Are The Pro'lems With Top Do" Parsi"(? The following are the problems associated with top down parsing: · (acktracking · eft recursion
· eft factoring · Ambiguity
Write Short Notes O" 6a))% FA$$ is an automatic tool for generating the parser
o
program. o
o
FA$$ stands for Fet Another $ompiler $ompiler which is basically the utility available from "+G. (asically FA$$ is A' parser generator.
What Is Mea"t B$ Via'le Pre!i#es? The set of prefi#es of right sentential forms that can appear on the stack of a shift-reduce parser are called viable prefi#es. An equivalent definition of a viable prefi# is that it is a prefi# of a right sentential form that does not continue past the right end of the rightmost handle of that sentential form.
De!i"e +a",le% 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 7 is a production A 48 and a position of 7 where the string 8 may be found and replaced by A to produce the previous right-sentential form in a rightmost derivation of 7. That is if S 3H9Aw 3H 98wthen A 48 in the position following 9 is a handle of 98w.
What Are :er"el ; No"21er"el Items? 7ernel items whish include the initial item SI 4 .S and all items whose dots are not at the left end. on-kernel items which have their dots at the left end.
What Is Phrase Le0el Error Re)o0er$?
/hrase level error recovery is implemented by filling in the blank entries in the predictive parsing table with pointers to error routines. These routines may change insert or delete symbols on the input and issue appropriate error messages. They may also pop from the stack.
What Are The Be"e!its O! I"terme,iate Co,e /e"eratio"?
o
A $ompiler for different machines can be created by attaching different back end to the e#isting front ends of each machine.
o
A $ompiler for different source languages can be created by proving different front ends for corresponding source languages t e#isting back end.
o
A machine independent code optimi%er can be applied to intermediate code in order to optimi%e the code generation.
What Are The Vario&s T$pes O! I"terme,iate Co,e Represe"tatio"? There are mainly three types of intermediate code representations.
2.
Synta# tree /ostfi#
3.
Three address code
1.