CS536 Lecture Notes: Feb 10, 1999 Recitation

By Lenny Gottesman (gottesma)


In today's recitation, we discussed a handout that was given out in this morning's lecture. In order for JLex to run correctly, be sure to set up your CLASSPATH environment variable properly, as described in Assignment #2.

Format of a JLex input file:

The JLex input file, usually called [something].jlex, is broken up into three sections (delimited by %% on a line by itself) as follows:

  1. User Classes Section
    In this section, you can place any java code that you would like dropped verbatim into JLex's output. In ex.jlex, we define a class, Token.
  2. JLex Directives
    In this section, you can define any macros that you wish to use (see handout), such as Digit=[0-9]. Also, you must tell JLex what type to return as a token (%type Token), and what to do upon hitting End Of File. If you want extra fields in class yytext() then enter them between '%{' and '%}' without the tick marks.
  3. Regular Expression Rules
    This is where all of the definitions go. You type in the definition and put the code you want executed inside of curly braces. In this example, we are trying to find all five-letter words that begin with P and end with T. The reason that first definition matches white space is that we do not want to match a longer word whose prefix is P???T. yytext() is always of type String.

csx.jlex is a starting point for Project 2. All those classes extend the CSXToken class and need no further explanation. The Pos, or Position class is used to keep track of where we are in the file. You can use the feature built into JLex to accomplish this, but apparently it returns weird values for the column number. Check it out in the documentation. There is a link from the course webpage.

A Symbol() is defined in java_cup.runtime.* and takes as its arguements a sym and then the data. Its use is pretty obvious from the handout, and all of the symbols in class sym appear at the end in that big thing that looks like it would be an enum in C++.