views:

409

answers:

8

I'd like to find a tree parser generator to help me transform ASTs for a compiler written in C++.

After some quick research I found out about ANTLR (which can be targeted to work with C but not C++). I also found a website that warns against using ANTLR with C++: http://www.bearcave.com/software/antlr/antlr_treeparse.html. The article is also ten years old so maybe its complaints are out-of-date.

Anyway, what I'd like to know is if there are any alternatives out there, or am I stuck with using ANTLR or writing my own?

A: 

You can use ANTLR to generate C code which can then be called from C++. I think the problems might arise if you are using it to generate C++ code itself. Even there I don't really know of any particular issues, we are using ANTLR to generate C++ code without any problems.

1800 INFORMATION
+4  A: 

There are plenty of other parser generators, but I have to say that after being unhappy with ANTLR for years, the newest version seems pretty good.

Charlie Martin
A: 

I just found this paper entitled "Language Translation Using PCCTS and C++ (A Reference Guide)": http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.2.9850

(It references both ANTLR and Sorcerer, which was the name of ANTLR's tree parser before it was ANTLR). This might be exactly what I need.

stonea
Afer looking through this it sounds like Sorcerer has a C++ interface but ANTLR does not. ANTLR incorporated Sorcerer and is more modern (and likely mature), but Sorcerer is still around at: http://www.polhode.com/pccts.html .
stonea
+2  A: 

A parser requires a stream of input tokens in a predefined manner from the lexer. For a tree parser, it requires the AST 'stream' to be defined so it can understand it. Hence, your parser and lexer must agree on an interface, and your tree parser and parser must also agree.

Tree parsing allows you to do transformations on AST trees - which as I'm sure you know is quite useful. The only c/c++ tree parser I've heard of is iBurg. I haven't used it, and I think tree parser are mostly used in academic circles. For most applications I've seen, one they have an AST, they do all their work directly on the tree. This means they also have to do their own matching rules.

brianegge
+3  A: 

Check out the spirit boost library, especially the chapter about Parse Trees and ASTs.

lothar
Spirit is cool, but debugging errors in the grammar is not...
Zifre
+1  A: 

We also use ANTLR generating C++ code for our compiler, no issues there. Sure the ANTLR doesn't directly support C++ but that doesn't technically prevent you from using it with C++.

ANTLR has also another advantage with the GUI ANTLRWorks which allows you to visible see your syntax. This can be a great help when you are developing a grammar.

And the best of all: its free. :) (that said, it does help buying the book)

Anders K.
+1  A: 

If you already have a compiler and what you want to do is use a library to walk / transform your AST I am not sure whether Antlr can handle it or was designed to do this.

In normal usage of Antlr the setting up of the AST is done with Antlr by defining a lexer and parser . One can then define tree walkers and transformations on this AST.

hugok
+1  A: 

You may be able to use Prop. It is a C++ extension that supports ML-like algebraic data types and pattern matching. It has a number of features that make writing compilers with it really fun.

Tree transformation is so much easier with pattern matching.

Unfortunately, development seems to be dead, but it works pretty well. I did a toy Scheme-like language with it, and it was really awesome. I haven't really had any problems with it.

Zifre