[antlr-interest] Location dependent token?

Gary R. Van Sickle g.r.vansickle at att.net
Mon Dec 29 06:10:38 PST 2008


> From: Mats Ekberg
> 
> Lets say a three letter word in uppercase can mean one of two 
> tings like:
> 
>   10  EUR
> where EUR means a monetary unit
>   
>   10 EUR / TNE
> where EUR still means a monetary unit but the three letters 
> TNE now means a measurement uniot.
> 
> How can that be expressed in a grammar??
> 
> /mats 

Your question must be missing some information, because what you're asking
is the most basic of lexing/parsing issues:


Lexer does something like this:

NUMBER : [0..9]+ ;

EUR : 'EUR' ;

TNE : 'TNE' ;


Parser does something like this:

num_with_monetary_unit_and_optional_per_unit
    : NUMBER monetary_unit ('/' measurement_unit)?
    ;

monetary_unit
    : EUR
    | <<whatever other monies you support>>
    ;

measurement_unit
    : TNE
    | <<whatever other measurement units you support>>
    ;


But was that really your question?

-- 
Gary R. Van Sickle
 



More information about the antlr-interest mailing list