我正在开发一个必须创建解析器的项目。该项目的想法是让方法 lex() 充当词法分析器并扫描我将在下面包含的输入文件。该文件在输入文件中包含一行,说明正在解析哪个语句,然后在语句下方包含该语句的每个标记。我创建了一个要在我的方法enum中进行比较的令牌。lex()我的 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");
}
/*********************************************************/
}
Parsing the statement: sumTotal = (sum + 47 ) / total
IDENT
ASSIGN_OP
LEFT_PAREN
IDENT
ADD_OP
INT_LIT
RIGHT_PAREN
DIV_OP
IDENT
Parsing the statement: Total = (sum + 47 ) /
IDENT
ASSIGN_OP
LEFT_PAREN
IDENT
ADD_OP
INT_LIT
RIGHT_PAREN
DIV_OP
Parsing the statement: area = (length + width) / 2
IDENT
ASSIGN_OP
LEFT_PAREN
IDENT
ADD_OP
IDENT
RIGHT_PAREN
DIV_OP
INT_LIT
Parsing the statement: ageNumbers = age + 3 - 5 * (D / C)
IDENT
ASSIGN_OP
IDENT
ADD_OP
INT_LIT
SUB_OP
INT_LIT
MULT_OP
LEFT_PAREN
IDENT
DIV_OP
IDENT
RIGHT_PAREN
END_OF_FILE