[antlr-interest] Lexer: C-style comments with DOS/UNIX newlines

Joan Pujol joanpujol at gmail.com
Fri Aug 20 00:31:04 PDT 2004


You want greedy=true not false (thus you don't need to put the greedy
option because true is the default)

This is the grammar:
NL: 
	(
	    (options {generateAmbigWarnings=false;}:
	    "\r\n"  //DOS
	    | '\r'		//MAC
	    | '\n'		//UNIX
	    )
	) {newline();};


Or you can use syntactic predicates (less eficient but more polite):

NL: 
	(
	    ("\r\n")=> "\r\n"  //DOS
	    | '\r'		//MAC
	    | '\n'		//UNIX
	) {newline();};


In your grammar if you don't put the  generateAmbigWarnings=false you
always get non determinism warnings because it is ambiguous, you can
get \r\n in two diferent ways.


Cheers,


On Fri, 20 Aug 2004 12:43:21 +1000, Nigel Sheridan-Smith
<nbsherid at secsme.org.au> wrote:
> 
> Hi there,
> 
> I'm a relative newcomer to ANTLR. I am trying to define C/C++/Java style
> comments, and having some trouble.
> 
> I have the following rule, similar to that in the manual (with k=3 and ANTLR
> 2.7.4):
> 
> /* COMMENT rules */
> CCOMMENT: "/*"
>        ( options {greedy=false;}
> 
>         : '\r' ('\n')? {newline();}
>         | '\n' {newline();}
>         | .
>        )*
>       "*/";
> 
> 
> I get the following messages:
> 
> ANTLR Parser Generator   Version 2.7.4   1989-2004 jGuru.com
> PersistenceGrammar.g:70:17: warning:lexical nondeterminism between alts 1
> and 2 of block upon
> PersistenceGrammar.g:70:17:     k==1:'\n'
> PersistenceGrammar.g:70:17:     k==2:'\t','\n','\r','
> '..'"','%'..'?','A'..'_','
> a'..'~'
> PersistenceGrammar.g:70:17:     k==3:'\t','\n','\r','
> '..'"','%'..'?','A'..'_','
> a'..'~'
> 
> If I remove the  ('\n')?  from the 5th line then the problem goes away.
> However, this isn't actually what seems to be reported as the error.
> Changing the "/*" and "*/" to "{" and "}" do not make any difference; nor
> does increasing the value of k in the lexer.
> 
> Any help would be appreciated! The rest of the grammar is attached.
> 
> Thanks
> 
> 
> 
> Nigel
> 
> --
> Nigel Sheridan-Smith
> PhD research student
> 
> Faculty of Engineering
> University of Technology, Sydney
> Phone: 02 9514 7946
> Fax: 02 9514 2435
> 
> Yahoo! Groups Links
> 
> 
> 
> 
> 
> 
> 


-- 
Joan Jesús Pujol Espinar


 
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