1 / 28

Why study compilers?

Why study compilers?. Ties lots of things you know together: Theory (finite automata, grammars) Data structures Modularization Utilization of software tools You might build a parser. The theory of computation/formal language still applies today.

khuyen
Download Presentation

Why study compilers?

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  16. Parse Tree (Parser) Program Data Declaration datatype ID >> ; cin int x

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

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

  19. Who is responsible for errors? • 45ab • One wrong token? • Two tokens (45 & ab)? Are whitespaces needed? • 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. Structure of Compilers Syntax Analysis (Parser) Lexical Analyzer (scanner) Tokens Source Program Syntactic Structure Semantic Analysis int x; cin >> x; if(x>5) x = “SHERRY”; else cout << “BOO”; Symbol Table

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

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

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

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

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

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

  27. Structure of Interpreters Program Output Interpreter Source Program Data

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

More Related