Compiler Designviva
Compiler Designviva
Compiler Designviva
1. What is a compiler?
A compiler is a program that reads a program written in one language –the source
language and translates it into an equivalent program in another language-the target
language. The compiler reports to its user the presence of errors in the source
program.
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.
Linear Analysis.
Hierarchical Analysis.
Semantic Analysis.
4. Depict diagrammatically how a language is processed.
Preprocessor
Source program
Compiler
Assembler
Linear 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.
Lexical Analyzer
Syntax Analyzer
Semantic Analyzer
Intermediate code generator
Code optimizer
Code generator
7. What are the classifications of a compiler?
· Single- pass
· Multi-pass
· Load-and-go
· Debugging or optimizing
8. What is a symbol table?
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.
· Preprocessors
· Assemblers
· Loaders and Link-Editors
10. List the phases that constitute the front end of 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
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, just the
intermediate language. These include
· Code optimization
· Code generation, along with error handling and symbol- table operations.
12. Define compiler-compiler.
Systems to help with the compiler-writing process are often been referred to as
compiler-compilers, compiler-generators or translator-writing systems.
Largely they are oriented around a particular model of languages , and they are
suitable for generating compilers of languages similar model.
· Parser generators
· Scanner generators
· Syntax-directed translation engines
· Automatic code generators
· Data-flow engines
14. Differentiate tokens, patterns, lexeme.
· 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)
16. Write a regular expression for an identifier.
An identifier is defined as a letter followed by zero or more letters or digits.
Hierarchical analysis is one in which the tokens are grouped hierarchically into
nested collections with collective meaning.
Semantic analysis is one in which certain checks are performed to ensure that
components of a program fit together meaningfully.
20. List the various error recovery strategies for a lexical analysis.
· Specification of syntax
· Representation of input after parsing.
3. Why lexical and syntax analyzers are separated out?
Reasons for separating the analysis phase into lexical and syntax analyzers:
Simpler design.
Compiler efficiency is improved.
Compiler portability is enhanced.
4. Define a context free grammar.
A context free grammar G is a collection of the following
ii) Choice of rule from production rules for corresponding non terminal.
i.e. both leftmost and rightmost derivations are same for the given sentence.
2. There should not be any production rule possessing two adjacent non terminals at
the right hand side.
· Backtracking
· Left recursion
· Left factoring
· Ambiguity
11. Write the algorithm for FIRST and FOLLOW.
FIRST
FOLLOW
Disadvantages
1. The operator like minus has two different precedence(unary and binary).Hence it
is hard to handle tokens like minus sign.
| other
YACC stands for Yet Another Compiler Compiler which is basically the utility
available from UNIX.
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
A→.XYZ
A→X.YZ
A→XY.Z
A→XYZ.
· Syntax tree
· Postfix
· Three address code
3. Define backpatching.
Backpatching is the activity of filling up unspecified information of labels using
appropriate semantic actions in during the code generation process.In the semantic
actions the functions used are mklist(i),merge_list(p1,p2) and backpatch(p,i)
4. Mention the functions that are used in backpatching.
· mklist(i) creates the new list. The index i is passed as an argument to this function
where I is an index to the array of quadruple.
· merge_list(p1,p2) this function concatenates two lists pointed by p1 and p2. It returns
the pointer to the concatenated list.
· backpatch(p,i) inserts i as target label for the statement pointed by pointer p.
5. What is the intermediate code representation for the expression a or b and not c?
The intermediate code representation for the expression a or b and not c is the three
address sequence
t1 := not c
t2 := b and t1
t3 := a or t2
E.true := new_label()
E.false :=S.next
S1.next :=S.next
2. S → if E then S1 else S2
E.true := new_label()
E.false := new_label()
S1.next :=S.next
S2.next :=S.next
MOV R0,R1 1
MOV R1,M 2
SUB 5(R0),*10(R1) 3
Eg. t1:=a*5
t2:=t1+7
t3:=t2-5
t4:=t1+t3
t5:=t2+b
· Static allocation
· Stack allocation
· Heap allocation
6. What are the contents of activation record?
The activation record is a block of memory used for managing the information
needed by a single execution of a procedure. Various fields f activation record are:
· Temporary variables
· Local variables
· Saved machine registers
· Control link
· Access link
· Actual parameters
· Return values
7. What is dynamic scoping?
In 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. In dynamic scoping symbol tables can
be required at run time.
The hash function should result in uniform distribution of names in symbol table.
The hash function should be such that there will be minimum number of collisions.
Collision is such a situation where hash function results in same location for storing
the names.