Stephen Gaito | 8 Apr 2012 16:02
Picon
Favicon

Debuging ANTLR3.4.1-snapshot C target ASTTreeParse.stg

Hello,

As part of my ongoing work to build a LaTeXCreole filter for PHP/Drupal, I have gone down the route of building AST trees AST trees using a tree grammar.

My current grammars antlr-compile in Java but I noticed they did not antlr-compile in the C target.

I have traced the problem to 11 characters missing in three places in the C target ASTTreeParse.stg string template.

(I was pleasantly surprised at how little time it took me to diagnose these two problems.... this is entirely down to the cleanness of ANTLR3's architecture.... while there is a lot of code, there are a lot of examples with which to intuit your original intent... so again, many thanks for the excellent work and thought of both Terence and Jim!)

There were two overall problems:

(1)    The antlr compiler was complaining that two anonymous templates were missing arguments:

<at> <at>  -319,7 +319,7  <at> <at>  if ( <actions.(actionScope).synpredgate> ) {<\n>
 }
 <endif>
 <ruleDescriptor.allTokenRefsInAltsWithRewrites
-    :{if (stream_<it> != NULL) stream_<it>->free(stream_<it>);}; separator="\n">
+    :{it | if (stream_<it> != NULL) stream_<it>->free(stream_<it>);}; separator="\n">
 <ruleDescriptor.allRuleRefsInAltsWithRewrites
-    :{if (stream_<it> != NULL) stream_<it>->free(stream_<it>);}; separator="\n">
+    :{it | if (stream_<it> != NULL) stream_<it>->free(stream_<it>);}; separator="\n">
 >>

(2)    The antlr compiler was complaining that /noRewrite was called without its declared two arguments:
<at> <at>  -209,7 +209,7  <at> <at>  ADAPTOR->addChild(ADAPTOR, root_<treeLevel>, <label>_tree);
 
 matchRuleBlockSet(s,label,terminalOptions,elementIndex,postmatchCode,treeLevel="0") ::= <<
 <matchSet(...)>
-<noRewrite()> <! set return tree !>
+<noRewrite(...)> <! set return tree !>
 >>
 
 matchSetBang(s,label,terminalOptions,elementIndex,postmatchCode) ::= <<

Both of these patches are on the file:

tool/src/main/resources/org/antlr/codegen/templates/C/ASTTreeParser.stg

in the gitHub stephengaito/antlr3 fork of the main antlr/antlr3 project.


As per my previous bug report:

[antlr-dev] Inconsistencies between ANTLR 3.4 C.stg and ANTLR gitHub C.stg versions

I am happy to either raise a gitHub issue (on antlr/antlr3 or antlr/antlr4) or issue a pull request.  Please let me know how you would like me to deal with this.

Regards,

Stephen Gaito

<div>
    Hello,<br><br>
    As part of my ongoing work to build a LaTeXCreole filter for
    PHP/Drupal, I have gone down the route of building AST trees AST
    trees using a tree grammar.<br><br>
    My current grammars antlr-compile in Java but I noticed they did not
    antlr-compile in the C target.<br><br>
    I have traced the problem to 11 characters missing in three places
    in the C target ASTTreeParse.stg string template.<br><br>
    (I was pleasantly surprised at how little time it took me to
    diagnose these two problems.... this is entirely down to the
      cleanness of ANTLR3's architecture.... while there is a lot of
    code, there are a lot of examples with which to intuit your original
    intent... so again, many thanks for the excellent work and thought
    of both Terence and Jim!)<br><br>
    There were two overall problems:<br><br>
    (1)&nbsp;&nbsp;&nbsp; The antlr compiler was complaining that two anonymous
    templates were missing arguments:<br><br><blockquote type="cite">
      <table cellpadding="0" cellspacing="0" width="100%">
<tr data-position="0">
<td width="100%">
              <div class="gc"> <at>  <at> &nbsp;-319,7&nbsp;+319,7&nbsp; <at>  <at> &nbsp;if&nbsp;(&nbsp;&lt;actions.(actionScope).synpredgate&gt;&nbsp;)&nbsp;{&lt;\n&gt;</div>
            </td>
          </tr>
<tr data-position="1">
<td width="100%">
              <div class="">&nbsp;}</div>
            </td>
          </tr>
<tr data-position="2">
<td width="100%">
              <div class="">&nbsp;&lt;endif&gt;</div>
            </td>
          </tr>
<tr data-position="3">
<td width="100%">
              <div class="">&nbsp;&lt;ruleDescriptor.allTokenRefsInAltsWithRewrites</div>
            </td>
          </tr>
<tr data-position="4">
<td width="100%">
              <div class="gd">-&nbsp;&nbsp;&nbsp;&nbsp;:{i<span class="x"></span>f&nbsp;(stream_&lt;it&gt;&nbsp;!=&nbsp;NULL)&nbsp;stream_&lt;it&gt;-&gt;free(stream_&lt;it&gt;);};&nbsp;separator="\n"&gt;</div>
            </td>
          </tr>
<tr data-position="5">
<td width="100%">
              <div class="gi">+&nbsp;&nbsp;&nbsp;&nbsp;:{i<span class="x">t&nbsp;|&nbsp;i</span>f&nbsp;(stream_&lt;it&gt;&nbsp;!=&nbsp;NULL)&nbsp;stream_&lt;it&gt;-&gt;free(stream_&lt;it&gt;);};&nbsp;separator="\n"&gt;</div>
            </td>
          </tr>
<tr data-position="6">
<td width="100%">
              <div class="">&nbsp;&lt;ruleDescriptor.allRuleRefsInAltsWithRewrites</div>
            </td>
          </tr>
<tr data-position="7">
<td width="100%">
              <div class="gd">-&nbsp;&nbsp;&nbsp;&nbsp;:{i<span class="x"></span>f&nbsp;(stream_&lt;it&gt;&nbsp;!=&nbsp;NULL)&nbsp;stream_&lt;it&gt;-&gt;free(stream_&lt;it&gt;);};&nbsp;separator="\n"&gt;</div>
            </td>
          </tr>
<tr data-position="8">
<td width="100%">
              <div class="gi">+&nbsp;&nbsp;&nbsp;&nbsp;:{i<span class="x">t&nbsp;|&nbsp;i</span>f&nbsp;(stream_&lt;it&gt;&nbsp;!=&nbsp;NULL)&nbsp;stream_&lt;it&gt;-&gt;free(stream_&lt;it&gt;);};&nbsp;separator="\n"&gt;</div>
            </td>
          </tr>
<tr data-position="9">
<td width="100%">
              <div class="">&nbsp;&gt;&gt;</div>
            </td>
          </tr>
</table>
</blockquote>
    <br>
    (2)&nbsp;&nbsp;&nbsp; The antlr compiler was complaining that /noRewrite was called
    without its declared two arguments:<br><blockquote type="cite">
      <table cellpadding="0" cellspacing="0" width="100%">
<tr data-position="0">
<td width="100%">
              <div class="gc"> <at>  <at> &nbsp;-209,7&nbsp;+209,7&nbsp; <at>  <at> &nbsp;ADAPTOR-&gt;addChild(ADAPTOR,&nbsp;root_&lt;treeLevel&gt;,&nbsp;&lt;label&gt;_tree);</div>
            </td>
          </tr>
<tr data-position="1">
<td width="100%">
              <div class="">&nbsp;</div>
            </td>
          </tr>
<tr data-position="2">
<td width="100%">
              <div class="">&nbsp;matchRuleBlockSet(s,label,terminalOptions,elementIndex,postmatchCode,treeLevel="0")&nbsp;::=&nbsp;&lt;&lt;</div>
            </td>
          </tr>
<tr data-position="3">
<td width="100%">
              <div class="">&nbsp;&lt;matchSet(...)&gt;</div>
            </td>
          </tr>
<tr data-position="4">
<td width="100%">
              <div class="gd">-&lt;noRewrite(<span class="x"></span>)&gt;&nbsp;&lt;!&nbsp;set&nbsp;return&nbsp;tree&nbsp;!&gt;</div>
            </td>
          </tr>
<tr data-position="5">
<td width="100%">
              <div class="gi">+&lt;noRewrite(<span class="x">...</span>)&gt;&nbsp;&lt;!&nbsp;set&nbsp;return&nbsp;tree&nbsp;!&gt;</div>
            </td>
          </tr>
<tr data-position="6">
<td width="100%">
              <div class="">&nbsp;&gt;&gt;</div>
            </td>
          </tr>
<tr data-position="7">
<td width="100%">
              <div class="">&nbsp;</div>
            </td>
          </tr>
<tr data-position="8">
<td width="100%">
              <div class="">&nbsp;matchSetBang(s,label,terminalOptions,elementIndex,postmatchCode)&nbsp;::=&nbsp;&lt;&lt;</div>
            </td>
          </tr>
</table>
</blockquote>
    <br>
    Both of these patches are on the file:<br><br><blockquote type="cite"><a href="https://github.com/stephengaito/antlr3/commit/888b90b3d81af76c152ae73abed32e3662094011#diff-0">tool/src/main/resources/org/antlr/codegen/templates/C/ASTTreeParser.stg</a></blockquote>
    <br>
    in the gitHub stephengaito/antlr3 fork of the main antlr/antlr3
    project.<br><br><br>
    As per my previous bug report:<br><br><blockquote type="cite"><a href="http://www.antlr.org/pipermail/antlr-dev/2012-April/002732.html">[antlr-dev]
        Inconsistencies between ANTLR 3.4 C.stg and ANTLR gitHub C.stg
        versions</a></blockquote>
    <br>
    I am happy to either raise a gitHub issue (on antlr/antlr3 or
    antlr/antlr4) or issue a pull request.&nbsp; Please let me know how you
    would like me to deal with this.<br><br>
    Regards,<br><br>
    Stephen Gaito<br><br>
</div>

Gmane