【发布时间】:2012-03-11 20:55:23
【问题描述】:
问题来了: 我有一个令牌列表,这些令牌是 TI 编程语言中的有效令牌。我正在尝试编写一个程序,它将包含有效令牌的文本编译成一个 TI-83/84 程序,该程序完全由十六进制数据组成。我知道如何将数据写入文件,以及如何从文件中获取数据。 我需要编写一个程序来搜索程序并识别有效令牌并能够保持程序的顺序。我已经考虑过将一个标记作为关键字并像这样在文本中搜索,但是首先选择的标记并不总是在文本中的第一个,所以顺序会丢失。
我已经编写了自己的令牌对象,并且在我的解析器中使用了以下变量:
- ArrayList tokens - 一个 Token 对象的 ArrayList(TI-BASIC 的所有有效标记)
- DataInputStream dis - 也是文本文件
- 字符串数据 - 程序使用 loadData 方法从文件中提取的文本数据(不是问题,所以它如何工作并不重要)
- DataOutputStream dos - 要写入的 .8xp 文件
令牌类:
public class Token {
private String tokenText;
int[] hexValue;
boolean isTwoByte;
/**
* The default constructor.
* Assigns empty values to all instance variables.
*/
public Token() {
tokenText = "";
hexValue = new int[0];
isTwoByte = false;
}
/**
* Constructs a Token with <tt>text</tt> text and an empty int[] for <tt>hexValue</tt>
* @param text the text to be assigned to the Token
*/
public Token(String text) {
tokenText = text;
hexValue = new int[0];
isTwoByte = false;
}
/**
* Constructs a Token with an empty String for <tt>text</tt> and <tt>hexValue</tt> value
* This is a one-byte Token
* @param value the value to be assigned to the Token
*/
public Token(int value) {
tokenText = "";
hexValue = new int[1];
hexValue[0] = value;
isTwoByte = false;
}
/**
* Constructs a Token with an empty String for <tt>text</tt> and <tt>hexValue></tt> v1v2
* This is a two-byte Token
* @param v1 the first byte of the two-byte value that will be assigned to the Token
* @param v2 the second byte of the two-byte value
*/
public Token(int v1, int v2) {
tokenText = "";
hexValue = new int[2];
hexValue[0] = v1; hexValue[1] = v2;
isTwoByte = true;
}
/**
* Constructs a Token with <tt>text</tt> text and <tt>hexValue</tt> value
* This is a one-byte Token
* @param text the text to be assigned to the Token
* @param value the value to be assigned to the one-byte Token
*/
public Token(String text, int value) {
tokenText = text;
hexValue = new int[1];
hexValue[0] = value;
isTwoByte = false;
}
/**
* Constructs a Token with tokenText <tt>text</tt> text and <tt>hexValue</tt> v1v2
* This is a two-byte Token
* @param text the text to be assigned to the Token
* @param v1 the first byte of the two-byte value that will be assigned to the Token
* @param v2 the second byte of the two-byte value
*/
public Token(String text, int v1, int v2) {
tokenText = text;
hexValue = new int[2];
hexValue[0] = v1; hexValue[1] = v2;
isTwoByte = true;
}
/**
* Returns the text that is assigned to the particular Token
* @return the text that is assigned to the particular Token
*/
public String getText() {
return tokenText;
}
/**
* Returns the byte value of the Token
* @return the byte value of the Token
*/
public int[] getValue() {
return hexValue;
}
/**
* Returns <tt>true</tt> if the Token is a two-byte Token, and <tt>false</tt> otherwise
* @return <tt>true</tt> if the Token is a two-byte Token, and <tt>false</tt> otherwise
*/
public boolean isTwoByte() {
return isTwoByte;
}
/**
* Sets the tokenText text of the Token to the String passed it.
* @param text the new text to be assigned to the Token
*/
public void setText(String text) {
tokenText = text;
}
/**
* Sets the byte value of the Token to the integer passed it. This sets the isTwoByte variable to <tt>false</tt>.
* @param value the new byte value to be assigned to the Token
*/
public void setValue(int value) {
hexValue = new int[1];
hexValue[0] = value;
isTwoByte = false;
}
/**
* Sets the byte value of the Token to the integers passed it. This sets the isTwoByte variable to <tt>true</tt>.
* @param v1 value of the first byte of the two-byte Token
* @param v2 value of the second byte
*/
public void setValue(int v1, int v2) {
hexValue = new int[2];
hexValue[0] = v1; hexValue[1] = v2;
isTwoByte = true;
}
}
我希望有人能够帮助我解决这个问题。如果您需要更多信息,例如更多代码,这不是问题,无论如何该项目都是开源的。在此先感谢您提供任何帮助。
【问题讨论】:
-
您能否展示一下输入数据的外观以及该数据集的输出结果?希望每个只需要几行。
-
输入数据存储在一个 UTF-8 .txt 文件中,如下所示: :TI-84 Prog "Hello World"->Str1 Disp Str1:Stop 并且最终的输出应该被存储在 .8xp 文件中,该文件应包含以下原始十六进制数据(不包括标题): 3E 54 49 71 38 34 29 50 BBC2 BBBF BBB6 3F 2A 48 BBB4 BBBC BBBC BBBF 29 57 BBBF BBC2 BBBC BBB3 2A 04 AA00 3F DE AA00 3E D9 [:]=3E ["]=2A [-]=71 [
]=29 [->]= [Disp ]=DE [Stop]=D9 [Str1]=token值AA00大写的预定变量字母与常规 ASCII 编码相同 小写字母是从 BBB0 开始的两字节标记 -
对于上述信息的格式不佳,我深表歉意,cmets 不允许换行。在“Prog”之后和第一个“Str1”之后应该有一个换行符