开发者

Are there any simple languages implemented using ANTLR or similar?

开发者 https://www.devze.com 2022-12-23 08:01 出处:网络
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 us

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 to开发者_如何学编程ols such as ANTLR, or similar?

Something like the following might be nice:

x = 1

if x == 1  
     print "true"


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


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?


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 http://www.eclipse.org/Xtext/documentation/0_7_2/images/getting-started-editor.png


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消