Compiler Construction Lecture 4 1 Symbol Table Management
Compiler Construction Lecture 4 1
Symbol Table Management q q Symbol Table: A data structure for storing the names of the variables, and their associated attributes Ø Attributes (Variables): name, type, scope Ø Attributes (functions): name, number of arguments and type of arguments, method of argument passing (by reference / by value) It should be designed so that the compiler can: Ø Find the record for each variable quickly. Ø Store/retrieve data for a record quickly. 2
Grouping of Phases into Passes q q One or more phases can be grouped into a pass that reads an input file and generates an output file, e. g. , front-end phases can be grouped into one pass It is also possible to skip some phase(s), e. g. , all optimization activity can be avoided Typically, compilers produce intermediate code from a source language that can be translated into different target languages Ø A front end can be interfaced with different back-ends Several front-ends (source code in different languages) can be interfaced with the same back-end. 3
Compiler Construction Tools q q Compiler Construction Toolkits: Integrated set of routines for the implementation of various compilation phases Scanner Generators / Scanners: Automatically produce lexical analyzers from a description of the language tokens Parser Generators / Parsers: Automatically produce syntax analyzers from a grammatical description of the programming language Syntax-directed Translation Engines: Produce collection of routines for walking a parse tree and generating intermediate code. 4
Programming Languages q q First generation language: Machine Language Ø Very low-level operations coded in 0’s and 1’s Ø Slow, tedious Ø Programs were hard to understand modify Second generation language: Assembly Language Ø First step towards people-friendly language Ø Instructions are mnemonic representations of machine instructions 5
Programming Languages q q Third generation languages: More user-friendly than the second-generation ones Ø FORTRAN (scientific computation) Ø COBOL (business data processing) Ø LISP (symbolic computation) Ø C, C++, C#, Java (Object-oriented languages) Fourth generation languages: designed for specific applications Ø SQL (DB) 6
Programming Languages q q Impact of Language on Compilers Ø Compilers should (and have) adapt(ed) to the evolution of languages Ø Promote the use of high-level languages by reducing the execution overhead Impact of Computer Architecture on Compilers Ø Architecture has evolved immensely, and compilers are now being used to evaluate a new architecture, before it can be marketed. 7
Science of Building a Compiler q q q Building a compiler is like solving a complicated, real-world mathematical problem Ø Take a problem Ø Captures the key characteristics Ø Solve it using mathematical techniques Designing the right mathematical model, while balancing the need for generality against simplicity and efficiency Examples of models: finite-state machines, regular expressions, context-free grammars. 8
Code Optimization q q q To optimize is to produce better code Complex: Complexity of the processor architectures is continuously increasing Important: Much data is now being parallelized Optimization should: Ø Be Correct: preserve the original meaning Ø Improve performance of many programs (and not only one) Ø Keep the compilation time reasonable Ø Keep the engineering effort reasonable No perfect optimization technique exists Ø Trade-offs should be made. 9
Applications of Compilers q q q Implementation of High-Level Languages Ø These languages are easier to program BUT target programs run quite slowly Compiler optimizations has allowed the creation of efficient target code Compilation techniques have been developed to deal with all high-level functionality, e. g. , Ø Object-orientation Ø Memory management Ø Management of data flows. 10
Applications of compilers (contd. ) q q q Optimizations for Computer Architecture The instruction set of modern microprocessors uses parallelism Compilation techniques have been developed that can rearrange the execution of the instructions, in order to make instruction-level parallelism more effective Memory hierarchy: Ø Small and fastest nearest to the processor Compilation techniques have been developed to improve the effectiveness of the memory hierarchy by changing the instruction execution order. 11
Applications q q q Binary Translation: Translate binary (machine) code of one machine into binary code for another machine Database Query Interpreters: A query is interpreted in order to execute and find the results Software Productivity Tools: Ø Detecting errors by analyzing the data flow (before the program is run) Ø Type checking. 12
(Some) Programming Basics q q q Building a compiler depends on what decisions the compiler can take about the given program Ø Static Policy: One that allows a compiler to take decisions at compile time Ø Dynamic Policy: Allows the taking of decisions at run (exec) time only Static Scope Ø Possible to determine the scope of a variable by looking at the code (before exec) Dynamic Scope Ø When the scope can only be determined at runtime. C and Java use static scope. 13
Programming Basics q q Static scope: Ø The keyword “static” in Java: static int x Ø The compiler can determine the location of x (in memory) Ø Use of public, protected and private keywords Dynamic Scope: Ø When the value of some variable can only be computed at run time, e. g. , based on the input of the user Call by Value (a copy of the variable is passed) Call by Reference (the address is passed). 14
- Slides: 14