Saturday, September 6, 2008

Antlr LL

So in fact the grammar I wrote yesterday is an LL(2) grammar. Antlr is an LL(*) parser, but you're allowed to specify how many tokens you want it to look ahead (I'm still unclear if Antlr is smart enough to take something that is LL(K) and generate source code that takes advantage of it).

LL(K) means for a given language the parser has to look forward up to K token to determine what rule you're using. There are parsing optimizations made if you specify the LL.

See: http://en.wikipedia.org/wiki/LL_parser for more info on LL parsers

In Antlr option { k=INT } specifies LL(INT). Three examples:

LL(1)

grammar lookAhead1;

options {
 k = 1;
}
prog : rule+;
rule : 'A' ':' 'B'+
 ;

WS : (' '|'\t'|'\r'|'\n')+ {skip();}
 ;

LL(2)

grammar lookAhead2;

options {
 k = 2;
}
prog : rule+;
rule : 'A' ':' 'A'+
 ;

WS : (' '|'\t'|'\r'|'\n')+ {skip();}
 ;

LL(3)

grammar lookAhead3;

options {
 k = 3;
}
prog : rule+;
rule : 'A' 'A' ':' 'A'+
 ;

WS : (' '|'\t'|'\r'|'\n')+ {skip();}
 ;

No comments:

 
Web Statistics