[antlr-interest] simple question on V2 grammar

Loring Craymer lgcraymer at yahoo.com
Tue Oct 9 14:56:37 PDT 2007


Jim gave you ANTLR 3 advice.  What you really want is
something like:

class Foo extends Lexer;
options {
   k = 3;
}

tokens {
    AND;
    OR;
}

Letter
    :
    'a' ( 'n' 'd' { _ttype = AND; } )?
    |
        'o' ('r' { _ttype = OR; } )?
    |
        'b'..'n'
    |
        'q'..'z'
    ;

protected
WS : /* insert the usual definition here */ ;

and you might get further.  Another option is to have
an IDENT rule instead of a Letter rule:
IDENT
    :  'a' .. 'z' ('a' .. 'z' )*
    ;
with the lexer (or IDENT rule) option "checkLiterals"
set to true and your string literals defined in the
tokens section.

--Loring


--- "Nagesh, Harsha" <harsha.nagesh at credit-suisse.com>
wrote:

> I have the option "caseSensitive = false;" set in my
> lexer.
>  
> where should I use
>  
> AND:'and' ?
>  
>  I tried it as lexer rule and also as a token, in
> all cases antlr gives a syntax error...can we really
> give 
> singleQuote and singleQuote, anywhere ? 
>  
>   _____  
> 
> From: antlr-interest-bounces at antlr.org
> [mailto:antlr-interest-bounces at antlr.org] On Behalf
> Of Jim Idle
> Sent: Tuesday, October 09, 2007 5:29 PM
> To: antlr-interest at antlr.org
> Subject: Re: [antlr-interest] simple question on V2
> grammar
> 
> 
> 
> You want:
> 
>  
> 
> AND : 'and';
> 
>  
> 
> I believe. And your Letter rule matches lower case
> only, but the sample you gave has upper case.
> 
>  
> 
> Hope this helps,
> 
>  
> 
> Jim
> 
>  
> 
> From: antlr-interest-bounces at antlr.org
> [mailto:antlr-interest-bounces at antlr.org] On Behalf
> Of Nagesh, Harsha
> Sent: Tuesday, October 09, 2007 2:21 PM
> To: antlr-interest at antlr.org
> Subject: [antlr-interest] simple question on V2
> grammar
> 
>  
> 
> Hi - Can anybody please help me in this simple
> grammar ?
> 
>  
> 
> My language consists only of Letters (a..z) and a
> few tokens ("and", "or"). I am trying to match an
> expression "A and B" using the following
> 
>  
> 
> Parser
> 
> ---------
> 
> myOp:
> 
>             atom (AND atom)*
> 
>        ;
> 
>  
> 
> atom: Letter
> 
>         ;
> 
>  
> 
> Lexer
> 
> -------
> 
>  
> 
> tokens { AND = "and";}
> 
> Letter: 'a'..'z'
> 
>  
> 
>  
> 
> When I feed the expression "A and B" to this, the
> parser fails. The second token, Instead of matching
> with AND, matches with Letter and thus fails....I am
> not sure how can I fix this ? I tried to use
> syntactic predicate 
> 
>  
> 
> myOp:
> 
>             (atom AND) => atom (AND atom)*
> 
>        ;
> 
>  
> 
> but antlr ignored it when generating the code saying
> that syntactic predicate not required when only one
> alternative exists...
> 
>  
> 
> Can anybody please help
> 
>  
> 
> Thanks
> 
> Harsha
> 
>  
> 
>
==============================================================================
> Please access the attached hyperlink for an
> important electronic communications disclaimer: 
>  
>
http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
>
==============================================================================
> 
>  
> 
> No virus found in this incoming message.
> Checked by AVG Free Edition.
> Version: 7.5.488 / Virus Database: 269.14.5/1058 -
> Release Date: 10/8/2007 4:54 PM
> 
> 
> No virus found in this outgoing message.
> Checked by AVG Free Edition.
> Version: 7.5.488 / Virus Database: 269.14.5/1058 -
> Release Date: 10/8/2007 4:54 PM
> 
> 
> 
>
==============================================================================
> Please access the attached hyperlink for an
> important electronic communications disclaimer: 
> 
>
http://www.credit-suisse.com/legal/en/disclaimer_email_ib.html
>
==============================================================================
> 



       
____________________________________________________________________________________
Yahoo! oneSearch: Finally, mobile search 
that gives answers, not web links. 
http://mobile.yahoo.com/mobileweb/onesearch?refer=1ONXIC


More information about the antlr-interest mailing list