1 / 16

.

. 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.

logan-frank
Download Presentation

.

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. . • COMPILERS • AND • INTERPRETERS

  2. -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.

  3. -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.

  4. Syntax tree for position:=initial+rate*60 • := • position + • initial * • rate 60

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

  6. source program lexical analizer • syntax analyzer • semantic analyzer • symbol table error handler • manager intermediate code • generator • code optimizer • code generator • target program

  7. 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).

  8. 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.

  9. 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

  10. 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

  11. 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.

  12. 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.

  13. 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

  14. 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.

  15. 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.

  16. Refrences • Compilers principles, Techniques, and Tools by Ahfred V.Aho • Ravi Sethi • Jeffrey D. Ullman • CS 488 notes

More Related