ANTLR4 C target or compatibility?

47 views
Skip to first unread message

Tom Merchat

unread,
Jul 5, 2019, 4:47:04 PM7/5/19
to antlr-discussion
Hi guys

Thanks for all the effort towards ANTLR, it is truly a wonderful tool.

I'm developing a language and I have some limitation on the code generation target. It must be either C, or C++ with plain old data structures (POD).

I was wondering whether there are any plans for a C target in the near future. Also, I would be grateful for any resources or advice to help me achieve either case because as far as I am aware, ANTLRv4 doesn't support either. Would it be worth the development effort of generating a C++ target and changing it to POD? Can I use some kind of tool to facilitate this?

Thanks in advance.

Best,
Tom

Jason Filby

unread,
Jul 6, 2019, 7:37:13 AM7/6/19
to antlr-discussion
Hi,

There is already a C++ code generation target, you can see it listed on ANTLR's download page: https://www.antlr.org/download.html

For C I would suggest writing your visitor code in C++ first and then writing a C API that wraps your C++ class where you have your AST.

Jason

Tom Merchat

unread,
Jul 6, 2019, 9:44:38 AM7/6/19
to antlr-discussion
Hi

Thanks for your reply.

I would like to emphasize again that I'm not looking for C++. I am looking for C target or a special kind of C++ target with POD types. The reason is that I will be calling legacy C functionality from ANTLR's C++ code (visitor/listener) and therefore I have such a restriction in place.

So I'm not sure how much I can apply your advice. :(

Tom

Jason Filby

unread,
Jul 7, 2019, 5:56:32 AM7/7/19
to antlr-discussion
What I wrote about wrapping a C++ class with a C API is essentially C++ with POD types/functions. But instead of calling C from ANTLR's C++ code (which is actually your C++ listener code called by ANTLR since you don't modify ANTLR itself) you would have a separate C++ class wrapped in C functions. Your legacy code would then call those C functions. Please check the examples I listed to see how this can be done, not specifically for this case, but for wrapping C++ classes in C functions in general.

This is the proposed flow of code/data:

ANTLR C++ target --> custom listener functions --> C++ AST class --> C types/functions wrapper --> Legacy C code

1. ANTLR C++ target will call custom functions that are implemented.
2. Custom listener functions build up an AST in a C++ class.
3. C functions access the AST C++ class and return data in C types.
4. Legacy C code calls the wrapper C functions.

Jason
Reply all
Reply to author
Forward
0 new messages