菜鸟初学,求解---用java代码来实现。
学java学的晕了,问个菜鸟级的问题哦,如何添加一个类或者是一个方法就好咯,来实现将这其中会产生的字符串输出到某个盘下的output.txt文本中。(PS:实验内容是:通过分析一段在input.txt的代码,读取文件输入,然后进行check分析,最后分析完的结果存在一个字符串中,然后输出到output.txt,这段代码不知道还有木有错……)
import java.io.*;
public class Xlex {
int loca = 0;
int locb = 0;
int code;
int value;
String filename;
// String strToken="";
// String filename;
StringBuffer buffer = new StringBuffer();
StringBuffer strToken = new StringBuffer();
String output = "";
private String KeyTable[] = { "DIM", "IF", "DO", "STOP", "END", "=", "+",
"*", "**", ",", "(", ")" };
private String SymbolTable[] = new String[50];
private String ConstTable[] = new String[50];
public Xlex(String filename) {
this.filename = filename;
}
public boolean IsDigit(char c) {
/*
if(c>='0'||c<='9') return true; else return false;
*/
return Character.isDigit(c);
}
public boolean IsLetter(char c) {
return Character.isLetter(c);
// if ((c >= 'a') && (c <= 'z') || (c >= 'A') && (c <= 'Z'))
// return true;
// else
// return false;
}
public int Reserve(String str) {
int s = 1;
value = 0;
while ((value < KeyTable.length) && (s != 0)) {
s = str.compareToIgnoreCase(KeyTable[value]);
value++;
}
if (s != 0) {
value = 6;
return value;
} else {
if (value >= 5)
value += 2;
}
return value;
}
public int InsertId(String str) {
if (loca < 50) {
SymbolTable[loca++] = str;
return loca;
} else {
System.out.println("符号表溢出!");
return -1;
}
}
public int InsertConst(String str) {
if (locb < 50) {
ConstTable[locb++] = str;
return locb;
} else {
System.out.println("常数表溢出!");
return -1;
}
}
public void readFile() throws Exception
{
try
{
FileReader fr=new FileReader(this.filename);
BufferedReader br=new BufferedReader(fr);
String temp=null;
while((temp=br.readLine())!=null)
{
buffer.append(temp);
}
}
catch (Exception e)
{
System.out.println("文件操作错误:"+e.toString()+"\r\n");
//outputToFile("文件操作错误:"+e.toString()+"\n");
}
}
public void outputToFile(String msg) throws Exception {
try {
String filename = "D:\\output.txt";
OutputStream os = new FileOutputStream(filename);
os.write(msg.getBytes());
os.close();
} catch (Exception e) {
System.out.println("输出文件错误!");
}
}
public void Check() {
int i = 0;
while (i < buffer.length()) {
char ch = buffer.charAt(i);
strToken.append(ch);
if (Character.isLetter(ch)) {
boolean done = false;
while ((i + 1) < buffer.length()) {
ch = buffer.charAt(++i);
if (Character.isLetter(ch) || Character.isDigit(ch)) {
strToken.append(ch);
} else {
i--;
strToken.substring(0, strToken.length() - 1);
int r = Reserve(strToken.toString());
if (r == 6) {
output += "($ID," + InsertId(strToken.toString())
+ ")";
} else {
switch (r) {
case 1:
output += "($DIM,-)";
break;
case 2:
output += "($IF,-)";
case 3:
output += "($DO,-)";
case 4:
output += "($STOP,-)";
case 5:
output += "($END,-)";
}
}
done = true;
break;
}
}
if (!done) {
int r = Reserve(strToken.toString());
if (r == 6) {
output += "($ID," + InsertId(strToken.toString()) + ")";
} else {
switch (r) {
case 1:
output += "($DIM,-)";
break;
case 2:
output += "($IF,-)";
case 3:
output += "($DO,-)";
case 4:
output += "($STOP,-)";
case 5:
output += "($END,-)";
}
}
}
} else if (Character.isDigit(ch)) {
boolean done = false;
while ((i + 1) < buffer.length()) {
ch = buffer.charAt(++i);
if (Character.isDigit(ch)) {
strToken.append(ch);
} else {
System.out.println(strToken);
i--;
// strToken.substring(0, strToken.length() - 1);
output += "($INT," + InsertConst(strToken.toString())
+ ")";
done = true;
break;
}
}
if (!done) {
output += "($INT," + InsertConst(strToken.toString()) + ")";
}
} else if (ch == '*') {
if ((i + 1) < buffer.length()) {
ch = buffer.charAt(++i);
if (ch == '*')
output += "($POWER,-)";
else {
i--;
output += "($STAR,-)";
}
}
} else if (ch == '+') {
output += "($PLUS,-)";
} else if (ch == '=') {
output += "($ASSIGN,-)";
} else if (ch == ',') {
output += "($COMMA,-)";
} else if (ch == '(') {
output += "($LPAR,-)";
} else if (ch == ')') {
output += "($RPAR,-)";
} else {
output += "($error," + ch + ")";
}
i++;
strToken = new StringBuffer();
}
}
}
--------------------编程问答--------------------
呜呜。。。顶上
补充:Java , Java相关