[antlr-interest] RE: antlr-interest Digest, Vol 5, Issue 11

Peter Kronenberg PKronenberg at technicacorp.com
Wed Apr 6 10:27:34 PDT 2005


Well, I think it makes sense for it to be a lexer rule, since the parser
shouldn't care or need to know the exact for of the token, either EQ or
'='.
Remember that I already have a Lexer rule  EQ: '='.
So, I would need another token to represent EQ.  And then the parser
would have to test both alternatives.  That can get rather messy (if you
throw in gt, ge, lt, le, etc).

I thought that defining it as a token would also give a clue to the
lexer so when it parses "EQ" followed by whitespace, it would know to
stop and not assume it's an IDENT.  I thought something like this might
also work in the lexer:

EQ: '=' | ("EQ ")=>"EQ"

----------------


1) If EQ can be a keyword, define it in the tokens section: tokens{ EQ =
"EQ";} and then have the IDENT rule as

IDENT options {testLiterals=true;}
: ('a'..'z' | 'A'..'Z') ('a'..'z' | 'A'..'Z' | '0'..'9' | '_' | '$')*;

Finally, you could have a parser rule of form 
assign: IDENT EQ expression; 


2) If EQ has to be a lexer rule, you could do this..

protected EQ: "EQ"; // To avoid clash with IDENT
IDENT: 
(("EQ"){$setType(EQ);} 
| ('a'..'z' | 'A'..'Z') ('a'..'z' | 'A'..'Z' | '0'..'9' | '_' | '$')*; 

NOTE: I used the above rule just to give you an example. With this rule,
whenever "EQ" is matched, the type will automatically be set to EQ,
which means you cannot have an IDENTIFIER like "EQU".

However, if EQ will always be followed by, let's say, the rule "HASH",
though it doesn't make any sense, You could do this:

(("EQ") (HASH))=>"EQ"{$setType(EQ);}

In this case, only when "EQ" is followed by HASH, it will not be an
IDENTIFIER.
 
Hope this helps.

Bharath.


The information contained in this transmission may contain privileged and confidential information. It is intended only for the use of the person(s) named above. If you are not the intended recipient, you are hereby notified that any review, dissemination, distribution or duplication of this communication is strictly prohibited. If you are not the intended recipient, please contact the sender by reply e-mail and destroy all copies of the original message. Technica Corporation does not represent this e-mail to be free from any virus, fault or defect and it is therefore the responsibility of the recipient to first scan it for viruses, faults and defects. To reply to our e-mail administrator directly, please send an e-mail to postmaster at technicacorp.com. Thank you.



More information about the antlr-interest mailing list