StringTemplates for C++

172 Views Asked by At

There are plenty of Antlr4 grammars available, e.g. https://github.com/antlr/grammars-v4/tree/master/cpp

As a use-case consider parsing a C++ source file, using the corresponding Antlr4 grammar, and writing the resulting AST back out with ST4-C++-templates. I am particularly interested in the C++/C99 grammar.

I am not looking for a general purpose Antlr parse tree rewriter. If such templates do not already exist...

This puzzle can be answered in a couple of ways:

  • Do corresponding ST4 templates for these Antlr grammars?
  • How to generate ST4 templates from an Antlr grammar?

note:: The actual situation involves generating C++ from parse trees from non-Antlr parses. The pretty-print example is just illustrative so, please do not recommend using an existing pretty printer.

Here is an fragment of the CPP Antlr grammar.

declaration:
    blockDeclaration
    | functionDefinition
    | templateDeclaration
    | explicitInstantiation
    | explicitSpecialization
    | linkageSpecification
    | namespaceDefinition
    | emptyDeclaration
    | attributeDeclaration;

This implies a ST that might look like:

declaration(obj) ::= << 
  <declartionDict.(obj.type)>
>>

This would need a dictionary of templates for the different types of declarations.

0

There are 0 best solutions below