7

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?

1
  • Just curious, what did you end up doing? JavaCC can generate C++ output.
    – boggy
    Commented Oct 19, 2015 at 18:08

4 Answers 4

8

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).

1
  • Language targets in ANTLR 4 are now fully independent projects. The reference implementation is only Java, and a C# target is now available as well. The C++ target will be available if/when it is contributed by an independent developer/team. Commented Jan 5, 2014 at 22:43
3

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.

3
  • 1
    Until a full C/C++ generation target is being provided, I would have to use another approach then... somehow lame. Commented Feb 16, 2013 at 19:22
  • 1
    Lame, really? Anyway, you could go with ANTLR 3. The grammar syntax isn't all that different, and when a C/C++ is provided for v4, you could switch to the newer version?
    – Bart Kiers
    Commented Feb 16, 2013 at 19:37
  • Are there many differences between v3 and v4? I thought that version 4 is much more efficient and with the book as a reference for the newest release I didn't want to work with the old version. I think about just piping the output from Java as input to C++ with a call to Java until there is a solution to include everything without using Java. Commented Feb 16, 2013 at 19:42
3

In case you are still interested, version 4.7 of antlr does have a c++ target.

1

To John G. answer

  • I agree that ANTLR3 C target is very hacking. Me, C/C++ expert with 20 years, was not able even guess how to use it without answers from author. Yes, ideas was quite good, but without docs near to impossible to understand.

  • I not agree that main trouble with exceptions. In times of ANTLR2 and C++ implementation fir v2, exceptions did exists ... And there was opinion that if to remove exceptions it will be faster. In v3 they have try do that, but ...

  • But speed have not became better. We was in hope switch from ANTRL2 to ANTLR3 in our Valentina Database engine, we have spend months re-writing to v3 grammar, and ... zero speed up. Just zero. So we use up to now v2 of ANTLR.

  • I think main issue if speed in ANTLR is the fact that for each rule it produce separate function. Yes this is its strong side, and this is its weak side.

In the v4 Terrence have invent how to use state machines in Lexer. If we could get that for parsers also. I think in ideal, ANTLR could produce functions as now, while we develop grammar, and state machines for release. But this is a dream so far.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.