Assignment 2: Some Additional Information
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}
As of Friday evening, there will be some sample EJay programs for testing your solution to assignment 2. The samples will be here:
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.falcon:~gfisher/classes/330/assignments/2/sample-test-files
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"); }
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
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: