n compilers n n and n n interpreters. -compilers na compiler is a program thatt reads a program...

16
. COMPILERS AND INTERPRETERS

Upload: roberta-payne

Post on 13-Dec-2015

222 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

.

COMPILERS AND INTERPRETERS

Page 2: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

-Compilers

A compiler is a program thatt reads a program written in one language - the source language- and translates it into an equivalent program in another language -tha target language.

Page 3: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

-Interpreters

Instead of producing a target program as a translation, an interpreter performs the operations implied by the source program. For an assignment statement an interpreter might build a tree and then carry out the operations at the nodes as it “walks” the tree.

Page 4: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

Syntax tree for position:=initial+rate*60

:=

position + initial * rate 60

Page 5: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

The phases of a compiler

A compiler operates in phases, each of which transforms the source program from one representation to another.

Page 6: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

source program lexical analizer

syntax analyzer semantic analyzer symbol table error handler manager intermediate code generator code optimizer code generator target program

Page 7: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

Why break compiler design to phases?

Good to use “divide & conquer” strategy, but more importantly, to maintain its flexibility and

REUSABILITY. If we wanted to change our target language for a different machine, we would not have to rewrite all the phases, but only the latter one(s).

Page 8: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

Lexical analyzer

parses stream of characters and group characters into tokens (a language will have a valid set of tokens).

The lexer should generate an error if an invalid token is encountered . Lexer takes a RE and converts it to DFA. It does this via an intermediate

step which takes the RE and converts it to NFA, then converts the NFA to DFA.

Page 9: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

Lexical Analizer(cont) For example, consider code fragment:

position := initial + rate * 60

The lexer will read character by character from left to right and break it into the following tokens: a. ID position b. ASSGN_OP := c. ID initial d. ADD_OP + e. ID rate f. MULT_OP * g. INT_CONST 60

Page 10: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

ERRORS that lexer should generate:

Invalid token, does not match any of our patterns.

Invalid character (no in our grammar). Unterminated string constant. Unterminated comments

Page 11: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

Syntax analyzer

In the compiler method , the parser obtains a string of tokens from the lexical analyzer, and verifies that the string can be generated by the grammer for the source language.

Page 12: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

Syntax analyzer (cont.)

Ther are three general types of parser for grammers.

The methods commonly used in compilers are either top-down or bottom-up. In both cases, the input to the parser is scanned from left to right, one symbol at a time.

Page 13: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

Syntax Analyzer(cont)

ASSGN STMT := ID EXPR POSITION + EXPR EXPR ID

initial * EXPR EXPR ID number rate 60 syntax tree for position:=initial+rate*0

Page 14: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

Semantic analyzer

• detects three types of semantic errors:a. TYPE CHECKING: ex. Make sure when we add an int to a float that we convert the int to a float. b. INHERITENCE CYCLES.

• c. SCOPE of variables.

Page 15: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

Code generator

• The final phase of the compiler model is the code generator. It takes as input an intermediate representation of the source program and produces as output an equivalent target program typically relocatable machine code or assembly code.

Page 16: n COMPILERS n n AND n n INTERPRETERS. -Compilers nA compiler is a program thatt reads a program written in one language - the source language- and translates

Refrences

Compilers principles, Techniques, and Tools by Ahfred V.Aho

Ravi Sethi Jeffrey D. Ullman

CS 488 notes