LEX

Peter Zadrozny peter at edsews.EDS.COM
Sat Feb 6 05:06:21 AEST 1988


nyit.uucp is unknown...

In article <260 at nyit.UUCP>, michael at nyit.UUCP (Michael Gwilliam) writes:
> Is it possible to write a regular expression that will
> transform a /* comment */ into nothing?

A while ago I had to do something similar for a PL/1 preprocessor
(PL/1 also uses /* ... */ as comment delimiter).

After toying a while trying to get the perfect lex syntax, I remembered
that I had to do more stuff than just skip the comment such as line counting,
error trapping and more, therefore my final solution was to recognize
"/*" and send it to a comment eater routine similar to:

#define EOF_MESSAGE	"Unexpected EOF in comment"

void skip_comm ()
{
    register int    c;

    for (;;) {
	if ((c = input ()) == '\n')
	     line_counter++;
	else {
	    if (c == 0)
		lex_error (EOF_MESSAGE, UNRECOVERABLE);
	    if (c == '*') {
		if ((c = input ()) == '/')
		    break;
		else
		    if (c == 0)
			lex_error (EOF_MESSAGE, UNRECOVERABLE);
		    else
			unput (c);
	}
    }
}

input () and unput () are lex routines, and lex_error is my error
handler which uses line_counter to output a decent error message.

--------------------------------------------------------------------
Peter Zadrozny					peter at edsews.eds.com
Oh my god, he can speak!!!			uunet!edsews!peter
Yeah, but don't tell anyone, he'll get fired	(313) 645-4725
--------------------------------------------------------------------



More information about the Comp.lang.c mailing list