Presentation is loading. Please wait.

Presentation is loading. Please wait.

CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization.

Similar presentations


Presentation on theme: "CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization."— Presentation transcript:

1 CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization of software tools n n You might build a parser. n n The theory of computation/formal language still applies today. – –As long as we still program with 1-D text. n n Helps you to be a better programmer

2 2 One-dimensional Text int x; cin >> x; if(x>5) cout << “Hello”; else cout << “BOO”; int x;cin >> x;if(x>5) cout << “Hello”; else … The formatting has no impact on the meaning of program

3 3 What is a translator? n Takes input (SOURCE) and produces output (TARGET) SOURCETARGET ERROR

4 4 Types of Target Code: n “Pure” machine code »No operating system required. »No library routines. »Good for developing software for new hardware. n “Augmented” code »More common »Executable code relies on o/s provided support and library routines loaded as program is prepared to execute.

5 5 Conventional Translator skeletal source program preprocessor source program library, relocatable object files compiler assembler target assembly program loader / linker relocatable machine code absolute machine code

6 6 Types of Target Code (cont.) n Virtual code »Code consists entirely of “virtual” instructions. »Used by “Re-Targetable” compilers n Transporting to a new platform only requires implementing a virtual machine on the new hardware. »Similar to interpreters

7 7 Translator for Java Java source code Java compiler Java bytecode absolute machine code Java interpreter Bytecode compiler Java bytecode

8 8 Types of Translators n Compilers –Conventional (textual source code) »Imperative, ALGOL-like languages »Other paradigms n Interpreters n Macro processors n Text formatters n Silicon compilers

9 9 Types of Translators (cont.) n Visual programming language n Interface –Database –User interface –Operating System

10 10 Conventional Translator skeletal source program preprocessor source program library, relocatable object files compiler assembler target assembly program loader / linker relocatable machine code absolute machine code

11 11 Structure of Compilers Lexical Analyzer (scanner) Source Program Syntax Analysis (Parser) Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator Intermediate Representation Target machine code Symbol Table

12 12 Structure of Compilers Lexical Analyzer (scanner) Source Program Tokens int x; cin >> x; if(x>5) cout << “Hello”; else cout << “BOO”; int x ; cin >> x ; if ( x > 5 ) cout << “Hello” ; else cout << “BOO” ; What about white spaces? Do they matter?

13 13 Tokenize First or as needed? int x; cin >> x; if(x>5) cout << “Hello”; else cout << “BOO”; int datatype x ID ; symbol cin >> Tokens = Meaningful units in a program Value/Type pairs

14 14 Tokenize First or as needed? Array > someArray; Array< int > Array > someArray; Array< int> >>

15 15 Structure of Compilers Lexical Analyzer (scanner) Source Program Syntax Analysis (Parser) Tokens Syntactic Structure Parse Tree

16 16 Parse Tree (Parser) intx ;cin >> datatypeID Data Declaration Program

17 17 Who is responsible for errors? n int x$y; n int 32xy; n 45b n 45ab n x = x @ y; Lexical Errors / Token Errors?

18 18 Who is responsible for errors? n X = ; n Y = x +; n Z = [; Syntax errors

19 19 Who is responsible for errors? n 45ab –One wrong token? –Two tokens (45 & ab)? Are whitespaces needed? n Either way is okay. –Lexical analyzer can catch the illegal token (45ab) –Parser can catch the syntax error. Most likely 45 followed by ab will not be syntactically correct.

20 20 Structure of Compilers Lexical Analyzer (scanner) Source Program Syntax Analysis (Parser) Tokens Semantic Analysis Syntactic Structure Symbol Table int x; cin >> x; if(x>5) x = “SHERRY”; else cout << “BOO”;

21 21 Structure of Compilers Lexical Analyzer (scanner) Source Program Syntax Analysis (Parser) Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator Intermediate Representation Target machine code Symbol Table

22 22 Structure of Compilers Lexical Analyzer (scanner) Source Program Syntax Analysis (Parser) Tokens Semantic Analysis Syntactic Structure Optimizer Code Generator Intermediate Representation Target machine code Symbol Table

23 23 Translation Steps: n n Recognize when input is available. n n Break input into individual components. n n Merge individual pieces into meaningful structures. n n Process structures. n n Produce output.

24 24 Translation (Compilers) Steps: n n Break input into individual components. (lexical analysis) n n Merge individual pieces into meaningful structures. (parsing) n n Process structures. (semantic analysis) n n Produce output. (code generation)

25 25 Compilers n Two major tasks: –Analysis of source –Synthesis of target n Syntax-directed translation –Compilation process driven by syntactic structure of the source being translated

26 26 Interpreters n Executes source program without explicitly translating to target code. n Control and memory management reside in interpreter, not user program. n Allow: –Modification of program as it executes. –Dynamic typing of variables –Portability n Huge overhead (time & space)

27 27 Structure of Interpreters Interpreter Source Program Data Program Output

28 28 Misc. Compiler Discussions n History of Modern Compilers n Front and Back ends n One pass vs. Multiple passes n Compiler Construction Tools –Compiler-Compilers, Compiler-generators, Translator- writing Systems »Scanner generator »Parse generator »Syntax-directed engines »Automatic code generator »Dataflow engines


Download ppt "CST320 - Lec 11 Why study compilers? n n Ties lots of things you know together: –Theory (finite automata, grammars) –Data structures –Modularization –Utilization."

Similar presentations


Ads by Google