This might not be an elegant fix. But it does fix the problem<div><br></div><div><div>13c13</div><div>< : PROCEDURE IDENTIFIER SEMICOLON subroutineBlock SEMICOLON</div><div>---</div><div>> : PROCEDURE IDENTIFIER SEMICOLON subroutineBlock</div>
<div>17,19c17,19</div><div>< : external</div><div>< | forward</div><div>< | asmBlock</div><div>---</div><div>> : external SEMICOLON</div><div>> | forward SEMICOLON</div><div>> | asmBlock SEMICOLON</div>
<div><br></div><div>The season for the problem is, a look ahead is needed at 'subroutineBlock' to decide which production to use. ANTLR notes that 'asmBlock' has a semicolon while the other two don't. So now the confusion is between the other two. It is only at this instance (after deciding lookahead(';') as the deciding factor for asmBlock), ANTLR looks for the predicates. Predicates is used to decide between 'external' and 'forward'. Unfortunaly in our case what follows 'subroutineBlock' (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 'external' and 'forward' 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. </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'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"><<a href="mailto:spamhole@gmx.at">spamhole@gmx.at</a>></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'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 ("Keywords as Variables") 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 "procedureDecl" and feed it the input "PROCEDURE Proc; FORWARD;"<br>
<br>
The problem happens in "subroutineBlock" 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 "subroutineBlock" (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>
language=Java;<br>
k=*;<br>
}<br>
<br>
procedureDecl<br>
: PROCEDURE IDENTIFIER SEMICOLON subroutineBlock SEMICOLON<br>
;<br>
<br>
subroutineBlock<br>
: external<br>
| forward<br>
| asmBlock<br>
;<br>
<br>
asmBlock<br>
: assembler SEMICOLON ASM END<br>
;<br>
<br>
external: {input.LT(1).getText().toLowerCase().equals("external")}? IDENTIFIER;<br>
forward: {input.LT(1).getText().toLowerCase().equals("forward")}? IDENTIFIER;<br>
assembler: {input.LT(1).getText().toLowerCase().equals("assembler")}? IDENTIFIER;<br>
<br>
SEMICOLON: ';';<br>
PROCEDURE: 'PROCEDURE';<br>
ASM: 'ASM';<br>
BEGIN: 'BEGIN';<br>
END: 'END';<br>
IDENTIFIER: ('a'..'z'|'A'..'Z')+;<br>
WS: (' '|'\r'|'\n')+ {$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>