LEX rule, anyone???

Christopher Lott cml at tove.umd.edu
Wed Dec 6 02:42:39 AEST 1989


In article <601 at vice2utc.chalmers.se> d5kwedb at dtek.chalmers.se (Kristian Wedberg) writes:
>	Does anyone know how to write a LEX rule for C comments,
>	ie for everything between /* and */, nesting not allowed?

[ because I would love to see other solutions to this, I posted. ]

This sounds suspiciously like someone's homework assignment.  In fact,
I had exactly such a homework assignment, and this resulted :-)

The following lines are a lex program to recognize c comments, not nested. 
This does NOT take into account any quote marks within a comment; i.e., 
quote marks don't 'escape' the close comment marker.

I compile this via "lex ccom.l ; cc lex.yy.c -o ccom -ll"

----snip----
   /* this is a regular expression to match a c comment	*/
   /* written by cml 890922   (probably not minimal)	*/
%%
"/*"([^*]|[*]*[^*/])*[*]+"/"	{printf("saw a c comment.\n");}
.				{putchar(*yytext);}
----snip----

chris...
--
cml at tove.umd.edu    Computer Science Dept, U. Maryland at College Park
		    4122 A.V.W.  301-454-8711	<standard disclaimers>



More information about the Comp.lang.c mailing list