I'm trying to build a simple interpreted language for learning purposes. I've read countless theory and tutorials on ANTLR and JavaCC, but I can't figure out how to actually make it do something useful.
I learn best by "taking something apart and putting it back together again", so, are there any working examples of simple languages implemented with the help of tools such as ANTLR, or similar?
Something like the following might be nice:
x = 1
if x == 1
print "true"
It turns out ANTLR4 lets you generate parser code in a variety of languages: Java, C#, Python, Go, C++, Swift, JavaScript and even TypeScript!
ANTLR, ANother Tool for Language Recognition, (formerly PCCTS) is a language tool that provides a framework for constructing recognizers, compilers, and translators from grammatical descriptions containing Java, C#, C++, or Python actions.
ANTLR is a powerful parser generator that you can use to read, process, execute, or translate structured text or binary files. It's widely used in academia and industry to build all sorts of languages, tools, and frameworks. Twitter search uses ANTLR for query parsing, with over 2 billion queries a day.
What you need to do to get a parse tree: define a lexer and parser grammar. invoke ANTLR: it will generate a lexer and a parser in your target language (e.g., Java, Python, C#, JavaScript) use the generated lexer and parser: you invoke them passing the code to recognize and they return to you a parse tree.
[shameless plug] Why not buy my Language Implementation Patterns book? I have all the pieces you need to put together a language including multiple interpreters etc...
You can probably find lots of small languages on Google Code and Github.
Here's a toy language that I did in ANTLR as a class project a few years ago: http://code.google.com/p/bcis/
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With