10

Are there any existing C++ grammar files for ANTLR?

I'm looking to lex, not parse some C++ source code files.

I've looked on the ANTLR grammar page and it looks like there is one listed created by Sun Microsystems here.

However, it seems to be a generated Parser.

Can anyone point me to a C++ ANTLR lexer or grammar file?

c14ppy
  • 247
  • 3
  • 6

1 Answers1

1

C++ parsers are tough to build.

I can't speak with experience about using ANTLR's C++ grammars. Here I discuss what I learned by reading the notes attached to the the one I did see at the ANTLR site; in essence, the author produced an incomplete grammar. And that was for just C++98. It has been awhile since I looked; there may be others.

Our DMS Software Reengineering Toolkit has a robust C++ front end.

The lexer handles all the cruft for ANSI, GCC3, MS Visual Studio 2008, including large-precision floating point numbers, etc.

[EDIT: 12/2011. Now handles C++11 and OpenMP directives]

[EDIT: 3/2015: Now handles C++14 in both GCC and MS variants. See some parse trees here on SO]

Having "just" a parser is actually not very useful. Above and beyond "just parsing", our front end will build ASTs, build accurate symbol tables (for C++, this is extremely hard to do), perform function-local flow analysis, and allow you to carry out program transformations, etc. See Life After Parsing.

[EDIT: 5/2019: Now handles C++17 in ANSI, GCC and MS variants. Does complete name and type resolution across compilation units. Used to automate large scale God-class refactoring/splitting across systems of 3000 compilation units.]

Ira Baxter
  • 93,541
  • 22
  • 172
  • 341
  • 3
    I checked out your website, seems like you have some cool tools at reasonable prices, but your website could do with some work in both structure and look and feel. – Andre Artus Jun 07 '10 at 11:49
  • @Andre: any constructive remarks you might make are welcome; we're always interested in improving. Please mail to "info@semanticdesigns.com". – Ira Baxter Nov 07 '10 at 19:02
  • How does your project compare to, say, [Clang](http://clang.llvm.org/), which does all you say, for free? I'm jesting, but still, interested in your answer! – rubenvb Mar 19 '15 at 12:02
  • If all you want is a parser for *preprocessed* C++, a tree builder for that C++, and non-composable procedural transforms on that AST, Clang is pretty good and free. If you want to parse C++ *retaining* the preprocessor directives, apply arbitrary strings of transforms done procedurally or with surface syntax rewrites (see http://www.semanticdesigns.com/Products/DMS/DMSRewrites.html), or you want process many other languages this same way, DMS is more effective than Clang. See http://www.semanticdesigns.com/Company/Publications/WCRE05.pdf for a task I've never seen anybody try with Clang. – Ira Baxter Mar 19 '15 at 14:05
  • For a more recent application of DMS which I think Clang would have a hard time, see discussion on automated refactoring of God classes: https://stackoverflow.com/a/56081753/120163 – Ira Baxter Jan 05 '21 at 13:30