CSC448: Homework [1/1] |
INTRODUCTION In this assignment you will use JFlex and CUP to parse a small subset of Java. You should implement your lexer/parser in examples/skeleton. You must modify: examples/skeleton/parser/skeleton.flex examples/skeleton/parser/skeleton.cup You can: * Build your parser by running "ant". * Clean the directory of class files and the output of JFlex/CUP by running "ant clean". * Run your parser using "run-main.bat" and typing input into the console (Control-Z on a line to signal end of input on Windows; Control-D on UNIX). Run your parser using "run-main.bat Test.java" to read input from Test.java. ======================================================================== SCOPE Java is a fairly large language. You do not need to parse all of Java. Roughly, your parser must (and must only) deal with a Java source code file that consists of an optional package declaration, following by optional import statements, followed by exactly one class declaration. That class declaration must only consist of field declarations without initializers: class Foo { // Must deal with this one. private int x; // Not allowed by your parser. Has an initializer (1+2). private int y = 1 + 2; // Not allowed by your parser. It's a method declaration not a field declaration. void f () { x = 5; } } Specifically, your parser must allow: * package declarations * import statements (with and without wildcards) * modifiers (public, protected, private, static, abstract, final, native, synchronized, transient, volatile, strictfp) * do not worry about modifiers that do not make sense in a particular place---that is cleaned up after the parser has done its work * extends/implements on the class * field declarations * types, including array types for fields Your parser must not allow: * initializers on field declarations * method declarations * nested classes * more than one class declaration in a file Your parser can assume that identifiers consist of ASCII rather than Unicode characters. Identifiers cannot consist of Java keywords, nor the "true", "false", "null" literals, i.e., your parser should reject: class Foo { int if = 1; } The Java keywords are: abstract default if private this boolean do implements protected throw break double import public throws byte else instanceof return transient case extends int short try catch final interface static void char finally long strictfp volatile class float native super while const for new switch continue goto package synchronized ======================================================================== TESTING You should write six Java files (called Test1.java,...,Test6.java) to test your parser. Try to exercise boundary cases, e.g., a class that has no extends, or no implements, or no fields. You may include the following test case (leaving you with only five test cases to write). package com.xyz.abc; import java.awt.*; import java.util.List; import java.util.HashMap; public class Foo extends Bar implements Baz { synchronized final public int w; static protected Foo x; volatile private static Baz[] y; private final Baz[][] z; } ======================================================================== HINTS You will almost certainly find it useful to look at the Java Language Specification: http://java.sun.com/docs/books/jls/second_edition/html/jTOC.doc.html Especially the discussion of identifiers in section 3.8 and the grammar in chapter 18. Lateral thinkers may realize that there are CUP grammars for Java on the web already. The plagiarism policy would not allow you to submit such work as your own, but there is a specific exception here: you are allowed to look at the existing CUP grammars on the web (the ones from one of the CUP websites only), but you must not cut and paste from one of those grammars into your own files. It is OK to retype from one window to another. Work incrementally. Writing the whole parser at once is difficult. I recommend that you start by writing a parser that expects to find a package declaration only. Make sure it compiles and works as expected. After that, gradually add more to your parser---and test as you go. For example, when you start to parse classes, assume that no field declarations are permitted initially. ======================================================================== CRITERIA FOR YOUR SUBMISSION TO BE GRADED Submissions will only be graded if they meet the following criteria: * examples/skeleton/grader.txt must be completed (after copying the template from the examples directory). * build.xml must not be modified. * Files should not be moved/renamed---it makes automated testing impossible * Must build successfully when run via "ant". * Not compiling = no points. * If you cannot get something to build, then comment it out and explain what is going wrong, what you think the problem is, and how you think you might fix it in grader.txt. ======================================================================== SUBMISSION Copy grader.txt from the examples directory to the examples/skeleton directory. Complete grader.txt. Run "ant submit" in the examples/skeleton directory. Upload the resulting submit-xxx.zip file to COL.