[antlr-interest] Grammar definition problem

Johannes Luber jaluber at gmx.de
Thu Feb 5 13:56:47 PST 2009


leiche im teich schrieb:
> Hello,
> 
> I'm a ANTLR newbie and have a problem to define the right grammar for
> the following input.
> - one input line is one record
> - the structure is: <fieldname> <one colon followed by one or more
> spaces as delimiter> <fieldvalue> <newline>*
> - the fieldnames are known
> 
> My problem is, than the fieldvalue can have colon or space characters too.
> 
> This is my grammar:
> 
> grammar Foo;
> 
> options {
>     output=AST;
>     ASTLabelType=CommonTree;
> }
> 
> entry    : fieldname FIELDSEPERATOR  fieldvalue '\n'*
>     ;
> 
> fieldvalue    : ID
>     ;
> 
> fieldname: NAME
>     |GIVENNAME
>     |STREET
>     |ZIP
>     |CITY
>     |COUNTRY
>     |TEXT
>     ;
> 
> NAME            : 'Name';
> GIVENNAME         : 'Givenname';
> STREET         : 'Street';
> ZIP            : 'Zip';
> CITY            : 'City';
> COUNTRY        : 'Country';
> TEXT            : 'Text';
> 
> FIELDSEPERATOR    : ':' ' '+
>     ;
> 
> ID    : AllowedChars+
>     ;
> 
> fragment
> AllowedChars    : 'A'..'Z'
>     | 'a'..'z'
>     | '0'..'9'
>     | '+'
>     | '-'
>     | ' '
> //    | ':'
>     | '#'
>     | '.'
>     ;
> 
> 
> If i try the input "Text:      Free Text with # sign" it works. But if i
> do not comment the ':' in the AllowedChars and try the input "Text:     
> Free Text with : sign" it goes wrong :-(
> 
> How can i solve this problem????
> 
> Thank's

Make the colon its fragment rule and call it from ID and FIELDSEPARATOR.
A further improvement would be a rule

WS : ' '+ {skip();};

to remove all WS handling from the parser (modify the FIELDSEPARATOR
rule at your leisure as the spaces would be correctly recognized here,
but doing so will make the rule cleaner).

Johannes
> 
> 
> Matthias
> 
> 
> ------------------------------------------------------------------------
> 
> 
> 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