Ans: 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. Compilers are sometime classified as single – pass, multi – pass, load and go, debugging or optimizing, depending on how they have been constructed or on what function they are supposed to perform.
Compilation parts: There are two parts to compilation which are as follows: --
1. Analysis
2. Synthesis
Analysis: The analysis part breaks up the source program into consistent pieces and creates an intermediate representation of the source program. During analysis, the operations implied by the source program are determined and recorded in a hierarchical structure called a tree. Often a special kind of tree called a syntax tree is used, in which each node represents an operation and the children of a node represent the arguments of the operation.
Example: The syntax tree for an assignment operation as follows:

Synthesis: The synthesis part constructs the desired target program from the intermediate representation of the two parts; synthesis requires the most specialized techniques.
Q. What are the phases of analysis and describe them.
Ans: Analysis of the source program: In compiling, analysis consists of three phases: --
1. Linear analysis (Lexical Analysis)
2. Hierarchical analysis (Syntax Analysis)
3. Semantic analysis
Linear (Lexical) analysis: In a compiler, linear analysis is called lexical analysis or scanning. In this case, the stream of characters which make the source program is read from left to right and generates the tokens which are sequences of characters having a collective meaning.
Example: In lexical analysis, the characters in the assignment statement
position:= initial + rate * 60
would be grouped into the following token ---
a. The identifier position
b. The assignment symbol :=
c. The identifier initial
d. The plus sign
e. The identifier rate
f. The multiplication sign
g. The number 60
The blanks separating the characters of these tokens would normally be eliminated during lexical analysis.
Hierarchical analysis / Parsing / Syntax analysis: Hierarchical analysis is called parsing or syntax analysis. In this case the tokens of the source program are grouped hierarchically into grammatical parses that are used by the compiler to synthesize output. Usually, the grammatical phrases of the source program are represented by a parse tree as follows: --

The hierarchical structure of a program is usually expressed by recursive rules. The rules of expression are as follows:--
1. Any identification is an expression.
2. Any number is an expression
3. If expression1 and expression2 are expressions then so are
expression1 + expression2
expression1 * expression2
(expression1)
Semantic analysis: The semantic analysis phase checks the source program for semantic errors and gathers type information for the subsequent code – generation phase. It uses the hierarchical structure determined by the syntax analysis phase to identify the operators and operands of expressions and statements. An important component of semantic analysis is type checking.
Q. What is symbol table? What are the functions of symbol table?
Ans: Symbol table: A symbol table is a data structure containing a record for each identifier with fields for the attributes of the identifier. It is generally used to store information about various source language constructs. The information is collected by the analysis phases of the compiler and used by the synthesis phases to generate the target code.

Nice on for Students and refer also please
ReplyDeletevisit :http://compilerdesignforstudents.blogspot.in/