[antlr-interest] newbie help

Foolish Ewe foolishewe at hotmail.com
Fri Oct 20 05:19:22 PDT 2006



Hello All:

I'm sorry if this appears twice, the original message may have been 
accidentally
sent before I finished typing.

Regarding the first problem, the, I have keyword/reserved word completion in 
a grammar
I am working on, and I handle it in the lexer by creating rules of the form:

TOKENTORETURN: mandatoryprefix  (optionalsuffix)? ;

Where TOKENTORETURN is the token generated for parsing
mandatoryprefix is the shortest accepted version of the key word
and optionalsuffix recursively adds characters optionally until the keyword 
is complete.

For example suppose we have a reserved word, "interface" and for now let's 
restrict
ourself to lowercase letters only.  If we want to recognice "inter", 
"interf", "interfa", "interfac"
and "interface", we could do something of the form:
INTERFACE: "inter" ('f' ('a' ('c' ('e')?)?)?)?;

This appears to work using the 2.7.6 version of ANTLR.
>From what I can tell, you do need to be careful that the mandatory prefix is 
sufficiently
long that keywords do not become ambiguous, e.g. if your language
had say "char" and "const" as keywords, you could not abbreviate one of them 
as "c".

Regarding EOF handling, I'm not quite sure about that, I'm pretty new to 
ANTLR,
and am wondering about that one myself.

Regards:

Bill M.
>From: "Sridharan S" <sridharxp at gmail.com>
>To: antlr-interest at antlr.org
>Subject: [antlr-interest] newbie help
>Date: Fri, 20 Oct 2006 06:48:24 +0530
>
>Hi everybody
>
>I am a new to antlr and I have two problems.
>
>One is my grammar has keywords that can also be used interchangeably in
>their shortened form of 4 chars. What to do to avoid doubling of keywords?
>
>Second is my language statement always ends in a newline with one 
>exception.
>Eof can also work as a newline.ie When at the end of your program you need
>not supply a newline, it is optional. .
>
>Thanks everybody
>- Sridhar

_________________________________________________________________
All-in-one security and maintenance for your PC.  Get a free 90-day trial! 
http://clk.atdmt.com/MSN/go/msnnkwlo0050000002msn/direct/01/?href=http://www.windowsonecare.com/?sc_cid=msn_hotmail



More information about the antlr-interest mailing list