Parser Generator Wrapup For this assignment, you should put some final touches on your parser generator, including reasonable error reporting. You should use it to generate a parser for your truth table assignment and my "jBasic" interpreter. You will also practice using your generator by writing a few grammars. However, it is highly likely that you'll find bugs with your generator and spend much time tracking them down. Don't underestimate how long this last part might take. Reporting Parsing Errors. Errors can occur at several levels during compilation. At the lowest level, the lexical analyzer can encounter an unexpected symbol or end of input. At the next level, the input cannot be produced by the grammar. It is these errors that your parser must report. But other errors, such as type errors, are the not the responsibility of the parser. For example, if (x =) a); // this should be a parser error. int x = "abc"; // this is a type error but it's NOT a parser error. So don't expect your parser to be able to do too much all at once. Your grammar should not be trying to do type checking. When a parser error is encountered, you need to report enough information so the error can be tracked down. This means at least you should be able to report the line and column numbers of where parsing failed. My parser reports this along with the top ten elements of the parsing stack, plus the current lookahead (but this information is likely more useful for debugging the parser generator than during parsing in general). It is the lexer that records the line and column number. Take a look at the sample jbl.flex file on the homepage (for the jBasic interpreter). You'll find declared near the top: %{ public int linenum() {return yyline; } // yyline is private public int columnnum() {return yycolumn;} %} This code is added to the lexer class generated by JFlex. yyline and yycolumn are private variables within the class so we need to export them with these methods. But your parser should also be abstract and not assume that all lexers have these methods. For this purpose you can create interface abslexer { Gsym next() throws java.io.IOException; int line(); // should return -1 if information is not available int column(); } Here's a sample class (which was actually autmatically generated) that adopts a JFlex lexer to the abstract code. Here, calclexer is the class generated by JFlex: such lexers are called with yylex(). class Mycalclexer implements abslexer { calclexer lx; // calclexer was generated by JFlex public Mycalclexer(calclexer x) { lx=x; } public Mycalclexer(java.io.Reader x) { lx = new calclexer(x); } public Gsym next() throws java.io.IOException { return lx.yylex(); } public int line() {return lx.linenum()+1;} public int column() {return lx.columnnum()+1;} } // specialized lexer class Then you can write generic parser (absparser) code that reports the line and column numbers. Here's a sample usage of the Mycalclexer class in a program that I'm currently working on: BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.print("Enter Calc Term: "); String line = br.readLine(); Mylexer lexer = new Mycalclexer(new StringReader(line)); calcparser parser = new calcparser(lexer); Expr tree = parser.parse(); // get abstract syntax tree. /////////// You need to package your parser generator into a usable format. To help you do this, WRITE a README.txt that details how to go from grammar to using the generated parser. /////////// Depending on how you set up your own parsers, you need to devise experiments to test your generated parsers. Be sure to test for cases that SHOULD NOT parse as well as those that should, so we can see what error messages your parsers give out (at least line and column numbers). On thursday March 13th, I will ask you to demonstrate your program as follows: 1. Your truth table program should now completely work 2. my JBasic interpreter (you will have to edit jbrun.java to accomodate your parser setup). You can use my lexical analyzer. In addition to the sample JBasic program posted on the web page, I will also ask you to parse one that contains parser errors. 3. I will devise one or two other test cases for your parser generator. I will provide you with grammar plus any .java files needed. I will also provide you with a lexer so you won't have to write one on the spot, but you will need to quickly incorporate the lexer into your program. 4. In addition, you must show that your generator passes the stress test of generating the parser for the java1.4 grammar. The generated parser should compile.