Práctica 8: Compilación

Introducción

Hasta el momento, todos los sistemas de hardware y software que veníamos trabajando eran de bajo nivel, lo que significa que como humanos generalmente no interactuamos con ellos directamente. Esto cambia en el proyecto 09, en el cual nos presentan un lenguaje de alto nivel, llamado Jack, diseñado para permitirnos escribir programas de alto nivel. Este lenguaje está basado en objetos, tiene funciones básicas y el estilo de lenguajes como Java y C#. Con esto en mente, el objetivo de este proyecto es familiarizarnos con el lenguaje Jack mediante la implementación de alguna aplicación con el lenguaje Jack para estar preparados para el próximo proyecto.

Por otro lado, en el proyecto 10 trataremos al compilador, el cual es un programa que traduce programas de un idioma de origen a un idioma de destino. Este proceso de traducción se conoce como compilación y se basa en dos tareas distintas; análisis de sintaxis y generación de código pero esta ultima se tratará en el próximo proyecto. Por lo que el objetivo de nosotros en este proyecto es crear un analizador de sintaxis para analizar los programas de Jack según la gramática del lenguaje Jack que genere una salida de un archivo .xml

Proyecto 9. High-Level Language

Desarrollo

Para cumplir con el objetivo de este proyecto, se implementó con el lenguaje Jack una versión muy básica del juego Snake, en el cual partimos con una serpiente pequeña la cual podemos dirigir hacia la derecha, izquierda, arriba y abajo con las flechas. Además podemos incrementar su tamaño presionando la tecla X.

Luego de tener el juego en el lenguaje Jack con sus respectivas clases separadas, compilamos el juego utilizando el compilador Jack que nos suministra nand2tetris. El compilador traducirá todos las clases .jack que le pasemos y se guardarán los archivos .vm correspondientes en la misma ubicación. Finalmente, para probar el juego compilado, utilizamos el emulador de VM y cargamos la carpeta que contiene los .vm para luego ejecutar el programa.

Main.jack

class Main {

function void main() {

var SnakeGame game;

let game = SnakeGame.new();

do game.run();

do game.dispose();

return;

}

}

LinkedList.jack

class LinkedList {

field PixelNode first, last;


constructor LinkedList new(){

let first = null;

let last = null;

return this;

}


method void dispose(){

if( ~(first = null) ){

do first.dispose();

}

do Memory.deAlloc(this);

return;

}


method void push(PixelNode p){

if( last = null ){

let first = p;

let last = p;

} else {

do last.setNext(p);

let last = p;

}

return;

}


method void shift(){

var PixelNode old;

let old = first;


if( old.next() = null ){

let first = null;

let last = null;

} else {

let first = old.next();

}

do old.setNext(null);

do old.dispose();


return;

}


method PixelNode first(){

return first;

}


method PixelNode last(){

return last;

}

}


Node.jack

class Node {

field Node next;

field int data;


constructor Node new(int new_data) {

let data = new_data;

return this;

}

}


PixelNode.jack

class PixelNode {

field int x, y;

field PixelNode next;


constructor PixelNode new(int new_x, int new_y){

let x = new_x;

let y = new_y;

let next = null;

return this;

}


method void dispose(){

if(~(next = null)){

do next.dispose();

}

do Memory.deAlloc(this);

return;

}


method int x(){

return x;

}


method int y(){

return y;

}


method PixelNode next(){

return next;

}


method void setNext(PixelNode next_node){

let next = next_node;

return;

}

}


Snake.jack

class Snake {

field LinkedList pixels;


constructor Snake new(int x, int y) {

var PixelNode pix;

let pix = PixelNode.new(x, y);

let pixels = LinkedList.new();

do pixels.push(pix);

return this;

}


method void dispose() {

do pixels.dispose();

do Memory.deAlloc(this);

return;

}


method void crawlDown(bool increase_size){

var PixelNode last, new_pixel;

let last = pixels.last();

if( last.y() = 255){

return;

}

let new_pixel = PixelNode.new(last.x(), last.y() + 1);

do pixels.push(new_pixel);


if( ~increase_size ){

do pixels.shift();

}

return;

}


method void crawlLeft(bool increase_size){

var PixelNode last, new_pixel;

let last = pixels.last();

if( last.x() = 0){

return;

}

let new_pixel = PixelNode.new(last.x() - 1, last.y());

do pixels.push(new_pixel);


if( ~increase_size ){

do pixels.shift();

}

return;

}


method void crawlRight(bool increase_size){

var PixelNode last, new_pixel;

let last = pixels.last();

if( last.x() = 511 ){

return;

}

let new_pixel = PixelNode.new(last.x() + 1, last.y());

do pixels.push(new_pixel);


if( ~increase_size ){

do pixels.shift();

}

return;

}


method void crawlUp(bool increase_size){

var PixelNode last, new_pixel;

let last = pixels.last();

if( last.y() = 0){

return;

}

let new_pixel = PixelNode.new(last.x(), last.y() + -1);

do pixels.push(new_pixel);


if( ~increase_size ){

do pixels.shift();

}

return;

}


method PixelNode first(){

return pixels.first();

}


method LinkedList pixels(){

return pixels;

}

}


SnakeGame.jack

class SnakeGame {

field Snake snake;


constructor SnakeGame new() {

let snake = Snake.new(256, 128);

do Output.moveCursor(1, 13);

do Output.printString("Bienvenido al juego de la serpiente");

do Output.moveCursor(3, 10);

do Output.printString("Presione X para incrementar a la serpiente");

do Sys.wait(3000);

return this;

}


method void dispose(){

do snake.dispose();

do Memory.deAlloc(this);


return;

}


method void run(){

var char key;

var boolean exit, grow;

var int direction; // 0=ninguno, 1=arriba, 2=abajo, 3=izquierda, 4=derecha


let direction = 0;


while (~exit) {

// espera a que se presione una tecla

while (key = 0) {

let key = Keyboard.keyPressed();

do moveSnake(direction, grow);

do drawScreen();

}


if (key = 81) {

let exit = true;

}

if (key = 88) {

let grow = true;

}

if (key = 131) {

let direction = 1;

}

if (key = 133) {

let direction = 2;

}

if (key = 130) {

let direction = 3;

}

if (key = 132) {

let direction = 4;

}


// espera a que se suelte la tecla

while (~(key = 0)) {

let key = Keyboard.keyPressed();

do moveSnake(direction, grow);

do drawScreen();

let grow = false;

}

}

return;

}


method void moveSnake(int direction, bool grow) {

if (direction = 1) {

do snake.crawlUp(grow);

}

if (direction = 2) {

do snake.crawlDown(grow);

}

if (direction = 3) {

do snake.crawlLeft(grow);

}

if (direction = 4) {

do snake.crawlRight(grow);

}


do Sys.wait(5); // Tiempo de espera para el siguiente movimiento

return;

}


method void drawScreen(){

var PixelNode pix;


do Screen.clearScreen();

let pix = snake.first();


while( ~(pix = null) ){

do Screen.drawPixel(pix.x(), pix.y());

let pix = pix.next();

}

return;

}

}


Proyecto 10. Compiler I: Syntax Analysis

Desarrollo

Para el desarrollo del primer módulo del compilador (análisis de sintaxis) el cual tiene el trabajo de comprender la estructura de algún programa, tendremos que dividirlo en dos partes:

  • Tokenización (tokenizing): agrupación de caracteres de entrada en partes muy pequeñas de lenguaje.

  • El análisis (parsing): el intento de coincidir el flujo de esas partes pequeñas con las reglas de sintaxis del lenguaje.

Figura 1. Estructura del compilador Jack para el proyecto 10

Para que finalmente el analizador de sintaxis genere una estructura analizada del programa compilado por medio de un archivo .xml

