hi. Sorry for the delay. I'm going to use voice recognition here to describe 
what's going on. You can now look in the depot 

        read user mwright * //depot/code/antlr4/main/...

for a look at what I'm doing. If you look at the org.antlr.v4.codegen dir, and 
the subdirectory model, you'll find the source related to code generation.

 Basic idea is that I parse grammar into an AST and then create a graph ( 
augmented transition network) representation that is a lot like a syntax 
diagram version of the grammar. This ATN gets serialized and generated in 
parsers and lexers, just FYI. I do the necessary semantic analysis and so on to 
figure out what grammar means. Then, to generate code, I create a model of the 
output using the objects in the model package. Then, an automatic walker 
traverses this model and instantiates a template with the same name as the 
model object. I have some cleanup work to do and will add an annotation that 
says which of the object fields should be traversed by the object model walker.

The model is not necessarily inherently imperative, but there is probably a lot 
of subtle imperative stuff in there. At the highest level, I create a 
ParserFile which contains a Parser model object and a set of named actions 
consisting of Action objects. A parser has lots of things including a set of 
RuleFunction objects, which in turn, have a series of actions, including 
InvokeRule and MatchToken.

The templates are much simpler because my code generator is creating a very 
explicit output model. Templates should only say how to spit out that object in 
text. The previous v3 code generator required a huge amount of thinking inside 
the template (that's what I get for allowing nested IFs in StringTemplate! ;)).

Anyway, this is a start and you could look at the templates in Java.stg to see 
how much simpler they are.  We should be able to generate Haskell no problem.

You can also look at the source code at:

http://antlr.org/depot/antlr4/main
http://antlr.org/depot/antlr4/main/tool/src/org/antlr/v4/codegen

Ter

_______________________________________________
antlr-dev mailing list
[email protected]
http://www.antlr.org/mailman/listinfo/antlr-dev

Reply via email to