help-flex
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: how to recognize a multiple line comment ?


From: Bruce Lilly
Subject: Re: how to recognize a multiple line comment ?
Date: Thu, 7 Apr 2005 07:57:41 -0400
User-agent: KMail/1.8

On Thu April 7 2005 02:29, James Yu wrote:
> Dear all,
> 
> I am trying to define a rule for recognizing a multiple line comment in C, 
> but I have not yet sueccessed.
> Thus, I am posting my lex segment to this email and hope you can point out 
> some mistakes for me.
> 
> == flex code segment ==
> slash "/"
> asterisk "*"
> comment ({slash}{asterisk}+([^*]|[\n])*{asterisk}+{slash})
> == flex code segment ==

Don't try to do too much in lexical analysis; some things are better
handled in parsing under control of a grammar, where context is
available.

For example, your pattern won't handle the legal C comment
/* foo ***** bar */
and it will inappropriately match the text in the quoted string in
char foo[] = "/*bar*/";

A properly-designed grammar will also allow resolving conflicts by
using precedence and associativity to handle complex cases such as

int x, y, z, *px;
char foo[3];

x = 2;
px = &x;
y = 8;
z = y/*px;
...
strncpy(foo, "*/", sizeof(foo));

i.e. you have the opportunity to decide whether * binds more tightly
to / for comments than as a pointer indication.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]