Is there a working C++ grammar file for ANTLR?

自作多情 提交于 2019-12-05 17:22:28

问题


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?


回答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.]



来源:https://stackoverflow.com/questions/2329558/is-there-a-working-c-grammar-file-for-antlr

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!