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 home

Objective

Two outputs for each Jack class

Make the tokenizer correct first; then use its tokens to build the parse tree.

Contract

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.xml lists the tokens in order inside a <tokens> element. Each token has one of five type tags.
Parse XML
Xxx.xml uses 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 by TextComparer.
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.

Read

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.
Write

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 &lt;, &gt;, and &amp; 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.

First milestone

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.

Complete analyzer

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

  1. Obtain projects/10 and the TextComparer utility from the Nand to Tetris software package. Inspect one small .jack file beside its two reference XML files.
  2. Implement JackTokenizer. Generate XxxT.xml for Square and ArrayTest; compare every generated token file with the matching reference.
  3. Implement the parsing routines for the outer class structure and statements. Compare your Xxx.xml files for ExpressionlessSquare.
  4. Implement complete expressions and terms. Compare all generated Xxx.xml files for Square and ArrayTest.
  5. 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.