Figura 2. Compilador Jack en acción

Pero para "comprender la estructura de algún programa" debemos utilizar el conjunto de reglas del lenguaje, llamadas gramática libre de contexto. Por lo que entender y analizar un programa significa determinar la correspondencia exacta entre el texto del programa y las reglas gramaticales, es por eso que para eso tenemos primero que transformar el texto del programa en una lista de tokens.

Un programa es simplemente una secuencia de caracteres, almacenada en un archivo. Es por eso que el primer paso para el desarrollo del analizador de sintaxis es agrupar los caracteres en tokens (según la sintaxis del lenguaje), o sea la tokenización o análisis léxico. Como resultado de la tokenización, en lugar de ver caracteres vemos sus partes más básicas, y el "flujo de tokens" se convierte en la entrada principal del compilador.

Figura 3. Análisis Léxico

Una vez que tenemos el análisis léxico de un programa en un flujo de tokens, tenemos que realizar la otra parte del analizador de sintaxis, analizar el flujo de tokens para "convertirlo" en un estructura más formal (parsing) o sea, descubrir cómo agrupar esos tokens en construcciones como declaraciones de variables, declaraciones de expresiones, etc. Para hacer esta agrupación y clasificación, se puede intentar coincidir el flujo de tokens en algún conjunto de reglas (gramática). Por ejemplo, la gramática de un lenguaje como Java nos permite combinar los elementos básicos como 100, count, and <= en la expresión count<=100 y asimismo permite determinar que esa expresión es una expresión válida en Java.

Figura 4. Gramática de C (izquierda) y segmento de código aceptado por esa gramática (derecha).

Figura 5. Gramática completa del lenguaje Jack

JackAnalyzer.py

from JackTokenizer import JackTokenizer

from CompilationEngine import CompilationEngine

import sys

import os

import glob



class JackAnalyzer():

# Genera el archivo de salida en el directorio correspondiente

@classmethod

def xml_output_file_for(cls, input_file):

file_name = os.path.basename(input_file).split(".")[0]

# generando el nombre del archivo de salida

return "/".join(input_file.split("/")[:-1]) + "/" + file_name + ".xml"


@classmethod

def run(cls, input_file, output_file):

tokenizer = JackTokenizer(input_file)

compiler = CompilationEngine(tokenizer, output_file)

compiler.compile_class()



if __name__ == "__main__" and len(sys.argv) == 2:

arg = sys.argv[1]

# Determina sí es un directorio o un archivo,

# y finalmente elige la ruta destino

if os.path.isfile(arg):

files = [arg]

elif os.path.isdir(arg):

jack_path = os.path.join(arg, "*.jack")

files = glob.glob(jack_path)


# create output directory - MAY NEED TO REMOVE


for input_file_name in files:

output_file_name = JackAnalyzer.xml_output_file_for(input_file_name)

output_file = open(output_file_name, 'w')

input_file = open(input_file_name, 'r')

JackAnalyzer.run(input_file, output_file)



JackTokenizer.py

class JackTokenizer():

# símbolos del lenguaje Jack

SYMBOL_CONVERSIONS = {

'<': '&lt;',

'>': '&gt;',

'\"': '&quot;',

'&': '&amp;'

}

COMMENT_OPERATORS = ["/", "*"]

KEYWORDS = [

'class',

'constructor',

'function',

'method',

'field',

'static',

'var',

'int',

'char',

'boolean',

'void',

'true',

'false',

'null',

'this',

'let',

'do',

'if',

'else',

'while',

'return'

]


# va a través de archivo .jack y produce una secuencia de tokens

# ignora los espacios en blanco y comentarios


def __init__(self, input_file):

self.input_file = input_file

self.tokens_found = []

self.current_token = None

self.next_token = None

self.has_more_tokens = True


# Avanza en la lectura del documento

def advance(self):

# Lee el primer carácter

char = self.input_file.read(1)


# en este loop salta todos loc comentarios y espacios en blanco

while char.isspace() or char in self.COMMENT_OPERATORS:

if char.isspace():

char = self.input_file.read(1)

elif char in self.COMMENT_OPERATORS:

# se asegura de que no haya ningún operador

last_pos = self.input_file.tell()

next_2_chars = self.input_file.read(2)

if not self._is_start_of_comment(char, next_2_chars):

# vuelve atrás

self.input_file.seek(last_pos)

break


# lee toda la linea

self.input_file.readline()

# lee el siguiente carácter

char = self.input_file.read(1)

continue


# encontramos el token

token = ""


if self._is_string_const_delimeter(char):

token += char

char = self.input_file.read(1)


while not self._is_string_const_delimeter(char):

token += char

char = self.input_file.read(1)

token += char

elif char.isalnum():

while self._is_alnum_or_underscore(char):

token += char

last_pos = self.input_file.tell()

char = self.input_file.read(1)


# retrocede un carácter que se adelantó

self.input_file.seek(last_pos)

else:

if char in self.SYMBOL_CONVERSIONS:

token = self.SYMBOL_CONVERSIONS[char]

else:

token = char


# establece los tokens

if self.current_token:

self.current_token = self.next_token

self.next_token = token

self.tokens_found.append(token)

else:

self.current_token = token

self.next_token = token

self.tokens_found.append(token)

# actualiza el siguiente token

self.advance()


if not len(self.next_token) > 0:

self.has_more_tokens = False

return False

else:

return True


def part_of_subroutine_call(self):

if len(self.tokens_found) < 3:

return False


index = len(self.tokens_found) - 4

token = self.tokens_found[index]


if token == ".":

return True

else:

return False


def current_token_type(self):

if self.current_token[0] == "\"":

return "STRING_CONST"

elif self.current_token in self.KEYWORDS:

return "KEYWORD"

elif self.current_token.isnumeric():

return "INT_CONST"

elif self.current_token.isalnum():

return "IDENTIFIER"

else:

return "SYMBOL"


def _is_alnum_or_underscore(self, char):

return char.isalnum() or char == "_"


def _is_string_const_delimeter(self, char):

return char == "\""


def _is_start_of_comment(self, char, next_2_chars):

# comentario de forma: // or */

single_line_comment = next_2_chars[0] == self.COMMENT_OPERATORS[0]

# comentario de la forma: /**

multi_line_comment = char == self.COMMENT_OPERATORS[0] and next_2_chars == "**"

# comentario de la forma: * comment

part_of_multi_line_comment = char == self.COMMENT_OPERATORS[1] and next_2_chars[0].isspace() and next_2_chars[

1] != '('

return single_line_comment or multi_line_comment or part_of_multi_line_comment



CompilationEngine.py

class CompilationEngine():

STARTING_TOKENS = {

'var_dec': ['var'],

'parameter_list': ['('],

'subroutine_body': ['{'],

'expression_list': ['('],

'expression': ['=', '[', '(']

}

TERMINATING_TOKENS = {

'class': ['}'],

'class_var_dec': [';'],

'subroutine': ['}'],

'parameter_list': [')'],

'expression_list': [')'],

'statements': ['}'],

'do': [';'],

'let': [';'],

'while': ['}'],

'if': ['}'],

'var_dec': [';'],

'return': [';'],

'expression': [';', ')', ']', ',']

}

SUBROUTINE_TOKENS = ["function", "method", "constructor"]

STATEMENT_TOKENS = ['do', 'let', 'while', 'return', 'if']

TERMINAL_KEYWORDS = ["boolean", "class", "void", "int"]

CLASS_VAR_DEC_TOKENS = ["static", "field"]

UNARY_OPERATORS = ['-', '~']

TERMINAL_TOKEN_TYPES = ["STRING_CONST", "INT_CONST", "IDENTIFIER", "SYMBOL"]

