| Refresh | Home EGTry.com

nested (recursive) structure


Input

 <A> <<A>> <<<A>>>  <<<<A>>>>


Tokens Output

0: (6,# #)
1: (5,#<A>#)
2: (6,# #)
3: (5,#<<A>>#)
4: (6,# #)
5: (5,#<<<A>>>#)
6: (6,#  #)
7: (5,#<<<<A>>>>#)


antlr grammar

lexer grammar L1;

//INPUT: <A> <<A>> <<<A>>>  <<<<A>>>>
A: 'A';
One: '<' (One|A) '>';

WS: (' '|'\n'|'\r'|'\t')+ ;
Any: .;





use the lexer

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> <<A>> <<<A>>>  <<<<A>>>>");
    	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()+"#)");
    	}
        
    }
}