0

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.

phreed
  • 1,759
  • 1
  • 15
  • 30
  • 1
    Please take some time to refresh [the help pages](http://stackoverflow.com/help), especially ["What topics can I ask about here?"](http://stackoverflow.com/help/on-topic) – Some programmer dude Oct 03 '22 at 17:22
  • You are looking for an Antlr parse tree rewriter. No, there is nothing currently available, but I am working on one. ST is not a good choice in your use case because you would need to write a rule for every parse node type in order to output it. – kaby76 Oct 04 '22 at 03:36
  • Actually, I am not specifically looking for an Antlr parse tree rewriter. I have several parse trees produced by different parsers (i.e. Protobuf, Gremlin, Antlr, etc.). I suppose I could use Antlr to convert the parse trees I have into an Antlr parse tree. – phreed Oct 04 '22 at 19:29
  • Yes, recomputing the source from an AST (e.g., Protobuf, Gremin) representation of a program would best be done using ST. Recomputing the source from a CST is just the frontier of the tree (or serialization of the token stream or just the text of the char stream of the parse); ST is not needed. An Antlr grammar specifies an Antlr parser, which when computed, results in a CST. It cannot help in generating ST templates for Protobuf/Gremlin because the grammar knows nothing about either of those AST representations. The ST templates would need to be written by hand. – kaby76 Oct 05 '22 at 11:29

0 Answers0