Download presentation
Presentation is loading. Please wait.
Published byRoberta Payne Modified over 9 years ago
1
. n COMPILERS n n AND n n INTERPRETERS
2
-Compilers nA 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 n 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 n := n position + n initial * n rate 60
5
The phases of a compiler n A compiler operates in phases, each of which transforms the source program from one representation to another.
6
source program lexical analizer n syntax analyzer n semantic analyzer n symbol table error handler n manager intermediate code n generator n code optimizer n code generator n n target program
7
Why break compiler design to phases? n Good to use “divide & conquer” strategy, n 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 n 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. n 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 * 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
10
ERRORS that lexer should generate: n Invalid token, does not match any of our patterns. n Invalid character (no in our grammar). n Unterminated string constant. n Unterminated comments
11
Syntax analyzer n 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.) n Ther are three general types of parser for grammers. n 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) n ASSGN STMT n := n ID EXPR n POSITION + n EXPR EXPR n ID n initial * n EXPR EXPR n ID number n rate 60 n 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 n Compilers principles, Techniques, and Tools by Ahfred V.Aho n Ravi Sethi n Jeffrey D. Ullman n CS 488 notes
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.