[antlr-interest] ANTLR 3 matching an exact number or bounded number of items

The Researcher researcher0x00 at gmail.com
Wed Mar 2 19:04:49 PST 2011


Thanks for the feedback.

I am working on reading binary file structures and have set K=0 because the
file should be able to read without look ahead. Thus the need for exact
bounds. The parser gets only two types of tokens, one is the next byte, or
the EOF token that's it. It actually doesn't even have a lexer, it uses a
hand built token stream. < I didn't get a memo saying I needed one>

The methods you suggested did work for me on the simple test cases, but once
a few levels of nesting of the structures with fixed element bounds were
seen by ANTLR, it's analysis kicked in and started reporting
error(201) The following alternatives can never be matched.
The strategic use of {...}?=> is the only strategy that is allowing the
build to succeed.

The regular expression tools I use support the bounded syntax, and I was
hoping the ANTLR would have it and that I was just not able to find it.

Again thanks for the response, it nice to know I didn't miss any option.


On Wed, Mar 2, 2011 at 9:20 PM, Kevin J. Cummings <
cummings at kjchome.homeip.net> wrote:

>  On 03/02/2011 09:09 PM, The Researcher wrote:
> > Does ANTLR 3 have built-in support for matching an exact number or
> bounded
> > number of items that does not rely on using {...}?=>
> >
> > e.g for a 32 bit value of four bytes the rule statement would be
> >     byte[4]
> >
> > or for an a structure that has a bound of elements between 1 and 16 the
> rule
> > statement would be
> >     struc[1:16]
> > While ANTLR 3 uses [ ] for rule parameters, here [ ] is used to signify
> > element bounds.
> >
> > I have looked high and low for this, and found nothing tangible.
> >
> > Abusing {...}?=> works, but I would like to stop abusing it.
>
> Set up the action code for your rule to count the number of elements
> matched.  When you exceed the number you need, have the action output an
> error, or raise an exception, or, ....
>
> Or you can abuse gated semantic predicates.
>
> > List: http://www.antlr.org/mailman/listinfo/antlr-interest
> > Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>
> --
> Kevin J. Cummings
> kjchome at verizon.net
> cummings at kjchome.homeip.net
> cummings at kjc386.framingham.ma.us
> Registered Linux User #1232 (http://counter.li.org)
>
> List: http://www.antlr.org/mailman/listinfo/antlr-interest
> Unsubscribe:
> http://www.antlr.org/mailman/options/antlr-interest/your-email-address
>


More information about the antlr-interest mailing list