Writing out ASTs / Preserving comments / Handling erroneous tokens
I currently have a set of 3 or 4 grammars that are a work in progress that read stored procedures from various DB vendors and output a fairly generic AST. The AST from each vendor is then re-written via a tree grammar and lots of tree re-writing (again a work in progress) into a standard IngresDB AST, which a final tree grammar then prints out in Ingres DB Procedure syntax. I am currently working towards something which will eventually be released as an open source project and wondered if the community might be able to help me solve a few issues.
1) What is the best way for my final tree grammar to output the specific syntax I need. At the minute I have a tree grammar which has lots of {System.out.print($a.text)} in and amongst the tree nodes and if statements which check input.LA(1) and add comma's etc, etc. It makes the grammar harder to read. Is there a better way of doing this? The StringTemplates looked like they might be the solution but that is another thing to learn and I also don't understand how the string templates print out your syntax once you have converted your AST to a StringTemplate?
2) I pass the comments in the initial grammars to the hidden stream. Can anyone point me at the right article to show how I can output these comments from the hidden stream at the right point in my final grammar?
3) As this is an organically growing process there are many things my grammars can not yet cope with, there are also features that some vendors support and others do not. I want to be able to pass these things into each grammar and kind of 'TODO' tree node so that I can output them in the final grammar, probably commented out with a message to the developer to point out they have some manual effort to perform. Should I use the error handling to do this? Can anyone point me at a good article or offer any advice on how I should achieve this?
Thanks in advance.
Stuart - A new ANTLR enthusiast!
<div>
<p dir="LTR"><span lang="en-gb">I currently have a set of 3 or 4 grammars that are a work in progress that read stored procedures from various DB vendors and output a fairly generic AST. The AST from each vendor is then re-written via a tree grammar and lots of tree re-writing (again a work in progress) into a standard IngresDB AST, which a final tree grammar then prints out in Ingres DB Procedure syntax. I am currently working towards something which will eventually be released as an open source project and wondered if the community might be able to help me solve a few issues.</span></p>
<p dir="LTR"><span lang="en-gb"></span></p>
<p dir="LTR"><span lang="en-gb">1) What is the best way for my final tree grammar to output the specific syntax</span><span lang="en-gb"> I</span><span lang="en-gb"> need. At the minute</span><span lang="en-gb"> I</span><span lang="en-gb"> have a tree grammar which has lots of {System.out.print($a.text)} in and amongst</span><span lang="en-gb"> the tree nodes and</span><span lang="en-gb"> if statements which check input.LA(1) and add comma's etc, etc. It makes the grammar harder to read. Is there a better way of doing this? The StringTemplates looked like they might be the solution but that is another thing to learn and</span><span lang="en-gb"> I</span><span lang="en-gb"> also don't understand how the string templates print out your syntax once you have converted your AST to a StringTemplate?</span></p>
<p dir="LTR"><span lang="en-gb"></span></p>
<p dir="LTR"><span lang="en-gb">2) I pass the comments in the initial grammars to the hidden stream. Can anyone point me at the right article to show how</span><span lang="en-gb"> I</span><span lang="en-gb"> can output these comments from the hidden stream at the right point in my final grammar?</span></p>
<p dir="LTR"><span lang="en-gb"></span></p>
<p dir="LTR"><span lang="en-gb">3) As this is an organically growing process there are many things my grammars can not yet cope with, there are also features that some vendors support and others do not. I want to be able to pass these things into each grammar and kind of 'TODO' tree node so that</span><span lang="en-gb"> I</span><span lang="en-gb"> can output them in the final grammar, probably commented out with a message to the developer to point out they have some manual effort to perform. Should</span><span lang="en-gb"> I</span><span lang="en-gb"> use the error handling to do this? Can anyone point me at a good article or offer any advice on how</span><span lang="en-gb"> I</span><span lang="en-gb"> should achieve this?</span></p>
<p dir="LTR"><span lang="en-gb"></span></p>
<p dir="LTR"><span lang="en-gb">Thanks in advance.</span></p>
<p dir="LTR"><span lang="en-gb">Stuart - A new ANTLR enthusiast!</span><span lang="en-gb"></span></p>
</div>