| Refresh | Home EGTry.com

repetion, undetermined length of list


L1.g

lexer grammar L1;

List: ID '=' '(' INT (',' INT)* ')';
fragment
INT: '1' .. '9';
fragment
ID: 'a' .. 'z';
WS: (' '|'\n'|'\r'|'\t')+ ;
Any: .;





L1Main.java

import org.antlr.runtime.ANTLRStringStream;
import org.antlr.runtime.CommonTokenStream;
import org.antlr.runtime.Token;



public class L1Main {
    public static void main(String[] args) throws Exception {
    	ANTLRStringStream input=new ANTLRStringStream("a=(1) b=(2,3) c=(4,5,6) ");
    	L1 lexer = new L1(input);
    	int i=0;
    	Token token;   	
    	while(  (token=lexer.nextToken()).getType() !=Token.EOF) {
    		
    		System.out.println(i+++": ("+token.getType()+",#"+token.getText()+"#)");
    	}
        
    }
}