This preview shows pages 1–3. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.View Full Document
Unformatted text preview: A Tour of the Cool Support Code * 1 Introduction The Cool compiler project provides a number of basic data types to make the task of writing a Cool compiler tractable in the timespan of the course. This document provides an overview of the Cool compiler support code, which includes: a linked list data type; a string table package; a symbol table package; miscellaneous utilities for the parser and lexer; a package for managing Cool abstract syntax trees; routines for printing abstract syntax trees; miscellaneous routines for handling multiple input files and command-line flags; the runtime system. This document should be read in conjunction with the source code. With the exception of the abstract syntax tree package (which is automatically generated), there are also extensive comments in the source. The purpose of the Cool support code is to be easy to understand and use. There are much more efficient implementations of most of the data structures used in the system. It is recommended that students implementing a Cool compiler also stick to simple, obviously correct, and perhaps inefficient implementationswriting a compiler that works correctly is usually difficult enough. The Cool system is written in C++ and it is assumed that the reader has some familiarity with that language. The base system deliberately uses a simple subset of the language. The heavily used features are: classes, single inheritance, virtual functions, and templates. Overloading is used very sparingly; with one exception, operator overloading is avoided completely (the exception is the operator << ). Destructors are not used and in fact memory management is ignored completely. Memory management is a very important part of software development as it is currently practiced in industry, but memory management is also tricky, error-prone, and very time consuming to get completely right. It is suggested that students also not worry about memory management in writing their own Cool compilers. * Copyright c 1995-2004 by Alex Aiken. All rights reserved. 1 2 Lists The file list.h implements a simple linked list datatype. The operations are similar to those provided in Lisp-family languages: a constructor List adds a new element to the front of a list, hd returns the first element of a list, and tl returns the tail of a list. Functions are also provided for computing the length of a list, applying a function to every element of a list, and printing a list. Example uses of lists can be found in the implementation of both string tables and symbol tables. 3 String Tables All compilers manage large numbers of strings such as program identifiers, numerical constants, and string constants. Often, many of these strings are the same. For example, each identifier typically occurs many times in a program. To ensure that string constants are stored compactly and manipulated efficiently, a specialized data structure, the string table , is employed....
View Full Document
This note was uploaded on 01/12/2010 for the course MATH 42 at Stanford.