OPERATORS = [

'+',

'-',

'*',

'/',

'&amp;',

'|',

'&lt;',

'&gt;',

'='

]


def __init__(self, tokenizer, output_file):

self.tokenizer = tokenizer

self.output_file = output_file


# Compila una clase

def compile_class(self):

# lo básico para compilar una clase

self._write_current_outer_tag(body="class")


while self.tokenizer.has_more_tokens:

self.tokenizer.advance()


if self._terminal_token_type() or self._terminal_keyword():

self._write_current_terminal_token()

elif self.tokenizer.current_token in self.CLASS_VAR_DEC_TOKENS:

self.compile_class_var_dec()

elif self.tokenizer.current_token in self.SUBROUTINE_TOKENS:

self.compile_subroutine()


self._write_current_outer_tag(body="/class")


def compile_class_var_dec(self):

self._write_current_outer_tag(body="classVarDec")

self._write_current_terminal_token()


while self._not_terminal_token_for('class_var_dec'):

self.tokenizer.advance()

self._write_current_terminal_token()


self._write_current_outer_tag(body="/classVarDec")


# el encargado de compilar la rutina o función

def compile_subroutine(self):

self._write_current_outer_tag(body="subroutineDec")

self._write_current_terminal_token()


while self._not_terminal_token_for('subroutine'):

self.tokenizer.advance()


if self._starting_token_for('parameter_list'):

self.compile_parameter_list()

elif self._starting_token_for('subroutine_body'):

self.compile_subroutine_body()

else:

self._write_current_terminal_token()


self._write_current_outer_tag(body="/subroutineDec")


def compile_parameter_list(self):

# escribe el ( inicial

self._write_current_terminal_token()

self._write_current_outer_tag(body="parameterList")


while self._not_terminal_token_for(position='next', keyword_token='parameter_list'):

self.tokenizer.advance()

self._write_current_terminal_token()


self._write_current_outer_tag(body="/parameterList")

# avanza hasta cerrar el )

self.tokenizer.advance()

self._write_current_terminal_token()


def compile_subroutine_body(self):


self._write_current_outer_tag(body="subroutineBody")

# escribimos la llave inicial {

self._write_current_terminal_token()


while self._not_terminal_token_for('subroutine'):

self.tokenizer.advance()


if self._starting_token_for('var_dec'):

self.compile_var_dec()

elif self._statement_token():

self.compile_statements()

else:

self._write_current_terminal_token()


# cerramos la llave }

self._write_current_terminal_token()

self._write_current_outer_tag(body="/subroutineBody")


def compile_var_dec(self):


self._write_current_outer_tag(body="varDec")

self._write_current_terminal_token()


while self._not_terminal_token_for('var_dec'):

self.tokenizer.advance()

self._write_current_terminal_token()


self._write_current_outer_tag(body="/varDec")


def compile_statements(self):


self._write_current_outer_tag(body="statements")


while self._not_terminal_token_for('subroutine'):

if self.tokenizer.current_token == "if":

self.compile_if()

elif self.tokenizer.current_token == "do":

self.compile_do()

elif self.tokenizer.current_token == "let":

self.compile_let()

elif self.tokenizer.current_token == "while":

self.compile_while()

elif self.tokenizer.current_token == "return":

self.compile_return()


self.tokenizer.advance()


self._write_current_outer_tag(body="/statements")


def compile_statement_body(self, not_terminate_func, condition_func, do_something_special_func):

while not_terminate_func():

self.tokenizer.advance()


if condition_func():

do_something_special_func()

else:

self._write_current_terminal_token()


def compile_do(self):

self._write_current_outer_tag(body="doStatement")

self._write_current_terminal_token()


# experimental

def do_terminator_func():

return self._not_terminal_token_for('do')


def do_condition_func():

return self._starting_token_for('expression_list')


def do_do_something_func():

return self.compile_expression_list()


self.compile_statement_body(do_terminator_func, do_condition_func, do_do_something_func)


self._write_current_outer_tag(body="/doStatement")


def compile_let(self):

self._write_current_outer_tag(body="letStatement")

self._write_current_terminal_token()


while self._not_terminal_token_for('let'):

self.tokenizer.advance()


if self._starting_token_for('expression'):

self.compile_expression()

else:

self._write_current_terminal_token()


self._write_current_outer_tag(body="/letStatement")


def compile_while(self):

self._write_current_outer_tag(body="whileStatement")

# escribe

self._write_current_terminal_token()


# avanza hasta el comienzo del (

self.tokenizer.advance()


# compila lo que haya dentro de los paréntesis ()

self.compile_expression()


while self._not_terminal_token_for('while'):

self.tokenizer.advance()


if self._statement_token():

self.compile_statements()

else:

self._write_current_terminal_token()

# escribe el token del terminal

self._write_current_terminal_token()


self._write_current_outer_tag(body="/whileStatement")


def compile_if(self):

self._write_current_outer_tag(body="ifStatement")

# escribe el if

self._write_current_terminal_token()


# avanza hasta el comienzo de la expresión

self.tokenizer.advance()


# compila la expresión dentro de los paréntesis ()

self.compile_expression()


def not_terminate_func():

return self._not_terminal_token_for('if')


def condition_func():

return self._statement_token()


def do_something_special_func():

return self.compile_statements()


self.compile_statement_body(not_terminate_func, condition_func, do_something_special_func)


# compila el else

if self.tokenizer.next_token == "else":

self._write_current_terminal_token()

self.tokenizer.advance()

# escribe el else

self._write_current_terminal_token()

# lo mismo que arriba

self.compile_statement_body(

not_terminate_func,

condition_func,

do_something_special_func

)


# escribe el token del terminal

self._write_current_terminal_token()

self._write_current_outer_tag(body="/ifStatement")


def compile_expression(self):

self._write_current_terminal_token()

self._write_current_outer_tag(body="expression")


if self._starting_token_for('expression') and self._next_token_is_negative_unary_operator():

unary_negative_token = True

else:

unary_negative_token = False

self.tokenizer.advance()


while self._not_terminal_token_for('expression'):

if self._operator_token() and not unary_negative_token:

self._write_current_terminal_token()

self.tokenizer.advance()

else:

self.compile_term()


self._write_current_outer_tag(body="/expression")

self._write_current_terminal_token()


def compile_expression_in_expression_list(self):

self._write_current_outer_tag(body="expression")


# (

while self._not_terminal_token_for('expression'):

if self._operator_token():

self._write_current_terminal_token()

self.tokenizer.advance()

else:

self.compile_term()

# termina.


self._write_current_outer_tag(body="/expression")


# (expresión (',' expresión)* )?

def compile_expression_list(self):

# Escribe (

self._write_current_terminal_token()

self._write_current_outer_tag(body="expressionList")


# se salta el ( inicial

self.tokenizer.advance()


while self._not_terminal_token_for('expression_list'):

self.compile_expression_in_expression_list()

if self._another_expression_coming():

self._write_current_terminal_token()

self.tokenizer.advance()


self._write_current_outer_tag(body="/expressionList")

# escribe )

self._write_current_terminal_token()


def compile_term(self):

self._write_current_outer_tag(body="term")


while self._not_terminal_condition_for_term():

if self.tokenizer.part_of_subroutine_call():

self.compile_expression_list()

elif self._starting_token_for('expression'):

self.compile_expression()

elif self.tokenizer.current_token in self.UNARY_OPERATORS:

self._write_current_terminal_token()


if self._starting_token_for(keyword_token='expression', position='next'):

self.tokenizer.advance()

self.compile_term()

break

else:

self.tokenizer.advance()

# escribe el término interno

self._write_current_outer_tag(body="term")

self._write_current_terminal_token()

self._write_current_outer_tag(body="/term")

