课程名 编译原理 Compiling Techniques 浙江大学计算机学院与软件学院 编译原理课程组
Information Instructor 李莹 cnliying@zju.edu.cn 13335880866 Teacher Assistant 孙煦雪 sunxuxue@zju.edu.cn 15858138880
Text Books The Dragon Book: Compilers Principles, Techniques, & Tools, Second Edition, Alfred V. Aho The Tiger Book: Modern Compiler Implementation in C, Andrew Appel
Grade Distribution attend class 10% Homework Class test or Mid-Term Exam 15% Final during exam period: 65%
Course Structure Course has theoretical and practical aspects Compiling Techniques Compiler Design
Compiler: why? 1940’s The first electronic computers Programming in machine language Slow , tedious and error prone 1954 IBM develops the 704 All programming done in assembly Software costs exceeded hardware costs!
Compiler: why? Emerge of higher-level languages with higher-level notations more easily programming Fortran Scientific computation Cobol Business data processing Lisp Symbolic computation
Higher-level Language A compiler For machine Higher-level Language Machine Language For human Compiler
Why Study Compilers Basic idea for constructing translators. Principle and Techniques reusable in other CS domains. Influences hardware design, e.g., RISC, VLIW. Tools (mostly “optimization”) for enhancing software reliability and security. Excellent software-engineering example --- theory meets practice. Essential software tool.
A compiler A compiler is a program that can read a program in one language and translate it into an equivalent program in another language Compiler Target Program Source Program Input Output
A Interpreter An interpreter directly execute the operations of source program on inputs Interpreter Source program Input Output
A language-processing system
The structure Two parts of a compiler operate as a sequence of phases Analysis, front end Synthesis,back end operate as a sequence of phases
Lexical Analysis Reads stream of characters Group the characters into lexemes For each lexemes, output a Token <token-name, attribute-value>
Syntax Analysis Reads Token stream Create tree-like intermediate representation (syntax tree) The grammatical structure of the token stream
Semantic Analysis Uses syntax tree information in Symbol table understand the meaning of program Too hard for compilers only check Semantic consistency Type checking Type conversions (Coercions)
Intermediate Code Geneartion For portability and reuse Before generate target program Generate intermediate representations first Three address code Code sequence One operator Three operands (max)
Code Optimization To improve the intermediate code Run faster Use less memory Consume less power
Code Generation Takes an intermediate representation as input Maps it to target language Registers and memory allocation Translate IR instructions into machine instructions
Symbol-Table Management An essential function of a compiler Records variable names various attributes of them
Passes Several phases may be grouped into a pass A pass reads an input file and writes an output file An example of a pass including: Lexical analysis, parsing, semantic analysis IR generation.
Compiler construction tools-1 Some tools could be used to help compiler construction Parser Generators. automatically produce syntax analyzers from a grammatical description of a programming language.Yacc Scanner Generators. produce lexical analyzers from a regular-expression description of the tokens of a language.Lex Syntax-directed translation engines. produce collections of routines for walking a parse tree and generating intermediate code.
Compiler construction tools-2 Code-generator generators. produce a code generator from a collection of rules for translating each operation of the intermediate language into the machine language for a target machine. Data-flow analysis engines. facilitate the gathering of information about how values are transmitted from one part of a program to each other part. Compiler-construction toolkit. provide an integrated set of routines for constructing various phases of a compiler.
The move to Higher-level Languages First generation languages: machine languages. Second generation: assembly languages. Third generation: higher-level languages, Fortran, Cobol, Lisp, C…. Forth generation: languages designed for specific applications, NOMAD, SQL Fifth generation: logic- and constraint-based languages, Prolog and OPS5
Another classification of languages Imperative how a computation is to be done C, C++, C#, Java Declarative what computation is to be done ML, Haskell, Prolog Von neumann languages Object-oriented languages Scripting languages
Impacts on Compilers Can help promote the use of high-level languages Compiler writing is challenging. A compiler must translate correctly the potentially infinite set of programs
Application of Compiler Technology Implementation of higher-level programming languages Optimizations for computer architectures Design of new computer architectures Program translations Software productivity tools
Optimizations for computer architecture Parallelism Multi-core, many-core VLIW (very long instruction word), issue multiple operations in parallel Memory hierarchies Several levels of storage The closer, the faster Registers, CPU cache Main memory disks
Design of new computer architecture RISC Specialized architecture VLIW SIMD
Program translation Binary translation x86 →Sparc x86 →arm Hardware synthesis VHDL RTL Database query interpreters Compiled simulation
Software Productivity Tools Type checking Bounds Checking Memory-Management tools Code generators
Summary Language Processors Compiler Phases Machine and Assembly languages Modeling in Compiler Design Code Optimization Higher-level Languages Compilers and Computer Architecture Software Productivity and Software Security Programming Language Basics: Scope Rules, Environments, Block Structure, Parameter Passing, Aliasing
The end of Lecture01