Name: Date:
Syntax Analysis
Tokenizer Questions:
- Translate the following Jack code into its tokenized XML code.
Jack Code | Tokenized XML |
How many tokens are generated? | |
var int x;let x = 3; |
- Translate the following Jack code into its tokenized XML code.
Jack Code | Tokenized XML |
How many tokens are generated? | |
var int value;if (y > x){let value = x & y;} |
- How many tokens will be produced by the following Jack code? Do not translate into XML, just count tokens.
Jack Code | Answer = |
How many tokens are generated? | |
var int x, y, greatest;let x = 3;let y = 5;if (x > y){let greatest = x;}else{let greatest = y;}do Output.printInt(greatest);do Output.println(); |
Syntax Analyzer Questions:
- Translate the following Jack code into its XML parse tree (using the Jack grammar).
Jack Code | XML Parse Tree |
let greatest = x is greater; |
- Translate the following Jack code into its XML parse tree (using the Jack grammar).
Jack Code | XML Parse Tree |
do Output.printInt(greatest); |
- Translate the following Jack code into its XML parse tree (using the Jack grammar).
Jack Code | XML Parse Tree |
class Point{field int x, y; constructor Point new(){let x = 0;let y = 0;}} |
Summative Questions:
- Consider the following statements, taken from three different Jack programs. In each one of these programs, the identifier foo represents a different thing:
let x = 5 + foo a // Here foo represents a simple variable.
let y = foo[12] 3 // Here foo represents an array.
let z = 2 * foo.val() // Here foo represents an object.
Suppose that we are parsing any one of these statements (we dont know which), and that
the current token is foo. How many more tokens do we have to read until we can tell
what we have is a simple variable, an array reference, or a method call?
- Zero
- One
- Two
- Three
- All answers are wrong
- Consider the following rule, taken from the Jack grammar:
parameterList: ( ( type varName ) ( , type varName ) * ) ?
Select the correct statement(s):
- The rule will parse successfully an empty parameter list
- The symbol * (in this context) is part of the Jack language
- The symbol * means 0 or 1
- The symbol * means 0 or more
- The symbol ? means 0 or 1
- The symbol ? means 0 or more
- The rule will parse successfully the parameter list (int x int y)
- The rule will parse successfully the parameter list (int x, int y)
- XML code is structured as a:
- Stack
- Graph
- Tree
- Multi-dimensional array
Programming Exercise:
Using Java, complete the JackTokenizer class (JackTokenizer.java, part of the overall compiler) and implement the following enums (TokenType, Kind and Keyword). You may use the following API design to help guide your work, or choose to implement your own design.
Following is an explanation of each of the methods in the JackTokenizer class. Please note the methods keyword(), intVal(), stringVal(), symbol(), tokenType() are really accessor (getter) methods, thus they have been renamed to getKeyword(), getIntVal(), getStringVal(), getSymbol() and getTokenType() accordingly in the class diagram above.
The main method of the JackTokenizer should prompt the user to enter the name of a Jack file (e.g. SquareDance.jack), then the program will produce an XML output file of all the tokens in the file with the extension xxxT.xml (e.g. SquareDanceT.xml).
You can use the TextComparer.bat (or .sh) supplied with the nand2tetris software to test your output against the solution (attached to this lab assignment on Canvas).
Heres a sample input/output of the Jack Tokenizer:
Heres the provided API for the Jack Tokenizer (also on Helper Sheet):
When youre finished, please upload a single Word document (or PDF) containing the following in order on Canvas:
- The .xml files from your 2 sample programs (SquareDanceT.xml and ArrayMain.xml)
- The source code of your JackTokenizer
- The source code of each of your enums (TokenType, Kind and Keyword)
Reviews
There are no reviews yet.