[antlr-interest] Making a translator with C runtime and no string templates
Neha V. Karanjkar
neha.karanjkar at gmail.com
Fri Jul 1 00:05:07 PDT 2011
Hi.
I'm making a translator using antlr (with C runtime)
for a small language, which is somewhat like a hardware
description language(VHDL/Verilog).
I'm new to antlr, and (I think) string templates cannot be used
with a C target, so I'm using a scheme(described below) to generate code.
I'm not sure if this is the best way to go.
I'd appreciate any advice/comments.
----------
The input consists of a set of module descriptions.
Each module description should get translated to a C++ class
into a .cpp and a .h file.
As I cannot use string templates, I'm using 2 sets of Lexer+Parser.
One parses the input and fills up a data structure.
The second parses a template for generating the
.cpp/.h files and uses this template, along with the data structure,
to generate code.
The first parser has to instruct the second one
to dump code, after parsing *each* module description
as follows:
input : (module_description //fill up some data structure
{ask code generator to dump code}
) + <EOF>;
The template can have:
*plain text to be dumped as it is
*fields whose values have to be looked up from the data structure,
*instructions to take certain actions, like change destination file
I have the following questions:
1. Is there a better way of doing this?
(I don't want to use a lot of printfs to generate code)
2. For the code generation part, the template remains unchanged.
is there a way to reuse the token stream?
I'm using 3.1.4 SNAPSHOT of the C runtime.
I tried tstream->reset(), CodeGenerator_psr->reset(),
but it doesn't work.
Thanks,
-Neha Karanjkar
More information about the antlr-interest
mailing list