Re: [antlr-interest] Why doesn't this work?
Yes, I've read that page earlier and I understand it (and that is how
I've solved the problem for now). Without syntactic predicates I
understand ANTLR Lexer will try matching the longer string and might
fail in the middle. But when a syntactic predicate (which is a gated
semantic predicate) is placed before the production, shouldn't ANTLR
first try the predicate and go on and match the production only if the
syntactic predicate passes like what Terence says here:
http://www.antlr.org/pipermail/antlr-interest/2009-March/033526.html
For example, the following grammar wont work for the input "1.". It
wont give me INT_LIT, DOT. Instead it will try matching for FLOAT_LIT
and fail. :
grammar Test;
r : INT_LIT DOT
;
INT_OR_FLOAT
: NUMBER DOT NUMBER {$type=FLOAT_LIT;}
| NUMBER {$type=INT_LIT;}
;
fragment INT_LIT
: ;
fragment FLOAT_LIT
: ;
DOT : '.'
;
fragment NUMBER
: '0'..'9'+
;
But if you add syntactic predicate to INT_OR_FLOAT as shown below, it
will work:
INT_OR_FLOAT
: (NUMBER DOT NUMBER) => NUMBER DOT NUMBER {$type=FLOAT_LIT;}
| (NUMBER) => NUMBER {$type=INT_LIT;}
;
I was expecting the same thing on my example. But it for some reason
doesn't work for my example. What is the difference between the above
example and my example? Shouldn't both work fine since syntactic
predicate is present?
Thanks, Indhu
Johannes Luber wrote:
Indhu Bharathi schrieb:
Moving this to antlr-dev as I'm starting to feel maybe this is a bug...
No reply in antlr-interest for long time kindof confirms that feeling.
I can certainly do some work around for the work I'm doing now. But this
is something I've tried a lot of times and always failed. Would like to
know if I'm doing some mistake or is this a bug in ANTLR?
Thanks, Indhu
I think that your problem is described here:
<http://www.antlr.org/wiki/display/ANTLR3/Lexer+grammar+for+floating+point,+dot,+range,+time+specs>
Johannes
Indhu Bharathi wrote:
Hi,
Any clue why this doesn't work? I'm still clueless.
- Indhu
Indhu Bharathi wrote:
I was working in a big grammar and stumbled on a problem with
predicates. I've simplified the problem as much as possible and here it is:
When I give the input "1.", I expect the tokens <INT_LIT, DOT>. But what
I get is "No viable alternative at character 'EOF'. I'm not able to
understand why this happens. Any pointers?
grammar Test;
r : INT_LIT DOT+
;
INT_FLOAT_PATTERN
: (NUMBER DOT NUMBER LETTER ) => NUMBER DOT NUMBER LETTER
{ $type=PATTERN; }
| ( NUMBER DOT NUMBER ) => NUMBER DOT NUMBER
{ $type=FLOAT_LIT; }
| (NUMBER) => NUMBER
{ $type=INT_LIT; }
;
DOT : '.'
;
fragment PATTERN
: ;
fragment FLOAT_LIT
: ;
fragment INT_LIT
: ;
fragment
NUMBER : ('0'..'9')+
;
fragment
LETTER : 'a'..'z'
;
Thanks, Indhu
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
List: http://www.antlr.org/mailman/listinfo/antlr-interest
Unsubscribe: http://www.antlr.org/mailman/options/antlr-interest/your-email-address
------------------------------------------------------------------------
_______________________________________________
antlr-dev mailing list
antlr-dev-ErkRXerACLvYtjvyW6yDsg@public.gmane.org
http://www.antlr.org/mailman/listinfo/antlr-dev
<div>
Yes, I've read that page earlier and I understand it (and that is how
I've solved the problem for now). Without syntactic predicates I
understand ANTLR Lexer will try matching the longer string and might
fail in the middle. But when a syntactic predicate (which is a gated
semantic predicate) is placed before the production, shouldn't ANTLR
first try the predicate and go on and match the production only if the
syntactic predicate passes like what Terence says here:
<a class="moz-txt-link-freetext" href="http://www.antlr.org/pipermail/antlr-interest/2009-March/033526.html">http://www.antlr.org/pipermail/antlr-interest/2009-March/033526.html</a><br><br>
For example, the following grammar wont work for the input "1.". It
wont give me INT_LIT, DOT. Instead it will try matching for FLOAT_LIT
and fail. :<br><br>
grammar Test;<br><br>
r : INT_LIT DOT<br>
;<br><br>
INT_OR_FLOAT<br>
: NUMBER DOT NUMBER {$type=FLOAT_LIT;}<br>
| NUMBER {$type=INT_LIT;}<br>
;<br><br>
fragment INT_LIT<br>
: ;<br>
<br>
fragment FLOAT_LIT<br>
: ;<br><br>
DOT : '.'<br>
;<br><br>
fragment NUMBER<br>
: '0'..'9'+<br>
;<br>
<br><br>
But if you add syntactic predicate to INT_OR_FLOAT as shown below, it
will work:<br><br>
INT_OR_FLOAT<br>
: (NUMBER DOT NUMBER) => NUMBER DOT NUMBER {$type=FLOAT_LIT;}<br>
| (NUMBER) => NUMBER {$type=INT_LIT;}<br>
;<br><br><br>
I was expecting the same thing on my example. But it for some reason
doesn't work for my example. What is the difference between the above
example and my example? Shouldn't both work fine since syntactic
predicate is present?<br><br>
Thanks, Indhu<br><br>
Johannes Luber wrote:
<blockquote cite="mid:49DC6120.2010200@..." type="cite">
Indhu Bharathi schrieb:
<blockquote type="cite">
Moving this to antlr-dev as I'm starting to feel maybe this is a bug...
No reply in antlr-interest for long time kindof confirms that feeling.
I can certainly do some work around for the work I'm doing now. But this
is something I've tried a lot of times and always failed. Would like to
know if I'm doing some mistake or is this a bug in ANTLR?
Thanks, Indhu
</blockquote>
I think that your problem is described here:
<a class="moz-txt-link-rfc2396E" href="http://www.antlr.org/wiki/display/ANTLR3/Lexer+grammar+for+floating+point,+dot,+range,+time+specs"><http://www.antlr.org/wiki/display/ANTLR3/Lexer+grammar+for+floating+point,+dot,+range,+time+specs></a>
Johannes
<blockquote type="cite">
Indhu Bharathi wrote:
<blockquote type="cite">
Hi,
Any clue why this doesn't work? I'm still clueless.
- Indhu
Indhu Bharathi wrote:
<blockquote type="cite">
I was working in a big grammar and stumbled on a problem with
predicates. I've simplified the problem as much as possible and here it is:
When I give the input "1.", I expect the tokens <INT_LIT, DOT>. But what
I get is "No viable alternative at character 'EOF'. I'm not able to
understand why this happens. Any pointers?
grammar Test;
r : INT_LIT DOT+
;
INT_FLOAT_PATTERN
: (NUMBER DOT NUMBER LETTER ) => NUMBER DOT NUMBER LETTER
{ $type=PATTERN; }
| ( NUMBER DOT NUMBER ) => NUMBER DOT NUMBER
{ $type=FLOAT_LIT; }
| (NUMBER) => NUMBER
{ $type=INT_LIT; }
;
DOT : '.'
;
fragment PATTERN
: ;
fragment FLOAT_LIT
: ;
fragment INT_LIT
: ;
fragment
NUMBER : ('0'..'9')+
;
fragment
LETTER : 'a'..'z'
;
Thanks, Indhu
List: <a class="moz-txt-link-freetext" href="http://www.antlr.org/mailman/listinfo/antlr-interest">http://www.antlr.org/mailman/listinfo/antlr-interest</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="http://www.antlr.org/mailman/options/antlr-interest/your-email-address">http://www.antlr.org/mailman/options/antlr-interest/your-email-address</a>
</blockquote>
List: <a class="moz-txt-link-freetext" href="http://www.antlr.org/mailman/listinfo/antlr-interest">http://www.antlr.org/mailman/listinfo/antlr-interest</a>
Unsubscribe: <a class="moz-txt-link-freetext" href="http://www.antlr.org/mailman/options/antlr-interest/your-email-address">http://www.antlr.org/mailman/options/antlr-interest/your-email-address</a>
</blockquote>
------------------------------------------------------------------------
_______________________________________________
antlr-dev mailing list
<a class="moz-txt-link-abbreviated" href="mailto:antlr-dev@...">antlr-dev@...</a>
<a class="moz-txt-link-freetext" href="http://www.antlr.org/mailman/listinfo/antlr-dev">http://www.antlr.org/mailman/listinfo/antlr-dev</a>
</blockquote>
</blockquote>
<br>
</div>