else:

self._write_current_terminal_token()


# es decir ., i *

if self._next_token_is_operation_not_in_expression():

self.tokenizer.advance()

break


self.tokenizer.advance()


self._write_current_outer_tag(body="/term")


def compile_return(self):

self._write_current_outer_tag(body="returnStatement")


if self._not_terminal_token_for(keyword_token='return', position='next'):

self.compile_expression()

else: # escribe return y ;

self._write_current_terminal_token()

self.tokenizer.advance()

self._write_current_terminal_token()


self._write_current_outer_tag(body="/returnStatement")


def _write_current_outer_tag(self, body):

self.output_file.write("<{}>\n".format(body))


def _write_current_terminal_token(self):

# conforme al xml esperado

if self.tokenizer.current_token_type() == "STRING_CONST":

tag_name = "stringConstant"

elif self.tokenizer.current_token_type() == "INT_CONST":

tag_name = "integerConstant"

else:

tag_name = self.tokenizer.current_token_type().lower()


if self.tokenizer.current_token_type() == "STRING_CONST":

value = self.tokenizer.current_token.replace("\"", "")

else:

value = self.tokenizer.current_token


self.output_file.write(

"<{}> {} </{}>\n".format(

tag_name,

value,

tag_name

)

)


def _terminal_token_type(self):

return self.tokenizer.current_token_type() in self.TERMINAL_TOKEN_TYPES


def _terminal_keyword(self):

return self.tokenizer.current_token in self.TERMINAL_KEYWORDS


def _not_terminal_token_for(self, keyword_token, position='current'):

if position == 'current':

return not self.tokenizer.current_token in self.TERMINATING_TOKENS[keyword_token]

elif position == 'next':

return not self.tokenizer.next_token in self.TERMINATING_TOKENS[keyword_token]


def _starting_token_for(self, keyword_token, position='current'):

if position == 'current':

return self.tokenizer.current_token in self.STARTING_TOKENS[keyword_token]

elif position == 'next':

return self.tokenizer.next_token in self.STARTING_TOKENS[keyword_token]


def _statement_token(self):

return self.tokenizer.current_token in self.STATEMENT_TOKENS


def _operator_token(self, position='current'):

if position == 'current':

return self.tokenizer.current_token in self.OPERATORS

elif position == 'next':

return self.tokenizer.next_token in self.OPERATORS


def _next_token_is_negative_unary_operator(self):

return self.tokenizer.next_token == "-"


def _another_expression_coming(self):

return self.tokenizer.current_token == ","


def _not_terminal_condition_for_term(self):

# la expresión cubre todas las bases

return self._not_terminal_token_for('expression')


def _next_token_is_operation_not_in_expression(self):

return self._operator_token(position='next') and not self._starting_token_for('expression')



Testeo con programas y resultados

El trabajo del analizador de sintaxis es analizar programas escritos en el lenguaje Jack. Por lo tanto, para probar nuestro analizador debemos hacer que analice varios programas Jack suministrados, Square Dance y Array Test. El primero incluye todas las características del lenguaje Jack excepto el procesamiento de matrices, que aparece en el segundo.

Para cada uno de los tres programas, se nos proporciona todos los archivos fuente .Jack que componen el programa. Para cada uno de estos archivos Xxx.jack, nos suministran dos archivos de comparación llamados XxxT.xml y Xxx.xml. Estos archivos contienen, respectivamente, la salida que debe producir un tokenizador y un analizador (parser) aplicado al programa . Jack.

Pruebas para el tokenizador:

  • Para probar el tokenizador utilizaremos los programas Square Dance y TestArray.

  • Luego aplicaremos el tokenizador a cada archivo de clase en los programas de prueba generado los archivos GXxx.xml. Luego usando el programa suministrado por nand2tetris TextComparer compararemos la salida generada con los archivos de comparación .xml suministrados.

Pruebas del analizador (parser):

  • Utilizaremos el analizador de sintaxis con los programas de prueba mencionados para las pruebas del tokenizador para luego usar el TextComparer suministrado para comparar la salida generada (GXxx.xml) con los archivos de comparación .xml suministrados.

Square Dance

Square Dance es un juego interactivo trivial que permite mover un cuadrado negro alrededor de la pantalla usando las cuatro teclas de flecha del teclado.

GMainT.xml

<tokens>

<keyword> class </keyword>

<identifier> Main </identifier>

