Project 0: Parsing

In this project you will think through the details of one of the simplest parsing algorithms, LL(1), which is also called recursive descent (which would make a great name for a rock band or sci-fi novel). This project also will make you look closely at the Jay grammar and abstract syntax and ease you into Python programming.

1. Setup

In a directory for this class and project, copy and untar the files for this project

cp ~tvandrun/Public/cs365/proj0.tar .
tar xvf proj0.tar

This will have two subfolders, jay and samples In jay you'll find the following things:

Additionally, samples contains a sample Jay file for testing. I suppose the plural name of this folder is misleading, but it's an appropriate place to add your own samples.

2. Your task

Your task is to finish the class HandMadeParser in the file handmade_parser.py. This will be similar to the LL(1) example we saw in class for MathEx. Your work will essentially be translating the grammar into an LL(1) algorithm.

Details of how the HandMadeParser class should work are found in the documentation. The methods for syntactic entities program, declarations, declaration, identifiers, block, printStatement, expression, and negation are done for you. The rest you are to write (deleting the line return None).

3. Testing

To test your program, you can print an parenthesized ("s-expression") printing of the generated abstract syntax tree or you can execute the Jay program. For example, to do the "AST-print", type

python jay/astPrint_hmp.py  samples/Euclid.jay

which, if your parser is correct, should produce the following (except that I added newlines and tabs by hand to make it more readable):

(Program 
    (Declaration int (a b t) 
    (Block 
      (Assign a (IntLitExpr 12))
      (Assign b (IntLitExpr 9))
      (Loop (BinaryExpr (Var b) > (IntLitExpr 0)) 
          (Block 
               (Assign t (BinaryExpr (Var a) / (Var b)))
               (Assign t (BinaryExpr (Var t) * (Var b)))
               (Assign t (BinaryExpr (Var a) - (Var t)))
               (Assign a (Var b))
               (Assign b (Var t))))
      (Print (Var a))))

Or, to run (interpret) the Jay program, type

python jay/jay_hmp.py samples/Euclid.jay

Note that you can use the interpreter only on correct programs. It's a "dangerous" interpreter in the sense that it will run the program without doing any typechecking, which we'll do in a later project. The AST-printer, however, should be able to handle programs with syntax errors, in that it should report an appropriate parse error.

4. Turn-in

Copy your handmade_parser.py to

/cslab.all/linux/class/cs365/(your id)/proj0

I will grade your project by running it against a collection of test files.

DUE: Monday, Feb 1, 5:00 pm.


Thomas VanDrunen
Last modified: Tue Jan 26 11:43:34 CST 2016