Module 3:
A Parser for Klein: Producing Abstract Syntax Trees
Stage 3 of the Klein compiler project
Out: Friday, October 3
Due: Friday, October 17, at 11:59 PM
STATUS CHECK DUE: Friday, October 10
Tasks
This stage consists of the second part of the second component of your Klein compiler and three additional programs.
1. A Parser that Produces Abstract Syntax Trees
Your primary task is:
Modify your table-driven parser to produce an abstract syntax tree.
This parser is a function or object that examines a sequence of Klein tokens and produces as output either an abstract syntax tree corresponding to the program (if the program is valid) or a suitable "nothing" value (if the program is not valid).
Implement your parser by adding semantic actions to the parsing algorithm and parse table that you built for Module 2.
Be sure that your parser detects and reports any syntactic error that prevents it from processing the rest of the program.
- At the bare minimum, the parser can report the combination of non-terminal and token that caused the error. It will be more helpful to the programmer if the parser also reports the next few tokens in the stream.
- Bonus points will be awarded if the parser's error messages report the line and character on which the error occurs, or if the messages provide custom messages for common error states.
- As before, your parser must catch all error messages generated by your implementation language and report errors as Klein errors. Ideally, the user of your Klein compiler will not know the compiler's implementing language.
2. An AST-Listing Program
Once your parser is able to generate an abstract syntax tree
Write a program that uses your parser to produce a formatted listing of the input program's AST.
In order to show the structure of the abstract syntax tree, the entry for each construct should be indented under its parent element. The display of identifiers and numbers must include the semantic content of the node.
For example, when given print-one.kln, it might generate something like this:
program
name main
params
returns integer
body
function call
name print
args integer_literal 1
integer_literal 1
The specific fields and names displayed will depend on the abstract syntax you design.
See below for alternative clients of your parser that you might implement for extra credit.
3. The kleinp Command
In order to use your client program as a tool,
Create an executable or a Unix command-line script named
kleinp that takes the name of a Klein source
file as an argument and runs your AST-printing program on it.
For example:
$ ./kleinp programs/print-one.kln
program
name main
params
returns integer
body [...]
This is the third in a series of tools that makes up the command-line suite of your Klein compiler.
4. A New Klein Program
Finally, in order to test our parsers,
Write at least one meaningful legal program in Klein.
Your test program should be at least at the level of a mid-CS1 program. Feel free to to to write something a more advanced program; you certainly will understand Klein well by the time you have a working parser.
You might write a Klein program that shows your parser working in a situation that you had to debug while implementing your parser. Such a program will be a good test for any Klein compiler.
If you are stumped for what to do, try one of these ideas.
You should, of course, produce as many tests as necessary in order to ensure that your parser works correctly. You are also encouraged to use any test programs available from the project home page.
Deliverables
As before, submit only one copy of each assignment per team. The team captain or a designated team member can be responsible for the submission.
Status Check
You will need to define the abstract syntax of Klein and add semantic actions to the refactored grammar you used to implement your parse table.
Submit:
- a list of the AST classes or records you intend to build and
- your extended grammar.
Make these documents a part of the ongoing documentation of your compiler.
Record your answers in one file or two, plaintext or PDF, and email them to the instructor by the status check due date.
Final Deliverable
By the due time and date, use
the course submission system
to submit
your project directory
electronically as a zip file named project03.zip
or project03.tar.gz.
Alternative Client Programs
A formatted listing of an abstract syntax tree is a useful tool, and printing one is sufficient to demonstrate your parser's ability to produce an AST. However, you might want to demonstrate your parser on a more useful or interesting task. Here are two alternatives:
-
Generate a legal
.dotfile. We can then give the.dotfile as input to Graphviz and produce a two-dimensional graph of the abstract syntax tree! Be sure that the nodes and edges of the graph include the same information required in the formatted listing. -
Write a code formatter that displays the Klein program in
a standard form and that uses
syntax coloring
to improve the readability and context of the code.
A prominent example of a code formatter these days is
gofmt, which implements standard practices of the Go programming language.
Either of these alternatives is worth extra credit beyond the value of the assigned client. But they will also likely require more work! Let me know if you would like to attempt either of these.