[antlr-interest] RE : Re: RE : Re: How can I ignore reserved words in certain cases ?

JeanChristophe Gautier jchgautier at yahoo.fr
Fri Dec 1 02:11:59 PST 2006


Hi all,

Thanks to everyone for their help.
Using Ryan's solution, we have managed to overcome the
problem.

The expression line now looks like this:

expr1: PRINT {isValidString(LT(1).getText())}? .;

For us it works as we want.
Thanks again for all the input. It helps us to
understand the possibilities with Antlr.

JC



Ryan Hollom <ryan.hollom at us.lawson.com> a écrit : I,
too, have this issue with my DSL.  The way I have
handled it (which doesn't work in all cases, due to
ambiguous parser rules), is to define the parser rule
kind of like this:

expr: PRINT {isValidString(input.LT(1))}=> .

Where isValidString() checks to see whether the input
is a bunch of lowercase chars.  The rule will then
match any token after that as part of the rule.

Hope this helps,
Ryan


-----antlr-interest-bounces at antlr.org wrote: -----

To: antlr-interest at antlr.org
From: JeanChristophe Gautier <jchgautier at yahoo.fr>
Sent by: antlr-interest-bounces at antlr.org
Date: 11/30/2006 06:59AM
Subject: [antlr-interest] RE : Re: How can I ignore
reserved words in certain cases ?

Hi,
 
 Thanks for your help but that doesn't solve my
problem. We have a hundred or so key words (such as
print) declared in the lexer, all of which should be
accepted as a STRING. We could always do as you
suggested but we would have a parser rule several
lines long and we would need to modify it whenever we
added a new key word. Is there no way to simply ignore
the key word tokens for certain rules?
 
 Many thanks,
 JC
 

Micheal J <open.zone at virgin.net>  a écrit :          
Hi,


I am writing a command line editor that has reserved
words, such
 as

"print", that should accept, at times, any string
value. For example the

following should be allowed:


print hello

print print


The grammar is defined as follows:




class ExprParser extends Parser;


options {

        k = 2;

    buildAST=true;

}


expr: expr1;


expr1: PRINT STRING;
 
==> expr: PRINT ( STRING | PRINT ) ;
 




class ExprLexer extends Lexer;


options {

    k=2;

    charVocabulary='\u0000'..'\u007F';

    testLiterals = false;

}


tokens {

        PRINT = "print";

}


STRING options { testLiterals = true; } : ('a'..'z')+;


WS    : ( ' '

        | '\r' '\n'

        | '\n'

        | '\t'

  
      )

        {$setType(Token.SKIP);}

      ;



Obviously the second example ("print print") does not
work

 because the

token PRINT takes priority since we have testLiterals
= true (note
     that
this example is taken from a more complex grammar).

The    question is: how do I, under certain
circumstances, ignore reserved
word    tokens?

Bythe way, I am currently using antlr 2.7.   
      
    ==> ANTLR2's latest version is 2.7.7   

Many    thanks,
JC



        

---------------------------------
    Yahoo! Mail réinvente le mail !  Découvrez le 
nouveau     Yahoo! Mail  et son interface
révolutionnaire.  
                   

---------------------------------
  Yahoo! Mail réinvente le mail !  Découvrez le 
nouveau Yahoo! Mail  et son interface révolutionnaire.

 




	

	
		
___________________________________________________________________________ 
Découvrez une nouvelle façon d'obtenir des réponses à toutes vos questions ! 
Profitez des connaissances, des opinions et des expériences des internautes sur Yahoo! Questions/Réponses 
http://fr.answers.yahoo.com


More information about the antlr-interest mailing list