Sei sulla pagina 1di 22

One pass and Multi pass Compiler

Collected By: Neha

What is the difference between Phase and Pass in Compiler?


Phase and Pass are two terms used in the area of compilers. Pass is a reading of a file followed by processing of data from file. Phase is a logical part of the compilation process. A pass is a single time the compiler passes over (goes through) the sources code or some other representation of it.

Typically, most compilers have at least two phases called front end and back end, while they could be either one-pass or multi-pass.

Phase is used to classify compilers according to the construction, while pass is used to classify compilers according to how they operate.
Collected By: Neha 2

Several other types:


A "source-to-source compiler"
is a type of compiler that takes a high level language as its input and outputs a high level language. For example, an automatic parallelizing compiler will frequently take in a high level language program as an input and then transform the code and annotate it with parallel code annotations (e.g. OpenMP) or language constructs (e.g. Fortran's DOALL statements).

Just-in-time compiler,

used by Smalltalk and Java systems, and also by Microsoft .Net's Common Intermediate Language (CIL) Applications are delivered in bytecode, which is compiled to native machine code just prior to execution.

Collected By: Neha

Introduction
What is a compiler ? Compiler is a computer program that translates a computer program written in 1 computer language into an equivalent program written in another computer language.
Types of compilers

1.Single pass compiler 2.Multi-pass compiler


Collected By: Neha 4

Types of Compiler
A single pass compiler makes a single pass over the source text, parsing, analyzing, and generating code all at once.
A Multi pass compiler makes more than 1 pass over the source code ,producing intermediate forms of code after each stages, optimizing the program and generates object code.
Collected By: Neha 5

Stages of Single pass compiler


1 Lexical analysis: Lexical analysis involves scanning of source code and splitting it up into tokens
2 Syntactical analysis: syntactical analysis recognizes language constructs described by the grammar 3 Code generator generates the target language code
Collected By: Neha

Lexical analyzer
It is convenient to regard a source program as a sequence of tokens rather than simply string of characters. Token can be keyword, variable name, an integer, arithmetic operator etc. This task is called lexical analysis and part of compiler is called scanner, and input stream of source program is called the lexeme.
Collected By: Neha 7

Syntactical analysis
After the token scan, each statement in the program must be recognized as some language construct, such as declaration or an assignment, described by grammar .This process is called syntax analysis or parsing.

Syntactical analysis can be analyzed by constructing parse trees.

Collected By: Neha

Grammars
Grammars are very much required if we consider construction of a compiler,The high level language is usually described in terms of grammar. This grammar specifies the form ,or syntax, of legal statements in the language. Then the problem of compilation becomes one of matching statements written by the programmer to structures defined by the grammar, and generating object code for each statement.
Collected By: Neha
1 Pass Compiler

Code generator
It is last process of compilation. Its a process in compiler, which translates the lexed and parsed non-erroneous code into the required object code. It involves a set of routines called semantic routines or code generation routines, which generate the object code. The object code generation may depend on computer, but doesn't depend on parsing techniques.
Collected By: Neha 10

Single pass compiler process


If parser gets the some unknown grammar constraint derivation it calls the error, and the object code is not generated. If parser requires some more tokens to recognize the tokens, it calls the lexer again. First compiler calls the parser which calls the lexer for getting the tokens.

Source program
If parser gets some grammar valid constraints derivation then it calls the The lexer goes through the source code generator and generates the object program generating the tokens and Collected By: Neha 11 code. return it to the parser.

Single pass compiling process


First compiler calls the parser,which calls the lexer for getting the tokens. The lexer goes through the source program generating the tokens and return it to the parser. If parser requires some more tokens to recognize the tokens, it calls the lexer again. If parser gets the some unknown grammar constraint derivation it calls the error, and the object code is not generated. If parser gets some grammar valid constraints derivation then it calls the code generator and generates the object code.
Collected By: Neha 12

Single pass compiling process


So in single pass the compiler does not optimize the code. It directly converts it to source program.

Single pass compiler no intermediate codes are generated.


Collected By: Neha 13

In multi pass assembler the step may continue to do


Intermediate code generation,

machine independent code optimization


Code generation

Machine dependent code optimization


And then machine code is generated

Collected By: Neha

14

Multi pass compiler


First step of compiler is calling lexer to convert entire source into tokens.
IF there is forward reference that can be analyzed by scanning entire program more than once. That is not allowed in single pass assembler.

Collected By: Neha

15

In some cases the design of a language feature may require a compiler to perform more than one pass over the source. For instance, consider a declaration appearing on line 20 of the source which affects the translation of a statement appearing on line 10. In this case, the first pass needs to gather information about declarations appearing after statements that they affect, with the actual translation happening during a subsequent pass.
Collected By: Neha 16

Use of single pass


The single pass compiler required only 1 pass and no intermediate code step, so its quite efficient and fast in compiling.

Pascal was explicitly designed to be easy to implement with a single pass compiler and hence to produce the code at faster.
They have accomplished using the strict constructs of defining the variables to be defined before they are used.
Collected By: Neha 17

Problem of single pass


The forward referencing problem. whenever a single pass is done we encounter a problem if any element that has to be processed not has been defined before. Then the single-pass compiler cant compile it and it may give error so next slide illustrates us with examples how Pascal program overcomes these, by defining strict rules.
Collected By: Neha 18

Pascal Example function odd(n : integer) : boolean; begin if n = 0 then odd := false else if n < 0 then odd := even(n + 1) { Compiler error: 'even' is not defined } else odd := even(n - 1) end;
Collected By: Neha 19

Advantages of Multi-pass compilers


Machine Independent: Since the multiple passes include a modular structure, and the code generation decoupled from the other steps of the compiler, the passes can be reused for different hardware/machines. More Expressive Languages: Many programming languages cannot be represented with a single pass compilers, for example Pascal can be implemented with a single pass compiler since it requires that all definitions come before their use.
Collected By: Neha 20

Languages like Java require a multi-pass compiler since the definition of x would not be required to come before the use. public class Example { public static void main(String [] args) { assert(x==0); x++; assert(x==1); } static int x=0; }
The multi-pass compiler would allocate the memory location for x during the semantic analysis phase of compilation and would have processed the declaration of x before its use.
Collected By: Neha 21

CONCLUSION
Single compiler is very fast and are used when very fast compilation is required. The forward reference problem in 1 pass compiler can be solved by defining strict language constructs like in the Pascal. Single pass compiler are better for large programs in general. Example of a platform where single pass can be used are: Pascal.
Collected By: Neha 22

Potrebbero piacerti anche