[antlr-interest] Re: Match the End of File

thrutchy eric_mahurin at yahoo.com
Sat Jul 17 13:36:50 PDT 2004


--- In antlr-interest at yahoogroups.com, "xcolwell" <xcolwell at y...> wrote:
> Hi,
>
> I am trying to write a lexer to match a multi-line comment.
> However, there is a catch. If a "comment" is started but never
> terminated, I want to match it also. For example, I would like to
> match
>
> /* comment */EOF
>
> as well as
>
> /* comment EOF
>
> under the same token. I am just getting into ANTLR, so I used Mr.
> Parr's Java grammar from antlr.org as a starting point. It appears
> that '\uFFFF' is the EOF character used internally. I can't find a
> nice identifier in the documents. The modification I would like to
> make is below.
>
> ML_COMMENT
> :
> options {
> generateAmbigWarnings=false;
> }
> :
> { LA(2)!='/' }? '*'
> | '\r' '\n' {newline();}
> | '\r' {newline();}
> | '\n' {newline();}
> | ~('*'|'\n'|'\r')

Try changing the above line to:

| ~('*'|'\n'|'\r'|'\uFFFF')

> )*
> ("*/" | '\uFFFF' )
> ;
>
>
> But unfortunately this does not work. Is there an EOF identifier I am
> missing? How would one write a rule for what I am trying to do?
>
> Best Regards,
>
> Brien

Another solution would be greedy=false:

"/*" ( options{greedy=false;} :
: '\r' '\n' {newline();}
| '\r' {newline();}
| '\n' {newline();}
| ~('\r'|'\n')
)*
("*/" | '\uFFFF' )

Be warned that I have not tried these.



 
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