18-12-2012, 04:36 PM
Compilers
1Compilers.pdf (Size: 1.48 MB / Downloads: 316)
Introduction
Compiler: A program that reads a program written in one language (the source program)
and translates it into an equivalent program in another language (the target program)
Important part of this translation process: the compiler reports to its user the presence of
errors in the source program.
So... we need to build too many compilers!?
There are thousands of source languages (Fortran, C, Pascal or specialized
languages) and target languages (another programming language or a machine
language): Too many compilers?
Despite an apparent complexity, the basic tasks that any compiler must perform
are essentially the same
At the highest level, a compiler has a front end and a back end
It is desirable for the front end to deal with aspects of the input language, but to
keep it as independent of the machine as possible
The back end should concentrate on dealing with the specifics of output
language, and try to remain independent of the input.
Problem of generating a suite of compilers for n different languages to m different
machines.
The phases of a compiler
Conceptually, a compiler operates in phases, each of which transforms the
source program from one representation to another
In practice, some of the phases may be grouped together
One can say that the phases are grouped into two parts:
1. Analysis - The typical compiler front end
Breaks up the source program into constituent pieces and creates an
intermediate representation of the source program
(a) Lexical analysis
(b) Syntax analysis
© Semantic analysis
2. Synthesis
Constructs the desired target program from the intermediate representation
The back end corresponds to the phases of code generation and
optimization
Syntax Analysis: Parser
The parser receives the tokens from the lexical analyzer and checks if they
arrive in the correct order (the one defined in the language)
It involves grouping the tokens into grammatical phrases that are used by the
compiler to synthesize output
In general, the syntax of a programming language is described by a context
free grammar
The job of the parser is to recover the hierarchical structure of the program from
the stream of tokens received from the lexical analyzer
The output of the parser depends on the implementation of the compiler: usually
a tree
Parse tree: describes the syntactic structure of the input
Syntax tree: a compressed (and more common) representation of the parse tree
in which the operators appear as the interior node and the operands of an
operator are the children of the node
Applications of compiler techniques
Compiler technology is useful for a more general class of applications
Many programs share the basic properties of compilers: they read textual input,
organize it into a hierarchical structure and then process the structure
An understanding how programming language compilers are designed and
organized can make it easier to implement these compiler like applications as
well
More importantly, tools designed for compiler writing such as lexical analyzer
generators and parser generators can make it vastly easier to implement such
applications
Thus, compiler techniques - An important knowledge for computer science
engineers