-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
59 lines (48 loc) · 2.15 KB
/
Copy pathMain.java
File metadata and controls
59 lines (48 loc) · 2.15 KB
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
import java.io.IOException;
public class Main {
public static void main(String[] args) {
String inputFile = "sample_input.txt";
if (args.length > 0) {
inputFile = args[0];
}
System.out.println("Processing input file: " + inputFile);
try {
// Read input file
CharStream input = CharStreams.fromFileName(inputFile);
// Instantiate lexer
HTMLStructureLexer lexer = new HTMLStructureLexer(input);
// Get token stream
CommonTokenStream tokens = new CommonTokenStream(lexer);
// Instantiate parser
HTMLStructureParser parser = new HTMLStructureParser(tokens);
// Add standard error listener to output syntax errors clearly
parser.removeErrorListeners();
parser.addErrorListener(new BaseErrorListener() {
@Override
public void syntaxError(Recognizer<?, ?> recognizer, Object offendingSymbol,
int line, int charPositionInLine, String msg,
RecognitionException e) {
System.err.println("Syntax Error at line " + line + ":" + charPositionInLine + " - " + msg);
}
});
// Parse document rule
ParseTree tree = parser.htmlDoc();
if (parser.getNumberOfSyntaxErrors() > 0) {
System.err.println("Parsing finished with " + parser.getNumberOfSyntaxErrors() + " syntax error(s). Exiting.");
System.exit(1);
}
// Create and execute custom visitor
HTMLStructureBaseVisitorExtended visitor = new HTMLStructureBaseVisitorExtended();
visitor.visit(tree);
} catch (IOException e) {
System.err.println("Error reading input file: " + e.getMessage());
System.exit(1);
} catch (Exception e) {
System.err.println("Unexpected error occurred: " + e.getMessage());
e.printStackTrace();
System.exit(1);
}
}
}