我正在一个必须创建解析器的项目中。该项目的想法是让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
答案 0 :(得分:1)
在while循环中,没有中断条件。因此,如果您输入了str = "any random value except those tokens"
,则会陷入无限循环。由于您使用的是str = scanner.nextLine()
,因此它读取的是整行,而不是标记(即包括尾部空格等),因此您得到的是str = "DIV_OP "
,而不是str="DIV_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("Parsing ...")
来检测那些行。请看一下,如果您有任何疑问,请随时提问。