[antlr-interest] Handling identifiers

mafw14 98vdxjfr02 at sneakemail.com
Tue Jul 22 06:49:19 PDT 2003


Hi,

I know this ought to be a FAQ but I have been hunting for a long
time for a solution to this and have failed to find anything
useful.

I am trying to develop a parser to handle an expression evaluator.  I
am really stuck on one particular part of the grammar file.  The lexer
needs to differentiate between literal strings and identifiers.  If
literal strings always were enclosed in quotes there would be no
problem, but in this language the double quotes are optional if there
is no other ambiguity.  Therefore a group of letters could be an
identifier or a literal string.  I can maintain a list of identifiers
in a hash table but I don't know how to call a routine from the lexer
to accomplish this.

Extract from grammar:

// string literals - this is easy!
STRING_T:
    '\"'!
    (LETTER_OR_DIGIT)* 
    '\"'!
    ;

protected
LETTERS
    options {testLiterals=true; } :
    (LETTER)+
    ;

// This could be an identifier or a literal string?
// -- don't know what to do here.....	
IDENTIFIER
    options {testLiterals=true; } :
    t:LETTERS { System.out.println(t.getText()); }
    ;

Please advise.  Thanks,
Mark


 

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




More information about the antlr-interest mailing list