[antlr-interest] Dynamic token matching

Bart Kiers bkiers at gmail.com
Mon Jan 2 06:51:44 PST 2012


>
> Is there a way to avoid the hardcoded "{{" in the call to match()?
> something like:
>
> ..
>
> match(this.LEFT_DELIM);
>
>
>

Sure, try something like this:

---------------------------------------------------------------------
grammar Test;

@parser::members {
  public static void main(String[] args) throws Exception {
    TestLexer lexer = new TestLexer(new ANTLRStringStream("foo {{ bar ?>
42"));
    TestParser parser = new TestParser(new CommonTokenStream(lexer));
    parser.parse();
  }
}

@lexer::members {

  String TXT_OPEN = "{{";
  String TXT_CLOSE = "?>";

  boolean ahead(String str) {
    for(int i = 0; i < str.length(); i++) {
      if(input.LA(i + 1) != str.charAt(i)) {
        return false;
      }
    }
    return true;
  }
}

parse
 : (t=. {System.out.printf("\%-10s '\%s'\n", tokenNames[$t.type],
$t.text);})* EOF
 ;

OPEN
 : {ahead(TXT_OPEN)}?=> {match(TXT_OPEN);}
 ;

CLOSE
 : {ahead(TXT_CLOSE)}?=> {match(TXT_CLOSE);}
 ;

IDENT  : LETTER+;
NUMBER : DIGIT+;
SPACE  : (' ' | '\t' | '\r' | '\n') {$channel=HIDDEN;};

fragment LETTER : ('a'..'z' | 'A'..'Z');
fragment DIGIT  : '0'..'9';
---------------------------------------------------------------------

which  would print:

IDENT      'foo'
OPEN       '{{'
IDENT      'bar'
CLOSE      '?>'
NUMBER     '42'

Regards,

Bart.


More information about the antlr-interest mailing list