[antlr-interest] Parse 1 - N repeats

Adam Connelly adam.rpconnelly at googlemail.com
Mon Feb 8 04:56:58 PST 2010


Hi,

Sorry if this is answered elsewhere, but I'm not really sure what to search
for.

I'm trying to parse a language that includes repeating groups. The problem
is that they don't include terminators, so you can't tell the difference
between the last item in the group, and the next section. Here's an example:

FIELD1
REPEATING_GROUP   <fields=2> <min=0, max=20>
    FIELD2
    FIELD3
FIELD4
...

"fields" specifies the number of fields contained in the group. At the
moment I've got the following rules, but the problem is that it means that
the repeating group rule doesn't get its fields associated with it:

recordDefinition
    :    RECORD (IDENTIFIER | repeatingGroup)+
    ;

repeatingGroup
    :    IDENTIFIER
        '<' NUMBER_OF_FIELDS '=' fieldCount=NUMBER '>'
        '<' NUMBER_OF_REPEATS '=' min=NUMBER ',' max=NUMBER '>'
    ;

Ideally I could do something like:

repeatingGroup
    :    IDENTIFIER
        '<' NUMBER_OF_FIELDS '=' fieldCount=NUMBER '>'
        '<' NUMBER_OF_REPEATS '=' min=NUMBER ',' max=NUMBER '>'
        IDENTIFIER{1, $fieldCount}
    ;

But I know you can't do that. What would the best way be to go about parsing
this? Can I build an AST then modify it to put the identifiers for the
repeating group in the right place.

Cheers,
Adam


More information about the antlr-interest mailing list