这是问题所在: 我有一个令牌列表,这些令牌是TI编程语言中的有效令牌。我正在尝试编写一个程序,将包含有效标记的文本编译成TI-83/84程序,该程序由完全十六进制数据组成。我知道如何将数据写入文件,以及如何从文件中获取数据。 我需要编写一个程序来搜索程序并识别有效的令牌并能够保持程序的顺序。我已经考虑过将一个令牌作为一个关键字并像这样搜索文本,但首先选择的令牌并不总是成为文本中的第一个,因此订单将会丢失。
我编写了自己的令牌对象,我在解析器中使用了以下变量:
令牌类:
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;
}
}
我希望有人能够帮助我。如果您需要更多信息,例如任何更多的代码,这不会成为问题,那么项目无论如何都将是开源的。提前感谢您提供任何帮助。
答案 0 :(得分:0)
我自己没有使用它,但java.io.StreamTokenizer
课程是否满足您的需要?或者,如果您可以完整地阅读源文件,则可以使用简单的StringTokenizer
。
由于您按顺序单步调试文件,因此无需担心保留订单。