[antlr-interest] handling abbreviated keywords in ANTLR

Micheal J open.zone at virgin.net
Sun Jul 8 15:28:15 PDT 2007


> Hi,
> 
> I need to parse a language which allows abbreviated keyword 
> definitions (I have at least 52 keywords). For example, each 
> of the following 'application' definitions is deemed valid:
> 
> 'application : helloworld'
> 
> 'appl : helloworld'
> 
> 'applic : helloworld'
> 
> or 'sequence : dna' - 'seq : dna'
> 
> I need to recognise and match keyword variants to their 
> complete definition. I am using ANTLR 3. Which is the best 
> way to handle this recognition problem? In the lexer or parser?

Lexer.

APPLICATION : 'appl' ( 'ic' ( 'ation' )? )? ;

OR (if any prefix - staring from 'appl' - is allowed)

APPLICATION : 'appl' ( 'i' ( 'c' ( 'a' ( 't' ( 'i' ( 'o' ( 'n' )? )? )? )?
)? )? )? ;

Just ensure it comes before the generic IDENTIFIER rule in your grammar. Do
same for other keywords.

Micheal

-----------------------
The best way to contact me is via the list/forum. My time is very limited.



More information about the antlr-interest mailing list