[antlr-interest] Re: lexical nondeterminism Or rule contain rule

idontwantanidwith2000init idontwantanidwith2000init at yahoo.com
Fri Apr 9 05:23:49 PDT 2004


--- In antlr-interest at yahoogroups.com, "idontwantanidwith2000init" 
Hi,
Quoted from eclipse antlr help:
protected // not a token; only invoked by another rule.
ESC
    :    '\\'
        (    'n'
        |    'r'
        |    't'
        |    'b'
        |    'f'
        |    '"'
        |    '\''
        |    '\\'
        |    ('u')+
             HEX_DIGIT HEX_DIGIT HEX_DIGIT HEX_DIGIT 
        ...
       )
    ;

So I've made up a little example:
class P1 extends Parser;

startRule
	: "mee" DIGIT NEWLINE
	| "moo" MOO NEWLINE
	;

class L1 extends Lexer;

STRING: ( 'a'..'z'|'A'..'Z' )+
	;

protected
DIGIT : ('0'..'9');

MOO : (DIGIT)+
	;

WS : ( ' ' | '\t' )+
     { $setType(Token.SKIP); }
   ;

NEWLINE
    :   '\r' '\n'   // DOS
    |   '\n'        // UNIX
    ;

MOO contains DIGIT
when I ran it with
Input: mee 2
I got:
line 1:5: expecting DIGIT, found '2'

If I can't use DIGIT in the parser how can I still parse such 
language?

<idontwantanidwith2000init at y...> wrote:
> Thanx, (I've just seen this message)
> 
> What does protected means?
> I thought it means that the parser cannot use this lexam.
> In my case the parser is using both but in a detemenistic way.
> 
> Tal
> 
> --- In antlr-interest at yahoogroups.com, Terence Parr <parrt at c...> 
> wrote:
> > 
> > On Apr 8, 2004, at 4:30 AM, idontwantanidwith2000init wrote:
> > 
> > > Hi,
> > > I have one lexical rule that contains another.
> > > STRING: ( 'a'..'z'|'A'..'Z' )+
> > > 	;
> > >
> > > STRING_LITERAL
> > >   : '"'!
> > >     ( '\\' .
> > >     | ~('"'|'\n'|'\r')
> > >     )*
> > >     ( '"'!
> > >     | // nothing -- write error message
> > >     )
> > > 	;
> > >
> > > I'm using eclipse.
> > > It's warning me that I have nondeterminism but I'm not using
> > > STRING_LITERAL and STRING in the same parser rule.
> > > What can I do to avoid this nondeterminism?
> > 
> > Hi.  Both are considered valid tokens and hence nextToken has 
them 
> as 
> > alternatives.  Use protected keyword on one of the rules.
> > 
> > Terence
> > --
> > Professor Comp. Sci., University of San Francisco
> > Creator, ANTLR Parser Generator, http://www.antlr.org
> > Cofounder, http://www.jguru.com
> > Cofounder, http://www.knowspam.net enjoy email again!
> > Cofounder, http://www.peerscope.com pure link sharing



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

<*> Your use of Yahoo! Groups is subject to:
     http://docs.yahoo.com/info/terms/
 



More information about the antlr-interest mailing list