views:

277

answers:

5

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"
+1  A: 

Note: you could use another approach and check out XText for generating all the code for you.

From the XText documentation:

In contrast to common parser generators (like e.g. JavaCC or ANTLR), Xtext derives much more than just a parser and lexical analyzer (lexer) from an input grammar. The grammar language is used to describe and generate:

  • an incremental, ANTLR 3 based parser and lexer to read your models from text,
  • Ecore models (optional),
  • a serializer to write your models back to text,
  • a linker, to establish cross links between model elements,
  • an implementation of the EMF Resource interface with full support for loading and saving EMF models, and
  • an integration of the language into your Eclipse IDE.

Once all the parts generated, you can analyze them (and test them) as you want.

alt text

VonC
A: 

Why don't you define a grammar for a really simple language which matches your example and try to implement it? Use ANTLR to create the parse tree, and then figure out how to "evaluate" the tree. To start with, don't bother with declarations or data types (every value is an integer) and support 3 statement types: assignment, if statement and print.

Stephen C
+2  A: 

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/

Stuart Sierra
I didn't test it, but browsed through your google-page: looks nice, and well documented!
Bart Kiers
I just wanted to add that although I'm using ANTLRv3, your language there has been an invaluable learning aide.
etheros
+6  A: 

[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...

Terence

Terence Parr
Although *Language Implementation Patterns* very briefly mentions how ANTLR works. So, you really should have recommended `etheros` to first buy your *ANTLR reference* book, and only then get the *Language Implementation Patterns* one. :)
Bart Kiers
I had a look at the code for the 'Q' language from that link, and it's exactly what I was looking for! Thanks for the link, and I look forward to purchasing the book.
etheros
+1  A: 

Scott "JavaDude" Stanchfield's video tutorial series on ANTLR3 is pretty good.

And of course, there is StackOverflow, which has tons of ANTLR content. For example:

ANTLR: Is there a simple example?

Jörg W Mittag