<div dir="ltr"><div>I want to do a similar thing. I'm using slightly adapted versions of <a href="http://www.antlr.org/grammar/ANTLR/ANTLRv3.g">ANTLRv3.g</a> and <a href="http://www.antlr.org/grammar/ANTLR/ANTLRv3Tree.g">ANTLRv3Tree.g</a> to regenerate ANTLRv3 grammars. I have extended the walker (ANTLRv3Tree) with template rewrite rules to regenerate the original antlr grammar, parsed with antlrv3.g. It works great, except for the comments. These are placed in a hidden channel and are not seen by the walker and thus not given to a template rewrite rule. I realize it is not appropriate to place the comments in the tree, because comments can be put everywhere and this would make the parser to complex. But how exactly do I tell my walker to look for tokens in the hidden channel or a self defined channel. Can someone give an example, because I really don't know where to begin or what syntax to use?<br>
<br><div style="margin-left: 40px;">"you can search within the start and stop tokens for the AST rule and find anything on channel 2, doing with it as you will."<br></div><br>How and where exactly do I need to do this? In ANTLRv3Tree.g itself and if so with what syntax? Or is it done somewhere else in java code? I thought an AST rule didn't have a stop token, only start?<br>
<br>An example what I'm trying to do:<br></div>parsing of a grammar: <i> r: INTEGER ; //comments</i><br>ANTLRv3.g makes a tree: <i> (RULE r (BLOCK (ALT INTEGER EOA) EOB) EOR)</i><br>Now from this tree I reconstruct the grammar but I get <i> r: INTEGER ; </i>thus the comments are gone.<br>
<div>When I walk this tree in ANTLRv3Tree.g the rule "rule" is used. Should I add something to "rule" in ANTLRv3Tree.g?<br><br>Sorry if this is a basic question, but an example would make things much clearer.<br>
<br>Jens<br><br><div class="gmail_quote">2008/7/14 Jim Idle <<a href="mailto:jimi@temporal-wave.com">jimi@temporal-wave.com</a>>:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
<div><div class="Ih2E3d">
On Mon, 2008-07-14 at 12:43 +0530, <a href="mailto:nilesh.kapile@tcs.com" target="_blank">nilesh.kapile@tcs.com</a> wrote:<br>
<blockquote type="CITE">
<br>
<font size="2">Hello,</font> <br>
<br>
<font size="2">I need to preserve comments and want to collect it in some data structure. How can we do that in ANTLR?</font> <br>
<br>
<font size="2">Currently, I've following rule for comments:</font> <br>
<br>
<font size="2">LINE_COMMENT</font> <br>
<font size="2"> : '//' ~('\n'|'\r')* '\r'? '\n' {$channel=HIDDEN;}</font> <br>
<font size="2"> ;</font> <br>
</blockquote></div>
The easiest way os to use your own channel, say 2, which means the parser will ignore them but they are preserved in the input stream (actually they are when HIDDEN but that really means 'anything you want to hide' and you specifically want to inspect comments. Then, when you walk your tree (assuming you are using a tree but that is best), at any point where the comments are required, you can search within the start and stop tokens for the AST rule and find anything on channel 2, doing with it as you will. You can also do this from the parser of course.<br>
<br>
The other option is to pass the COMMENT token through as a normal token, and create parser rules to assemble them at various points. The problem here comes when the COMMENT can be anywhere, such as in the middle of expressions, so the parser ends up having the COMMENT token everywhere and complicates your grammar enormously.<br>
<font color="#888888">
<br>
Jim
</font></div>
</blockquote></div><br></div></div>