forked from paceuniversity/cs361-paceuniversity-classroom-21e866-kay-parser-2024-Kay-Parser-Starter-Code-2023-1
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ParserDemo.java
36 lines (27 loc) · 1.18 KB
/
ParserDemo.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import java.nio.file.Files;
import java.nio.file.Paths;
public class ParserDemo {
public static void main(String[] args) {
try {
// Path to the KAY program file
String filePath = "C:\\Users\\sande\\Desktop\\prog1.kay";
// To check if file exists
if (!Files.exists(Paths.get(filePath))) {
System.err.println("Error: File not found at " + filePath);
return;
}
// File reading
TokenStream tStream = new TokenStream(filePath); // Generate tokens from the file
ConcreteSyntax cSyntax = new ConcreteSyntax(tStream); // Create a parser instance
Program p = cSyntax.program(); // Parse the program
System.out.println(p.display());
} catch (RuntimeException e) {
// Catch and display syntax errors
System.err.println(e.getMessage());
} catch (Exception e) {
// Catch and display any unexpected errors
System.err.println("An unexpected error occurred: " + e.getMessage());
e.printStackTrace();
}
}
}