[antlr-interest] Quoted Name Value Pairs

Jesse Anderson jesse.anderson at 4bright.com
Tue Apr 22 17:00:00 PDT 2008


The file is parsing correctly now.  I had some extra code commented out 
that I removed from my previous E-mails.  I removed that extra code from 
my local copy and the parser started working.  Are there any known bugs 
that pertain to this?

Jesse

grammar bcadmin;

tokens {
    ADD    = 'add';
    REMOVE    = 'remove';
    SET     = 'set';
    LIST    = 'list';
   
    /* Possible tokens for values */
    ID    = 'id';
    WORKSPACE = 'workspace';
    TARGET    = 'target';
    PATTERN    = 'pattern';
    USER    = 'user';
    START    = 'start';
    FILESIZE = 'filesize';
    THROTTLE = 'throttle';
    STATUS    = 'status';
}

@members {
    public static void main(String[] args) throws Exception {
        bcadminLexer lex = new bcadminLexer(new ANTLRFileStream(args[0]));
           CommonTokenStream tokens = new CommonTokenStream(lex);

        bcadminParser parser = new bcadminParser(tokens);

        try {
            parser.expr();
        } catch (RecognitionException e)  {
            e.printStackTrace();
        }
    }
}

/*------------------------------------------------------------------
 * PARSER RULES
 *------------------------------------------------------------------*/

expr        : ( add | remove | set | list )* ;

add        : ADD addterms SEMICOLON ;

addterms     : ( WHITESPACE ( WORKSPACE | TARGET | PATTERN | USER | 
START | FILESIZE | THROTTLE | STATUS ) equalsvalue )* ;

remove        : REMOVE removeterms SEMICOLON ;

removeterms    : WHITESPACE ID equalsvalue ;

set        : SET setterms SEMICOLON ;

setterms    : ( WHITESPACE ( ID | WORKSPACE | TARGET | PATTERN | USER | 
START | FILESIZE | THROTTLE | STATUS ) equalsvalue )* ;

list        : LIST listterms SEMICOLON ;

listterms    : ( WHITESPACE WORKSPACE equalsvalue )? ;

equalsvalue     : EQUALS QUOTEDVALUE ;

/*------------------------------------------------------------------
 * LEXER RULES
 *------------------------------------------------------------------*/

QUOTEDVALUE : '"' (~'"')* '"' ;

WHITESPACE : ( '\t' | ' ' | '\r' | '\n'| '\u000C' ) ;

EQUALS : ( '=' ) ;

SEMICOLON : ( ';' )+ ;


Jesse Anderson wrote:
> Also, here is the complete file.
>
> grammar bcadmin;
>
> tokens {
>    ADD    = 'add';
>    REMOVE    = 'remove';
>    SET     = 'set';
>    LIST    = 'list';
>      /* Possible tokens for values */
>    ID    = 'id';
>    WORKSPACE = 'workspace';
>    TARGET    = 'target';
>    PATTERN    = 'pattern';
>    USER    = 'user';
>    START    = 'start';
>    FILESIZE = 'filesize';
>    THROTTLE = 'throttle';
>    STATUS    = 'status';
> }
>
> @members {
>    public static void main(String[] args) throws Exception {
>        bcadminLexer lex = new bcadminLexer(new ANTLRFileStream(args[0]));
>           CommonTokenStream tokens = new CommonTokenStream(lex);
>
>        bcadminParser parser = new bcadminParser(tokens);
>
>        try {
>            parser.expr();
>        } catch (RecognitionException e)  {
>            e.printStackTrace();
>        }
>    }
> }
>
> /*------------------------------------------------------------------
> * PARSER RULES
> *------------------------------------------------------------------*/
>
> expr        : ( add | remove | set | list )* ;
>
> add        : ADD addterms SEMICOLON ;
>
> addterms     : ( WHITESPACE ( WORKSPACE | TARGET | PATTERN | USER | 
> START | FILESIZE | THROTTLE | STATUS ) equalsvalue )* ;
>
> remove        : REMOVE removeterms SEMICOLON ;
>
> removeterms    : WHITESPACE ID equalsvalue ;
>
> set        : SET setterms SEMICOLON ;
>
> setterms    : ( WHITESPACE ( ID | WORKSPACE | TARGET | PATTERN | USER 
> | START | FILESIZE | THROTTLE | STATUS ) equalsvalue )* ;
>
> list        : LIST listterms SEMICOLON ;
>
> listterms    : ( WHITESPACE WORKSPACE equalsvalue )? ;
>
> equalsvalue     : EQUALS QUOTEDVALUE ;
>
> /*------------------------------------------------------------------
> * LEXER RULES
> *------------------------------------------------------------------*/
>
> QUOTEDVALUE : '"' (~'"')* '"' ;
>
> WHITESPACE : WHITESPACECHAR+ ;
>
> WHITESPACECHAR : ( '\t' | ' ' | '\r' | '\n'| '\u000C' ) ;
>
> EQUALS : ( '=' ) ;
>
> SEMICOLON : ( ';' )+ ;
>
>
> Jesse Anderson wrote:
>> Gavin Lambert wrote:
>>> At 07:29 23/04/2008, Jesse Anderson wrote:
>>> >Here is my file:
>>>
>>> You've left some of the rules out, so it's hard to give a complete 
>>> answer.  But for starters:
>>>
>>> >WHITESPACE : WHITESPACECHAR+ ;
>>> >
>>> >WHITESPACECHAR : ( '\t' | ' ' | '\r' | '\n'| '\u000C' ) ;
>>>
>>> WHITESPACECHAR should be a fragment.
>>>
>>> >SEMICOLON : ( ';' )* ;
>>>
>>> This permits SEMICOLON to match nothing, and lexer rules that can 
>>> match nothing are a big no-no.  Either change the * to a + or remove 
>>> it entirely and match multiple SEMICOLON tokens in the parser.
>>>
>>> When you're starting out, it's useful to write a test harness for 
>>> the lexer alone; feed it some sample input and print out the tokens 
>>> it generates.  You'll often find that the parser isn't behaving the 
>>> way you expect because it's getting different tokens than you 
>>> expect.  ANTLRworks currently doesn't really show this very well.
>>>
>> I tried that.  Still no joy.  I changed it to:
>>
>> SEMICOLON : ( ';' )+;


More information about the antlr-interest mailing list