PA1 - CS 143 Compilers Handout 1 Programming Assignment I...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: CS 143 Compilers Handout 1 Programming Assignment I Due Thursday, October 8, 2009 at 11:59pm 1 Overview of the Programming Project Programming assignments I–IV will direct you to design and build a compiler for Cool. Each assignment will cover one component of the compiler: lexical analysis, parsing, semantic analysis, and code generation. Each assignment will ultimately result in a working compiler phase which can interface with other phases. You will have an option of doing your projects in C++ or Java. For this assignment, you are to write a lexical analyzer, also called a scanner , using a lexical analyzer generator . (The C++ tool is called flex ; the Java tool is called jlex .) You will describe the set of tokens for Cool in an appropriate input format, and the analyzer generator will generate the actual code (C++ or Java) for recognizing tokens in Cool programs. On-line documentation for all the tools needed for the project will be made available on the CS143 web site. This includes manuals for flex and jlex (used in this assignment), the documentation for bison and java cup (used in the next assignment), as well as the manual for the spim simulator. You may work either individualy or in pairs for this assignment. 2 Introduction to Flex/JLex Flex allows you to implement a lexical analyzer by writing rules that match on user-defined regular expressions and performing a specified action for each matched pattern. Flex compiles your rule file (e.g., “lexer.l”) to C (or, if you are using JLex, Java) source code implementing a finite automaton recognizing the regular expressions that you specify in your rule file. Fortunately, it is not necessary to understand or even look at the automatically generated (and often very messy) file implementing your rules. Rule files in flex are structured as follows: %{ Declarations %} Definitions %% Rules %% User subroutines The Declarations and User subroutines sections are optional and allow you to write declarations and helper functions in C (or for JLex, Java). The Definitions section is also optional, but often very useful as definitions allow you to give names to regular expressions. For example, the definition \DIGIT [0-9] allows you to define a digit. Here, DIGIT is the name given to the regular expression matching any single character between 0 and 9. The following table gives an overview of the common regular expressions Fall 2009/2010 page 1 of 8 CS 143 Compilers Handout 1 that can be specified in Flex: x the character ”x” "x" an ”x”, even if x is an operator. \x an ”x”, even if x is an operator. [xy] the character x or y. [x-z] the characters x, y or z. [^x] any character but x. . any character but newline....
View Full Document

This note was uploaded on 01/12/2010 for the course CS 143 at Stanford.

Page1 / 8

PA1 - CS 143 Compilers Handout 1 Programming Assignment I...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online