[antlr-interest] Re: Help with Java grammar

Ric Klaren klaren at cs.utwente.nl
Tue Mar 9 06:26:38 PST 2004


On Tue, Mar 09, 2004 at 02:08:09PM -0000, cliftonccraig wrote:
> I just tried this:
> SL_COMMENT
> 	:	"//"
> 		(~('\n'|'\r'))* ('\n'|'\r'('\n')?)
> 		{
> //*CCC- Allow comments to flow through to the rewrite engine
> //		    $setType(Token.SKIP);
> 		    newline();
> 		}
> 		|
> 		"//" (~('\n'|'\r'))*
> 	;

How about this? 

SL_COMMENT
   :  "//" 
      ( ~('\n'|'\r') )*                         // not a newline part...
      ( ('\n'|'\r'('\n')? { newline(); } ) )?   // optional newline
   ;

If this gives trouble generate the lexer with -traceLexer and see where it
gets stuck. (or check with debugger)

There's a few dirty tricks you can do with EOF checks that work by checking
LA(i) for EOF in the init action of a closure rule, but I don't think these
should be necessary for this. (unless I'm missing the point somewhere)

> And I got an warning saying:
> D:\scm\tools\parsers\grammar\ANTLR\java.g:1235: warning:lexical
> nondeterminism between alts 1 and 2 of block upon
> D:\scm\tools\parsers\grammar\ANTLR\java.g:1235:     k==1:'/'
> D:\scm\tools\parsers\grammar\ANTLR\java.g:1235:     k==2:'/'
> D:\scm\tools\parsers\grammar\ANTLR\java.g:1235:    
> k==3:'\u0003'..'\t','\u000b','\u000c','\u000e'..'\uffff'
> D:\scm\tools\parsers\grammar\ANTLR\java.g:1235:    
> k==4:<end-of-token>,'\u0003'..'\t','\u000b','\u000c','\u000e'..'\uffff'

Don't worry too much about warnings like these ;) Read the source for what
antlr generated for the rule and it often becomes obvious if the
parser/lexer will do the right thing. (and it helps in getting a feel for
things)

Cheers,

Ric
-- 
-----+++++*****************************************************+++++++++-------
    ---- Ric Klaren ----- j.klaren at utwente.nl ----- +31 53 4893722  ----
-----+++++*****************************************************+++++++++-------
 Time what is time - I wish I knew how to tell You why - It hurts to know -
          Aren't we machines - Time what is time - Unlock the door
               - And see the truth - Then time is time again
                From: 'Time what is Time' by Blind Guardian



 
Yahoo! Groups Links

<*> To visit your group on the web, go to:
     http://groups.yahoo.com/group/antlr-interest/

<*> To unsubscribe from this group, send an email to:
     antlr-interest-unsubscribe at yahoogroups.com

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



More information about the antlr-interest mailing list