Starting from:

$30

CS 220 Lab #10 Solved

                              

The Jack Compiler 1:

Syntax Analysis

Tokenizer Questions:

1.       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;

 
2.        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;

}

 3.       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:

1.       Translate the following Jack code into its XML parse tree (using the Jack grammar).

Jack Code
XML Parse Tree
let greatest = “x is greater”;

 
 

 2.       Translate the following Jack code into its XML parse tree (using the Jack grammar).

Jack Code
XML Parse Tree
do Output.printInt(greatest);

 
 

3.       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:

 

1)      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 don’t 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?

 

a)      Zero

b)      One

c)      Two

d)      Three

e)      All answers are wrong

 

2)      Consider the following rule, taken from the Jack grammar:

parameterList: ( ( type varName ) ( ‘,’ type varName ) * ) ?

 

Select the correct statement(s):



a)      The rule will parse successfully an empty parameter list

b)      The symbol * (in this context) is part of the Jack language

c)      The symbol * means 0 or 1

d)      The symbol * means 0 or more

e)      The symbol ? means 0 or 1

f)       The symbol ? means 0 or more

g)      The rule will parse successfully the parameter list (int x int y)

h)      The rule will parse successfully the parameter list (int x, int y)

 

3)      XML code is structured as a:

a)      Stack

b)      Graph

c)      Tree

d)      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).

 

 

Here’s a sample input/output of the Jack Tokenizer:

 

 

Here’s the provided API for the Jack Tokenizer (also on Helper Sheet):

  

 

When you’re finished, please upload a single Word document (or PDF) containing the following in order on Canvas:

1.       The .xml files from your 2 sample programs (SquareDanceT.xml and ArrayMain.xml)

2.       The source code of your JackTokenizer

3.       The source code of each of your enums (TokenType, Kind and Keyword)

More products