[antlr-interest] grammar for jdk1.5 parameterized types

Ernest Pasour sasecp at wnt.sas.com
Fri Sep 19 06:09:19 PDT 2003


I looked at Jamie's solution, but I don't believe it works correctly (I may be wrong, I am no parsing expert).  It appears that he has kept a counter that he increments when he sees LT and decrements on GT.  Then he uses a syntactic predicate (I think that's what it's called) to prevent lexical recognition of ">>" if the counter is at 2.  This forces the '>' to be recognized, which allows the parameterized types to be parsed.  However, I believe a piece of code like the following will cause problems:

{
	if (a<b) a++;
	if (a<b) a++;
	a>>2;
}

In this case, the ">>" will be recognized as two separate '>' symbols since it was preceded by 2 '<' without any intervening operation that zeroes out the counter.

-----Original Message-----
From: Terence Parr [mailto:parrt at antlr.org] 
Sent: Thursday, September 18, 2003 3:02 PM
To: antlr-interest at yahoogroups.com
Subject: Re: [antlr-interest] grammar for jdk1.5 parameterized types


For some reason my last post didn't appear.

See the C++ templates added to Java solution by Jamie Herre on the 
antlr site.  Not sure what he did any more.  However, it's a simple 
matter in the lexer to track a tiny bit of context I think (i.e., did I 
see "class" or a class name)?  It means the lexer needs access to the 
symbol table.  I think Jamie did something clever, but can't remember.  
Perhaps my audio lectures have the answer ;)

Ter

On Thursday, September 18, 2003, at 11:55 AM, John P N Pybus wrote:

> mzukowski at yci.com wrote:
>> You can't switch your lexer from the parser safely.  ANTLR doesn't 
>> work that way (infinite lookahead and all that).  I suggest getting 
>> rid of ">>" as a
>> token and making the parser look for '>' '>' as GT.
>>
>> Monty
>
> Hmm, with the lexer ignoring Whitespace wouldn't the parser then allow
> "n > > 3" as well as "n >> 3"?
>
> I'd suggest using lookahead in the lexer to define 3 tokens GT_GT,
> GT_EQ, and GT corresponding to a '>' directly followed by another '>'; 
> '>' followed by '=' and all other '>' chars, respectively.
>
> You can use ( GT | GT_GT ) in your parser rules for generics, and can
> define the various shift operators as GT_GT GT; GT_GT GT_EQ EQ etc...
>
> I haven't done this with the antlr java grammar myself, but I believe
> I've seen this approach used in other java1.5 recognisers (sorry no 
> reference handy).
>
> Hope this makes some sense.
>
> Yours,
>
> John
>
>
>
>
> Your use of Yahoo! Groups is subject to 
> http://docs.yahoo.com/info/terms/
>
>
>
--
Professor Comp. Sci., University of San Francisco
Creator, ANTLR Parser Generator, http://www.antlr.org Co-founder, http://www.jguru.com Co-founder, http://www.knowspam.net enjoy email again! Co-founder, http://www.peerscope.com pure link sharing




 

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



 

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




More information about the antlr-interest mailing list