You are on page 1of 2

Chapter No.

1 Introduction to Compiling

1.1 Compilers
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 the presence of errors in the source program.
The Analysis and Synthesis Model of compilation
There are two parts of compilation: analysis and synthesis. The analysis part breaks up the source program into
constituent pieces and create and intermediate representation of the source program. The synthesis part constructs the
desired target program from the intermediate representation. During analysis the operations implied by the source
program are determines and recorded in a hierarchical structure called a tree.
1.2 Analysis of the source program
1. Linear Analysis/Lexical 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. It produces
pairs- a word and its part of speech. For example the input x=x+y becomes
<id, x>
<assign, =>
<id, x>
<op, +>
<id,y> we call the pair”<token type,word>” a token. Typical tokens are number, identifier, +,-,new, while, if etc.
Blanks separating the characters of tokens would normally be eliminated during lexical analysis.
2. Hierarchical Analysis/Parsing/syntax analysis: in which characters or tokens are grouped hierarchically into
nested collections with collective meaning. It involves grouping the tokens of the source program into
grammatical phrases that are used by the compiler to synthesize output.
The syntax of most programming languages is specified using CFG or BNF (Backus Naur Form). CFG is specified
with a grammar G=(S,N,T,P)
 S is the start symbol
 N is a set of non-terminal
 T is set of terminal symbols or words
 P is asset of productions or rewrite rules.
Example:
1) goalexpr // S=goal
2) expr expr op term | term // T={number, id, +, -}
3) termnumber |id // N={goal,expr, term,op}
4) op+ |- // P={1,2,3,4,5,6,7}
Given a CFG we can derive senetnces by repeated substitution. Consider the sentence x+2-y

To recognize a valid sentence in some CFG, we reverse this process and build up a parse, thus the name parser. (Parse is
to break up a sentence or group of words into separate components). A parse can be represented by a tree called parse
tree.
3. Semantic Analysis: in which certain checks are performed to ensure that the components of a program fit
together meaningfully. It checks the source program for semantic errors and gathers type information for the
subsequent code-generation phase. An important component of semantic analysis is type checking. For
example, many programming language definitions require a compiler to report an error every time a real
number is used to index an array.
1.3 Phases of compiler

Conceptually, a compiler operates in phases, each of which


transforms the source program from one representation to another. Two other activities, symbol table management and
error handling interact with all six phases of compiler.

Symbol Table Management


An essential function of a compiler is to record the identifiers used in the source program and collect information about
various attributes of each identifier. These attributes may provide information about the storage allocated for an
identifier, its type, its scope and in the case of procedure names, number and types of its arguments, the method of
passing arguments and the type returned if any. A symbol table is a data structure containing a record of each identifier,
with fields for the attributes of the identifier.
Error Detection and Reporting
Each phase can encounter errors. The syntax and semantic analysis phases usually handle a large fraction of the errors
detectable by the compiler. The lexical phase can detect errors where the characters remaining the input do not form
any token of the language. Errors where the token stream violates the structure rules of the language are determined by
the syntax analysis phase. During semantic analysis the compiler tries to detect constructs that have the right syntactic
structure but no meaning to the operation involved. E.g. if we try to add two identifiers, one is name of array and other
the name of a procedure.
4. Intermediate Code Generation. Some compilers generate an intermediate representation of the source
program, this should have two important properties; It should be easy to produce, and easy to translate into the
target program.
5. Code optimization. It attempts to improve the intermediate code, so that fatser-running machine code will
result.
6. Code Generation: The final phase of compiler is the generation of target code, consisting normally of relocatable
machine code or assembly code.

You might also like