Use Java Cup to parse Tiny programs (6 points+1 bonus)

Due date: Feb 26/28

Where to submit

You must submit your assignment at the web submission site. We will use your last submission for your final marks for this assignment. For each submission you will get your marks instantly by email.

You must test your program on our sample inputs and other cases devised by you before submission.  Passing the sample inputs will not guarantee a high mark. You must fully understand the grammar and implement that grammar correctly so that your parser can cope with all situations.


Use JavaCup to parse Tiny programs. Your program will be able to judge whether a program is valid or not with respect to its syntax. 

Assignment Specification

This assignment is to parse Tiny program using JavaCUP.

You need to write a JavaCUP file and a JLex file so that a parser for Tiny language can be generated. The parser will be able to tell whether a Tiny program is syntactically correct.   You need to rewrite EBNF grammar into CFG that is acceptable by JavaCUP.  Comments in Tiny program should be thrown away in the scanner.

We will run the following commands to generate the scanner and the parser. You can modify the lex file you created in assignment 2. 

   >java JLex.Main A3.lex
   >java java_cup.Main -parser A3Parser -symbols A3Symbol < A3.cup 
   >java A3User 

The program A3User invokes the parser and  is defined as below:

class A3User {
  public static void main(String[] args) throws Exception {
       File inputFile = new File ("A3.tiny");
       A3Parser parser= new A3Parser(new A3Scanner(new FileInputStream(inputFile)));
       Integer result =(Integer)parser.parse().value;
       FileWriter fw=new FileWriter(new File("A3.output"));
       fw.write("Number of methods: "+ result.intValue());

If your lex and cup files are correct, all of those command and especially A3User will run smoothly without any error report, and an A3.output file will be created which should consists of one like as follows:

   Number of methods: numberOfMehtodsInA2Input

If your programs are not correct, during the process there will be some error messages. 

Here is a sample A3.tiny file and A3.output file. For incorrect Tiny programs such as A31.tiny and A32.tiny, your parser will report an error and no A3.output file is generated. 

Note that you don't need to write any Java programs. The parser and the scanner are generated from your cup and lex specifications.  Also, we will test your program on our own data.

What to submit

You need to turn in 2 files:

bulletthe JLex file, named A3.lex, which can be used to generate the scanner;
bulletthe javaCUP file, named A3.cup, which can be used to generate the parser.

Common Errors

Marking scheme

if ( A3.lex and A3.cup  files not submitted or file names are incorrect) return; 
if ( && && A3Symbol are generated after the second command) {
   if ( generated programs are compiled correctly && 
        A3Scanner.class, A3Parser.class and A3Symbol.class are generated) {
            for (each of the 12 test A3.tiny) {
                  if (A3.tiny is valid && A3.output is generated && method count is correct)  {
                  if (A3.tiny is not a correct Tiny program && A3.output is not generated )
yourMark+= (size of your lex and cup file<500)?0.5*500/fileSize:0; 
for (each day of your late submission)  yourMark=yourMark*0.7;