[antlr-interest] Get results of multible tokens

Andreas Volz lists at brachttal.net
Wed Sep 2 14:13:54 PDT 2009


Hello,

I have this grammar file:

grammar VCard;

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

        VCardParser parser = new VCardParser(tokens);

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

line
	: property=TOKEN subtoken=SUBTOKEN* DPOINT attribute=TOKEN
	{
		System.out.println ("Property: " + $property.text);
		System.out.println ("Attribute: " + $attribute.text);
		System.out.println ("Subtoken: " + $subtoken.text);

	}
	;

TOKEN
	: (ALPHA | DIGIT)+
	;
	
SUBTOKEN
	: SEMI TOKEN
	;
	
WS 	
	: ('\n' | ' ' | '\t')* {$channel=HIDDEN;}
	;	

fragment DIGIT  	
	: '0'..'9'
	;
	
fragment ALPHA
	: 'a'..'z' | 'A'..'Z'
	;
	
DPOINT
	: ':'
	;
	
SEMI
	: ';'
	;
	

And this input:

a;b;c;2:3a3bcde

This is the output:

Property: a
Attribute: 3a3bcde
Subtoken: ;2

What I like to get is:

Property: a
Subtoken: b
Subtoken: c
Subtoken: 2
Attribute: 3a3bcde

I couldn't find in the docs how to match multiple tokens that I get
from a * or + parser.

A second question is how to not include the ';' in the match.

I tried it for some time now, but I find no way. Could someone give me
an hint.

regards
Andreas


More information about the antlr-interest mailing list