[antlr-interest] C++ parser usage ideas

Jim Crafton jim.crafton at gmail.com
Wed Oct 12 19:58:13 PDT 2005


I working on a C++ IDE (yes, yet *another* IDE project :) ).
I am using antlr(2.7.5) and the C++ grammar provided by David Wigg and
friends to handle the job of taking a chunk of C++ and parsing it into
an AST.

My first problem (which I have resolved) was that the parser couldn't
handle macro code at all. so something like:

class MY_API FooBar {
//code here
};

would cause exceptions to be thrown. I found a preprocessor library
which seems to work quite well, that can replace these macros with
their real values.

I can create my custom code graph from this modified text. Each node
in this graph contains the line it's on, and it's column, or start
position.

So now I have another problem, which I am hoping folks here may have
some ideas about how best to tackle:

All the line/col positions are based on the *modified*, pre-processed
code. Ideally I want this information so that I can use it, say, to
position the cursor at a given position in the editor, or to
replace/modify a chunk of text that corresponds to that node. But the
"real" positions need to be based on the original code, so I need some
sort of translation back from one (parsed code) to the other (original
code).

Has anyone done anything like this? Is there a better way to tackle
this? Is there a way to modify the orignal C++ grammar to just skip
over the macros entirely (this would be great, as it would get around
the whole issue). Apologies if this isn't strictly antlr specific, and
if it's inapropriate, just let me know.

Thanks

Jim Crafton


PS: This is for David: I have made some changes to the grammar, if
you'd like them please let me know where to send them to, and what the
best way to do that is. And thanks for GREAT head start on this!


More information about the antlr-interest mailing list