[antlr-interest] Error in predicate logic
Dr. Hartmut Kocher
hwk.cortex-brainware at t-online.de
Wed Feb 14 15:09:22 PST 2007
The following stripped grammar shows an error in the predicate code
generation:
Whereas for aa a correct predicate is generated, this is not the case for
aab.
grammar Simple;
options {
language=Java;
output=AST;
ASTLabelType=CommonTree;
}
tokens {
AA = 'aa';
AAB = 'aab';
}
t :
vars INT -> ^(vars INT)
| ID
;
vars : 'aa' | 'aab' ;
fragment DIGIT : '0'..'9';
INT : DIGIT+;
ID :
(AA DIGIT) => AA {$type = AA;}
| (AAB DIGIT) => AAB {$type = AAB;}
| ('a'..'z'|'A'..'Z') ('a'..'z'|'A'..'Z'|'_'|'0'..'9')+ ;
WS : (' '|'\r'|'\t'|'\f'|'\n')+ { $channel=HIDDEN; };
The code excerpt shows that for aab the result is simply set without
checking the predicate first. aab does not check for a following digit,
but accepts any character
Note this only happens if the tokens are a subset of each other, e.g. aa
and aab, which also starts with aa
// Simple.g:28:5: ( ( AA DIGIT )=> AA | ( AAB DIGIT )=> AAB |
('a'..'z'|'A'..'Z') ( ('a'..'z'|'A'..'Z'|'_'|'0'..'9'))+ )
int alt3=3;
int LA3_0 = input.LA(1);
if ( (LA3_0=='a') ) {
int LA3_1 = input.LA(2);
if ( (LA3_1=='a') ) {
if ( (LA3_3=='b') ) {
alt3=2; // a predicate function should be
called here!!!
}
else if ( (synpred1()) ) {
alt3=1;
}
else if ( (true) ) {
alt3=3;
}
Hope this helps to fix it
Kind regards
Dr. Hartmut Kocher
Cortex Brainware Consulting & Training GmbH
Kirchplatz 5
D-82049 Pullach
Tel: +49 (89) 744 850 0
Fax: +49 (89) 744 850 11
<http://www.cortex-brainware.de/> http://www.cortex-brainware.de
E-Mail: hwk at cortex-brainware.de
Handelsregister:
Registergericht: Amtsgericht München
HRB-Nr. 120614
Vertretungsberechtigte Geschäftsführer:
Dipl.-Inform. Dorothea Burger, Dr.-Ing. Hartmut Kocher, Dr.-Ing. Martin
Lang, Dr. rer. nat. Rolf Peter Wehrum
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.antlr.org/pipermail/antlr-interest/attachments/20070215/2e75585a/attachment.html
More information about the antlr-interest
mailing list