[英]Unable to match the first word from a file using Java
我正在尝试匹配数组中的单词以创建用于词法分析的符号表(编译器实验室)。 我正在从Java读取C代码文件。 我可以从文件中找到所有内容,但第一个单词除外。 不管我尝试什么,尽管第一个单词是一个有效单词,但它与任何单词都不匹配。 在我的文件中,第一个单词是int(两个变量的初始化),第二行是float(初始化)。 如果我交换它,我的代码可以匹配int但不匹配float。
这是我正在阅读的文件:
float d, e;
int a, b, c;
这是从文件读取的代码:
public static void fileRead(String fileName)
{
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(fileName));
try {
String x;
while ( (x = br.readLine()) != null )
{
// printing out each line in the file
System.out.println(x);
parser(x);
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
System.out.println(e);
e.printStackTrace();
}
}
解析器是另一种方法,用于解析不同的单词:
public static void parser(String line)
{
String text = "";
for(int i = 0; i < line.length(); i++)
{
String temp = line.charAt(i) + "";
if(!(temp.equals(" ")
|| temp.equals(",")
|| temp.equals(";")
|| temp.equals(")")
|| temp.equals("}")
|| temp.equals("(")
|| temp.equals("{")
|| temp.equals("[")
|| temp.equals("]")
))
{
text = text + temp;
}
else
{
text = text.trim();
if(text.equals("int"))
{
System.out.println("Say cheese");
}
addToarray(text);
text = "";
}
}
我以为结尾可能有空间,所以我将其修剪和备份。
这就是我要添加到数组中的方式:if(item.equals(text))在这里,“ int”似乎丢失了,并且从未进入过if块
public static void addToarray(String text)
{
boolean flag = false;
//look for keyWords first.
for (String item : keyWords)
{
if(item.equals(text))
{
if(resultKey.size() == 0)
{
System.out.println("Size zero> "+resultKey.size());
resultKey.add(text);
text = "";
flag = true;
break;
}
else
{
boolean checker = true;
for(String key : resultKey)
{
if(key.equals(text))
{
checker = false;
break;
}
}
if(checker)
{
resultKey.add(text);
flag = true;
text = "";
}
}
}
}
这是我用来匹配的数组:
final static String []keyWords = {"float", "if", "else",
"long", "double", "BigInteger","int"};
这些是用于存储变量的ArrayList。
static ArrayList <String> resultKey, resultIdent , resultMath,
resultLogic, resultNumeric, resultOthers;
谢谢你的帮助。
启动这个简单的应用程序可以正常工作,
不知道为什么您看不懂第一个单词。
编辑 :100%是@Fildor注意到的文件中的起始BOM。
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
public class Parser {
final static String[] keyWords = { "float", "if", "else", "long", "double", "BigInteger", "int" };
static ArrayList<String> resultKey = new ArrayList<>();
public static void main(String[] args) {
fileRead("src/test/resources/test.txt");
for (final String key : resultKey) {
System.out.println(key);
}
}
public static void fileRead(String fileName) {
BufferedReader br = null;
try {
br = new BufferedReader(new FileReader(fileName));
try {
String x;
while ((x = br.readLine()) != null) {
// printing out each line in the file
System.out.println(x);
parser(x);
}
br.close();
} catch (final IOException e) {
e.printStackTrace();
}
} catch (final FileNotFoundException e) {
System.out.println(e);
e.printStackTrace();
}
}
public static void parser(String line) {
String text = "";
for (int i = 0; i < line.length(); i++) {
final String temp = line.charAt(i) + "";
if (!(temp.equals(" ") || temp.equals(",") || temp.equals(";") || temp.equals(")") || temp.equals("}")
|| temp.equals("(") || temp.equals("{") || temp.equals("[") || temp.equals("]"))) {
text = text + temp;
} else {
text = text.trim();
if (text.equals("int")) {
System.out.println("Say cheese");
}
addToarray(text);
text = "";
}
}
}
public static void addToarray(String text) {
boolean flag = false;
// look for keyWords first.
for (final String item : keyWords) {
if (item.equals(text)) {
if (resultKey.size() == 0) {
System.out.println("Size zero> " + resultKey.size());
resultKey.add(text);
text = "";
flag = true;
break;
} else {
boolean checker = true;
for (final String key : resultKey) {
if (key.equals(text)) {
checker = false;
break;
}
}
if (checker) {
resultKey.add(text);
flag = true;
text = "";
}
}
}
}
}
}
并且文件test.txt
完全包含
float d, e;
int a, b, c;
启动打印
float d, e;
Size zero> 0
int a, b, c;
Say cheese
float
int
“ int”不匹配,因为您的输入文件可能包含Byte-Order-Mark。
您可以通过代码或十六进制编辑器进行检查。 最有可能是0xEFBBBF(UTF-8),0xFEFF(UTF-16大端)或0xFFFE(UTF-16小端)之一。 但是还有更多。 我已经在注释中引用了有关该主题的W3C文档。 这是Wikipedia-Article,其中包含更多BOM。
边注:
哪位老师分发了“脏”输入文件! 他一定是某种虐待狂,或者(不是更糟糕的是,恕我直言)他不是故意这样做的。 我会尝试将文件的(可打印的)内容复制到一个新文件中,并将其作为输入进行测试。 因此,如果清理后的文件令人满意,则可以找到一些清除输入内容的方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.