Laboratory assignment ยท 22 October
Lab 10: Build a Jack Syntax Analyzer
Read a Jack source file, identify its tokens, recognize its grammatical structure, and write that structure as XML. This is Chapter 10, Project 10 of the textbook.
Back to course homeObjective
Two outputs for each Jack class
Make the tokenizer correct first; then use its tokens to build the parse tree.
Xxx.jack becomes XxxT.xml and Xxx.xml
Write a Python syntax analyzer that accepts either one .jack file or a directory of Jack files. Process every Jack file in the selected input.
- Token XML
XxxT.xmllists the tokens in order inside a<tokens>element. Each token has one of five type tags.- Parse XML
Xxx.xmluses nested tags such as<class>,<statements>, and<expression>to record the grammatical structure.- Correctness
- For each supplied
Xxx.jack, both generated files must match the corresponding Project 10 compare files, apart from whitespace ignored byTextComparer. - Scope
- This project ends at syntax analysis. It does not generate VM code.
Stage 1
Tokenize the source
Use the five token categories from Lecture 28.
Recognize tokens correctly
- Ignore whitespace and comments between tokens.
- Keep text inside string quotes together as one string constant, even when it contains spaces or comment-like characters.
- Classify each token as a keyword, symbol, identifier, integer constant, or string constant.
- Remove the surrounding quotation marks from the value of a string constant.
Produce valid XML
Enclose the token list in <tokens> ... </tokens>. Write one tag for each token, in source order.
Escape Jack symbols that XML uses specially: <, >, and & become <, >, and & in the XML file.
Checkpoint: Generate XxxT.xml for every .jack file in projects/10/Square and projects/10/ArrayTest. Compare each file with its supplied token XML. You do not need ExpressionlessSquare for this tokenizer checkpoint.
Stage 2
Parse the token stream
Each parsing routine handles one named grammar construct.
Program structure and statements
Implement the routines for classes, declarations, subroutines, parameter lists, bodies, and the five statement forms. Start with projects/10/ExpressionlessSquare, whose expressions have been reduced to single variables.
For return;, for example, write a <returnStatement> element containing the return keyword and semicolon symbol.
Expressions and all term forms
Add expression and term parsing, including operators, parentheses, unary operations, array entries, and subroutine calls. Use the next token to distinguish a simple variable from an array entry or call.
Then compare your parser output for Square and ArrayTest with the supplied Xxx.xml files.
Testing workflow
Compare one class file at a time
The supplied reference files have the same names as your generated files, so keep your output in a separate directory.
Recommended order
- Obtain
projects/10and theTextComparerutility from the Nand to Tetris software package. Inspect one small.jackfile beside its two reference XML files. - Implement
JackTokenizer. GenerateXxxT.xmlforSquareandArrayTest; compare every generated token file with the matching reference. - Implement the parsing routines for the outer class structure and statements. Compare your
Xxx.xmlfiles forExpressionlessSquare. - Implement complete expressions and terms. Compare all generated
Xxx.xmlfiles forSquareandArrayTest. - If a comparison fails, locate the first mismatched tag or token and fix the routine responsible for that construct. Regenerate the XML before retesting.
Submit: your Python analyzer source files and the generated XML outputs for the supplied Project 10 programs. Keep the supplied reference XML files separate from your generated files.