I am working on this java program, which is a recursive descent parser. The output provided below does not contain any syntax errors, however the program printing an error message. The program should print "SUCCESS: ..." but is printing "ERROR: ..." instead. Here is my code:
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Main {
private static BufferedReader reader;
private static String token;
public static void main(String[] args) {
String inputFile = "input1.txt"; // Specify the input file name here
try {
reader = new BufferedReader(new FileReader(inputFile));
token = getNextToken();
if (parse()) {
System.out.println("SUCCESS: the code has been successfully parsed.");
} else {
System.out.println("ERROR: the code contains a syntax mistake.");
}
reader.close();
} catch (IOException e) {
System.err.println("Error reading file: " + e.getMessage());
}
}
private static String getNextToken() throws IOException {
return reader.readLine(); // Read the next line from the file
}
private static boolean parse() throws IOException {
if (!token.equals("{")) {
return false; // Program should start with "{"
}
while (!token.equals("$")) {
if (!token.equals("compute")) {
return false; // Expecting "compute" token
}
if (!isValidComputeStatement()) {
return false; // Invalid compute statement
}
}
return true; // Parsing successful
}
private static boolean isValidComputeStatement() throws IOException {
token = getNextToken(); // Move to the next token
if (!token.equals(":")) {
return false; // Expecting colon after "compute"
}
token = getNextToken(); // Move to the next token
if (!isValidAssignmentStatement()) {
return false; // Invalid assignment statement
}
return true; // Valid compute statement
}
private static boolean isValidAssignmentStatement() throws IOException {
if (!token.equals("id")) {
return false; // Expecting an identifier (variable name)
}
token = getNextToken(); // Move to the next token
if (!token.equals("=")) {
return false; // Expecting "=" after the identifier
}
token = getNextToken(); // Move to the next token
if (!token.equals("num")) {
return false; // Expecting a number after "="
}
token = getNextToken(); // Move to the next token
if (!token.equals(";")) {
return false; // Expecting ";" to terminate the statement
}
token = getNextToken(); // Move to the next token
return true; // Valid assignment statement
}
}
Here is the input txt file (Each line is one token):
{
compute
:
id
=
num
;
compute
:
id
=
num
;
compute
:
id
=
id
+
id
;
compute
:
id
=
id
-
id
;
call
:
id
(
id
,
num
,
id
)
;
}
$
Thank you.
This is not rocket-science, but your code is completely wrong. It does not validate the schema of the file. It's easier to give you a working solution to make you realise the way of solving this task, than explain problems in your code.
I am not sure that my solution is
100%correct, but it works well for you input example.P.S. My advice to you. Run this code in the debugger mode and go throught the whole logic. I think you get the main idea of the solution.