
Use Java Cup to parse Tiny programs (6 points+1 bonus)
Due date: March 2, 12:50pm
Where to submit
You must submit your assignment at the
web submission site . You
can submit 5 times, and 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.
Purpose
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
>javac A3.lex.java A3Parser.java A3Symbol.java A3User.java
>java A3User
The program A3User invokes the parser and is defined as below:
import java.io.*;
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());
fw.close();
}
}
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: