Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Integrating ANTLR 4 in a C++ application

Tags:

c++

antlr

dsl

Recently I picked up a copy of The Definitive ANTLR 4 Reference and since I am sophisticated when it comes to working with grammars and languages I wanted to work on my DSL I once have written using yacc and bison. The general idea is to write a translator (with included validation for type safety(1)) which translates the DSL to JavaScript during runtime which is then executed by v8.

Although ANTLR was designed for inclusion in Java applications I would like to stay with native C++. Can ANTLR 4 produce such a C parser/lexer(2) which I can include using a C++-style wrapper? And how to do so?


(1) The book has some good examples which I will use as a template.
(2) I am not sure but I think that I read somewhere that ANTLR doesn't support output in C++, am I right?

like image 302
Christian Ivicevic Avatar asked Feb 16 '13 18:02

Christian Ivicevic


2 Answers

I found the ANTLR 3 C/C++ target almost unusable. It contains so many hacks to circumvent the lack of exceptions in C that it was recommended for experts only. Though it's Terr's call, I hope ANTLR 4 doesn't support target languages without native exceptions unless it can isolate whatever hackery is required to do so from end users. The ANTLR 2 C++ target is cleaner than ANTLR 3's, but ANTLR 2 itself has limitations, including extremely messy licensing (making it difficult to use in commercial products).

like image 141
John G. Avatar answered Oct 13 '22 21:10

John G.


ANTLR v3 has various different targets, most notably Java (of course), C, C#, JavaScript and Python. For a full list, see: http://www.antlr.org/wiki/display/ANTLR3/Code+Generation+Targets

ANTLR v4, however, only has a Java target at this moment.

like image 32
Bart Kiers Avatar answered Oct 13 '22 20:10

Bart Kiers