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.
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:
jay_ast.py
, which contains the classes
representing Jay abstract syntax trees.
You will need to be familiar with these classes.
jay_ast.cfg
, the grammar file from which
jay_ast.py
was generated.
ast_values.py
, some code used by
the parser to make the abstract syntax trees.
You can ignore this file in this project
(though it will be important in future projects).
__init__.py
, some file that Python needs
for any project.
astPrinterVisitor.py
and interpreterVisitor.py
,
which contain classes that operate on Jay programs in AST form
to "AST-print" and interpret the programs, respectively.
astPrint_hmp.py
and jay_hmp.py
,
program files that run the AST-printing and interpreting.
The "hmp" part of their names indicates that they use the "hand-made parser"
as opposed to the generated parser we'll use in later projects.
handmade_parser.py
, the code for the parser itself,
which you need to finish.
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.
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
).
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.
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.