Java 如何创建搜索输入文件的方法
我正在做一个项目,我必须创建一个解析器。该项目的想法是让方法lex()充当词法分析器,并扫描我将在下面介绍的输入文件。该文件在输入文件中包含一行,说明正在解析哪个语句,然后在该语句下面包含该语句的每个标记。我在Java 如何创建搜索输入文件的方法,java,Java,我正在做一个项目,我必须创建一个解析器。该项目的想法是让方法lex()充当词法分析器,并扫描我将在下面介绍的输入文件。该文件在输入文件中包含一行,说明正在解析哪个语句,然后在该语句下面包含该语句的每个标记。我在lex()方法中创建了要与之进行比较的令牌的enum。我的lex方法应该做的是使用扫描器查找文件的下一行,将其与枚举进行比较,如果匹配,则将其存储在nextToken中,然后返回它。我的问题是,当nextLine()和tokens.toString()不匹配时,我不知道如何处理,这表明文件
lex()
方法中创建了要与之进行比较的令牌的enum
。我的lex方法应该做的是使用扫描器查找文件的下一行,将其与枚举进行比较,如果匹配,则将其存储在nextToken
中,然后返回它。我的问题是,当nextLine()
和tokens.toString()
不匹配时,我不知道如何处理,这表明文件是空的,它是文件的结尾,或者它比较的行是“解析语句:语句”。如何更新我的lex()
方法
public static tokens lex()
{
String str = scanner.nextLine();
while(str != null)
{
for(tokens token : tokens.values())
{
if(str.equals(token.toString()))
{
tokens nextToken = token;
return nextToken;
}
}
}
return nextToken;
能够处理对语句行的解析,并且只比较标记
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintStream;
import java.util.Scanner;
public class Parse
{
//Scanner to read for each line of the input file
static Scanner scanner = new Scanner("statements.txt");
private static tokens nextToken = null;
public static String str = null;
public static PrintStream outputPrint;
public static void main(String[] args) throws FileNotFoundException, IOException
{
outputPrint = new PrintStream("lexOutput.txt");
outputPrint.println("********************************************************************************");
outputPrint.println("Shane Hampton, CSCI4200, Fall 2019, Parser");
outputPrint.println("********************************************************************************");
/*lex();
if(nextToken != null)
{
if(nextToken == tokens.IDENT)
{
outputPrint.println
outputPrint.println
}
}
else
{
outputPrint.println(str);
}*/
}
/** Lex Method to return the token from each line when called **/
public static tokens lex()
{
String str = scanner.nextLine();
while(str != null)
{
for(tokens token : tokens.values())
{
if(str.equals(token.toString()))
{
tokens nextToken = token;
return nextToken;
}
}
}
return nextToken;
/*while(str != null)
{
for(tokens token : tokens.values())
{
if(str.equals(token.toString()))
{
tokens nextToken = token;
return nextToken;
}
}
}
return nextToken;*/
}/** END OF LEX METHOD **/
enum tokens
{
END_OF_FILE, LEFT_PAREN, RIGHT_PAREN, ASSIGN_OP, ADD_OP,
SUB_OP, MULT_OP, DIV_OP, IDENT, INT_LIT
}
/**********************************************************/
/* assign
Parses strings in the language generated by the rule:
<assign> -> id = <expr>
*/
public void assign() throws IOException
{
System.out.printf("Enter <assign>\n");
/* Parse the first expression */
expr();
System.out.printf("Exit <assign>\n");
}/* End of function assign */
/**********************************************************/
/* expr
Parses strings in the language generated by the rule:
<expr> -> <term> {(+ | -) <term>}
*/
public void expr() throws IOException
{
System.out.printf("Enter <expr>\n");
/* Parse the first term */
term();
/* As long as the next token is + or -, get
the next token and parse the next term */
while (nextToken == tokens.ADD_OP || nextToken == tokens.SUB_OP)
{
lex();
term();
}
System.out.printf("Exit <expr>\n");
} /* End of function expr */
/**********************************************************/
/* term
Parses strings in the language generated by the rule:
<term> -> <factor> {(* | /) <factor>)
*/
public void term() throws IOException
{
System.out.printf("Enter <term>\n");
/* Parse the first factor */
factor();
/* As long as the next token is * or /, get the
next token and parse the next factor */
while (nextToken == tokens.MULT_OP || nextToken == tokens.DIV_OP)
{
lex();
factor();
}
System.out.printf("Exit <term>\n");
} /* End of function term */
/**********************************************************/
/* factor
Parses strings in the language generated by the rule:
<factor> -> id | int_constant | ( <expr )
*/
public void factor() throws IOException
{
System.out.printf("Enter <factor>\n");
/* Determine which RHS */
if (nextToken == tokens.IDENT || nextToken == tokens.INT_LIT)
{
/* Get the next token */
lex();
}
else
{
if (nextToken == tokens.LEFT_PAREN)
{
lex();
expr();
if (nextToken == tokens.RIGHT_PAREN)
{
lex();
}
else
{
Error(null);
}
} /* End of if (nextToken == ... */
else
{
Error(null);
} /* End of else */
}
System.out.printf("Exit <factor>\n");
} /* End of function factor */
/*********************************************************/
/* Method to show an that an error exists when the method is called*/
private void Error(String s)
{
System.out.printf("There is an Error");
}
/*********************************************************/
}
在while循环中,没有中断条件。因此,如果您有input
str=“除了那些标记以外的任何随机值”
,您将陷入一个无限循环中。由于您使用的是str=scanner.nextLine()
,它读取整行,而不是令牌(即,包括尾随空格等),因此您得到的是str=“DIV\u OP”
,而不是str=“DIV\u OP”
。其中有一些问题。
总之,我刚刚编写了一个示例代码:
import java.util.*;
public class Start{
static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
while(sc.hasNext()){
try{
tokens t = lex();
if(t != null){
System.out.println("Found token: "+t.toString());
}
}catch(Exception x){ x.printStackTrace(); }
}
}
public static tokens lex(){
tokens ret = null;
String s = sc.nextLine();
if(s.contains("Parsing the statement:")){
System.out.println("Found parsing statement line!!!");
return null;
}else{
//System.out.print(s);
for(tokens token : tokens.values()){
if(s.contains(token.toString())){
System.out.println("Found a token!!!");
ret = token;
return ret;
}
}
}
System.out.println("Default case!!!");
return ret;
}
enum tokens
{
END_OF_FILE, LEFT_PAREN, RIGHT_PAREN, ASSIGN_OP, ADD_OP,
SUB_OP, MULT_OP, DIV_OP, IDENT, INT_LIT
}
}
在这里,我使用str.contains(“解析…”)
来检测这些行。请看一看,如果您有任何问题,请随时提问
import java.util.*;
public class Start{
static Scanner sc;
public static void main(String[] args) {
sc = new Scanner(System.in);
while(sc.hasNext()){
try{
tokens t = lex();
if(t != null){
System.out.println("Found token: "+t.toString());
}
}catch(Exception x){ x.printStackTrace(); }
}
}
public static tokens lex(){
tokens ret = null;
String s = sc.nextLine();
if(s.contains("Parsing the statement:")){
System.out.println("Found parsing statement line!!!");
return null;
}else{
//System.out.print(s);
for(tokens token : tokens.values()){
if(s.contains(token.toString())){
System.out.println("Found a token!!!");
ret = token;
return ret;
}
}
}
System.out.println("Default case!!!");
return ret;
}
enum tokens
{
END_OF_FILE, LEFT_PAREN, RIGHT_PAREN, ASSIGN_OP, ADD_OP,
SUB_OP, MULT_OP, DIV_OP, IDENT, INT_LIT
}
}