forked from HaventSlept/Compilerbau24
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAntlrAstVisitorMain.java
More file actions
26 lines (21 loc) · 918 Bytes
/
AntlrAstVisitorMain.java
File metadata and controls
26 lines (21 loc) · 918 Bytes
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
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
public class AntlrAstVisitorMain {
public static void main(String[] args) throws Exception {
// create input stream
CharStream input = CharStreams.fromFileName("compiler/language.txt");
// create lexer
compiler.antlrcompiler.languageLexer lexer = new compiler.antlrcompiler.languageLexer(input);
// create token stream
CommonTokenStream tokens = new CommonTokenStream(lexer);
// create parser
compiler.antlrcompiler.languageParser parser = new compiler.antlrcompiler.languageParser(tokens);
parser.setBuildParseTree(true);
// build parse tree
ParseTree tree = parser.expr();
// create visitor for expression evaluation
compiler.antlrvisitor.ExprAstVisitor exprEvalVisitor = new compiler.antlrvisitor.ExprAstVisitor();
compiler.ast.ASTExprNode result = exprEvalVisitor.visit(tree);
System.out.println(result);
}
}