Quick Guide in Terminal
-
first generate parser and lexer (and visitor)
antlr -Dlanguage=JavaScript -visitor test.g4
In JavaScript we don’t need to compile or test, but we could do that in java target project. |
Context in Parser
THe ctx has a children
field, which includes all the ternimalNode here include operation like "=",
or we could use ctx.op
to get it.
More important, ctx has method to access all of elements it mentioned like ctx.block()
. For getting a text or token, use getText()
or getSymbol()
.
You can use both ways to access the node.
Some other useful function:
-
getChildCount()
-
addErrorNode()
See the Antlr Doc here
Visitor and Listener
- Listener
-
Listener use when we walk the parser tree, it can covered every node in the code.
- Visitor
-
Visitor visit specific node, and could give return value.
The two method use the same context regarding to the same state. |
Error Detection
For detect the ambiguous error please use DiagnosticErrorListener instead :
parser.removeErrorListeners();
parser.addErrorListener(new DiagnosticErrorListener());
//one more stuff here
parser.getInterpreter()
.setPredictionMode(PredictioniMode.LL_EXACT_AMBIG_DETECTION);