<symbol> { </symbol>

<keyword> static </keyword>

<keyword> boolean </keyword>

<identifier> test </identifier>

<symbol> ; </symbol>

<keyword> function </keyword>

<keyword> void </keyword>

<identifier> main </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> var </keyword>

<identifier> SquareGame </identifier>

<identifier> game </identifier>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> game </identifier>

<symbol> = </symbol>

<identifier> SquareGame </identifier>

<symbol> . </symbol>

<identifier> new </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> game </identifier>

<symbol> . </symbol>

<identifier> run </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> game </identifier>

<symbol> . </symbol>

<identifier> dispose </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> function </keyword>

<keyword> void </keyword>

<identifier> more </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> var </keyword>

<keyword> int </keyword>

<identifier> i </identifier>

<symbol> , </symbol>

<identifier> j </identifier>

<symbol> ; </symbol>

<keyword> var </keyword>

<identifier> String </identifier>

<identifier> s </identifier>

<symbol> ; </symbol>

<keyword> var </keyword>

<identifier> Array </identifier>

<identifier> a </identifier>

<symbol> ; </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<keyword> false </keyword>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> let </keyword>

<identifier> s </identifier>

<symbol> = </symbol>

<stringConstant> string constant </stringConstant>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> s </identifier>

<symbol> = </symbol>

<keyword> null </keyword>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> a </identifier>

<symbol> [ </symbol>

<integerConstant> 1 </integerConstant>

<symbol> ] </symbol>

<symbol> = </symbol>

<identifier> a </identifier>

<symbol> [ </symbol>

<integerConstant> 2 </integerConstant>

<symbol> ] </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> else </keyword>

<symbol> { </symbol>

<keyword> let </keyword>

<identifier> i </identifier>

<symbol> = </symbol>

<identifier> i </identifier>

<symbol> * </symbol>

<symbol> ( </symbol>

<symbol> - </symbol>

<identifier> j </identifier>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> j </identifier>

<symbol> = </symbol>

<identifier> j </identifier>

<symbol> / </symbol>

<symbol> ( </symbol>

<symbol> - </symbol>

<integerConstant> 2 </integerConstant>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> i </identifier>

<symbol> = </symbol>

<identifier> i </identifier>

<symbol> | </symbol>

<identifier> j </identifier>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<symbol> } </symbol>

</tokens>



Resultados obtenidos

Inicializa e inicia una nueva sesión de "square dance".

GMain.xml

<class>

<keyword> class </keyword>

<identifier> Main </identifier>

<symbol> { </symbol>

<classVarDec>

<keyword> static </keyword>

<keyword> boolean </keyword>

<identifier> test </identifier>

<symbol> ; </symbol>

</classVarDec>

<subroutineDec>

<keyword> function </keyword>

<keyword> void </keyword>

<identifier> main </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<varDec>

<keyword> var </keyword>

<identifier> SquareGame </identifier>

<identifier> game </identifier>

<symbol> ; </symbol>

</varDec>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> game </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> SquareGame </identifier>

<symbol> . </symbol>

<identifier> new </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<doStatement>

<keyword> do </keyword>

<identifier> game </identifier>

<symbol> . </symbol>

<identifier> run </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<doStatement>

<keyword> do </keyword>

<identifier> game </identifier>

<symbol> . </symbol>

<identifier> dispose </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<subroutineDec>

<keyword> function </keyword>

<keyword> void </keyword>

<identifier> more </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<varDec>

<keyword> var </keyword>

<keyword> int </keyword>

<identifier> i </identifier>

<symbol> , </symbol>

<identifier> j </identifier>

<symbol> ; </symbol>

</varDec>

<varDec>

<keyword> var </keyword>

<identifier> String </identifier>

<identifier> s </identifier>

<symbol> ; </symbol>

</varDec>

<varDec>

<keyword> var </keyword>

<identifier> Array </identifier>

<identifier> a </identifier>

<symbol> ; </symbol>

</varDec>

<statements>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<keyword> false </keyword>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> s </identifier>

<symbol> = </symbol>

<expression>

<term>

<stringConstant> string constant </stringConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<letStatement>

<keyword> let </keyword>

<identifier> s </identifier>

<symbol> = </symbol>

<expression>

<term>

<keyword> null </keyword>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<letStatement>

<keyword> let </keyword>

<identifier> a </identifier>

<symbol> [ </symbol>

<expression>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

<symbol> ] </symbol>

<symbol> = </symbol>

<expression>

<term>

<identifier> a </identifier>

<symbol> [ </symbol>

<expression>

<term>

<integerConstant> 2 </integerConstant>

</term>

</expression>

<symbol> ] </symbol>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

</statements>

<symbol> } </symbol>

<keyword> else </keyword>

<symbol> { </symbol>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> i </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> i </identifier>

</term>

<symbol> * </symbol>

<term>

<symbol> ( </symbol>

<expression>

<term>

<symbol> - </symbol>

<term>

<identifier> j </identifier>

</term>

</term>

</expression>

<symbol> ) </symbol>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<letStatement>

<keyword> let </keyword>

<identifier> j </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> j </identifier>

</term>

<symbol> / </symbol>

<term>

<symbol> ( </symbol>

<expression>

<term>

<symbol> - </symbol>

<term>

<integerConstant> 2 </integerConstant>

</term>

</term>

</expression>

<symbol> ) </symbol>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<letStatement>

<keyword> let </keyword>

<identifier> i </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> i </identifier>

</term>

<symbol> | </symbol>

<term>

<identifier> j </identifier>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<symbol> } </symbol>

</class>



Resultados obtenidos

Inicializa e inicia una nueva sesión de "square dance".

GSquareT.xml

<tokens>

<keyword> class </keyword>

<identifier> Square </identifier>

<symbol> { </symbol>

<keyword> field </keyword>

<keyword> int </keyword>

<identifier> x </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> ; </symbol>

<keyword> field </keyword>

<keyword> int </keyword>

<identifier> size </identifier>

<symbol> ; </symbol>

<keyword> constructor </keyword>

<identifier> Square </identifier>

<identifier> new </identifier>

<symbol> ( </symbol>

<keyword> int </keyword>

<identifier> Ax </identifier>

<symbol> , </symbol>

<keyword> int </keyword>

<identifier> Ay </identifier>

<symbol> , </symbol>

<keyword> int </keyword>

<identifier> Asize </identifier>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> let </keyword>

<identifier> x </identifier>

<symbol> = </symbol>

<identifier> Ax </identifier>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> y </identifier>

<symbol> = </symbol>

<identifier> Ay </identifier>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> size </identifier>

<symbol> = </symbol>

<identifier> Asize </identifier>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> draw </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> return </keyword>

<keyword> this </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> dispose </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> Memory </identifier>

<symbol> . </symbol>

<identifier> deAlloc </identifier>

<symbol> ( </symbol>

<keyword> this </keyword>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> draw </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<keyword> true </keyword>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<identifier> x </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> , </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> erase </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<keyword> false </keyword>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<identifier> x </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> , </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> incSize </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<symbol> ( </symbol>

<symbol> ( </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> &lt; </symbol>

<integerConstant> 254 </integerConstant>

<symbol> ) </symbol>

<symbol> &amp; </symbol>

<symbol> ( </symbol>

<symbol> ( </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> &lt; </symbol>

<integerConstant> 510 </integerConstant>

<symbol> ) </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> erase </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> size </identifier>

<symbol> = </symbol>

<identifier> size </identifier>

<symbol> + </symbol>

<integerConstant> 2 </integerConstant>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> draw </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> decSize </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<identifier> size </identifier>

<symbol> &gt; </symbol>

<integerConstant> 2 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> erase </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> size </identifier>

<symbol> = </symbol>

<identifier> size </identifier>

<symbol> - </symbol>

<integerConstant> 2 </integerConstant>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> draw </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> moveUp </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<identifier> y </identifier>

<symbol> &gt; </symbol>

<integerConstant> 1 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<keyword> false </keyword>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<identifier> x </identifier>

<symbol> , </symbol>

<symbol> ( </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> - </symbol>

<integerConstant> 1 </integerConstant>

<symbol> , </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> y </identifier>

<symbol> = </symbol>

<identifier> y </identifier>

<symbol> - </symbol>

<integerConstant> 2 </integerConstant>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<keyword> true </keyword>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<identifier> x </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> , </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<integerConstant> 1 </integerConstant>

<symbol> ) </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> moveDown </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<symbol> ( </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> &lt; </symbol>

<integerConstant> 254 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<keyword> false </keyword>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<identifier> x </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> , </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<integerConstant> 1 </integerConstant>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> y </identifier>

<symbol> = </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<integerConstant> 2 </integerConstant>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<keyword> true </keyword>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<identifier> x </identifier>

<symbol> , </symbol>

<symbol> ( </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> - </symbol>

<integerConstant> 1 </integerConstant>

<symbol> , </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> moveLeft </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<identifier> x </identifier>

<symbol> &gt; </symbol>

<integerConstant> 1 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<keyword> false </keyword>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<symbol> ( </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> - </symbol>

<integerConstant> 1 </integerConstant>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> , </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> x </identifier>

<symbol> = </symbol>

<identifier> x </identifier>

<symbol> - </symbol>

<integerConstant> 2 </integerConstant>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<keyword> true </keyword>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<identifier> x </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> , </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<integerConstant> 1 </integerConstant>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> moveRight </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<symbol> ( </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> &lt; </symbol>

<integerConstant> 510 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<keyword> false </keyword>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<identifier> x </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> , </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<integerConstant> 1 </integerConstant>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> x </identifier>

<symbol> = </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<integerConstant> 2 </integerConstant>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<keyword> true </keyword>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<symbol> ( </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> - </symbol>

<integerConstant> 1 </integerConstant>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> , </symbol>

<identifier> x </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> + </symbol>

<identifier> size </identifier>

<symbol> ) </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<symbol> } </symbol>

</tokens>



Resultados obtenidos

Implementa un cuadrado animado. Un objeto cuadrado tiene una ubicación en la pantalla y propiedades de tamaño, y métodos para dibujar, borrar, mover y cambiar el tamaño.

GSquare.xml

<class>

<keyword> class </keyword>

<identifier> Square </identifier>

<symbol> { </symbol>

<classVarDec>

<keyword> field </keyword>

<keyword> int </keyword>

<identifier> x </identifier>

<symbol> , </symbol>

<identifier> y </identifier>

<symbol> ; </symbol>

</classVarDec>

<classVarDec>

<keyword> field </keyword>

<keyword> int </keyword>

<identifier> size </identifier>

<symbol> ; </symbol>

</classVarDec>

<subroutineDec>

<keyword> constructor </keyword>

<identifier> Square </identifier>

<identifier> new </identifier>

<symbol> ( </symbol>

<parameterList>

<keyword> int </keyword>

<identifier> Ax </identifier>

<symbol> , </symbol>

<keyword> int </keyword>

<identifier> Ay </identifier>

<symbol> , </symbol>

<keyword> int </keyword>

<identifier> Asize </identifier>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> x </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> Ax </identifier>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<letStatement>

<keyword> let </keyword>

<identifier> y </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> Ay </identifier>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<letStatement>

<keyword> let </keyword>

<identifier> size </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> Asize </identifier>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<doStatement>

<keyword> do </keyword>

<identifier> draw </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<returnStatement>

<keyword> return </keyword>

<expression>

<term>

<keyword> this </keyword>

</term>

</expression>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<subroutineDec>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> dispose </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> Memory </identifier>

<symbol> . </symbol>

<identifier> deAlloc </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<keyword> this </keyword>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<subroutineDec>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> draw </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<keyword> true </keyword>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<identifier> x </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<subroutineDec>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> erase </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<keyword> false </keyword>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<identifier> x </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<subroutineDec>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> incSize </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<statements>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<symbol> ( </symbol>

<expression>

<term>

<symbol> ( </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> ) </symbol>

</term>

<symbol> &lt; </symbol>

<term>

<integerConstant> 254 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

</term>

<symbol> &amp; </symbol>

<term>

<symbol> ( </symbol>

<expression>

<term>

<symbol> ( </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> ) </symbol>

</term>

<symbol> &lt; </symbol>

<term>

<integerConstant> 510 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> erase </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<letStatement>

<keyword> let </keyword>

<identifier> size </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> size </identifier>

</term>

<symbol> + </symbol>

<term>

<integerConstant> 2 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<doStatement>

<keyword> do </keyword>

<identifier> draw </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<subroutineDec>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> decSize </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<statements>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> size </identifier>

</term>

<symbol> &gt; </symbol>

<term>

<integerConstant> 2 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> erase </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<letStatement>

<keyword> let </keyword>

<identifier> size </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> size </identifier>

</term>

<symbol> - </symbol>

<term>

<integerConstant> 2 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<doStatement>

<keyword> do </keyword>

<identifier> draw </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<subroutineDec>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> moveUp </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<statements>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> &gt; </symbol>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<keyword> false </keyword>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<identifier> x </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<symbol> ( </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> ) </symbol>

</term>

<symbol> - </symbol>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<letStatement>

<keyword> let </keyword>

<identifier> y </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> - </symbol>

<term>

<integerConstant> 2 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<keyword> true </keyword>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<identifier> x </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<subroutineDec>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> moveDown </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<statements>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<symbol> ( </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> ) </symbol>

</term>

<symbol> &lt; </symbol>

<term>

<integerConstant> 254 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<keyword> false </keyword>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<identifier> x </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<letStatement>

<keyword> let </keyword>

<identifier> y </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<integerConstant> 2 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<keyword> true </keyword>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<identifier> x </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<symbol> ( </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> ) </symbol>

</term>

<symbol> - </symbol>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<subroutineDec>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> moveLeft </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<statements>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> &gt; </symbol>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<keyword> false </keyword>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<symbol> ( </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> ) </symbol>

</term>

<symbol> - </symbol>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<letStatement>

<keyword> let </keyword>

<identifier> x </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> - </symbol>

<term>

<integerConstant> 2 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<keyword> true </keyword>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<identifier> x </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<subroutineDec>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> moveRight </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<statements>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<symbol> ( </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> ) </symbol>

</term>

<symbol> &lt; </symbol>

<term>

<integerConstant> 510 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<keyword> false </keyword>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<identifier> x </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<letStatement>

<keyword> let </keyword>

<identifier> x </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<integerConstant> 2 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> setColor </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<keyword> true </keyword>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Screen </identifier>

<symbol> . </symbol>

<identifier> drawRectangle </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<symbol> ( </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> ) </symbol>

</term>

<symbol> - </symbol>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> x </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<identifier> y </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> size </identifier>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<symbol> } </symbol>

</class>




Resultados obtenidos

Implementa un cuadrado animado. Un objeto cuadrado tiene una ubicación en la pantalla y propiedades de tamaño, y métodos para dibujar, borrar, mover y cambiar el tamaño.

GSquareGameT.xml

<tokens>

<keyword> class </keyword>

<identifier> SquareGame </identifier>

<symbol> { </symbol>

<keyword> field </keyword>

<identifier> Square </identifier>

<identifier> square </identifier>

<symbol> ; </symbol>

<keyword> field </keyword>

<keyword> int </keyword>

<identifier> direction </identifier>

<symbol> ; </symbol>

<keyword> constructor </keyword>

<identifier> SquareGame </identifier>

<identifier> new </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> let </keyword>

<identifier> square </identifier>

<symbol> = </symbol>

<identifier> Square </identifier>

<symbol> . </symbol>

<identifier> new </identifier>

<symbol> ( </symbol>

<integerConstant> 0 </integerConstant>

<symbol> , </symbol>

<integerConstant> 0 </integerConstant>

<symbol> , </symbol>

<integerConstant> 30 </integerConstant>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> direction </identifier>

<symbol> = </symbol>

<integerConstant> 0 </integerConstant>

<symbol> ; </symbol>

<keyword> return </keyword>

<keyword> this </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> dispose </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> square </identifier>

<symbol> . </symbol>

<identifier> dispose </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Memory </identifier>

<symbol> . </symbol>

<identifier> deAlloc </identifier>

<symbol> ( </symbol>

<keyword> this </keyword>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> moveSquare </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<identifier> direction </identifier>

<symbol> = </symbol>

<integerConstant> 1 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> square </identifier>

<symbol> . </symbol>

<identifier> moveUp </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<identifier> direction </identifier>

<symbol> = </symbol>

<integerConstant> 2 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> square </identifier>

<symbol> . </symbol>

<identifier> moveDown </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<identifier> direction </identifier>

<symbol> = </symbol>

<integerConstant> 3 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> square </identifier>

<symbol> . </symbol>

<identifier> moveLeft </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<identifier> direction </identifier>

<symbol> = </symbol>

<integerConstant> 4 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> square </identifier>

<symbol> . </symbol>

<identifier> moveRight </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> do </keyword>

<identifier> Sys </identifier>

<symbol> . </symbol>

<identifier> wait </identifier>

<symbol> ( </symbol>

<integerConstant> 5 </integerConstant>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> run </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> var </keyword>

<keyword> char </keyword>

<identifier> key </identifier>

<symbol> ; </symbol>

<keyword> var </keyword>

<keyword> boolean </keyword>

<identifier> exit </identifier>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> exit </identifier>

<symbol> = </symbol>

<keyword> false </keyword>

<symbol> ; </symbol>

<keyword> while </keyword>

<symbol> ( </symbol>

<symbol> ~ </symbol>

<identifier> exit </identifier>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> while </keyword>

<symbol> ( </symbol>

<identifier> key </identifier>

<symbol> = </symbol>

<integerConstant> 0 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> let </keyword>

<identifier> key </identifier>

<symbol> = </symbol>

<identifier> Keyboard </identifier>

<symbol> . </symbol>

<identifier> keyPressed </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> moveSquare </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<identifier> key </identifier>

<symbol> = </symbol>

<integerConstant> 81 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> let </keyword>

<identifier> exit </identifier>

<symbol> = </symbol>

<keyword> true </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<identifier> key </identifier>

<symbol> = </symbol>

<integerConstant> 90 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> square </identifier>

<symbol> . </symbol>

<identifier> decSize </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<identifier> key </identifier>

<symbol> = </symbol>

<integerConstant> 88 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> do </keyword>

<identifier> square </identifier>

<symbol> . </symbol>

<identifier> incSize </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<identifier> key </identifier>

<symbol> = </symbol>

<integerConstant> 131 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> let </keyword>

<identifier> direction </identifier>

<symbol> = </symbol>

<integerConstant> 1 </integerConstant>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<identifier> key </identifier>

<symbol> = </symbol>

<integerConstant> 133 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> let </keyword>

<identifier> direction </identifier>

<symbol> = </symbol>

<integerConstant> 2 </integerConstant>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<identifier> key </identifier>

<symbol> = </symbol>

<integerConstant> 130 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> let </keyword>

<identifier> direction </identifier>

<symbol> = </symbol>

<integerConstant> 3 </integerConstant>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> if </keyword>

<symbol> ( </symbol>

<identifier> key </identifier>

<symbol> = </symbol>

<integerConstant> 132 </integerConstant>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> let </keyword>

<identifier> direction </identifier>

<symbol> = </symbol>

<integerConstant> 4 </integerConstant>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> while </keyword>

<symbol> ( </symbol>

<symbol> ~ </symbol>

<symbol> ( </symbol>

<identifier> key </identifier>

<symbol> = </symbol>

<integerConstant> 0 </integerConstant>

<symbol> ) </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> let </keyword>

<identifier> key </identifier>

<symbol> = </symbol>

<identifier> Keyboard </identifier>

<symbol> . </symbol>

<identifier> keyPressed </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> moveSquare </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<symbol> } </symbol>

<symbol> } </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<symbol> } </symbol>

</tokens>



Resultados obtenidos

Dirige el show del juego de acuerdo con las reglas.

GSquareGame.xml

<class>

<keyword> class </keyword>

<identifier> SquareGame </identifier>

<symbol> { </symbol>

<classVarDec>

<keyword> field </keyword>

<identifier> Square </identifier>

<identifier> square </identifier>

<symbol> ; </symbol>

</classVarDec>

<classVarDec>

<keyword> field </keyword>

<keyword> int </keyword>

<identifier> direction </identifier>

<symbol> ; </symbol>

</classVarDec>

<subroutineDec>

<keyword> constructor </keyword>

<identifier> SquareGame </identifier>

<identifier> new </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> square </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> Square </identifier>

<symbol> . </symbol>

<identifier> new </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<integerConstant> 0 </integerConstant>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<integerConstant> 0 </integerConstant>

</term>

</expression>

<symbol> , </symbol>

<expression>

<term>

<integerConstant> 30 </integerConstant>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<letStatement>

<keyword> let </keyword>

<identifier> direction </identifier>

<symbol> = </symbol>

<expression>

<term>

<integerConstant> 0 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<returnStatement>

<keyword> return </keyword>

<expression>

<term>

<keyword> this </keyword>

</term>

</expression>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<subroutineDec>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> dispose </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> square </identifier>

<symbol> . </symbol>

<identifier> dispose </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Memory </identifier>

<symbol> . </symbol>

<identifier> deAlloc </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<keyword> this </keyword>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<subroutineDec>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> moveSquare </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<statements>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> direction </identifier>

</term>

<symbol> = </symbol>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> square </identifier>

<symbol> . </symbol>

<identifier> moveUp </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> direction </identifier>

</term>

<symbol> = </symbol>

<term>

<integerConstant> 2 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> square </identifier>

<symbol> . </symbol>

<identifier> moveDown </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> direction </identifier>

</term>

<symbol> = </symbol>

<term>

<integerConstant> 3 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> square </identifier>

<symbol> . </symbol>

<identifier> moveLeft </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> direction </identifier>

</term>

<symbol> = </symbol>

<term>

<integerConstant> 4 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> square </identifier>

<symbol> . </symbol>

<identifier> moveRight </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Sys </identifier>

<symbol> . </symbol>

<identifier> wait </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<integerConstant> 5 </integerConstant>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<subroutineDec>

<keyword> method </keyword>

<keyword> void </keyword>

<identifier> run </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<varDec>

<keyword> var </keyword>

<keyword> char </keyword>

<identifier> key </identifier>

<symbol> ; </symbol>

</varDec>

<varDec>

<keyword> var </keyword>

<keyword> boolean </keyword>

<identifier> exit </identifier>

<symbol> ; </symbol>

</varDec>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> exit </identifier>

<symbol> = </symbol>

<expression>

<term>

<keyword> false </keyword>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<whileStatement>

<keyword> while </keyword>

<symbol> ( </symbol>

<expression>

<term>

<symbol> ~ </symbol>

<term>

<identifier> exit </identifier>

</term>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<whileStatement>

<keyword> while </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> key </identifier>

</term>

<symbol> = </symbol>

<term>

<integerConstant> 0 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> key </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> Keyboard </identifier>

<symbol> . </symbol>

<identifier> keyPressed </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<doStatement>

<keyword> do </keyword>

<identifier> moveSquare </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

</statements>

<symbol> } </symbol>

</whileStatement>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> key </identifier>

</term>

<symbol> = </symbol>

<term>

<integerConstant> 81 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> exit </identifier>

<symbol> = </symbol>

<expression>

<term>

<keyword> true </keyword>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> key </identifier>

</term>

<symbol> = </symbol>

<term>

<integerConstant> 90 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> square </identifier>

<symbol> . </symbol>

<identifier> decSize </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> key </identifier>

</term>

<symbol> = </symbol>

<term>

<integerConstant> 88 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<doStatement>

<keyword> do </keyword>

<identifier> square </identifier>

<symbol> . </symbol>

<identifier> incSize </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> key </identifier>

</term>

<symbol> = </symbol>

<term>

<integerConstant> 131 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> direction </identifier>

<symbol> = </symbol>

<expression>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> key </identifier>

</term>

<symbol> = </symbol>

<term>

<integerConstant> 133 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> direction </identifier>

<symbol> = </symbol>

<expression>

<term>

<integerConstant> 2 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> key </identifier>

</term>

<symbol> = </symbol>

<term>

<integerConstant> 130 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> direction </identifier>

<symbol> = </symbol>

<expression>

<term>

<integerConstant> 3 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<ifStatement>

<keyword> if </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> key </identifier>

</term>

<symbol> = </symbol>

<term>

<integerConstant> 132 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> direction </identifier>

<symbol> = </symbol>

<expression>

<term>

<integerConstant> 4 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

</statements>

<symbol> } </symbol>

</ifStatement>

<whileStatement>

<keyword> while </keyword>

<symbol> ( </symbol>

<expression>

<term>

<symbol> ~ </symbol>

<term>

<symbol> ( </symbol>

<expression>

<term>

<identifier> key </identifier>

</term>

<symbol> = </symbol>

<term>

<integerConstant> 0 </integerConstant>

</term>

</expression>

<symbol> ) </symbol>

</term>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> key </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> Keyboard </identifier>

<symbol> . </symbol>

<identifier> keyPressed </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<doStatement>

<keyword> do </keyword>

<identifier> moveSquare </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

</statements>

<symbol> } </symbol>

</whileStatement>

</statements>

<symbol> } </symbol>

</whileStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<symbol> } </symbol>

</class>



Resultados obtenidos

Dirige el show del juego de acuerdo con las reglas.

Array Test

Un programa Jack de clase única diseñado para probar cómo el analizador de sintaxis maneja el procesamiento de matrices

GMainT.xml

<tokens>

<keyword> class </keyword>

<identifier> Main </identifier>

<symbol> { </symbol>

<keyword> function </keyword>

<keyword> void </keyword>

<identifier> main </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> var </keyword>

<identifier> Array </identifier>

<identifier> a </identifier>

<symbol> ; </symbol>

<keyword> var </keyword>

<keyword> int </keyword>

<identifier> length </identifier>

<symbol> ; </symbol>

<keyword> var </keyword>

<keyword> int </keyword>

<identifier> i </identifier>

<symbol> , </symbol>

<identifier> sum </identifier>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> length </identifier>

<symbol> = </symbol>

<identifier> Keyboard </identifier>

<symbol> . </symbol>

<identifier> readInt </identifier>

<symbol> ( </symbol>

<stringConstant> HOW MANY NUMBERS? </stringConstant>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> a </identifier>

<symbol> = </symbol>

<identifier> Array </identifier>

<symbol> . </symbol>

<identifier> new </identifier>

<symbol> ( </symbol>

<identifier> length </identifier>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> i </identifier>

<symbol> = </symbol>

<integerConstant> 0 </integerConstant>

<symbol> ; </symbol>

<keyword> while </keyword>

<symbol> ( </symbol>

<identifier> i </identifier>

<symbol> &lt; </symbol>

<identifier> length </identifier>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> let </keyword>

<identifier> a </identifier>

<symbol> [ </symbol>

<identifier> i </identifier>

<symbol> ] </symbol>

<symbol> = </symbol>

<identifier> Keyboard </identifier>

<symbol> . </symbol>

<identifier> readInt </identifier>

<symbol> ( </symbol>

<stringConstant> ENTER THE NEXT NUMBER: </stringConstant>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> i </identifier>

<symbol> = </symbol>

<identifier> i </identifier>

<symbol> + </symbol>

<integerConstant> 1 </integerConstant>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> let </keyword>

<identifier> i </identifier>

<symbol> = </symbol>

<integerConstant> 0 </integerConstant>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> sum </identifier>

<symbol> = </symbol>

<integerConstant> 0 </integerConstant>

<symbol> ; </symbol>

<keyword> while </keyword>

<symbol> ( </symbol>

<identifier> i </identifier>

<symbol> &lt; </symbol>

<identifier> length </identifier>

<symbol> ) </symbol>

<symbol> { </symbol>

<keyword> let </keyword>

<identifier> sum </identifier>

<symbol> = </symbol>

<identifier> sum </identifier>

<symbol> + </symbol>

<identifier> a </identifier>

<symbol> [ </symbol>

<identifier> i </identifier>

<symbol> ] </symbol>

<symbol> ; </symbol>

<keyword> let </keyword>

<identifier> i </identifier>

<symbol> = </symbol>

<identifier> i </identifier>

<symbol> + </symbol>

<integerConstant> 1 </integerConstant>

<symbol> ; </symbol>

<symbol> } </symbol>

<keyword> do </keyword>

<identifier> Output </identifier>

<symbol> . </symbol>

<identifier> printString </identifier>

<symbol> ( </symbol>

<stringConstant> THE AVERAGE IS: </stringConstant>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Output </identifier>

<symbol> . </symbol>

<identifier> printInt </identifier>

<symbol> ( </symbol>

<identifier> sum </identifier>

<symbol> / </symbol>

<identifier> length </identifier>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> do </keyword>

<identifier> Output </identifier>

<symbol> . </symbol>

<identifier> println </identifier>

<symbol> ( </symbol>

<symbol> ) </symbol>

<symbol> ; </symbol>

<keyword> return </keyword>

<symbol> ; </symbol>

<symbol> } </symbol>

<symbol> } </symbol>

</tokens>



Resultados obtenidos

Calcula el promedio de una secuencia de enteros proporcionada por el usuario utilizando una estructura de datos de matriz y comandos de manipulación de matriz.

GMain.xml

<class>

<keyword> class </keyword>

<identifier> Main </identifier>

<symbol> { </symbol>

<subroutineDec>

<keyword> function </keyword>

<keyword> void </keyword>

<identifier> main </identifier>

<symbol> ( </symbol>

<parameterList>

</parameterList>

<symbol> ) </symbol>

<subroutineBody>

<symbol> { </symbol>

<varDec>

<keyword> var </keyword>

<identifier> Array </identifier>

<identifier> a </identifier>

<symbol> ; </symbol>

</varDec>

<varDec>

<keyword> var </keyword>

<keyword> int </keyword>

<identifier> length </identifier>

<symbol> ; </symbol>

</varDec>

<varDec>

<keyword> var </keyword>

<keyword> int </keyword>

<identifier> i </identifier>

<symbol> , </symbol>

<identifier> sum </identifier>

<symbol> ; </symbol>

</varDec>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> length </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> Keyboard </identifier>

<symbol> . </symbol>

<identifier> readInt </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<stringConstant> HOW MANY NUMBERS? </stringConstant>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<letStatement>

<keyword> let </keyword>

<identifier> a </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> Array </identifier>

<symbol> . </symbol>

<identifier> new </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<identifier> length </identifier>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<letStatement>

<keyword> let </keyword>

<identifier> i </identifier>

<symbol> = </symbol>

<expression>

<term>

<integerConstant> 0 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<whileStatement>

<keyword> while </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> i </identifier>

</term>

<symbol> &lt; </symbol>

<term>

<identifier> length </identifier>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> a </identifier>

<symbol> [ </symbol>

<expression>

<term>

<identifier> i </identifier>

</term>

</expression>

<symbol> ] </symbol>

<symbol> = </symbol>

<expression>

<term>

<identifier> Keyboard </identifier>

<symbol> . </symbol>

<identifier> readInt </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<stringConstant> ENTER THE NEXT NUMBER: </stringConstant>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<letStatement>

<keyword> let </keyword>

<identifier> i </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> i </identifier>

</term>

<symbol> + </symbol>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

</statements>

<symbol> } </symbol>

</whileStatement>

<letStatement>

<keyword> let </keyword>

<identifier> i </identifier>

<symbol> = </symbol>

<expression>

<term>

<integerConstant> 0 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<letStatement>

<keyword> let </keyword>

<identifier> sum </identifier>

<symbol> = </symbol>

<expression>

<term>

<integerConstant> 0 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<whileStatement>

<keyword> while </keyword>

<symbol> ( </symbol>

<expression>

<term>

<identifier> i </identifier>

</term>

<symbol> &lt; </symbol>

<term>

<identifier> length </identifier>

</term>

</expression>

<symbol> ) </symbol>

<symbol> { </symbol>

<statements>

<letStatement>

<keyword> let </keyword>

<identifier> sum </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> sum </identifier>

</term>

<symbol> + </symbol>

<term>

<identifier> a </identifier>

<symbol> [ </symbol>

<expression>

<term>

<identifier> i </identifier>

</term>

</expression>

<symbol> ] </symbol>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

<letStatement>

<keyword> let </keyword>

<identifier> i </identifier>

<symbol> = </symbol>

<expression>

<term>

<identifier> i </identifier>

</term>

<symbol> + </symbol>

<term>

<integerConstant> 1 </integerConstant>

</term>

</expression>

<symbol> ; </symbol>

</letStatement>

</statements>

<symbol> } </symbol>

</whileStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Output </identifier>

<symbol> . </symbol>

<identifier> printString </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<stringConstant> THE AVERAGE IS: </stringConstant>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Output </identifier>

<symbol> . </symbol>

<identifier> printInt </identifier>

<symbol> ( </symbol>

<expressionList>

<expression>

<term>

<identifier> sum </identifier>

</term>

<symbol> / </symbol>

<term>

<identifier> length </identifier>

</term>

</expression>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<doStatement>

<keyword> do </keyword>

<identifier> Output </identifier>

<symbol> . </symbol>

<identifier> println </identifier>

<symbol> ( </symbol>

<expressionList>

</expressionList>

<symbol> ) </symbol>

<symbol> ; </symbol>

</doStatement>

<returnStatement>

<keyword> return </keyword>

<symbol> ; </symbol>

</returnStatement>

</statements>

<symbol> } </symbol>

</subroutineBody>

</subroutineDec>

<symbol> } </symbol>

</class>



Resultados obtenidos

Calcula el promedio de una secuencia de enteros proporcionada por el usuario utilizando una estructura de datos de matriz y comandos de manipulación de matriz.