Uploaded by Haniya Ali

Compiler notes

advertisement
Mr. Ali Raza
Computer Sci. Deptt
Edwardes College Peshawar
Compiler Construction Notes
Q1. What is Compiler? Explain with the help of diagram the two main parts 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.
As an important part of this translation process, the compiler reports to its user to presence of
errors in the source program.
Source
Program
compiler
Target
program
Error
Messages
The Analysis-Synthesis Model of Compilation
There are two parts to compilation:
 Analysis Part
 Synthesis Part
Analysis Part The analysis part breaks up the source program into constituent pieces and
creates an intermediate representation of the source program.
Synthesis Part The synthesis part constructs the desired target program from the intermediate
representation.
Source
Program
Analysis
Synthesis
Target
program
Intermediate
Representation
Q2. Analysis consists of how many phases? Explain.
three phases:
 Linear analysis.
 Hierarchical analysis.
 Semantic analysis.
In compiling, analysis consists of
Linear analysis
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.
Summary: Token Identification
Hierarchical analysis
1
Mr. Ali Raza
Computer Sci. Deptt
Edwardes College Peshawar
In which characters or tokens are grouped hierarchically into nested
collections with collective meaning.
Summary: Error Checking
Semantic analysis
In which certain checks are performed to ensure that the components of
the program fit together meaningfully.
Summary: Grammar Checking
Q5. Explain in detail the logical organization of a compiler.
Conceptually, a compiler
operates in phases, each of which transforms the source program from one representation to
another. A typical decomposition of a compiler is:
Source program
Lexical analyzer
Syntax analyzer
Semantic analyzer
Symbol-table
Error handler
ICG
Code optimizer
Code generator
Target program
The first three phases, forming the bulk of the analysis portion of a compiler. Two other
activities, symbol table management, and error handling are shown interacting with the six
phases of lexical analysis, syntax analysis, semantic analysis, intermediate code generation, code
optimization, and code generation. Informally, we shall also call the symbol table manager, and
error handler “phases”.
Lexical Analysis
In a compiler, linear analysis is called lexical analysis or scanning. For
example, in lexical analysis the characters in the assignment statement
position := initial + rate * 60
would be grouped into the following tokens:
1.
2.
3.
4.
5.
6.
7.
The identifier position.
The assignment symbol :=.
The identifier initial.
The plus sign.
The identifier rate.
The multiplication sign.
The number 60.
2
Mr. Ali Raza
Computer Sci. Deptt
Edwardes College Peshawar
The blanks separating the characters of these tokens would normally be eliminated during
lexical analysis.
Syntax Analysis
Hierarchical analysis is called parsing or syntax analysis. It involves grouping
the tokens of the source program into grammatical phrases that are used by the compiler to
synthesize output. Usually, the grammatical phrases of the source program are represented by a
parse tree.
The hierarchical structure of a program is usually expressed by recursive rules. For example, we
might have the following rules as part of the definition of expressions;
1. Any identifier is an expression.
2. Any number is an expression.
3. If expression1 and expression2 are expressions, then so sre
expression1 + expression2
expression1 * expression2
( expression )
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.
Symbol Table Management - Incomplete
Error Detection and Reporting  Incomplete
Intermediate Code Generation  Incomplete
Code Optimization  Incomplete
Code Generation  Incomplete
Q8. Explain with the help of a diagram the “Role of Lexical Analyzer”. What are
the main issued in lexical analysis?
The lexical analyzer is the first phase of the compiler.
Its main task is to read the input characters and produces as output the sequence of tokens that
the parser uses for syntax analysis.
token
SP
Lexical
analyzer
parser
Get next
token
Symbol table
Upon receiving the “get next token” command from the parser, the lexical analyzer reads input
characters until it can identify the next token. Under process
3
Download