
Phases of a Compiler
Explore the key phases of a compiler, from lexical analysis to machine code generation, and learn about the essential steps involved in converting code to executable programs. Dive into the intricacies of compilers and their role in software development.
Download Presentation

Please find below an Image/Link to download the presentation.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.
You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.
The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.
E N D
Presentation Transcript
CS152 Programming Paradigms Thaddeus Aid Department of Computer Science San Jose State University Spring 2016 1 Creative Commons Attribution-ShareAlike 4.0 International License
Compilers, Interpreters, and Virtual Machines Citation http://www.diku.dk/~torbenm/Basics/basics_lulu2.pdf Creative Commons Attribution-ShareAlike 4.0 International License 2
What is a Compiler? Programmers used to have to write in machine language This was error prone and took a long time to learn and write Amazing Grace Hopper felt that it would be better to write More human like programs Have a piece of software convert that to machine language This birthed FORTRAN and COBOL and then a huge number of other languages Creative Commons Attribution-ShareAlike 4.0 International License 3
Phases of a Compiler Lexical Analysis The translation of the text into tokens, the symbols that will make up the alphabet of the program. Syntax Analysis Turns the tokens into a syntax tree (also known as parsing) Type checking Checking the code to ensure there are no explicit errors in the code Intermediate Code Generation The tokens are translated into a simple machine language Creative Commons Attribution-ShareAlike 4.0 International License 4
Phases of a Compiler (Cont) Register Allocation The symbolic machine code is translated to register numbers targeting the correct machine architecture Machine Code Generation The source code is translated to assembly language for the machine Assembly and Linking The assembly language is translated to binary Creative Commons Attribution-ShareAlike 4.0 International License 5
Phases of a Compiler (Cont) At each phase the code is checked for errors and optimizations that can be made to the code. Each phase is dependent on the previous phase The Type Checker can assume that the code is Syntactically correct Assembly and Linking is usually done by the operating system itself Therefore not technically part of the compiler Creative Commons Attribution-ShareAlike 4.0 International License 6
Interpreters Work the same as compilers until the Syntax Tree From the tree it goes directly to execution This is why interpreters are slower, they don t have the same level of optimization They have no forward knowledge of action so cannot anticipate it However, they are easier to implement on new systems They work fine when speed isn t a primary concern Creative Commons Attribution-ShareAlike 4.0 International License 7
Virtual Machines A Virtual Machine is an in between state A computer running on another computer Code is compiled down to the VM The VM uses Just in Time compiling to change to binary on the physical machine The intermittent code can be deployed to any VM on any physical machine Creative Commons Attribution-ShareAlike 4.0 International License 8
Why learn about Compilers? It is expected of a well educated Computer Scientist to understand the compiler, we aren t just Software Engineers after all ;) You should understand the tools that you use, your compiler/interpreter/VM is a very important tool that you use Techniques used in compilers are useful elsewhere You may need to create a domain specific language in your future Creative Commons Attribution-ShareAlike 4.0 International License 9
Lexical Analysis Lexical means Pertaining to Words Lexical Analysis -> Analysis of Words Lexical Analyzer or Lexer Consumes text and translates it into tokens Removes white space such as space and new line Primary goal is to get code ready for Syntactic Analysis Generally use Regular Expressions and are Finite Automata Creative Commons Attribution-ShareAlike 4.0 International License 10