- Install java, for mac run:
brew cask install java
- Install antlr4 referring to the tutorial
- After installing antlr4 run:
antlr4 -Dlanguage=JavaScript -lib grammars -o lib -visitor -Xexact-output-dir grammars/ECMAScript.g4
- your grammar is in grammars folder and inside your lib you can find:
- ECMAScriptLexer.js splits a source code character stream into a token stream according to the rules specified in the grammar.
- ECMAScriptParser.js generates an abstract connected tree structure (i.e. parse tree) from the token stream.
- ECMAScriptVisitor.js is responsible for traversing the generated tree. Technically, we could manually process the tree by depth-first recursive traversal of children. However, if we have a large number of node types and complex processing logic, it is preferable to visit each node type using its special predefined method, as visitor does.
useful links: