How a Translator Works CS 222: Programming Languages
Translators The job of a tr translator is to convert data in a source language to an equivalent form in a target language. translator • Assemblers Assembly language Machine code Decrement the B DEC B 00000101 processor register by one: Load AL register with 61 hexadecimal value: MOV AL, 61h 10110000 01100001 2
More Translators • Compilers High-level language Machine code of physical processor • Programs in the source language are co compiled . • Produces executable program • Ex: GCC is a compiler for C, C++, Objective-C, Fortran, Ada • Interpreters High-level language Machine code of virtual machine • Programs in the source language are in interpreted . • Parses the source code into intermediate representation which is immediately executed • Ex: Thonny is an interpreter for Python • Combination thereof • Ex: Ju Just-in in-ti time me (JIT) compilati tion • Java compiler translates source code to byte code to be executed by JVM • JVM can compile byte code to machine code 3
Four Translator Design Principles 1. Correctness Principle The runtime behavior of a translated form must be that described by the input being translated. 2. Early-warning Principle Both syntax (form) and semantic (use) errors should be identified and reported at the earliest possible point in the translation process. • Type errors are identified via type type che hecki king ng . • St Statically typed languages type check at translation time • Ex: Java, C, C++ • Dy Dynami mically typed languages type check at execution time • Ex: Python, Ruby, Lisp 3. Efficiency Principle A translator must ensure that a translated form makes sensible and efficient use of the computational resources in the execution environment. 4. Portability Principle A translator should be designed so that it can be ported to a new execution environment with a minimum of effort. 4
Phases of Translation Analysis Synthesis character stream -creates- Final Translated Source Program Form 5
Phases of Translation Analysis Synthesis Syntax Checking Semantic Analysis character stream -creates- Final Translated Source Program Form 6
Phases of Translation Analysis Synthesis Syntax Checking Semantic Analysis Lexical Syntax analysis character stream analysis -creates- Final Translated Source Program Form 7
Phases of Translation Analysis Synthesis Syntax Checking Semantic To Tokenizer Pa Parser Analysis Lexical Syntax analysis character stream analysis -creates- token stream Final Translated Source Program Form 8
Phases of Translation Analysis Synthesis Syntax Checking Semantic Tokenizer To Parser Pa Analysis Lexical Syntax analysis character stream analysis -creates- token stream Final Syntax Translated Source Tree -creates- Program Form Symbol Table -creates- 9
Phases of Translation Analysis Synthesis Se Semantic c Code Ch Cod Checker Syntax Checking Semantic Tokenizer To Parser Pa Analysis Lexical Syntax analysis character stream analysis -creates- token stream Final Syntax Translated Source Tree -creates- Program Form Symbol Table -creates- 10
Phases of Translation Analysis Synthesis Se Semantic c In Inter ermed media iate e Ob Object Cod ode Code Ch Cod Checker Co Code Generator Gen Gener erator Syntax Checking Semantic Intermediate Tokenizer To Pa Parser Object Code Code Analysis Optimization Generation Lexical Syntax analysis character stream analysis -creates- -creates- token stream Final Intermediate Syntax Translated Source Code Tree -creates- Program Form Symbol Table -creates- 11
Recommend
More recommend