CSC 330 Assignment 2

Assignment 2: Some Additional Information



A Hint on Recognizing Nested Comments

There is more than one approach to recognizing nested comments using JFlex. Probably the most straightforward approach involves the use of start conditions. You can read about this in the "Rules and Actions" section of the JFlex manual. There are examples of JFlex specs that use start conditions in the examples directory of the JFlex download.

One approach that does not work for nested comments is to use a recursive pattern definition. E.g., consider the following approach to defining nested comments in the Pascal lexer:

leftbrace       = \{
rightbrace      = \}
nonbrace        = [^{}]
comment_body    = {nonbrace}*
comment         = {leftbrace}({comment_body}|{comment}){rightbrace}


You should figure out what JFlex thinks about this definition.

Sample EJay Test Files

As of Friday evening, there will be some sample EJay programs for testing your solution to assignment 2. The samples will be here:


falcon:~gfisher/classes/330/assignments/2/sample-test-files

As noted in the Assignment 2 writeup, there will be additional "hidden" test cases used to test your turn ins, beyond what is in the samples. To fully test your solution, focus specifically on additional forms of literals and nested comments.

Incremental Testing of Ideas

A very handy way to try ideas for patterns or rules is with simple stand-alone JFlex files, rather than within the entire Assignment 2 solution. If you put the "%standalone" directive in a JFlex file, it will include a main method in the generated Java file, which you can compile and run by itself. For example, here's a quick JFlex test program to see if it's OK to put whitespace around the '|' operator in a patter definition:

%%
%class TestOrDef
%standalone

x_or_y = x | y

%%
{x_or_y}        { System.out.println("x or y"); }
.               { System.out.println("other"); }

Given this specification, JFlex generates the file TestOrDef.java, which you can compile and run. The program takes a command-line filename as input.

Compiling and Running on Windows

There is a sample Windows batch file that illustrates how to compile and run a JFlex lexer. The file is here:


falcon:~gfisher/classes/330/examples/jflex/.make.bat

Using Online Examples in Your Solution

For this assignment, it is NOT considered cheating to use solutions you may find in online JFlex examples, as long as the following two conditions are met:

  1. You cannot use more than two lines of a specification verbatim; if you use three or more lines, you must rewrite the specification using different pattern names, and rule bodies.
  2. You must fully understand what the specifications you're reusing do.

The point of the first condition is to have you go at least through the motions of writing your own definitions, even if you are reusing someone else's solution concepts. The second condition will be verified on the midterm and final.