[antlr-interest] Newbie question...

Buck, Robert rbuck at verisign.com
Tue Jul 24 08:44:06 PDT 2007


Hi,

I never have written a grammar before in ANTLR. Could someone provide
some hint as to how to accomplish what I am attempting. I think I am
pretty close, but I run into some errors when I run my simple
application main, and when I do a grammar check.

I have a language of sorts I use to express traits for messaging system
drivers. A sample traits description follows:

 
(TRAITS={(PERSISTENCE=FS),(PROTOCOL={(POST={DIRECT,RENAME}),(TAKE={INDIR
ECT,CREATE})})})

A greatly simplified example using this grammar could be:

    (message-version=1.0)

I wrote a grammar that seems to work, for the most part.

##############################################
grammar SetTuple;

options {
	output=AST;
}

@members {
    public static void main(String[] args) throws Exception {
        SetTupleLexer lex = new SetTupleLexer(new
ANTLRStringStream(args[0]));
       	CommonTokenStream tokens = new CommonTokenStream(lex);
        SetTupleParser parser = new SetTupleParser(tokens);
        try {
            parser.prog();
        } catch (RecognitionException e)  {
            e.printStackTrace();
        }
    }
}

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

prog
	:	tuple
	;

tuple
	:	L_PAREN tuple_declaration EQUALS ( tuple_value | set )
R_PAREN
	;

set
	:	L_CURLY tuple ( COMMA tuple )*
	;

tuple_declaration
	:	tuple_key ( AT_SIGN set )? 
	;

tuple_key
	:	IDENT
	;
tuple_value
	:	( PCHAR )*
	;

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

PCHAR	:	PCT_ENCODED | UNRESERVED
	;

UNRESERVED
	:	ALPHA | DIGIT | '-' | '.' | '_' | '~' | ':'
	;
PCT_ENCODED
	:	'%' HEXDIGIT HEXDIGIT
	;
HEXDIGIT:	('0'..'9'|'a'..'f'|'A'..'F')
	;
IDENT	:	ALPHA ( ALPHA | UNDERSCORE | HYPHEN | DIGIT)*
	;
ALPHA
	:	('a'..'z')|('A'..'Z')
	;
L_PAREN
	:	'('
	;
R_PAREN :	')'
	;
L_CURLY	:	'{'
	;
R_CURLY	:	'}'
	;
DIGIT	:	'1'..'9'
	;
COMMA	:	','
	;
EQUALS	:	'='
	;
L_BRACKET
	:	'['
	;
R_BRACKET
	:	']'
	;
AT_SIGN	:	'@'
	;
UNDERSCORE
	:	'_'
	;
HYPHEN	:	'-'
	;
##############################################

But when I check the grammar using ANTLRWorks, I get the following
warning:

[10:54:15] warning(208): SetTuple.g:93:1: The following token
definitions are unreachable:
UNRESERVED,PCT_ENCODED,ALPHA,DIGIT,UNDERSCORE,HYPHEN
[10:54:15] warning(208): SetTuple.g:93:1: The following token
definitions are unreachable:
UNRESERVED,PCT_ENCODED,ALPHA,DIGIT,UNDERSCORE,HYPHEN
[10:55:18] Checking Grammar...

When I run the simplified example, I see the following error on the
command line:

    line 1:19 mismatched input '0' expecting R_PAREN

What am I missing here?

-Bob


More information about the antlr-interest mailing list