[antlr-interest] Location dependent token?

Gavin Lambert antlr at mirality.co.nz
Mon Dec 29 13:23:00 PST 2008


At 07:18 30/12/2008, Mats Ekberg wrote:
>Ok, maybe I was a bit unsharp.
>Monetary units are expressed as three-letter words; EUR GBP and 
>so on.
>Measurement unitts are also expressed with three letters; TNE KGM 
>and so on.
>
>The only way to know which is which is where the three letters 
>are located. In one location its a monetary and another its a 
>measurement.

As Gary said earlier, I'm not sure why you think this is a 
problem.

If the complete set of possible monetary units and measurement 
units are known in advance, then you can make them keywords 
recognised by the lexer (as Gary's email showed); then at parse 
time you can make sure that they only used a valid measurement 
unit in the spot where you were expecting one, and so forth.

If the complete set of units is not known in advance, then you can 
simply match them in the lexer as an id (whether general form or 
restricted to exactly three uppercase letters depends on what else 
you need your lexer to match) and have the parser figure out what 
they mean based on the context (though you wouldn't be able to 
validate in that case, obviously).



More information about the antlr-interest mailing list