In [Part 1](https://medium.com/@thosakwe/creating-a-scripting-language-with-antlr-part-1-1b42c3e4d718), we learned the basics of compiler theory, and the role of ANTLR in computerized language recognition. Now, we will move onto using ANTLR to build an AST in code. If you do not have ANTLR installed already, follow the instructions on the official [Getting Started Page](https://github.com/antlr/antlr4/blob/master/doc/getting-started.md). If you don’t know how to write or read ANTLR 4 grammars, then you can learn via the very detailed ANTLR 4 documentation. This, believe it or not, is not a tutorial on ANTLR syntax itself. Regardless, you will be able to get through reading this guide without really knowing ANTLR at all. Now that we have ANTLR available on our system, we can design our language and write our grammar. We will follow the example from Part 1 and write a grammar that runs code of this type: set sky to "blue" set roses to 3 say "The sky is colored ${sky}." say "I have ${roses} roses." Our grammar file might look like this: grammar Foo; compilationUnit: stmt\*; stmt: assignStmt | invocationStmt ; assignStmt: SET ID TO expr; invocationStmt: name=ID ((expr COMMA)\* expr)?; expr: ID | INT | STRING; COMMA: ','; SAY: 'say'; SET: 'set'; TO: 'to'; INT: \[0-9\]+; STRING: '"' (~('\\n' | '"'))\* '"'; ID: \[a-zA-Z\_\] \[a-zA-Z0-9\_\]\*; And then we can simply invoke ANTLR from the command line to generate the lexer and parser. At its most basic, we just have to supply the name of the input grammar, and _“-Dlanguage=JavaScript”_ to indicate to ANTLR that we want Javascript output. antlr4 -Dlanguage=JavaScript Foo.g4 This will leave you with about 5 files, including _FooLexer.js_, _FooParser.js_ and _FooListener.js_. Using this in code is remarkably easy. The following function takes input text and returns the generated [AST](https://hackernoon.com/tagged/ast). var antlr4 = require('antlr4'); var FooLexer = require('./FooLexer').FooLexer; var FooParser = require('./FooParser').FooParser; **function buildAst**(inputText) { **var** chars = **new** antlr4.InputStream(inputText); **var** lexer = **new** FooLexer(chars); **var** tokens = **new** antlr4.CommonTokenStream(lexer); **var** parser = **new** FooParser(tokens); parser.buildParseTrees = **true**; **return** parser.compilationUnit(); } module.exports = buildAst; With ANTLR, it takes a matter of milliseconds to create both a lexer _and_ a parser. Imagine writing these by hand! In [Part 3](https://medium.com/@thosakwe/creating-a-scripting-language-with-antlr-part-2-e41fc68c60ba), we will wrap everything up by spitting out [Javascript](https://hackernoon.com/tagged/javascript) from our compiler. Stay tuned! > [Hacker Noon](http://bit.ly/Hackernoon) is how hackers start their afternoons. We’re a part of the [@AMI](http://bit.ly/atAMIatAMI)family. We are now [accepting submissions](http://bit.ly/hackernoonsubmission) and happy to [discuss advertising &sponsorship](mailto:partners@amipublications.com) opportunities. > To learn more, [read our about page](https://goo.gl/4ofytp), [like/message us on Facebook](http://bit.ly/HackernoonFB), or simply, [tweet/DM @HackerNoon.](https://goo.gl/k7XYbx) > If you enjoyed this story, we recommend reading our [latest tech stories](http://bit.ly/hackernoonlatestt) and [trending tech stories](https://hackernoon.com/trending). Until next time, don’t take the realities of the world for granted!