This might not be an elegant fix. But it does fix the problem<div><br></div><div><div>13c13</div><div>&lt; &nbsp; &nbsp; &nbsp; : PROCEDURE IDENTIFIER SEMICOLON subroutineBlock SEMICOLON</div><div>---</div><div>&gt; &nbsp; &nbsp; &nbsp; : PROCEDURE IDENTIFIER SEMICOLON subroutineBlock</div>
<div>17,19c17,19</div><div>&lt; &nbsp; &nbsp; &nbsp; : external</div><div>&lt; &nbsp; &nbsp; &nbsp; | forward</div><div>&lt; &nbsp; &nbsp; &nbsp; | asmBlock</div><div>---</div><div>&gt; &nbsp; &nbsp; &nbsp; : external SEMICOLON</div><div>&gt; &nbsp; &nbsp; &nbsp; | forward SEMICOLON</div><div>&gt; &nbsp; &nbsp; &nbsp; | asmBlock SEMICOLON</div>
<div><br></div><div>The season for the problem is, a look ahead is needed at &#39;subroutineBlock&#39; to decide which production to use. ANTLR notes that &#39;asmBlock&#39; has a semicolon while the other two don&#39;t. So now the confusion is between the other two. It is only at this instance (after deciding lookahead(&#39;;&#39;) as the deciding factor for asmBlock), ANTLR looks for the predicates. Predicates is used to decide between &#39;external&#39; and &#39;forward&#39;. Unfortunaly in our case what follows &#39;subroutineBlock&#39; (in proceureDecl) is also a SEMICOLON and that semicolon is looked-ahead and a wrong decision (for asmBlock) is being made.</div>
<div><br></div><div>In the new modified productions (in the fix), even &#39;external&#39; and &#39;forward&#39; has SEMICOLON following it. So ANTLR cannot now use SEMICOLON as deciding factor :-)</div><div><br></div><div>
That said this is only a hackish fix. I would prefer ANTLR using my own predicates to decide when I have explicitly mentioned the predicate.&nbsp;</div><div><br></div><div>Is there an option to ask ANTLR to forcefully use my predicate when I have specified one?</div>
<div><br></div><div>Cheers,</div><div>Indhu Bharathi</div><div><br></div><div>PS: I&#39;ve attached the fixed grammar file with this mail</div><div><br></div><br><div class="gmail_quote">On Wed, Dec 31, 2008 at 10:52 PM, Markus Stoeger <span dir="ltr">&lt;<a href="mailto:spamhole@gmx.at">spamhole@gmx.at</a>&gt;</span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">First of all, a happy new year to everyone!<br>
<br>
I&#39;m writing a grammar for one of those twisted languages, written by social deviants, that allow keywords to be used as variables.<br>
<br>
The solution provided in the ANTLR Reference book on page 287 (&quot;Keywords as Variables&quot;) seems to work fine in most cases.<br>
<br>
However I have found one case where my tests fail:<br>
<br>
Please have a look at the attached grammar in keywords.g. Debug it with the start symbol &quot;procedureDecl&quot; and feed it the input &quot;PROCEDURE Proc; FORWARD;&quot;<br>
<br>
The problem happens in &quot;subroutineBlock&quot; where the decision dfa chooses the wrong alternative 3 (asmBlock) instead of 2 (forward).<br>
<br>
To me the generated decision DFA for the symbol &quot;subroutineBlock&quot; (also attached) looks erroneous. It correctly evaluates the disambiguating semantic predicates for alternatives 1 (external) and 2 (forward), but it skips the evaluation for alternative 3 (asmBlock) and makes its decision based on the SEMICOLON token, which is wrong because the semicolon can belong to the outer procedureDecl.<br>

<br>
I have tested this with the latest stable version of the ANTLRWorks bundle (1.2.2).<br>
<br>
thanks for any hints,<br><font color="#888888">
Markus<br>
<br>
</font><br>PROCEDURE Proc; FORWARD;<br>grammar keywords;<br>
<br>
options {<br>
 &nbsp; &nbsp; &nbsp; &nbsp;language=Java;<br>
 &nbsp; &nbsp; &nbsp; &nbsp;k=*;<br>
}<br>
<br>
procedureDecl<br>
 &nbsp; &nbsp; &nbsp; &nbsp;: PROCEDURE IDENTIFIER SEMICOLON subroutineBlock SEMICOLON<br>
 &nbsp; &nbsp; &nbsp; &nbsp;;<br>
<br>
subroutineBlock<br>
 &nbsp; &nbsp; &nbsp; &nbsp;: external<br>
 &nbsp; &nbsp; &nbsp; &nbsp;| forward<br>
 &nbsp; &nbsp; &nbsp; &nbsp;| asmBlock<br>
 &nbsp; &nbsp; &nbsp; &nbsp;;<br>
<br>
asmBlock<br>
 &nbsp; &nbsp; &nbsp; &nbsp;: assembler SEMICOLON ASM END<br>
 &nbsp; &nbsp; &nbsp; &nbsp;;<br>
<br>
external: {input.LT(1).getText().toLowerCase().equals(&quot;external&quot;)}? IDENTIFIER;<br>
forward: {input.LT(1).getText().toLowerCase().equals(&quot;forward&quot;)}? IDENTIFIER;<br>
assembler: {input.LT(1).getText().toLowerCase().equals(&quot;assembler&quot;)}? IDENTIFIER;<br>
<br>
SEMICOLON: &#39;;&#39;;<br>
PROCEDURE: &#39;PROCEDURE&#39;;<br>
ASM: &#39;ASM&#39;;<br>
BEGIN: &#39;BEGIN&#39;;<br>
END: &#39;END&#39;;<br>
IDENTIFIER: (&#39;a&#39;..&#39;z&#39;|&#39;A&#39;..&#39;Z&#39;)+;<br>
WS: (&#39; &#39;|&#39;\r&#39;|&#39;\n&#39;)+ {$channel = HIDDEN;};<br>
<br><br>
List: <a href="http://www.antlr.org/mailman/listinfo/antlr-interest" target="_blank">http://www.antlr.org/mailman/listinfo/antlr-interest</a><br>
Unsubscribe: <a href="http://www.antlr.org/mailman/options/antlr-interest/your-email-address" target="_blank">http://www.antlr.org/mailman/options/antlr-interest/your-email-address</a><br>
<br></blockquote></div><br><br clear="all"><br>-- <br>- Cheers<br>Indhu Bharathi<br>
</div>