forked from HaventSlept/Compilerbau24
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAntlrExprListenerMain.java
More file actions
28 lines (23 loc) · 1005 Bytes
/
AntlrExprListenerMain.java
File metadata and controls
28 lines (23 loc) · 1005 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
27
28
import org.antlr.v4.runtime.*;
import org.antlr.v4.runtime.tree.*;
public class AntlrExprListenerMain {
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.ExprEvalListener exprEvalListener = new compiler.antlrvisitor.ExprEvalListener();
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(exprEvalListener, tree);
Integer result = exprEvalListener.m_values.get(tree);
System.out.println(result);
}
}