English 中文(简体)
llvm-clang: incremental or online parser?
原标题:

Is there anyway to use the llvm-clang parser in an incremental/online manner?

Say I m writing an editor and I want to be able to parse the C++ code I have in front of me.

I don t want to write my own hacked up parser.

I d like to use something full featured, like llvm-clang.

Is there an easy way to hijack the llvm-clang parser? (And is it fast enough to run it continuously in the background)?

Thanks!

问题回答

I don t think clang can incrementally parse C++ files, but it s one of this project goals: http://clang.llvm.org/features.html

I ve written something similar for my final year project. It wasn t C++ editor, but a Visual Studio plugin, which main task was improving C++ intellisense (like Visual Assist X).

When I was writing this project I ve been also thinking about C++ incremental parser, but I haven t found any suitable solution. To solve the C++ intellisense problem I used normal C++ parser from GCC. However it was to slow, to parse file after each code completion request (ctrl+space), just try including boost::spirit. To make this project work properly I parsed files in the background and after each code completion request I compared current file with it s previous version (via diff) to detect changes made from last parsing. Having those changes I updated syntax tree, mostly by adding or removing variables.

Except incremental parsing, there is also another problem with projects like this. Mostly you ll be parsing C++ code which is being edited so it s invalid code. Given the complex C++ grammar, sometimes parser won t be able to recover from syntax errors, so it won t detect correctly some symbols in code.

Another issue are C++ parsers / compilers differences. Let s say I m using working in Visual Studio and I have used some VC++ compiler specific contruction in my code. Clang parser won t be able to parse it correctly.

For writing something similair to IntelliSense, I would advise you to write your own parser using the LALR parsing algorithm. Since you can save its state in each line so you don t have to reparse the whole file when a file has been editted, which is very fast!
Note that C++ can t be fully expressed in BNF, but I think you could get pretty far with some adjustments. It s ofcourse a lot more work than using Clang s frontend, but you could still use Clang for analysing header files in coöperation with you own written parser.





相关问题
Parse players currently in lobby

I m attempting to write a bash script to parse out the following log file and give me a list of CURRENT players in the room (so ignoring players that left, but including players that may have rejoined)...

How to get instance from string in C#?

Is it possible to get the property of a class from string and then set a value? Example: string s = "label1.text"; string value = "new value"; label1.text = value; <--and some code that makes ...

XML DOM parsing br tag

I need to parse a xml string to obtain the xml DOM, the problem I m facing is with the self closing html tag like <br /> giving me the error of Tag mismatch expected </br>. I m aware this ...

Ruby parser in Java

The project I m doing is written in Java and parsers source code files. (Java src up to now). Now I d like to enable parsing Ruby code as well. Therefore I am looking for a parser in Java that parses ...

Locating specific string and capturing data following it

I built a site a long time ago and now I want to place the data into a database without copying and pasting the 400+ pages that it has grown to so that I can make the site database driven. My site ...

热门标签