1 pass compiler 1. 1.introduction 1.1 types of compilers 2.stages of 1 pass compiler 2.1 lexical...

25
1 Pass Compiler 1

Upload: laurence-price

Post on 17-Dec-2015

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

1 Pass Compiler 1

Page 2: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

1.Introduction1.1 Types of compilers

2.Stages of 1 Pass Compiler2.1 Lexical analysis2.2. syntactical analyzer2.3. Code generation

3.Single pass compiling process4.Comparison with multi pass5.Advantages and limitations

1 Pass Compiler 2

Page 3: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

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

1 Pass Compiler 3

Page 4: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

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.

1 Pass Compiler 4

Page 5: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

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 generatorgenerates the target language code

1 Pass Compiler 5

Page 6: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

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.

For example an identifier may be defined like this:

<indent>::=<letter>|<indent><letter>|<indent><digit>

<letter> ::= a|b|c|d|…|z

<digit> ::= 0|1|2|3|…|9

1 Pass Compiler 6

Page 7: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

And the restriction in length of keywords, recognizing both single (‘R’,’E’,’A’,’D’) and multi character tokens (READ).

Token specifier specify quality of a token like real no float no int type.

Comments are ignored by scanners. Lexical analyzer can be easily visualized by

using finite automata. Hence its representation will ease the implementation.

Table representation is much clearer, so we use tables after constructing DFA.

The tokens are accepted if it matches rules else error is reported.

1 Pass Compiler 7

Page 8: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

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.

2 general classes of parsing techniques: Top-down :begins with the rule of the grammar that

specifies goal of analysis, and attempt to construct the tree so terminals match statements being analyzed.

Bottom-up :begins with the terminal node of the tree and attempts to combine this statements into successively higher level nodes until the root is reached.

1 Pass Compiler 8

Page 9: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

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.

1 Pass Compiler 9

Page 10: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

1 <prog> ::=PROGRAM<prog_name>Var<dec_list>BEGIN<stmt_list>end

2<prog-name> ::=id3<dec-list>::=<id-list>:<type>4<dec> ::=<id_list>:<type>5<type>::=integer6<id_list> ::=id|<id-list>,id7<stmt-list>::=<stmt>|<stmt-list>;<stmt>8<stmt> ::=<assign>|<read>|<write>|<for>9<assign> ::=id :=<exp>10 <exp> ::=<term>|<exp>+<term>|<exp>-<term>11<term> ::=<factor>|<term>*<factor>|<term>div<factor>12<factor> ::=id|int|(<exp>)13<read> ::=READ(<id_list>)14<write>::=WRITE(<id_list>)15<for> ::=for<index-exp>do<body>16<index-exp> ::=id :=<exp> to <exp>17<body> ::=<stmt>|BEGIN<stmt-list> END

1 Pass Compiler 10

Page 11: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

It is last process of compilation. It’s 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.

1 Pass Compiler 11

Page 12: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

1 Pass Compiler 12

Source program

Page 13: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

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.

1 Pass Compiler 13

Page 14: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

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.

In multi pass assembler the step may continue to do intermediate code generation,machine independent code optimizationCode generationMachine dependent code optimizationAnd then machine code is generated.

1 Pass Compiler 14

Page 15: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

In multi-pass the code is entirely lexed first and then parsed entirely, then may syntactically analyzed, and remaining steps are carried out.

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.

1 Pass Compiler 15

Page 16: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

1 Pass Compiler 16

Page 17: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

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

Computers running students jobs tend to spend a large amount of time performing compilation. The resulting object code is usually executed only once or twice. so, thus increases its significant benefits in system performance and job turn around time.

1 Pass Compiler 17

Page 18: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

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.

1 Pass Compiler 18

Page 19: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

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 compilercant compile itand it may give error

so next slide illustrates us with examples how Pascal program overcomes these, by defining strict rules.

1 Pass Compiler 19

Page 20: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

1 Pass Compiler 20

procedure inc;begin n:=n+1end;var n:integer;

Ex.1Ex.2

Here the example 1 is correct pascal program.

var n:integer;procedure inc;begin n:=n+1end

Page 21: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

procedure ping(x:integer)begin ... pong(x-1);...end;procedure pong(x:integer)begin …ping(x–1);...end;

1 Pass Compiler 21

Here there is problem of pong calling ping and ping calling pong.Which 1 to write 1st for single compiling!!

Page 22: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

1 Pass Compiler 22

Page 23: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

If an program contains a forward reference, then it cant be compiled in single pass.

If the variables are a mixture of Real and integer types 1 or more conversion of program operation will be need to be included in object code.

Not all programs, some program due their language construct .ex:Hunter showed ALGOL 68 required at least 3 passes to compile.

1 Pass Compiler 23

Page 24: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

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.

1 Pass Compiler 24

Page 25: 1 Pass Compiler 1. 1.Introduction 1.1 Types of compilers 2.Stages of 1 Pass Compiler 2.1 Lexical analysis 2.2. syntactical analyzer 2.3. Code generation

1 Pass Compiler 25

ByAditya.S.Hedge 1RV08CS009Akhilesh kumar K C 1RV08CS010