160 likes | 278 Views
. COMPILERS AND INTERPRETERS. -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. -Interpreters.
E N D
. • COMPILERS • AND • INTERPRETERS
-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.
-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.
Syntax tree for position:=initial+rate*60 • := • position + • initial * • rate 60
The phases of a compiler • A compiler operates in phases, each of which transforms the source program from one representation to another.
source program lexical analizer • syntax analyzer • semantic analyzer • symbol table error handler • manager intermediate code • generator • code optimizer • code generator • target program
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).
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.
Lexical Analizer(cont) • For example, consider code fragment:position := initial + rate * 60The 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
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
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.
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.
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
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.
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.
Refrences • Compilers principles, Techniques, and Tools by Ahfred V.Aho • Ravi Sethi • Jeffrey D. Ullman • CS 488 notes