How can I ignore reserved words in certain cases ?
0 posts in topic
Flat View  Flat View
TOPIC ACTIONS:
 

Posted By:   Anonymous
Posted On:   Thursday, November 30, 2006 03:34 AM

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; }    More>>

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 : ( ' '

| '
' '
'

| '
'

| ' '

)

{$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    <<Less
About | Sitemap | Contact