I'm trying to build a parser for Promela in llvm. I have the parser SPIN uses, which is built using yacc, including the input that goes to yacc. Is there a way to use the yacc parser to quickly and painlessly generate a clang/llvm parser? I will be using it to generate call graphs and perform static analysis.
-
3What do you mean by "clang/llvm parser"? A parser that creates LLVM bitcode as its output? One that generates an AST that is then translated to LLVM bitcode? Sure you can do that, that's not an uncommon thing to do. Clang wouldn't have anything to with that though. If you want to generate a Clang AST, the answer is "kinda, but it'd be a bad idea". The Clang AST is specifically designed for C-family languages and you can't easily represent other languages in it. So creating a Clang AST for a non-C language would mean translating it to C. Also you don't need clang for a call graph - just LLVM. – sepp2k Jul 05 '19 at 12:53
-
I was hoping to find a way to use the existing yacc parser to build a clang parser (and therefore AST) with minimal effort – Brishna Batool Jul 08 '19 at 08:09
-
The term "a clang parser" still confuses me. Can you clarify what you mean by that? In my world there is *the* clang parser, which parses C, C++ and Objective C into a C-family specific AST, which is later translated to LLVM bitcode. So when you say that you want to build "a clang parser", do you mean that you want your parser to generate the same AST as the clang parser (which I think is a bad idea as explained in my previous comment) or something else? – sepp2k Jul 08 '19 at 10:37
-
I was thinking of clang+llvm as a complete framework, and wasn't properly aware of their difference/boundary, so thanks for pointing that out. My goal is to perform static analyses on Promela, starting with generating an AST. I wanted to do it on LLVM because of the wealth of analysis tools available. As you have explained, it seems clang has no place in this process. What I need to know now is whether I can use the existing Promela compiler, which was built with yacc, to quickly build a parser (and later, IR generator) using the llvm framework. – Brishna Batool Jul 12 '19 at 09:25
1 Answers
What I need to know now is whether I can use the existing Promela compiler, which was built with yacc, to quickly build a parser (and later, IR generator) using the llvm framework.
Yes, you can re-use the existing YACC-grammar (and if you want even the existing AST) for your project. "Building a parser using the llvm framework" is a bit misleading though because LLVM won't have anything to do with parsing and the AST. LLVM won't enter into it until you generate the LLVM IR and then work with it.
So you either take the existing YACC grammar and the existing AST or you only take the grammar and replace the actions with ones that create your own AST that you've defined yourself. Either way that part won't involve LLVM.
Then you'd write a separate phase that walks the AST and generates LLVM IR using the LLVM API, on which you can then run all the transformations and analyses supported by LLVM.

- 363,768
- 54
- 674
- 675
-
2Alternately, you can take the YACC grammar and write actions that generate LLVM IR using the LLVM API directly, bypassing the AST. – Chris Dodd Jul 14 '19 at 17:10