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

Foolish Ewe foolishewe at hotmail.com
Thu Nov 30 08:04:38 PST 2006


Hi All:

I faced this issue recently in a grammar at my job.  My suggestion is
to use delimited strings (i.e. require quotes around strings).

Since I didn't have (complete) control over the grammar specification,
I was forced to resort to using a rule enumerating all keywords and having
a string rule that took a non keyword string or a keyword.  I agree it is 
unpleasant,
but I tried approaches like using semantic actions in the parser but those 
approaches
either didn't work or appeared worse than the solution I used.

Regards:

Bill M.

>From: JeanChristophe Gautier <jchgautier at yahoo.fr>
>To: antlr-interest at antlr.org
>Subject: [antlr-interest] How can I ignore reserved words in certain cases 
>?
>Date: Thu, 30 Nov 2006 09:36:48 +0100 (CET)
>
>
>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;
>
>
>
>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.
>
>Many thanks,
>JC
>
>
>
>
>
>---------------------------------
>  Yahoo! Mail réinvente le mail ! Découvrez le nouveau Yahoo! Mail et son 
>interface révolutionnaire.

_________________________________________________________________
Stay up-to-date with your friends through the Windows Live Spaces friends 
list. 
http://clk.atdmt.com/MSN/go/msnnkwsp0070000001msn/direct/01/?href=http://spaces.live.com/spacesapi.aspx?wx_action=create&wx_url=/friends.aspx&mk



More information about the antlr-interest mailing list